@@ -74,6 +74,10 @@ type Listener struct {
74
74
// counts the bytes consumed by the experiment.
75
75
ExperimentByteCounter * bytecounter.Counter
76
76
77
+ // ListenSocks is OPTIONAL and allows you to override the
78
+ // function called by default to listen for SOCKS5.
79
+ ListenSocks func (network string , laddr string ) (SocksListener , error )
80
+
77
81
// Logger is the OPTIONAL logger. When not set, this library
78
82
// will not emit logs. (But the underlying pluggable transport
79
83
// may still emit its own log messages.)
@@ -98,10 +102,7 @@ type Listener struct {
98
102
laddr net.Addr
99
103
100
104
// listener allows us to stop the listener.
101
- listener ptxSocksListener
102
-
103
- // overrideListenSocks allows us to override pt.ListenSocks.
104
- overrideListenSocks func (network string , laddr string ) (ptxSocksListener , error )
105
+ listener SocksListener
105
106
}
106
107
107
108
// logger returns the Logger, if set, or the defaultLogger.
@@ -148,7 +149,7 @@ func (lst *Listener) forwardWithContext(ctx context.Context, left, right net.Con
148
149
// handleSocksConn handles a new SocksConn connection by establishing
149
150
// the corresponding PT connection and forwarding traffic. This
150
151
// function TAKES OWNERSHIP of the socksConn argument.
151
- func (lst * Listener ) handleSocksConn (ctx context.Context , socksConn ptxSocksConn ) error {
152
+ func (lst * Listener ) handleSocksConn (ctx context.Context , socksConn SocksConn ) error {
152
153
err := socksConn .Grant (& net.TCPAddr {IP : net .IPv4zero , Port : 0 })
153
154
if err != nil {
154
155
lst .logger ().Warnf ("ptx: socksConn.Grant error: %s" , err )
@@ -169,10 +170,10 @@ func (lst *Listener) handleSocksConn(ctx context.Context, socksConn ptxSocksConn
169
170
return nil // used for testing
170
171
}
171
172
172
- // ptxSocksListener is a pt.SocksListener-like structure .
173
- type ptxSocksListener interface {
173
+ // SocksListener is the listener for socks connections .
174
+ type SocksListener interface {
174
175
// AcceptSocks accepts a socks conn
175
- AcceptSocks () (ptxSocksConn , error )
176
+ AcceptSocks () (SocksConn , error )
176
177
177
178
// Addr returns the listening address.
178
179
Addr () net.Addr
@@ -181,8 +182,8 @@ type ptxSocksListener interface {
181
182
Close () error
182
183
}
183
184
184
- // ptxSocksConn is a pt.SocksConn-like structure .
185
- type ptxSocksConn interface {
185
+ // SocksConn is a SOCKS connection .
186
+ type SocksConn interface {
186
187
// net.Conn is the embedded interface.
187
188
net.Conn
188
189
@@ -192,7 +193,7 @@ type ptxSocksConn interface {
192
193
193
194
// acceptLoop accepts and handles local socks connection. This function
194
195
// DOES NOT take ownership of the socks listener.
195
- func (lst * Listener ) acceptLoop (ctx context.Context , ln ptxSocksListener ) {
196
+ func (lst * Listener ) acceptLoop (ctx context.Context , ln SocksListener ) {
196
197
for {
197
198
conn , err := ln .AcceptSocks ()
198
199
if err != nil {
@@ -243,15 +244,15 @@ func (lst *Listener) Start() error {
243
244
}
244
245
245
246
// listenSocks calles either pt.ListenSocks or lst.overrideListenSocks.
246
- func (lst * Listener ) listenSocks (network string , laddr string ) (ptxSocksListener , error ) {
247
- if lst .overrideListenSocks != nil {
248
- return lst .overrideListenSocks (network , laddr )
247
+ func (lst * Listener ) listenSocks (network string , laddr string ) (SocksListener , error ) {
248
+ if lst .ListenSocks != nil {
249
+ return lst .ListenSocks (network , laddr )
249
250
}
250
251
return lst .castListener (pt .ListenSocks (network , laddr ))
251
252
}
252
253
253
254
// castListener casts a pt.SocksListener to ptxSocksListener.
254
- func (lst * Listener ) castListener (in * pt.SocksListener , err error ) (ptxSocksListener , error ) {
255
+ func (lst * Listener ) castListener (in * pt.SocksListener , err error ) (SocksListener , error ) {
255
256
if err != nil {
256
257
return nil , err
257
258
}
@@ -264,7 +265,7 @@ type ptxSocksListenerAdapter struct {
264
265
}
265
266
266
267
// AcceptSocks adapts pt.SocksListener.AcceptSocks to ptxSockListener.AcceptSocks.
267
- func (la * ptxSocksListenerAdapter ) AcceptSocks () (ptxSocksConn , error ) {
268
+ func (la * ptxSocksListenerAdapter ) AcceptSocks () (SocksConn , error ) {
268
269
return la .SocksListener .AcceptSocks ()
269
270
}
270
271
@@ -307,11 +308,11 @@ func (lst *Listener) Stop() {
307
308
// Assuming that we are listening at 127.0.0.1:12345, then this
308
309
// function will return the following string:
309
310
//
310
- // obfs4 socks5 127.0.0.1:12345
311
+ // obfs4 socks5 127.0.0.1:12345
311
312
//
312
313
// The correct configuration line for the `torrc` would be:
313
314
//
314
- // ClientTransportPlugin obfs4 socks5 127.0.0.1:12345
315
+ // ClientTransportPlugin obfs4 socks5 127.0.0.1:12345
315
316
//
316
317
// Since we pass configuration to tor using the command line, it
317
318
// is more convenient to us to avoid including ClientTransportPlugin
0 commit comments