@@ -4,15 +4,13 @@ import (
44 "context"
55 "errors"
66 "fmt"
7- "io"
87 "net"
98 "slices"
109 "strings"
11- "sync"
1210 "time"
1311
12+ socks5Proxy "github.com/haxii/socks5"
1413 "github.com/ipfs/go-log/v2"
15- pool "github.com/libp2p/go-buffer-pool"
1614 "github.com/libp2p/go-libp2p/core/network"
1715
1816 "github.com/anywherelan/awl/config"
@@ -129,7 +127,7 @@ func (s *SOCKS5) ProxyStreamHandler(stream network.Stream) {
129127 // e.g reader on the other side may not read everything we sent because of stream.Reset()
130128 // in case of socks5 errors (small payload), receiver could get EOF
131129 // TODO: make better workaround for this. stream.CloseWrite(), etc doesn't help
132- time .Sleep (20 * time .Millisecond )
130+ time .Sleep (50 * time .Millisecond )
133131}
134132
135133func (s * SOCKS5 ) ServeConns (ctx context.Context ) {
@@ -144,6 +142,8 @@ func (s *SOCKS5) ServeConns(ctx context.Context) {
144142 _ = conn .Close ()
145143 }()
146144
145+ s .logger .Debug ("got new SOCKS5 proxy client connection" )
146+
147147 err := s .proxyConn (ctx , conn )
148148 if err != nil {
149149 _ = s .server .SendServerFailureReply (conn )
@@ -191,50 +191,25 @@ func (s *SOCKS5) proxyConn(ctx context.Context, conn net.Conn) error {
191191
192192 s .handleStream (conn , stream )
193193
194+ // stream.Write() + stream.Reset() are not guaranteed to run sequentially
195+ // e.g reader on the other side may not read everything we sent because of stream.Reset()
196+ // in case of socks5 errors (small payload), receiver could get EOF
197+ // TODO: make better workaround for this. stream.CloseWrite(), etc doesn't help
198+ time .Sleep (50 * time .Millisecond )
199+
194200 return nil
195201}
196202
197203func (s * SOCKS5 ) handleStream (conn net.Conn , stream network.Stream ) {
198- // TODO: SetDeadline on conn for ~5 min just in case?
199- wg := & sync.WaitGroup {}
200- wg .Add (2 )
204+ doneCh := make (chan struct {})
201205 go func () {
202- defer wg . Done ( )
206+ defer close ( doneCh )
203207 // Copy from conn to stream
204- _ = s .copyStream (conn , stream )
205- }()
206-
207- go func () {
208- defer wg .Done ()
209- // Copy from stream to conn
210- _ = s .copyStream (stream , conn )
211- }()
212-
213- wg .Wait ()
214- }
215-
216- func (s * SOCKS5 ) copyStream (from io.ReadCloser , to io.WriteCloser ) error {
217- const bufSize = 32 * 1024
218- buf := pool .Get (bufSize )
219-
220- defer func () {
221- pool .Put (buf )
208+ _ = socks5Proxy .ProxyStream (conn , stream )
222209 }()
223- _ , err := io .CopyBuffer (to , from , buf )
224210
225- type closeWriter interface {
226- CloseWrite () error
227- }
228- if conn , ok := to .(closeWriter ); ok {
229- _ = conn .CloseWrite ()
230- }
231-
232- type closeReader interface {
233- CloseRead () error
234- }
235- if conn , ok := from .(closeReader ); ok {
236- _ = conn .CloseRead ()
237- }
211+ // Copy from stream to conn
212+ _ = socks5Proxy .ProxyStream (stream , conn )
238213
239- return err
214+ <- doneCh
240215}
0 commit comments