Skip to content

Commit d9b261e

Browse files
committed
wiresocks: add deadline to connection proxying
Signed-off-by: Mark Pashmfouroush <mark@markpash.me>
1 parent 1fa7424 commit d9b261e

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ replace github.com/Psiphon-Labs/psiphon-tunnel-core => github.com/bepass-org/psi
66

77
require (
88
github.com/Psiphon-Labs/psiphon-tunnel-core v2.0.28+incompatible
9+
github.com/adrg/xdg v0.4.0
10+
github.com/carlmjohnson/versioninfo v0.22.5
911
github.com/fatih/color v1.16.0
1012
github.com/flynn/noise v1.1.0
1113
github.com/frankban/quicktest v1.14.6
@@ -16,6 +18,7 @@ require (
1618
github.com/quic-go/quic-go v0.40.1
1719
github.com/refraction-networking/utls v1.3.3
1820
github.com/rodaine/table v1.1.1
21+
github.com/things-go/go-socks5 v0.0.5
1922
golang.org/x/crypto v0.21.0
2023
golang.org/x/net v0.22.0
2124
golang.org/x/sys v0.18.0
@@ -31,11 +34,9 @@ require (
3134
github.com/Psiphon-Labs/goptlib v0.0.0-20200406165125-c0e32a7a3464 // indirect
3235
github.com/Psiphon-Labs/psiphon-tls v0.0.0-20240305020009-09f917290799 // indirect
3336
github.com/Psiphon-Labs/quic-go v0.0.0-20240305203241-7c4a760d03cc // indirect
34-
github.com/adrg/xdg v0.4.0 // indirect
3537
github.com/andybalholm/brotli v1.0.5 // indirect
3638
github.com/armon/go-proxyproto v0.0.0-20180202201750-5b7edb60ff5f // indirect
3739
github.com/bifurcation/mint v0.0.0-20180306135233-198357931e61 // indirect
38-
github.com/carlmjohnson/versioninfo v0.22.5 // indirect
3940
github.com/cheekybits/genny v0.0.0-20170328200008-9127e812e1e9 // indirect
4041
github.com/cognusion/go-cache-lru v0.0.0-20170419142635-f73e2280ecea // indirect
4142
github.com/dchest/siphash v1.2.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
204204
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
205205
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
206206
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
207+
github.com/things-go/go-socks5 v0.0.5 h1:qvKaGcBkfDrUL33SchHN93srAmYGzb4CxSM2DPYufe8=
208+
github.com/things-go/go-socks5 v0.0.5/go.mod h1:mtzInf8v5xmsBpHZVbIw2YQYhc4K0jRwzfsH64Uh0IQ=
207209
github.com/wader/filtertransport v0.0.0-20200316221534-bdd9e61eee78 h1:9sreu9e9KOihf2Y0NbpyfWhd1XFDcL4GTkPYL4IvMrg=
208210
github.com/wader/filtertransport v0.0.0-20200316221534-bdd9e61eee78/go.mod h1:HazXTRLhXFyq80TQp7PUXi6BKE6mS+ydEdzEqNBKopQ=
209211
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=

wiresocks/proxy.go

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@ package wiresocks
22

33
import (
44
"context"
5+
"errors"
56
"io"
67
"log/slog"
78
"net"
89
"net/netip"
10+
"time"
911

1012
"github.com/bepass-org/warp-plus/proxy/pkg/mixed"
1113
"github.com/bepass-org/warp-plus/proxy/pkg/statute"
1214
"github.com/bepass-org/warp-plus/wireguard/device"
1315
"github.com/bepass-org/warp-plus/wireguard/tun/netstack"
16+
"github.com/things-go/go-socks5/bufferpool"
1417
)
1518

1619
// VirtualTun stores a reference to netstack network and DNS configuration
1720
type VirtualTun struct {
18-
Tnet *netstack.Net
19-
Logger *slog.Logger
20-
Dev *device.Device
21-
Ctx context.Context
21+
Tnet *netstack.Net
22+
Logger *slog.Logger
23+
Dev *device.Device
24+
Ctx context.Context
25+
pool bufferpool.BufPool
2226
}
2327

2428
// StartProxy spawns a socks5 server.
@@ -60,12 +64,18 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
6064
done := make(chan error, 1)
6165
// Copy data from req.Conn to conn
6266
go func() {
63-
_, err := io.Copy(conn, req.Conn)
67+
req.Conn.SetReadDeadline(time.Now().Add(15 * time.Second))
68+
buf1 := vt.pool.Get()
69+
defer vt.pool.Put(buf1)
70+
_, err := copyConnTimeout(conn, req.Conn, buf1[:cap(buf1)], 15*time.Second)
6471
done <- err
6572
}()
6673
// Copy data from conn to req.Conn
6774
go func() {
68-
_, err := io.Copy(req.Conn, conn)
75+
conn.SetReadDeadline(time.Now().Add(15 * time.Second))
76+
buf2 := vt.pool.Get()
77+
defer vt.pool.Put(buf2)
78+
_, err := copyConnTimeout(req.Conn, conn, buf2[:cap(buf2)], 15*time.Second)
6979
done <- err
7080
}()
7181
// Wait for one of the copy operations to finish
@@ -75,10 +85,7 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
7585
}
7686

7787
// Close connections and wait for the other copy operation to finish
78-
conn.Close()
79-
req.Conn.Close()
8088
<-done
81-
8289
return nil
8390
}
8491

@@ -89,3 +96,44 @@ func (vt *VirtualTun) Stop() {
8996
}
9097
}
9198
}
99+
100+
var errInvalidWrite = errors.New("invalid write result")
101+
102+
func copyConnTimeout(dst net.Conn, src net.Conn, buf []byte, timeout time.Duration) (written int64, err error) {
103+
if buf != nil && len(buf) == 0 {
104+
panic("empty buffer in CopyBuffer")
105+
}
106+
107+
for {
108+
if err := src.SetReadDeadline(time.Now().Add(timeout)); err != nil {
109+
return 0, err
110+
}
111+
112+
nr, er := src.Read(buf)
113+
if nr > 0 {
114+
nw, ew := dst.Write(buf[0:nr])
115+
if nw < 0 || nr < nw {
116+
nw = 0
117+
if ew == nil {
118+
ew = errInvalidWrite
119+
}
120+
}
121+
written += int64(nw)
122+
if ew != nil {
123+
err = ew
124+
break
125+
}
126+
if nr != nw {
127+
err = io.ErrShortWrite
128+
break
129+
}
130+
}
131+
if er != nil {
132+
if er != io.EOF {
133+
err = er
134+
}
135+
break
136+
}
137+
}
138+
return written, err
139+
}

wiresocks/wiresocks.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/bepass-org/warp-plus/wireguard/conn"
1010
"github.com/bepass-org/warp-plus/wireguard/device"
1111
"github.com/bepass-org/warp-plus/wireguard/tun/netstack"
12+
"github.com/things-go/go-socks5/bufferpool"
1213
)
1314

1415
// StartWireguard creates a tun interface on netstack given a configuration
@@ -46,9 +47,10 @@ func StartWireguard(ctx context.Context, l *slog.Logger, conf *Configuration) (*
4647
}
4748

4849
return &VirtualTun{
49-
Tnet: tnet,
50-
Logger: l.With("subsystem", "vtun"),
51-
Dev: dev,
52-
Ctx: ctx,
50+
Tnet: tnet,
51+
Logger: l.With("subsystem", "vtun"),
52+
Dev: dev,
53+
Ctx: ctx,
54+
pool: bufferpool.NewPool(256 * 1024),
5355
}, nil
5456
}

0 commit comments

Comments
 (0)