@@ -3,6 +3,7 @@ package libp2p
3
3
import (
4
4
"context"
5
5
"crypto/rand"
6
+ "fmt"
6
7
7
8
crypto "github.com/libp2p/go-libp2p-crypto"
8
9
host "github.com/libp2p/go-libp2p-host"
@@ -34,9 +35,9 @@ type Config struct {
34
35
35
36
type Option func (cfg * Config ) error
36
37
37
- func Transports (tpts [] transport.Transport ) Option {
38
+ func Transports (tpts ... transport.Transport ) Option {
38
39
return func (cfg * Config ) error {
39
- cfg .Transports = tpts
40
+ cfg .Transports = append ( cfg . Transports , tpts ... )
40
41
return nil
41
42
}
42
43
}
@@ -54,70 +55,110 @@ func ListenAddrStrings(s ...string) Option {
54
55
}
55
56
}
56
57
57
- func ListenAddrs (addrs [] ma.Multiaddr ) Option {
58
+ func ListenAddrs (addrs ... ma.Multiaddr ) Option {
58
59
return func (cfg * Config ) error {
59
60
cfg .ListenAddrs = append (cfg .ListenAddrs , addrs ... )
60
61
return nil
61
62
}
62
63
}
63
64
64
- func NoSecio (cfg * Config ) error {
65
- cfg .DisableSecio = true
66
- return nil
65
+ type transportEncOpt int
66
+
67
+ const (
68
+ EncPlaintext = transportEncOpt (0 )
69
+ EncSecio = transportEncOpt (1 )
70
+ )
71
+
72
+ func TransportEncryption (tenc ... transportEncOpt ) Option {
73
+ return func (cfg * Config ) error {
74
+ if len (tenc ) != 1 {
75
+ return fmt .Errorf ("can only specify a single transport encryption option right now" )
76
+ }
77
+
78
+ // TODO: actually make this pluggable, otherwise tls will get tricky
79
+ switch tenc [0 ] {
80
+ case EncPlaintext :
81
+ cfg .DisableSecio = true
82
+ case EncSecio :
83
+ // noop
84
+ default :
85
+ return fmt .Errorf ("unrecognized transport encryption option: %d" , tenc [0 ])
86
+ }
87
+ return nil
88
+ }
67
89
}
68
90
69
- func WithMuxer (m mux.Transport ) Option {
91
+ func NoEncryption () Option {
92
+ return TransportEncryption (EncPlaintext )
93
+ }
94
+
95
+ func Muxer (m mux.Transport ) Option {
70
96
return func (cfg * Config ) error {
97
+ if cfg .Muxer != nil {
98
+ return fmt .Errorf ("cannot specify multiple muxer options" )
99
+ }
100
+
71
101
cfg .Muxer = m
72
102
return nil
73
103
}
74
104
}
75
105
76
- func WithPeerstore (ps pstore.Peerstore ) Option {
106
+ func Peerstore (ps pstore.Peerstore ) Option {
77
107
return func (cfg * Config ) error {
108
+ if cfg .Peerstore != nil {
109
+ return fmt .Errorf ("cannot specify multiple peerstore options" )
110
+ }
111
+
78
112
cfg .Peerstore = ps
79
113
return nil
80
114
}
81
115
}
82
116
83
- func WithNetProtector (prot pnet.Protector ) Option {
117
+ func PrivateNetwork (prot pnet.Protector ) Option {
84
118
return func (cfg * Config ) error {
119
+ if cfg .Protector != nil {
120
+ return fmt .Errorf ("cannot specify multiple private network options" )
121
+ }
122
+
85
123
cfg .Protector = prot
86
124
return nil
87
125
}
88
126
}
89
127
90
- func WithBandwidthReporter (rep metrics.Reporter ) Option {
128
+ func BandwidthReporter (rep metrics.Reporter ) Option {
91
129
return func (cfg * Config ) error {
130
+ if cfg .Reporter != nil {
131
+ return fmt .Errorf ("cannot specify multiple bandwidth reporter options" )
132
+ }
133
+
92
134
cfg .Reporter = rep
93
135
return nil
94
136
}
95
137
}
96
138
97
- func New (ctx context.Context , opts ... Option ) (host.Host , error ) {
98
- cfg := DefaultConfig ()
99
-
100
- for _ , opt := range opts {
101
- if err := opt (cfg ); err != nil {
102
- return nil , err
139
+ func Identity (sk crypto.PrivKey ) Option {
140
+ return func (cfg * Config ) error {
141
+ if cfg .PeerKey != nil {
142
+ return fmt .Errorf ("cannot specify multiple identities" )
103
143
}
104
- }
105
-
106
- return newWithCfg (ctx , cfg )
107
- }
108
144
109
- func WithPeerKey (sk crypto.PrivKey ) Option {
110
- return func (cfg * Config ) error {
111
145
cfg .PeerKey = sk
112
146
return nil
113
147
}
114
148
}
115
149
116
- func newWithCfg (ctx context.Context , cfg * Config ) (host.Host , error ) {
117
- if cfg == nil {
118
- cfg = DefaultConfig ()
150
+ func New (ctx context.Context , opts ... Option ) (host.Host , error ) {
151
+ var cfg Config
152
+ for _ , opt := range opts {
153
+ if err := opt (& cfg ); err != nil {
154
+ return nil , err
155
+ }
119
156
}
120
157
158
+ return newWithCfg (ctx , & cfg )
159
+ }
160
+
161
+ func newWithCfg (ctx context.Context , cfg * Config ) (host.Host , error ) {
121
162
// If no key was given, generate a random 2048 bit RSA key
122
163
if cfg .PeerKey == nil {
123
164
priv , _ , err := crypto .GenerateKeyPairWithReader (crypto .RSA , 2048 , rand .Reader )
@@ -166,16 +207,15 @@ func DefaultMuxer() mux.Transport {
166
207
return tpt
167
208
}
168
209
169
- func DefaultConfig () * Config {
210
+ func Defaults ( cfg * Config ) error {
170
211
// Create a multiaddress that listens on a random port on all interfaces
171
212
addr , err := ma .NewMultiaddr ("/ip4/0.0.0.0/tcp/0" )
172
213
if err != nil {
173
- panic ( err )
214
+ return err
174
215
}
175
216
176
- return & Config {
177
- ListenAddrs : []ma.Multiaddr {addr },
178
- Peerstore : pstore .NewPeerstore (),
179
- Muxer : DefaultMuxer (),
180
- }
217
+ cfg .ListenAddrs = []ma.Multiaddr {addr }
218
+ cfg .Peerstore = pstore .NewPeerstore ()
219
+ cfg .Muxer = DefaultMuxer ()
220
+ return nil
181
221
}
0 commit comments