-
Notifications
You must be signed in to change notification settings - Fork 15
/
cdn2proxy.go
298 lines (257 loc) · 6.25 KB
/
cdn2proxy.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package cdn2proxy
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/gorilla/websocket"
"github.com/ncruces/go-dns"
)
var (
DestAddr = "127.0.0.1:8000"
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
)
// use this logger
var Logger = log.New(os.Stderr, "cdn2proxy: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
// StartServer start websocket server
// port: listen on 127.0.0.1:port
// destAddr: send everything here, we only want a single purpose proxy
// wspath: access this path to reach ws service, eg. /ws
// if you use go-cdn2proxy along with other HTTP server, prevent multiple `/`
func StartServer(port, destAddr, wspath string, logOutput io.Writer) (err error) {
// set DestAddr
DestAddr = destAddr
// set log output
Logger = log.New(logOutput, "cdn2proxy server: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
// HTTP server
Logger.Printf("websocket server listening on 127.0.0.1:%s", port)
http.HandleFunc("/"+wspath, serveWS)
err = http.ListenAndServe("127.0.0.1:"+port, nil)
if err != nil {
Logger.Fatal(err)
}
return
}
func serveWS(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
Logger.Println(err)
return
}
wsconn := ws.UnderlyingConn()
ctx, cancel := context.WithCancel(context.Background())
Logger.Printf("Got a connection to websocket server: %s", ws.RemoteAddr())
defer func() {
cancel()
ws.Close()
}()
// connect to destination
conn, err := net.Dial("tcp", DestAddr)
if err != nil {
Logger.Printf("Cannot dial destination %s: %v", DestAddr, err)
return
}
defer conn.Close()
go func() {
defer cancel()
_, err := io.Copy(conn, wsconn)
if err != nil {
Logger.Printf("serveWS ioCopy ws->dest: %v", err)
return
}
}()
go func() {
defer cancel()
_, err := io.Copy(wsconn, conn)
if err != nil {
Logger.Printf("serveWS ioCopy dest->ws: %v", err)
return
}
}()
// keep the connection
for ctx.Err() == nil {
time.Sleep(1e9)
}
}
// StartProxy on client side, start a socks5 proxy
// url: websocket server. eg: wss://example.com/path/to/ws
// use wss instead of ws since your reverse proxy (nginx, Cloudflare)
// should be using TLS
// addr: local proxy address, used as agent's outgoing proxy
// proxy: proxy for websocket connection, if you cannot directly connect to ws server, use this
// eg. socks5://127.0.0.1:1080
// doh: DNS over HTTPS server, eg. https://9.9.9.9/dns-query, prevent DNS leaks
func StartProxy(addr, wsurl, proxy, doh string) error {
ctx, cancel := context.WithCancel(context.Background())
listener, err := net.Listen("tcp", addr)
if err != nil {
Logger.Printf("Cannot listen on %s: %v", addr, err)
cancel()
return err
}
Logger.Printf("socks proxy listening on %s", addr)
defer func() {
cancel()
listener.Close()
}()
// use DoH resolver
net.DefaultResolver, err = dns.NewDoHResolver(
doh,
dns.DoHCache())
if err != nil {
return err
}
for ctx.Err() == nil {
// dialer
dialer := websocket.DefaultDialer
// proxy
if proxy != "" {
proxyURL, err := url.Parse(proxy)
if err != nil {
return err
}
dialer.Proxy = http.ProxyURL(proxyURL)
}
ws, _, err := dialer.Dial(wsurl, nil)
if err != nil {
Logger.Printf("websocket connection to %s failed: %v", wsurl, err)
cancel()
return err
}
conn, err := listener.Accept()
if err != nil {
Logger.Print(err)
cancel()
return err
}
go handleConn(conn, ws)
}
return nil
}
func handleConn(conn net.Conn, ws *websocket.Conn) {
Logger.Printf("Got a connection to our proxy: %s", conn.RemoteAddr())
ctx, cancel := context.WithCancel(context.Background())
defer func() {
conn.Close()
ws.Close()
cancel()
}()
// socks5
// auth
if err := Socks5Auth(conn); err != nil {
fmt.Println("auth error:", err)
return
}
// parse
buf := make([]byte, 256)
n, err := io.ReadFull(conn, buf[:4])
if n != 4 {
Logger.Print("read header: " + err.Error())
return
}
ver, cmd, _, atyp := buf[0], buf[1], buf[2], buf[3]
if ver != 5 || cmd != 1 {
Logger.Print("invalid ver/cmd")
return
}
addr := ""
switch atyp {
case 1:
n, err = io.ReadFull(conn, buf[:4])
if n != 4 {
Logger.Print("invalid IPv4: " + err.Error())
return
}
addr = fmt.Sprintf("%d.%d.%d.%d", buf[0], buf[1], buf[2], buf[3])
case 3:
n, err = io.ReadFull(conn, buf[:1])
if n != 1 {
Logger.Print("invalid hostname: " + err.Error())
return
}
addrLen := int(buf[0])
n, err = io.ReadFull(conn, buf[:addrLen])
if n != addrLen {
Logger.Print("invalid hostname: " + err.Error())
return
}
addr = string(buf[:addrLen])
case 4:
Logger.Print("IPv6: no supported yet")
return
default:
Logger.Print("invalid atyp")
return
}
n, err = io.ReadFull(conn, buf[:2])
if n != 2 {
Logger.Print("read port: " + err.Error())
return
}
port := binary.BigEndian.Uint16(buf[:2])
// destination
destAddrPort := fmt.Sprintf("%s:%d", addr, port)
Logger.Printf("Client wants to connect to %s", destAddrPort)
// response
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
if err != nil {
Logger.Print("write rsp: " + err.Error())
return
}
// ws connection
wsconn := ws.UnderlyingConn()
// io copy to websocket
go func() {
defer cancel()
_, err := io.Copy(wsconn, conn)
if err != nil {
Logger.Printf("proxy handleConn ioCopy proxy->websocket: %v", err)
return
}
}()
go func() {
defer cancel()
_, err := io.Copy(conn, wsconn)
if err != nil {
Logger.Printf("proxy handleConn ioCopy websocket->proxy: %v", err)
return
}
}()
// keep the connection
for ctx.Err() == nil {
time.Sleep(1e9)
}
}
func Socks5Auth(client net.Conn) (err error) {
buf := make([]byte, 256)
// read VER and NMETHODS
n, err := io.ReadFull(client, buf[:2])
if n != 2 {
return errors.New("reading header: " + err.Error())
}
ver, nMethods := int(buf[0]), int(buf[1])
if ver != 5 {
return errors.New("invalid version")
}
// read auth methods
n, err = io.ReadFull(client, buf[:nMethods])
if n != nMethods {
return errors.New("reading methods: " + err.Error())
}
// no auth
n, err = client.Write([]byte{0x05, 0x00})
if n != 2 || err != nil {
return errors.New("write rsp: " + err.Error())
}
return nil
}