/
p2p.go
104 lines (87 loc) · 2.34 KB
/
p2p.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
package account
import (
"context"
"encoding/base64"
"berty.tech/core/network/p2p"
p2pcrypto "github.com/libp2p/go-libp2p-crypto"
"github.com/pkg/errors"
)
type P2PNetworkOptions struct {
Bind []string
Transport []string
Bootstrap []string
DefaultBootstrap bool
MDNS bool
Relay bool
Identity string
}
func WithP2PNetwork(opts *P2PNetworkOptions) NewOption {
return func(a *Account) error {
var err error
if opts == nil {
opts = &P2PNetworkOptions{}
}
p2pOptions := []p2p.Option{}
// Bind
if opts.Bind == nil {
opts.Bind = []string{"/ip4/0.0.0.0/tcp/0"}
}
// Bootstrap
if opts.Bootstrap == nil {
opts.Bootstrap = []string{}
}
if opts.DefaultBootstrap {
opts.Bootstrap = append(
opts.Bootstrap,
"/ip4/104.248.78.238/tcp/4004/ipfs/QmPCbsVWDtLTdCtwfp5ftZ96xccUNe4hegKStgbss8YACT",
)
}
var identity p2p.Option
if opts.Identity == "" {
identity = p2p.WithRandomIdentity()
} else {
bytes, err := base64.StdEncoding.DecodeString(opts.Identity)
if err != nil {
return errors.Wrap(err, "failed to decode identity opt, should be base64 encoded")
}
prvKey, err := p2pcrypto.UnmarshalPrivateKey(bytes)
if err != nil {
return errors.Wrap(err, "failed to unmarshal private key")
}
identity = p2p.WithIdentity(prvKey)
}
for _, v := range opts.Transport {
switch v {
case "default":
p2pOptions = append(p2pOptions, p2p.WithDefaultTransports())
case "ble":
p2pOptions = append(p2pOptions, p2p.WithTransportBle(opts.Bind, a.db))
}
}
p2pOptions = append(p2pOptions,
p2p.WithDefaultMuxers(),
p2p.WithDefaultPeerstore(),
p2p.WithDefaultSecurity(),
// @TODO: Allow static identity loaded from a file (useful for relay
// server for creating static endpoint for bootstrap)
// p2p.WithIdentity(<key>),
p2p.WithNATPortMap(), // @T\ODO: Is this a pb on mobile?
p2p.WithListenAddrStrings(opts.Bind...),
p2p.WithBootstrap(opts.Bootstrap...),
identity,
)
if opts.MDNS {
p2pOptions = append(p2pOptions, p2p.WithMDNS())
}
if opts.Relay {
p2pOptions = append(p2pOptions, p2p.WithRelayHOP())
} else {
p2pOptions = append(p2pOptions, p2p.WithRelayClient())
}
a.network, err = p2p.NewDriver(context.Background(), p2pOptions...)
if err != nil {
return err
}
return nil
}
}