-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
282 lines (239 loc) · 6.71 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
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
package quictun
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"time"
"github.com/julienschmidt/quictun/internal/atomic"
"github.com/julienschmidt/quictun/internal/socks"
"golang.org/x/net/http2"
"golang.org/x/net/http2/hpack"
quic "github.com/lucas-clemente/quic-go"
)
const protocolIdentifier = "QTP/0.1"
var (
ErrInvalidResponse = errors.New("server returned an invalid response")
ErrInvalidSequence = errors.New("client sequence number invalid")
ErrNotAQuictunServer = errors.New("server does not seems to be a quictun server")
ErrWrongCredentials = errors.New("authentication credentials seems to be wrong")
)
// Client holds the configuration and state of a quictun client
type Client struct {
// config
ListenAddr string
TunnelAddr string
UserAgent string
TlsCfg *tls.Config
QuicConfig *quic.Config
DialTimeout time.Duration
// state
session quic.Session
connected atomic.Bool
// replay protection
clientID uint64
sequenceNumber uint32
// header
headerStream quic.Stream
hDecoder *hpack.Decoder
h2framer *http2.Framer
}
func (c *Client) generateClientID() {
// generate clientID
rand.Seed(time.Now().UnixNano())
c.clientID = rand.Uint64()
}
func (c *Client) connect() error {
authURL := c.TunnelAddr
// extract hostname from auth url
uri, err := url.ParseRequestURI(authURL)
if err != nil {
log.Fatal("Invalid Auth URL: ", err)
return err
}
hostname := authorityAddr(uri.Hostname(), uri.Port())
fmt.Println("Connecting to", hostname)
c.session, err = quic.DialAddr(hostname, c.TlsCfg, c.QuicConfig)
if err != nil {
log.Fatal("Dial Err: ", err)
return err
}
// once the version has been negotiated, open the header stream
c.headerStream, err = c.session.OpenStream()
if err != nil {
log.Fatal("OpenStream Err: ", err)
return err
}
//fmt.Println("Header StreamID:", c.headerStream.StreamID())
dataStream, err := c.session.OpenStreamSync()
if err != nil {
log.Fatal("OpenStreamSync Err: ", err)
}
//fmt.Println("Data StreamID:", dataStream.StreamID())
// build HTTP request
// The authorization credentials are automatically encoded from the URL
req, err := http.NewRequest("GET", authURL, nil)
if err != nil {
log.Fatal("NewRequest Err: ", err)
return err
}
req.Header.Set("User-Agent", c.UserAgent)
// request protocol upgrade
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", protocolIdentifier)
// replay protection
c.sequenceNumber++
req.Header.Set("QTP", fmt.Sprintf("%016X%08X", c.clientID, c.sequenceNumber))
rw := newRequestWriter(c.headerStream)
endStream := true //endStream := !hasBody
fmt.Println("requesting", authURL)
err = rw.WriteRequest(req, dataStream.StreamID(), endStream)
if err != nil {
log.Fatal("WriteHeaders Err: ", err)
}
fmt.Println("Waiting...")
// read frames from headerStream
c.h2framer = http2.NewFramer(nil, c.headerStream)
c.hDecoder = hpack.NewDecoder(4096, func(hf hpack.HeaderField) {})
frame, err := c.h2framer.ReadFrame()
if err != nil {
// c.headerErr = qerr.Error(qerr.HeadersStreamDataDecompressFailure, "cannot read frame")
log.Fatal("cannot read frame: ", err)
}
hframe, ok := frame.(*http2.HeadersFrame)
if !ok {
// c.headerErr = qerr.Error(qerr.InvalidHeadersStreamData, "not a headers frame")
log.Fatal("not a headers frame: ", err)
}
mhframe := &http2.MetaHeadersFrame{HeadersFrame: hframe}
mhframe.Fields, err = c.hDecoder.DecodeFull(hframe.HeaderBlockFragment())
if err != nil {
// c.headerErr = qerr.Error(qerr.InvalidHeadersStreamData, "cannot read header fields")
log.Fatal("cannot read header fields: ", err)
}
//fmt.Println("Frame for StreamID:", hframe.StreamID)
rsp, err := responseFromHeaders(mhframe)
if err != nil {
log.Fatal("responseFromHeaders: ", err)
}
switch rsp.StatusCode {
case http.StatusSwitchingProtocols:
header := rsp.Header
if header.Get("Connection") != "Upgrade" {
return ErrInvalidResponse
}
if header.Get("Upgrade") != protocolIdentifier {
return ErrNotAQuictunServer
}
return nil
case http.StatusUnauthorized, http.StatusForbidden:
return ErrWrongCredentials
case http.StatusBadRequest:
c.generateClientID()
return ErrInvalidSequence
default:
return ErrInvalidResponse
}
}
func (c *Client) watchCancel() {
session := c.session
if session == nil {
fmt.Println("session is nil")
return
}
ctx := session.Context()
if ctx == nil {
fmt.Println("ctx is nil")
return
}
// TODO: add graceful shutdown channel
<-ctx.Done()
fmt.Println("session closed", ctx.Err())
c.connected.Set(false)
}
func (c *Client) tunnelConn(local net.Conn) {
local.(*net.TCPConn).SetKeepAlive(true)
// TODO: SetReadTimeout(conn)
localRd := bufio.NewReader(local)
// initiate SOCKS connection
if err := socks.Auth(localRd, local); err != nil {
fmt.Println(err)
local.Close()
return
}
req, err := socks.PeekRequest(localRd)
if err != nil {
fmt.Println(err)
socks.SendReply(local, socks.StatusConnectionRefused, nil)
local.Close()
return
}
fmt.Println("request", req.Dest())
switch req.Cmd() {
case socks.CmdConnect:
fmt.Println("[Connect]")
if err = socks.SendReply(local, socks.StatusSucceeded, nil); err != nil {
fmt.Println(err)
local.Close()
return
}
default:
socks.SendReply(local, socks.StatusCmdNotSupported, nil)
local.Close()
return
}
// TODO: check connected status again and reconnect if necessary
stream, err := c.session.OpenStreamSync()
if err != nil {
fmt.Println("open stream err", err)
local.Close()
return
}
fmt.Println("Start proxying...")
go proxy(local, stream) // recv from stream and send to local
proxy(stream, localRd) // recv from local and send to stream
}
// Close closes the client
func (c *Client) close(err error) error {
if c.session == nil {
return nil
}
return c.session.Close(err)
}
// Run starts the client to accept incoming SOCKS connections, which are tunneled
// to the configured quictun server.
// The tunnel connection is opened only on-demand.
func (c *Client) Run() error {
c.generateClientID()
listener, err := net.Listen("tcp", c.ListenAddr)
if err != nil {
return fmt.Errorf("Failed to listen on %s: %s", c.ListenAddr, err)
}
fmt.Println("Listening for incoming SOCKS connection...")
// accept local connections and tunnel them
for {
conn, err := listener.Accept()
if err != nil {
log.Println("Accept Err:", err)
continue
}
fmt.Println("new SOCKS conn", conn.RemoteAddr().String())
if !c.connected.IsSet() {
err = c.connect()
if err != nil {
fmt.Println("Failed to connect to tunnel host:", err)
conn.Close()
continue
}
// start watcher which closes when canceled
go c.watchCancel()
c.connected.Set(true)
}
go c.tunnelConn(conn)
}
}