-
Notifications
You must be signed in to change notification settings - Fork 654
/
client.go
171 lines (140 loc) · 3.6 KB
/
client.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
package goflyway
import (
"context"
"encoding/binary"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"github.com/coyove/goflyway/toh"
"github.com/coyove/goflyway/v"
"net"
)
type ClientConfig struct {
commonConfig
Upstream string
Bind string
URLHeader string
PathPattern string
WebSocket bool
VPN bool
Dynamic bool
}
func NewClient(localaddr string, config *ClientConfig) error {
config.check()
tr := *http.DefaultTransport.(*http.Transport)
if config.VPN {
tr.DialContext = func(ctx context.Context, network string, address string) (net.Conn, error) {
return vpnDial(address)
}
}
dialer := toh.NewDialer(config.Key, config.Upstream,
toh.WithWebSocket(config.WebSocket),
toh.WithInactiveTimeout(config.Timeout),
toh.WithTransport(&tr),
toh.WithMaxWriteBuffer(int(config.WriteBuffer)),
toh.WithHeader(config.URLHeader))
mux, err := net.Listen("tcp", localaddr)
if err != nil {
return err
}
for {
conn, err := mux.Accept()
if err != nil {
return err
}
go func(conn net.Conn) {
downconn := toh.NewBufConn(conn)
defer conn.Close()
var bind = config.Bind
if config.Dynamic {
dst, err := handleSOCKS5(downconn)
if err != nil {
v.Eprint("SOCKS5 server error: ", err)
return
}
bind = dst
v.Vprint("SOCKS5 destination: ", dst)
}
up, err := dialer.Dial()
if err != nil {
v.Eprint("dial server: ", err)
return
}
defer up.Close()
upconn := toh.NewBufConn(up)
if _, err := upconn.Write([]byte(bind + "\n")); err != nil {
v.Eprint("failed to req: ", err)
return
}
resp, err := upconn.ReadBytes('\n')
if err != nil || string(resp) != "OK\n" {
v.Eprint("server failed to ack: ", err, ", resp: ", string(resp))
return
}
if config.Dynamic {
// SOCKS5 OK response
downconn.Write([]byte{0x05, 0, 0, 1, 0, 0, 0, 0, 0, 0})
}
Bridge(upconn, downconn, nil, config.Stat)
}(conn)
}
}
func handleSOCKS5(conn net.Conn) (string, error) {
buf := make([]byte, 256)
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
return "", fmt.Errorf("failed to read header: %v", err)
}
if buf[0] != 0x05 {
return "", fmt.Errorf("unsupported SOCKS version: %v", buf[0])
}
numMethods := int(buf[1])
if _, err := io.ReadFull(conn, buf[:numMethods]); err != nil {
return "", fmt.Errorf("failed to read methods: %v", err)
}
if numMethods > 1 {
v.VVVprint("client supported methods: ", buf[:numMethods])
}
// TODO: auth
if _, err := conn.Write([]byte{0x05, 0}); err != nil {
return "", fmt.Errorf("failed to handshake: %v", err)
}
// read destination
_, err := io.ReadFull(conn, buf[:3+1])
if err != nil {
return "", fmt.Errorf("failed to read destination: %v", err)
}
var addrsize int
var method = buf[3]
switch method {
case 0x01:
addrsize = net.IPv4len + 2
case 0x04:
addrsize = net.IPv6len + 2
case 0x03:
// read one extra byte that indicates the length of the domain
if _, err := io.ReadFull(conn, buf[:1]); err != nil {
return "", fmt.Errorf("failed to read domain destination: %v", err)
}
addrsize = int(buf[0]) + 2
default:
return "", fmt.Errorf("invalid address type: %v", buf[3])
}
if _, err = io.ReadFull(conn, buf[:addrsize]); err != nil {
return "", fmt.Errorf("failed to read destination: %v", err)
}
var host string
var port = strconv.Itoa(int(binary.BigEndian.Uint16(buf[addrsize-2 : addrsize])))
switch method {
case 0x01, 0x04:
host = net.IP(buf[:addrsize-2]).String()
default:
host = string(buf[:addrsize-2])
}
if strings.Contains(host, ":") {
// IPv6?
host = "[" + host + "]"
}
return host + ":" + port, nil
}