-
Notifications
You must be signed in to change notification settings - Fork 2
/
transport.go
62 lines (54 loc) · 1.47 KB
/
transport.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
package main
import (
"fmt"
"net"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/webtunnel/transport/httpupgrade"
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/webtunnel/transport/tls"
)
type ClientConfig struct {
RemoteAddresses []string
Path string
TLSKind string
TLSServerName string
}
type Transport struct {
config *ClientConfig
}
func NewWebTunnelClientTransport(config *ClientConfig) (Transport, error) {
return Transport{config: config}, nil
}
func (t Transport) Dial() (net.Conn, error) {
var conn net.Conn
for _, addr := range t.config.RemoteAddresses {
if tcpConn, err := net.Dial("tcp", addr); err == nil {
conn = tcpConn
break
}
}
if conn == nil {
return nil, fmt.Errorf("Can't connect to %v", t.config.RemoteAddresses)
}
if t.config.TLSKind != "" {
conf := &tls.Config{ServerName: t.config.TLSServerName}
if tlsTransport, err := tls.NewTLSTransport(conf); err != nil {
return nil, err
} else {
if tlsConn, err := tlsTransport.Client(conn); err != nil {
return nil, err
} else {
conn = tlsConn
}
}
}
upgradeConfig := httpupgrade.Config{Path: t.config.Path, Host: t.config.TLSServerName}
if httpupgradeTransport, err := httpupgrade.NewHTTPUpgradeTransport(&upgradeConfig); err != nil {
return nil, err
} else {
if httpUpgradeConn, err := httpupgradeTransport.Client(conn); err != nil {
return nil, err
} else {
conn = httpUpgradeConn
}
}
return conn, nil
}