-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
260 lines (235 loc) · 8.71 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
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.
iceServersCommas := flag.String(
"ice",
// Copy-pasted from
// https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/-/blob/98db63ad01d9d78b8cd8aad77219a3d900bfdfef/client/README.md#L38
"stun:stun.l.google.com:19302,stun:stun.antisip.com:3478,stun:stun.bluesip.net:3478,stun:stun.dus.net:3478,stun:stun.epygi.com:3478,stun:stun.sonetel.com:3478,stun:stun.uls.co.za:3478,stun:stun.voipgate.com:3478,stun:stun.voys.nl:3478",
"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)
}
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()
// 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
snowflakeClientMuxSession, err := smux.Client(snowflakeClientConn, smuxConfig)
if err != nil {
log.Fatal(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,
)
acceptLoop(listener, snowflakeClientMuxSession)
}
func acceptLoop(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.Print("Got new connection! Forwarding")
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)
}()
}
}