-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
367 lines (334 loc) · 12 KB
/
main.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"github.com/WofWca/snowflake-generalized/common"
pionUDP "github.com/pion/transport/v3/udp"
"github.com/xtaci/smux"
safelog "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/ptutil/safelog"
snowflakeClient "gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/client/lib"
)
func main() {
// For the list of parameters of the original client, see
// - https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/97e21e3a29f8dd8306ed893a8341ce91846b02f7/client/snowflake.go#L166-179
// - https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/97e21e3a29f8dd8306ed893a8341ce91846b02f7/client/snowflake.go#L80-128
// TODO maybe spin up a public broker?
brokerURL := flag.String("broker-url", "", "URL of signaling broker")
serverId := flag.String(
"server-id",
"",
"40 hex character server ID to which to forward the connections."+
"See also \"server-url\".",
)
serverUrl := flag.String(
"server-url",
"",
"Server `URL` to which to forward the connections",
)
listenAddr := flag.String(
"listen-address",
"localhost:2080",
"Listen for application connections on this `address` and forward them to the server",
)
destinationProtocol := flag.String(
"destination-protocol",
"tcp",
"what type of packets to forward, i.e. what protocol the target "+
"application (WireGuard, SOCKS server) is using, \"udp\" or \"tcp\".\n"+
"This value must be the same on the target server",
)
// noTCP := flag.Bool("no-tcp", false)
// noUDP := flag.Bool("no-udp", false)
// TODO also add socket file support or something like that.
// https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40131
// ssh appears to have something like this.
//
// TODO perf: in UDP mode, make the client-server connection
// unreliable and unordered. When
// https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40352
// is done.
singleConnMode := flag.Bool(
"single-connection-mode",
false,
"If you want to do only a single TCP / UDP"+
" connection to the destination"+
" (e.g. if the destination server is a WireGuard"+
" or an OpenVPN server), you can toggle this flag on."+
"\nIt turns off multiplexing, and thus it _might_"+
" improve connection performance."+
"\nThe value of this flag must be the same for both"+
" the server and the client.",
)
serverIsOldVersion := flag.Bool(
"server-is-old-version",
false,
"Prior to 2025-01-13, we used smux version 1 instead of the latest 2."+
" If the server is of that older version, use this flag."+
"\nThis flag has no effect when using single-connection-mode",
)
iceServersCommas := flag.String(
"ice",
// Copy-pasted from
// curl https://gitlab.torproject.org/tpo/applications/tor-browser-build/-/raw/main/projects/tor-expert-bundle/pt_config.json | grep "ice=.* "
"stun:stun.antisip.com:3478,stun:stun.epygi.com:3478,stun:stun.uls.co.za:3478,stun:stun.voipgate.com:3478,stun:stun.mixvoip.com:3478,stun:stun.nextcloud.com:3478,stun:stun.bethesda.net:3478,stun:stun.nextcloud.com:443",
"comma-separated list of ICE servers",
)
frontDomainsCommas := flag.String("fronts", "", "comma-separated list of front domains")
ampCacheURL := flag.String("ampcache", "", "URL of AMP cache to use as a proxy for signaling")
sqsQueueURL := flag.String("sqsqueue", "", "URL of SQS Queue to use as a proxy for signaling")
sqsCredsStr := flag.String("sqscreds", "", "credentials to access SQS Queue")
utlsNoSni := flag.Bool("utls-nosni", false, "remove SNI from Client Hello")
utlsImitate := flag.String(
"utls-imitate",
"",
"imitate TLS client hello fingerprint of other client.\nPossible values: https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/33248f3dec5594c985cfd11e6c6143ddaa5613c0/common/utls/client_hello_id.go#L13-27",
)
// logFilename := flag.String("log", "", "name of log file")
keepLocalAddresses := flag.Bool(
"keep-local-addresses",
false,
"keep local LAN address ICE candidates",
)
unsafeLogging := flag.Bool("unsafe-logging", false, "prevent logs from being scrubbed")
max := flag.Int("max", 1,
"capacity for number of multiplexed WebRTC peers")
// versionFlag := flag.Bool("version", false, "display version info to stderr and quit")
flag.Parse()
if *brokerURL == "" {
flag.Usage()
log.Fatal("\"broker-url\" must be specified because the default broker only supports Tor relays.\nSee https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/issues/40166")
}
if *serverUrl == "" && *serverId == "" {
flag.Usage()
log.Fatal("Specify \"server-url\" or \"server-id\"")
} else if *serverUrl != "" && *serverId != "" {
flag.Usage()
log.Fatal("Don't specify both \"server-url\" and \"server-id\"")
}
var listener net.Listener
switch *destinationProtocol {
case "tcp":
listenAddrStruct, err := net.ResolveTCPAddr("tcp", *listenAddr)
if err != nil {
log.Fatal(err)
}
listener, err = net.ListenTCP("tcp", listenAddrStruct)
if err != nil {
log.Fatalf(
"Failed to listen on \"%v\" %v: %v",
*listenAddr,
destinationProtocol,
err,
)
}
case "udp":
listenAddrStruct, err := net.ResolveUDPAddr("udp", *listenAddr)
if err != nil {
log.Fatal(err)
}
listener, err = pionUDP.Listen("udp", listenAddrStruct)
if err != nil {
log.Fatalf(
"Failed to listen on \"%v\" %v: %v",
*listenAddr,
destinationProtocol,
err,
)
}
default:
log.Fatal("`destination-protocol` parameter value must either be \"tcp\" or \"udp\"")
}
var frontDomains []string
if *frontDomainsCommas != "" {
frontDomains = strings.Split(strings.TrimSpace(*frontDomainsCommas), ",")
}
// Setting scrubber _after_ initial checks
// so that addresses are printed properly.
logOutput := os.Stdout
if *unsafeLogging {
log.SetOutput(logOutput)
} else {
log.SetOutput(&safelog.LogScrubber{Output: logOutput})
}
config := snowflakeClient.ClientConfig{
BrokerURL: *brokerURL,
BridgeFingerprint: *serverId,
RelayURL: *serverUrl,
FrontDomains: frontDomains,
AmpCacheURL: *ampCacheURL,
SQSQueueURL: *sqsQueueURL,
SQSCredsStr: *sqsCredsStr,
ICEAddresses: strings.Split(strings.TrimSpace(*iceServersCommas), ","),
KeepLocalAddresses: *keepLocalAddresses,
Max: *max,
UTLSRemoveSNI: *utlsNoSni,
UTLSClientID: *utlsImitate,
}
snowflakeClientTransport, err := snowflakeClient.NewSnowflakeClient(config)
if err != nil {
log.Fatal("Failed to start snowflake transport: ", err)
}
var idOrUrlString string
if *serverUrl != "" {
idOrUrlString = *serverUrl
} else {
idOrUrlString = fmt.Sprintf("with ID %v", serverId)
}
log.Printf(
"Forwarding %v connections to \"%v\" to server %v",
*destinationProtocol,
*listenAddr,
idOrUrlString,
)
if *singleConnMode {
for {
err := serveOneConnInSingleConnMode(listener, snowflakeClientTransport)
if err != nil {
return
}
}
} else {
snowflakeClientConn, err := snowflakeClientTransport.Dial()
if err != nil {
// TODO should we retry?
log.Fatal("Snowflake dial failed", err)
}
// Why use a multiplexer instead of `snowflakeClientTransport.Dial()`-ing
// per each TCP connection?
// Firstly, connecting to a new proxy takes some seconds
// (sometimes minutes!).
// This is not great if we expect to receive connections frequently.
// E.g. for the case of SOCKS proxy. A browser SOCKS client
// makes a new TCP connection per nearly every HTTP request,
// so each request would be delayed by the amount of time
// it takes to get a new Snowflake proxy.
//
// Secondly, apparently the previous `Dial()` gets discarded
// and connection lost.
//
// https://github.com/xtaci/smux?tab=readme-ov-file#usage
//
// TODO perf: Snowflake already uses smux internally.
// Can we maybe modify the library so that it exposes the session
// so we can use it?
// https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/bf116939935b0a2ae2adf4f5976c349aae96e48b/client/lib/snowflake.go#L211-212
smuxConfig := smux.DefaultConfig()
if *serverIsOldVersion {
smuxConfig.Version = 1
} else {
// This seems to ~double TCP connection speed, as of 2025-01-13.
// Or so did it look based on a single test.
smuxConfig.Version = 2
}
// Connecting with Snowflake might take some minutes sometimes.
// Let's not close the connection on our own, and let Snowflake handle that.
//
// TODO we probably don't want to terminate the client at all and
// just keep retrying.
smuxConfig.KeepAliveDisabled = true
// This seems to increase download speed by about x2,
// at least for the SOCKS example, based on eyeball tests.
// See https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/merge_requests/48
smuxConfig.MaxStreamBuffer = snowflakeClient.StreamSize
snowflakeClientMuxSession, err := smux.Client(snowflakeClientConn, smuxConfig)
if err != nil {
log.Fatal(err)
}
muxModeAcceptLoop(listener, snowflakeClientMuxSession)
}
}
func muxModeAcceptLoop(
ln net.Listener,
snowflakeClientMuxSession *smux.Session,
) {
for {
netConn, err := ln.Accept()
if err != nil {
if err, ok := err.(net.Error); ok && err.Temporary() {
continue
}
log.Print("Failed to accept connection", err)
// TODO is this what we want? This will terminate the client.
break
}
log.Printf(
"Got new connection from %v! Forwarding",
netConn.RemoteAddr().String(),
)
go func() {
defer netConn.Close()
// TODO handle errors carefully.
// E.g. there is `ErrGoAway` which occurs when stream IDs
// get exhausted, and when that happens,
// we can never open a new stream, which means that
// we probably need to recreate a smux session.
snowflakeStream, err := snowflakeClientMuxSession.OpenStream()
if err != nil {
log.Print("smux.OpenStream() failed: ", err)
return
}
defer snowflakeStream.Close()
// TODO should we utilize `shutdownChan`?
shutdownChan := make(chan struct{})
common.CopyLoop(snowflakeStream, netConn, shutdownChan)
log.Printf(
"Connection ended %v (stream %v)",
netConn.RemoteAddr().String(),
snowflakeStream.ID(),
)
}()
}
}
// If an error is returned, this function should not be called another time.
func serveOneConnInSingleConnMode(
ln net.Listener,
snowflakeClientTransport *snowflakeClient.Transport,
) error {
snowflakeClientConn, err := snowflakeClientTransport.Dial()
if err != nil {
// TODO should we retry? With a timeout?
log.Print("Snowflake dial failed", err)
return err
}
defer snowflakeClientConn.Close()
// TODO it looks like the connection doesn't actually get fully closed.
// You can reproduce by doing a bunch of
// `curl localhost:2080` + Ctrl + C.
// The client will be printing `Traffic Bytes (in|out)`
// a lot more frequently.
// Maybe we really need to create a new `snowflakeClientTransport`
// for each `ln.Accept()`.
netConn, err := ln.Accept()
if err != nil {
log.Print("Failed to accept connection", err)
if err, ok := err.(net.Error); ok && err.Temporary() {
return nil
}
// TODO is this what we want? This will terminate the client.
return err
}
defer netConn.Close()
log.Printf(
"Got new connection from %v! Forwarding",
netConn.RemoteAddr().String(),
)
// Perhaps instead of blocking here we could make a new
// Snowflake client connection per each network connection,
// instead of never doing `ln.Accept()`.
// Though remember that apparently `snowflakeClientTransport.Dial()`
// closes all the previous `snowflakeClientConn` for the instance of
// `snowflakeClientTransport`,
// so a new `snowflakeClientTransport` needs to be created every time.
// TODO should we utilize `shutdownChan`?
shutdownChan := make(chan struct{})
common.CopyLoop(snowflakeClientConn, netConn, shutdownChan)
log.Printf(
"Connection ended %v",
netConn.RemoteAddr().String(),
)
return nil
}