-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialer.go
More file actions
194 lines (168 loc) · 4.97 KB
/
dialer.go
File metadata and controls
194 lines (168 loc) · 4.97 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package ttproxy
import (
"bufio"
"crypto/tls"
"encoding/base64"
"fmt"
"net"
"net/http"
"net/url"
"golang.org/x/net/proxy"
)
const (
RequestProtocol = "connect-udp"
CapsuleProtocolHeader = "Capsule-Protocol"
ConnectUDPBindHeader = "Connect-Udp-Bind"
ProxyAuthorizationHeader = "Proxy-Authorization"
ProxyPublicAddressHeader = "Proxy-Public-Address"
CapsuleProtocolHeaderValue = "?1"
ConnectUDPBindHeaderValue = "?1"
CompressionAssignValue = 0x1C0FE323
CompressionCloseValue = 0x1C0FE324
)
func init() {
proxy.RegisterDialerType("https", newHTTPDialer)
}
// add customized http dialer to golang.org/x/net/proxy
type httpDialer struct {
proxy.Dialer
HostPort string
BasicAuth string
tcpURL string
}
func newHTTPDialer(uri *url.URL, d proxy.Dialer) (proxy.Dialer, error) {
dialer := &httpDialer{Dialer: d, HostPort: uri.Host}
if user := uri.User.Username(); user != "" {
if password, ok := uri.User.Password(); ok && password != "" {
dialer.BasicAuth = "basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+password))
} else {
return nil, fmt.Errorf("user name: %v and password: %v error", user, password)
}
}
dialer.tcpURL = fmt.Sprintf("https://%s", dialer.HostPort)
return dialer, nil
}
func (d *httpDialer) Dial(network, addr string) (net.Conn, error) {
conn, err := d.Dialer.Dial("tcp", d.HostPort)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodConnect, d.tcpURL, nil)
if err != nil {
return conn, fmt.Errorf("request error: %w", err)
}
req.URL.Opaque = addr
// req.Header.Add("Authorization", srv.BasicAuth)
if d.BasicAuth != "" {
req.Header.Add("Proxy-Authorization", d.BasicAuth)
}
err = req.WriteProxy(conn)
if err != nil {
return conn, fmt.Errorf("write request error: %w", err)
}
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
return conn, fmt.Errorf("read response error: %w", err)
}
if resp.StatusCode != http.StatusOK {
return conn, fmt.Errorf("server status code error: %v", resp.StatusCode)
}
return conn, nil
}
// a tls dialer with a proxy.Dialer embedded
type tlsDialer struct {
proxy.Dialer
tls.Config
}
func (d *tlsDialer) Dial(network, addr string) (net.Conn, error) {
conn, err := d.Dialer.Dial(network, addr)
if err != nil {
return nil, err
}
return net.Conn(tls.Client(conn, &d.Config)), nil
}
// a HTTP dialer which utilizes HTTP CONNECT method for TCP and RFC 9298 for UDP
func (srv Server) Dial(network, addr string) (net.Conn, error) {
conn, err := srv.Dialer.Dial("tcp", srv.HostPort)
if err != nil {
return nil, err
}
switch network {
case "tcp":
conn, err = srv.dialTCP(conn, addr)
if err != nil {
conn.Close()
return nil, fmt.Errorf("dial tcp error: %w", err)
}
return conn, nil
case "udp":
conn, err = srv.dialUDP(conn, addr)
if err != nil {
conn.Close()
return nil, fmt.Errorf("dial udp error: %w", err)
}
return conn, nil
default:
conn.Close()
return nil, fmt.Errorf("incorrect network type: %v", network)
}
}
func (srv Server) dialTCP(conn net.Conn, addr string) (net.Conn, error) {
req, err := http.NewRequest(http.MethodConnect, srv.tcpURL, nil)
if err != nil {
return conn, fmt.Errorf("request error: %w", err)
}
req.URL.Opaque = addr
if srv.BasicAuth != "" {
req.Header.Add(ProxyAuthorizationHeader, srv.BasicAuth)
}
err = req.WriteProxy(conn)
if err != nil {
return conn, fmt.Errorf("write request error: %w", err)
}
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
return conn, fmt.Errorf("read response error: %w", err)
}
if resp.StatusCode != http.StatusOK {
return conn, fmt.Errorf("server status code error: %v", resp.StatusCode)
}
return conn, nil
}
func (srv Server) dialUDP(conn net.Conn, _ string) (net.Conn, error) {
req, err := http.NewRequest(http.MethodGet, srv.udpURL, nil)
if err != nil {
return conn, fmt.Errorf("request error: %w", err)
}
req.Header.Add("Connection", "Upgrade")
req.Header.Add("Upgrade", RequestProtocol)
if srv.BasicAuth != "" {
req.Header.Add(ProxyAuthorizationHeader, srv.BasicAuth)
}
req.Header.Add(CapsuleProtocolHeader, CapsuleProtocolHeaderValue)
req.Header.Add(ConnectUDPBindHeader, ConnectUDPBindHeaderValue)
err = req.WriteProxy(conn)
if err != nil {
return conn, fmt.Errorf("write request error: %w", err)
}
resp, err := http.ReadResponse(bufio.NewReader(conn), nil)
if err != nil {
return conn, fmt.Errorf("read response error: %w", err)
}
// slog.Info(fmt.Sprintf("header is %v", resp.Header))
if str := resp.Header.Get("Connection"); str != "Upgrade" {
return conn, fmt.Errorf("header Connection error: %v", str)
}
if str := resp.Header.Get("Upgrade"); str != "connect-udp" {
// no Upgrade from server
// return nil, fmt.Errorf("header Upgrade error: %v", str)
}
if resp.StatusCode != http.StatusSwitchingProtocols {
return conn, fmt.Errorf("server status code error: %v", resp.StatusCode)
}
return conn, nil
}
var (
_ proxy.Dialer = (*tlsDialer)(nil)
_ proxy.Dialer = (*httpDialer)(nil)
)