-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_options.go
89 lines (80 loc) · 2.12 KB
/
transport_options.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
package i2ptcp
import (
"fmt"
peer "github.com/libp2p/go-libp2p-peer"
"net"
"strconv"
"strings"
)
// Option is a functional argument
type Option func(*GarlicTCPTransport) error
//SAMHost sets the host of the SAM Bridge to use
func SAMHost(s string) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
st := ""
if !strings.Contains("/ip4/", s) || !strings.Contains("/ip6/", s) {
ip := net.ParseIP(s)
if ip.To4() != nil {
st = "/ip4/" + s + "/"
}
if ip.To16() != nil {
st = "/ip6/" + s + "/"
}
}
c.HostSAM = st
return nil
}
}
//SAMPort sets the port of the SAM bridge to use
func SAMPort(s string) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
st := strings.TrimPrefix(s, "/tcp/")
rt := strings.TrimSuffix(st, "/")
val, err := strconv.Atoi(rt)
if err != nil {
return fmt.Errorf("Transport Construction error: %s", err)
}
if val > 0 && val < 65536 {
c.PortSAM = "/tcp/" + rt + "/"
return nil
}
return fmt.Errorf("port is %s invalid", s)
}
}
//SAMPass sets the password to use when authenticating to the SAM bridge. It's
//ignored for now, and will return an error if it recieves a non-empty string.
func SAMPass(s string) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
if s != "" {
return fmt.Errorf("SAMPass is unused for now, pass no argument(or empty string). Failing closed.")
}
return nil
}
}
//KeysPath sets the path to the keys, if no keys are present, they will be generated.
func KeysPath(s string) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
c.keysPath = s
return nil
}
}
func OnlyGarlic(b bool) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
c.onlyGarlic = b
return nil
}
}
func GarlicOptions(s []string) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
for _, v := range s {
c.garlicOptions = append(c.garlicOptions, v)
}
return nil
}
}
func LocalPeerID(p peer.ID) func(*GarlicTCPTransport) error {
return func(c *GarlicTCPTransport) error {
c.id = p
return nil
}
}