@@ -18,15 +18,15 @@ const (
1818)
1919
2020var (
21- largeBufferSize = 32 * 1024 // 32KB large buffer
21+ largeBufferSize = 32 * 1024 // 32KB large buffer
2222 ErrNotSocks5Proxy = errors .New ("this is not a socks proxy server" )
2323)
2424
2525type BaseConfig struct {
26- ListenAddr string
27- IPRegionFlag int // 0: all 1: cannot bypass gfw 2: bypass gfw
28- Username string
29- Password string
26+ ListenAddr string
27+ IPRegionFlag int // 0: all 1: cannot bypass gfw 2: bypass gfw
28+ Username string
29+ Password string
3030 SelectStrategy int // 0: random, 1: Select the one with the shortest timeout, 2: Select the two with the shortest timeout, ...
3131}
3232
@@ -46,7 +46,6 @@ type NoAuthPreProcessor struct {
4646 cfg BaseConfig
4747}
4848
49-
5049// DownstreamPreProcess auth for socks5 server(local)
5150func (p * AuthPreProcessor ) DownstreamPreProcess (conn net.Conn ) (err error ) {
5251 buf := make ([]byte , 256 )
@@ -108,7 +107,7 @@ func (p *AuthPreProcessor) UpstreamPreProcess(conn net.Conn) (err error) {
108107 defer func () {
109108 if e := recover (); e != nil {
110109 err = fmt .Errorf ("%v" , e )
111- fmt . Printf ( "close connection: %v\n " , err )
110+ ErrorLog ( Warn ( "close connection: %v" , err ) )
112111 }
113112 }()
114113 if conn == nil {
@@ -145,7 +144,6 @@ func NewNoAuthPreProcessor(cfg BaseConfig) *NoAuthPreProcessor {
145144 return & NoAuthPreProcessor {cfg : cfg }
146145}
147146
148-
149147type RedirectClient struct {
150148 config * BaseConfig
151149
@@ -181,13 +179,13 @@ func (c *RedirectClient) Serve() error {
181179 return err
182180 }
183181 for IsProxyURLBlank () {
184- fmt . Println ( "[*] waiting for crawl proxy..." )
182+ InfoLog ( Noticeln ( "[*] waiting for crawl proxy..." ) )
185183 time .Sleep (3 * time .Second )
186184 }
187185 for {
188186 conn , err := l .Accept ()
189187 if err != nil {
190- fmt . Printf ( "[!] accept error: %v\n " , err )
188+ ErrorLog ( Warn ( "[!] accept error: %v" , err ) )
191189 continue
192190 }
193191 go c .HandleConn (conn )
@@ -206,20 +204,21 @@ func (c *RedirectClient) getValidSocks5Connection() (net.Conn, error) {
206204 cc , err = net .DialTimeout ("tcp" , key , 5 * time .Second )
207205 if err != nil {
208206 closeConn (cc )
209- fmt .Printf ("[!] cannot connect to %v\n " , key )
207+ markUnavail ()
208+ ErrorLog (Warn ("[!] cannot connect to %v" , key ))
210209 }
211- fmt . Printf ( "[*] use %v\n " , key )
210+ InfoLog ( Info ( "[*] use %v" , key ) )
212211 // write header for remote socks5 server
213212 err = c .preProcessor .UpstreamPreProcess (cc )
214213 if err != nil {
215214 closeConn (cc )
216215 if errors .Is (err , ErrNotSocks5Proxy ) {
217216 // 将该代理设置为不可用
218217 markUnavail ()
219- fmt . Println ( err )
218+ ErrorLog ( Warn ( "Error : %v" , err ) )
220219 continue
221220 }
222- fmt . Printf ( "socks handshake with downstream failed: %v\n " , err )
221+ ErrorLog ( Warn ( "socks handshake with downstream failed: %v" , err ) )
223222 continue
224223 }
225224 break
@@ -232,26 +231,26 @@ func (c *RedirectClient) HandleConn(conn net.Conn) {
232231 // auth for local socks5 serer
233232 err := c .preProcessor .DownstreamPreProcess (conn )
234233 if err != nil {
235- fmt . Printf ( "[!] socks handshake with downstream failed: %v\n " , err )
234+ ErrorLog ( Warn ( "[!] socks handshake with downstream failed: %v" , err ) )
236235 return
237236 }
238237 cc , err := c .getValidSocks5Connection ()
239238 if err != nil {
240- fmt . Printf ( "[!] getValidSocks5Connection failed: %v\n " , err )
239+ ErrorLog ( Warn ( "[!] getValidSocks5Connection failed: %v" , err ) )
241240 return
242241 }
243242 defer closeConn (cc )
244243 err = transport (conn , cc )
245244 if err != nil {
246- fmt . Printf ( "[!] transport error: %v\n " , err )
245+ ErrorLog ( Warn ( "[!] transport error: %v" , err ) )
247246 }
248247}
249248
250249func closeConn (conn net.Conn ) (err error ) {
251250 defer func () {
252251 if e := recover (); e != nil {
253252 err = fmt .Errorf ("%v" , e )
254- fmt . Printf ( "[*] close connection: %v\n " , err )
253+ ErrorLog ( Warn ( "[*] close connection: %v" , err ) )
255254 }
256255 }()
257256 err = conn .Close ()
@@ -260,11 +259,11 @@ func closeConn(conn net.Conn) (err error) {
260259
261260func transport (rw1 , rw2 io.ReadWriter ) error {
262261 g , _ := errgroup .WithContext (context .Background ())
263- g .Go (func () error {
262+ g .Go (func () error {
264263 return copyBuffer (rw1 , rw2 )
265264 })
266265
267- g .Go (func () error {
266+ g .Go (func () error {
268267 return copyBuffer (rw2 , rw1 )
269268 })
270269 var err error
0 commit comments