-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
109 lines (84 loc) · 2.67 KB
/
utils.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
package host
import (
"strings"
"github.com/agencyenterprise/go-libp2p-pubsub-benchmark-tools/pkg/logger"
libp2p "github.com/libp2p/go-libp2p"
mplex "github.com/libp2p/go-libp2p-mplex"
quic "github.com/libp2p/go-libp2p-quic-transport"
secio "github.com/libp2p/go-libp2p-secio"
yamux "github.com/libp2p/go-libp2p-yamux"
lconfig "github.com/libp2p/go-libp2p/config"
tcp "github.com/libp2p/go-tcp-transport"
ws "github.com/libp2p/go-ws-transport"
)
func parseTransportOptions(opts []string) (lconfig.Option, error) {
var lOpts []lconfig.Option
for _, opt := range opts {
switch strings.ToLower(opt) {
case "tcp":
lOpts = append(lOpts, libp2p.Transport(tcp.NewTCPTransport))
case "ws":
lOpts = append(lOpts, libp2p.Transport(ws.New))
case "quic":
lOpts = append(lOpts, libp2p.Transport(quic.NewTransport))
/* note: utp has a broken gx dep
case "utp":
lOpts = append(lOpts, libp2p.Transport(utp.NewUtpTransport))
*/
/* note: WIP
case "udp":
lOpts = append(lOpts, libp2p.Transport(utp.NewUdpTransport))
*/
/* note: need to pass private key? But we didn't for quic...
case "tls":
lOpts = append(lOpts, libp2p.Transport(tls.New))
*/
case "none":
if len(opts) > 1 {
logger.Error("when using the 'none' transport option, cannot also specify other transport options")
return nil, ErrImproperTransportOption
}
return libp2p.NoTransports, nil
case "default":
lOpts = append(lOpts, libp2p.DefaultTransports)
default:
logger.Errorf("unknown transport option: %s", opt)
return nil, ErrUnknownTransportOption
}
}
return libp2p.ChainOptions(lOpts...), nil
}
func parseMuxerOptions(opts [][]string) (lconfig.Option, error) {
var lOpts []lconfig.Option
for _, opt := range opts {
if len(opt) != 2 {
logger.Errorf("improper muxer format, expected ['name', 'type'], received %v", opt)
return nil, ErrImproperMuxerOption
}
switch strings.ToLower(opt[0]) {
case "yamux":
lOpts = append(lOpts, libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport))
case "mplex":
lOpts = append(lOpts, libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport))
// TODO: add others?
default:
logger.Errorf("unknown muxer option: %s", opt)
return nil, ErrUnknownMuxerOption
}
}
return libp2p.ChainOptions(lOpts...), nil
}
func parseSecurityOptions(opt string) (lconfig.Option, error) {
switch strings.ToLower(opt) {
case "secio":
return libp2p.Security(secio.ID, secio.New), nil
case "default":
return libp2p.Security(secio.ID, secio.New), nil
// TODO: add others?
case "none":
return libp2p.NoSecurity, nil
default:
logger.Errorf("unknown security option: %s", opt)
return nil, ErrUnknownSecurityOption
}
}