forked from ipfs-cluster/ipfs-cluster
-
Notifications
You must be signed in to change notification settings - Fork 2
/
transports.go
100 lines (88 loc) · 2.57 KB
/
transports.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
package client
import (
"context"
"crypto/tls"
"errors"
"net"
"net/http"
"time"
"github.com/elastos/Elastos.NET.Hive.Cluster/api"
p2phttp "github.com/hsanjuan/go-libp2p-http"
libp2p "github.com/libp2p/go-libp2p"
ipnet "github.com/libp2p/go-libp2p-interface-pnet"
peerstore "github.com/libp2p/go-libp2p-peerstore"
pnet "github.com/libp2p/go-libp2p-pnet"
madns "github.com/multiformats/go-multiaddr-dns"
)
// This is essentially a http.DefaultTransport. We should not mess
// with it since it's a global variable, and we don't know who else uses
// it, so we create our own.
// TODO: Allow more configuration options.
func (c *defaultClient) defaultTransport() {
c.transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
c.net = "http"
}
func (c *defaultClient) enableLibp2p() error {
c.defaultTransport()
pid, addr, err := api.Libp2pMultiaddrSplit(c.config.APIAddr)
if err != nil {
return err
}
var prot ipnet.Protector
if c.config.ProtectorKey != nil && len(c.config.ProtectorKey) > 0 {
if len(c.config.ProtectorKey) != 32 {
return errors.New("length of ProtectorKey should be 32")
}
var key [32]byte
copy(key[:], c.config.ProtectorKey)
prot, err = pnet.NewV1ProtectorFromBytes(&key)
if err != nil {
return err
}
}
h, err := libp2p.New(c.ctx, libp2p.PrivateNetwork(prot))
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(c.ctx, ResolveTimeout)
defer cancel()
resolvedAddrs, err := madns.Resolve(ctx, addr)
if err != nil {
return err
}
h.Peerstore().AddAddrs(pid, resolvedAddrs, peerstore.PermanentAddrTTL)
c.transport.RegisterProtocol("libp2p", p2phttp.NewTransport(h))
c.net = "libp2p"
c.p2p = h
c.hostname = pid.Pretty()
return nil
}
func (c *defaultClient) enableTLS() error {
c.defaultTransport()
// based on https://github.com/denji/golang-tls
c.transport.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
InsecureSkipVerify: c.config.NoVerifyCert,
}
c.net = "https"
return nil
}