forked from whyrusleeping/dhtHell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
127 lines (105 loc) · 2.63 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"errors"
"fmt"
"strconv"
"strings"
"code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/p2p/crypto"
"github.com/jbenet/go-ipfs/repo/config"
u "github.com/jbenet/go-ipfs/util"
b64 "encoding/base64"
b58 "github.com/jbenet/go-base58"
)
// GenIdentity creates a random keypair and returns the associated
// peerID and private key encoded to match config values
func GenIdentity() (string, string, error) {
k, pub, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 512, u.NewTimeSeededRand())
if err != nil {
return "", "", err
}
b, err := k.Bytes()
if err != nil {
return "", "", err
}
privkey := b64.StdEncoding.EncodeToString(b)
pubkeyb, err := pub.Bytes()
if err != nil {
return "", "", err
}
id := b58.Encode(u.Hash(pubkeyb))
return id, privkey, nil
}
// Creates an ipfs node that listens on the given multiaddr and bootstraps to
// the peer in 'bootstrap'
func nodeFromConfig(ctx context.Context, cfg *config.Config) *core.IpfsNode {
if !logquiet {
fmt.Printf("Creating node with id: '%s'\n", cfg.Identity.PeerID)
}
node, err := core.NewIPFSNode(ctx, core.Online(cfg))
if err != nil {
panic(err)
}
return node
}
// Parses a range of the form: "[x-y]"
// Also will parse: "x" and "[x]" as single value ranges
func ParseRange(s string) ([]int, error) {
if len(s) == 0 {
return nil, errors.New("no input given")
}
if s[0] == '[' && s[len(s)-1] == ']' {
parts := strings.Split(s[1:len(s)-1], "-")
if len(parts) == 0 {
return nil, errors.New("No value in range!")
}
if len(parts) == 1 {
n, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
return []int{n}, nil
}
low, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
high, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
var out []int
for i := low; i <= high; i++ {
out = append(out, i)
}
return out, nil
} else {
n, err := strconv.Atoi(s)
if err != nil {
return nil, err
}
return []int{n}, nil
}
}
func BuildConfig(addr string) *config.Config {
cfg := new(config.Config)
cfg.Addresses.Swarm = []string{addr}
cfg.Datastore.Type = "memory"
id, priv, err := GenIdentity()
if err != nil {
panic(err)
}
cfg.Identity.PeerID = id
cfg.Identity.PrivKey = priv
return cfg
}
func BootstrapTo(cfg *config.Config, root *config.Config) {
if !logquiet {
fmt.Printf("%s will connect to %s on startup.\n", cfg.Identity.PeerID, root.Identity.PeerID)
}
bsp := config.BootstrapPeer{}
bsp.Address = root.Addresses.Swarm[0]
bsp.PeerID = root.Identity.PeerID
cfg.Bootstrap = append(cfg.Bootstrap, bsp)
}