From 925fbca3630a7683a595120aed516ab35f4d3c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 13 Sep 2022 10:16:10 +0800 Subject: [PATCH] Fix concurrent write --- experimental/clashapi/connections.go | 2 +- experimental/clashapi/server.go | 2 +- go.mod | 6 +-- go.sum | 12 +++--- inbound/default.go | 2 - inbound/default_udp.go | 9 +---- test/box_test.go | 18 ++++++++- test/clash_test.go | 31 ++++++++------- test/go.mod | 6 +-- test/go.sum | 12 +++--- test/hysteria_test.go | 4 +- test/trojan_test.go | 59 ++++++++++++++++++++++++++++ test/v2ray_grpc_test.go | 2 +- test/v2ray_transport_test.go | 4 +- test/v2ray_ws_test.go | 2 +- test/vmess_test.go | 2 +- transport/v2raygrpclite/conn.go | 4 ++ transport/v2raywebsocket/client.go | 3 +- transport/v2raywebsocket/conn.go | 3 +- transport/v2raywebsocket/server.go | 3 +- 20 files changed, 128 insertions(+), 58 deletions(-) diff --git a/experimental/clashapi/connections.go b/experimental/clashapi/connections.go index 73f89059..8d1e5f1a 100644 --- a/experimental/clashapi/connections.go +++ b/experimental/clashapi/connections.go @@ -8,10 +8,10 @@ import ( "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol" + "github.com/sagernet/websocket" "github.com/go-chi/chi/v5" "github.com/go-chi/render" - "github.com/gorilla/websocket" ) func connectionRouter(trafficManager *trafficontrol.Manager) http.Handler { diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index 2d473931..830ec510 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -21,11 +21,11 @@ import ( E "github.com/sagernet/sing/common/exceptions" F "github.com/sagernet/sing/common/format" N "github.com/sagernet/sing/common/network" + "github.com/sagernet/websocket" "github.com/go-chi/chi/v5" "github.com/go-chi/cors" "github.com/go-chi/render" - "github.com/gorilla/websocket" ) var _ adapter.ClashServer = (*Server)(nil) diff --git a/go.mod b/go.mod index 8d14eb8d..957211f4 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,6 @@ require ( github.com/go-chi/cors v1.2.1 github.com/go-chi/render v1.0.2 github.com/gofrs/uuid v4.2.0+incompatible - github.com/gorilla/websocket v1.5.0 github.com/hashicorp/yamux v0.1.1 github.com/logrusorgru/aurora v2.0.3+incompatible github.com/mholt/acmez v1.0.4 @@ -20,12 +19,13 @@ require ( github.com/pires/go-proxyproto v0.6.2 github.com/sagernet/certmagic v0.0.0-20220819042630-4a57f8b6853a github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb - github.com/sagernet/sing v0.0.0-20220910144724-62c4ebdbcb3f + github.com/sagernet/sing v0.0.0-20220913004915-27ddefbb8921 github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666 github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 github.com/sagernet/sing-tun v0.0.0-20220828031750-185b6c880a83 - github.com/sagernet/sing-vmess v0.0.0-20220907073918-72d7fdf6825f + github.com/sagernet/sing-vmess v0.0.0-20220913015714-c4ab86d40e12 github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 + github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 go.uber.org/atomic v1.10.0 diff --git a/go.sum b/go.sum index 5a528383..3e1d2678 100644 --- a/go.sum +++ b/go.sum @@ -73,8 +73,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= -github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= @@ -135,18 +133,20 @@ github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb h1:wc0yQ+SBn4TaTY github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb/go.mod h1:MIccjRKnPTjWwAOpl+AUGWOkzyTd9tERytudxu+1ra4= github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= -github.com/sagernet/sing v0.0.0-20220910144724-62c4ebdbcb3f h1:w1TJq7Lw3It35tDyMsZLtYz4T2msf1UK9JxC85L5+sk= -github.com/sagernet/sing v0.0.0-20220910144724-62c4ebdbcb3f/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= +github.com/sagernet/sing v0.0.0-20220913004915-27ddefbb8921 h1:xUHzlIbdlPV/fkToIO9futp9lmKIY+72ezk/whQ8XsI= +github.com/sagernet/sing v0.0.0-20220913004915-27ddefbb8921/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666 h1:XUTocA/Ek0dFxUX+xJCWMPPFZCn2GC/uLrBjTSr1vHY= github.com/sagernet/sing-dns v0.0.0-20220822023312-3e086b06d666/go.mod h1:eDyH7AJmqBGjZQdQmpZIzlbTREudZuWDExMuGKgjRVM= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 h1:JJfDeYYhWunvtxsU/mOVNTmFQmnzGx9dY034qG6G3g4= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6/go.mod h1:EX3RbZvrwAkPI2nuGa78T2iQXmrkT+/VQtskjou42xM= github.com/sagernet/sing-tun v0.0.0-20220828031750-185b6c880a83 h1:SoWiHYuOCVedqA7T/CJSZUUrcPGKQb2wFKEq8DphiAI= github.com/sagernet/sing-tun v0.0.0-20220828031750-185b6c880a83/go.mod h1:76r07HS1WRcEI4mE9pFsohfTBUt1j/G9Avz6DaOP3VU= -github.com/sagernet/sing-vmess v0.0.0-20220907073918-72d7fdf6825f h1:6l9aXZqAl1JqXJWi89KHpWnM/moQUPGG+XiwMc+yD0A= -github.com/sagernet/sing-vmess v0.0.0-20220907073918-72d7fdf6825f/go.mod h1:u66Vv7NHXJWfeAmhh7JuJp/cwxmuQlM56QoZ7B7Mmd0= +github.com/sagernet/sing-vmess v0.0.0-20220913015714-c4ab86d40e12 h1:4HYGbTDDemgBVTmaspXbkgjJlXc3hYVjNxSddJndq8Y= +github.com/sagernet/sing-vmess v0.0.0-20220913015714-c4ab86d40e12/go.mod h1:u66Vv7NHXJWfeAmhh7JuJp/cwxmuQlM56QoZ7B7Mmd0= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 h1:5VBIbVw9q7aKbrFdT83mjkyvQ+VaRsQ6yflTepfln38= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195/go.mod h1:yedWtra8nyGJ+SyI+ziwuaGMzBatbB10P1IOOZbbSK8= +github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e h1:7uw2njHFGE+VpWamge6o56j2RWk4omF6uLKKxMmcWvs= +github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e/go.mod h1:45TUl8+gH4SIKr4ykREbxKWTxkDlSzFENzctB1dVRRY= github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= diff --git a/inbound/default.go b/inbound/default.go index 76e695cf..187c688f 100644 --- a/inbound/default.go +++ b/inbound/default.go @@ -3,7 +3,6 @@ package inbound import ( "context" "net" - "sync" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/settings" @@ -42,7 +41,6 @@ type myInboundAdapter struct { tcpListener net.Listener udpConn *net.UDPConn udpAddr M.Socksaddr - packetAccess sync.RWMutex packetOutboundClosed chan struct{} packetOutbound chan *myInboundPacket } diff --git a/inbound/default_udp.go b/inbound/default_udp.go index 35850333..d0c0810c 100644 --- a/inbound/default_udp.go +++ b/inbound/default_udp.go @@ -203,17 +203,12 @@ func (s *myInboundPacketAdapter) Upstream() any { } func (s *myInboundPacketAdapter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error { - s.packetAccess.RLock() - defer s.packetAccess.RUnlock() - select { + case s.packetOutbound <- &myInboundPacket{buffer, destination}: + return nil case <-s.packetOutboundClosed: return os.ErrClosed - default: } - - s.packetOutbound <- &myInboundPacket{buffer, destination} - return nil } func (s *myInboundPacketAdapter) Close() error { diff --git a/test/box_test.go b/test/box_test.go index ef2d8767..31853011 100644 --- a/test/box_test.go +++ b/test/box_test.go @@ -82,6 +82,20 @@ func testSuitSimple(t *testing.T, clientPort uint16, testPort uint16) { require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP)) } +func testSuitSimple1(t *testing.T, clientPort uint16, testPort uint16) { + dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "") + dialTCP := func() (net.Conn, error) { + return dialer.DialContext(context.Background(), "tcp", M.ParseSocksaddrHostPort("127.0.0.1", testPort)) + } + dialUDP := func() (net.PacketConn, error) { + return dialer.ListenPacket(context.Background(), M.ParseSocksaddrHostPort("127.0.0.1", testPort)) + } + require.NoError(t, testPingPongWithConn(t, testPort, dialTCP)) + require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP)) + require.NoError(t, testPingPongWithConn(t, testPort, dialTCP)) + require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP)) +} + func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) { dialer := socks.NewClient(N.SystemDialer, M.ParseSocksaddrHostPort("127.0.0.1", clientPort), socks.Version5, "", "") dialTCP := func() (net.Conn, error) { @@ -94,8 +108,8 @@ func testSuitWg(t *testing.T, clientPort uint16, testPort uint16) { } return bufio.NewUnbindPacketConn(conn), nil } + require.NoError(t, testPingPongWithConn(t, testPort, dialTCP)) + require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP)) require.NoError(t, testLargeDataWithConn(t, testPort, dialTCP)) require.NoError(t, testLargeDataWithPacketConn(t, testPort, dialUDP)) - // require.NoError(t, testPingPongWithConn(t, testPort, dialTCP)) - // require.NoError(t, testPingPongWithPacketConn(t, testPort, dialUDP)) } diff --git a/test/clash_test.go b/test/clash_test.go index c2495d09..8915c9aa 100644 --- a/test/clash_test.go +++ b/test/clash_test.go @@ -385,21 +385,24 @@ func testLargeDataWithPacketConn(t *testing.T, port uint16, pcc func() (net.Pack hashMap := map[int][]byte{} mux := sync.Mutex{} for i := 0; i < times; i++ { - buf := make([]byte, chunkSize) - if _, err = rand.Read(buf[1:]); err != nil { - t.Log(err.Error()) - continue - } - buf[0] = byte(i) + go func(idx int) { + buf := make([]byte, chunkSize) + if _, err := rand.Read(buf[1:]); err != nil { + t.Log(err.Error()) + return + } + buf[0] = byte(idx) - hash := md5.Sum(buf) - mux.Lock() - hashMap[i] = hash[:] - mux.Unlock() - if _, err = pc.WriteTo(buf, addr); err != nil { - t.Log(err) - continue - } + hash := md5.Sum(buf) + mux.Lock() + hashMap[idx] = hash[:] + mux.Unlock() + + if _, err := pc.WriteTo(buf, addr); err != nil { + t.Log(err.Error()) + return + } + }(i) } return hashMap, nil diff --git a/test/go.mod b/test/go.mod index d85a4472..5769fc06 100644 --- a/test/go.mod +++ b/test/go.mod @@ -10,7 +10,7 @@ require ( github.com/docker/docker v20.10.17+incompatible github.com/docker/go-connections v0.4.0 github.com/gofrs/uuid v4.2.0+incompatible - github.com/sagernet/sing v0.0.0-20220903085538-02b9ca1cc133 + github.com/sagernet/sing v0.0.0-20220913004915-27ddefbb8921 github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 github.com/spyzhov/ajson v0.7.1 github.com/stretchr/testify v1.8.0 @@ -37,7 +37,6 @@ require ( github.com/golang/mock v1.6.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/btree v1.0.1 // indirect - github.com/gorilla/websocket v1.5.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect github.com/klauspost/cpuid/v2 v2.1.0 // indirect github.com/libdns/libdns v0.2.1 // indirect @@ -63,8 +62,9 @@ require ( github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb // indirect github.com/sagernet/sing-dns v0.0.0-20220903082137-b1102b8fc961 // indirect github.com/sagernet/sing-tun v0.0.0-20220828031750-185b6c880a83 // indirect - github.com/sagernet/sing-vmess v0.0.0-20220907073918-72d7fdf6825f // indirect + github.com/sagernet/sing-vmess v0.0.0-20220913015714-c4ab86d40e12 // indirect github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 // indirect + github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect go.uber.org/atomic v1.10.0 // indirect diff --git a/test/go.sum b/test/go.sum index 22d77532..0b957bac 100644 --- a/test/go.sum +++ b/test/go.sum @@ -85,8 +85,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= -github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= @@ -155,18 +153,20 @@ github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb h1:wc0yQ+SBn4TaTY github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb/go.mod h1:MIccjRKnPTjWwAOpl+AUGWOkzyTd9tERytudxu+1ra4= github.com/sagernet/sing v0.0.0-20220812082120-05f9836bff8f/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= -github.com/sagernet/sing v0.0.0-20220903085538-02b9ca1cc133 h1:krnb8wKEFIdXhmJYlhJMbEcPsJFISy2fz90uHVz7hMU= -github.com/sagernet/sing v0.0.0-20220903085538-02b9ca1cc133/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= +github.com/sagernet/sing v0.0.0-20220913004915-27ddefbb8921 h1:xUHzlIbdlPV/fkToIO9futp9lmKIY+72ezk/whQ8XsI= +github.com/sagernet/sing v0.0.0-20220913004915-27ddefbb8921/go.mod h1:kZvzh1VDa/Dg/Bt5WaYKU0jl5ept8KKDpl3Ay4gRtRQ= github.com/sagernet/sing-dns v0.0.0-20220903082137-b1102b8fc961 h1:5JeqhvCGV6AQQiAO0V67Loh2eyO3JNjIQnvRF8NnTE0= github.com/sagernet/sing-dns v0.0.0-20220903082137-b1102b8fc961/go.mod h1:vKBBy4mNJRaFuJ8H6kYIOPofsZ1JT5mgdwIlebtvnZ4= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 h1:JJfDeYYhWunvtxsU/mOVNTmFQmnzGx9dY034qG6G3g4= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6/go.mod h1:EX3RbZvrwAkPI2nuGa78T2iQXmrkT+/VQtskjou42xM= github.com/sagernet/sing-tun v0.0.0-20220828031750-185b6c880a83 h1:SoWiHYuOCVedqA7T/CJSZUUrcPGKQb2wFKEq8DphiAI= github.com/sagernet/sing-tun v0.0.0-20220828031750-185b6c880a83/go.mod h1:76r07HS1WRcEI4mE9pFsohfTBUt1j/G9Avz6DaOP3VU= -github.com/sagernet/sing-vmess v0.0.0-20220907073918-72d7fdf6825f h1:6l9aXZqAl1JqXJWi89KHpWnM/moQUPGG+XiwMc+yD0A= -github.com/sagernet/sing-vmess v0.0.0-20220907073918-72d7fdf6825f/go.mod h1:u66Vv7NHXJWfeAmhh7JuJp/cwxmuQlM56QoZ7B7Mmd0= +github.com/sagernet/sing-vmess v0.0.0-20220913015714-c4ab86d40e12 h1:4HYGbTDDemgBVTmaspXbkgjJlXc3hYVjNxSddJndq8Y= +github.com/sagernet/sing-vmess v0.0.0-20220913015714-c4ab86d40e12/go.mod h1:u66Vv7NHXJWfeAmhh7JuJp/cwxmuQlM56QoZ7B7Mmd0= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 h1:5VBIbVw9q7aKbrFdT83mjkyvQ+VaRsQ6yflTepfln38= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195/go.mod h1:yedWtra8nyGJ+SyI+ziwuaGMzBatbB10P1IOOZbbSK8= +github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e h1:7uw2njHFGE+VpWamge6o56j2RWk4omF6uLKKxMmcWvs= +github.com/sagernet/websocket v0.0.0-20220913015213-615516348b4e/go.mod h1:45TUl8+gH4SIKr4ykREbxKWTxkDlSzFENzctB1dVRRY= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= diff --git a/test/hysteria_test.go b/test/hysteria_test.go index cb21bc3f..522fbea7 100644 --- a/test/hysteria_test.go +++ b/test/hysteria_test.go @@ -80,7 +80,7 @@ func TestHysteriaSelf(t *testing.T) { }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuitSimple1(t, clientPort, testPort) } func TestHysteriaInbound(t *testing.T) { @@ -171,5 +171,5 @@ func TestHysteriaOutbound(t *testing.T) { }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuit(t, clientPort, testPort) } diff --git a/test/trojan_test.go b/test/trojan_test.go index b24d5e3d..88521cca 100644 --- a/test/trojan_test.go +++ b/test/trojan_test.go @@ -122,3 +122,62 @@ func TestTrojanSelf(t *testing.T) { }) testSuit(t, clientPort, testPort) } + +func TestTrojanPlainSelf(t *testing.T) { + startInstance(t, option.Options{ + Inbounds: []option.Inbound{ + { + Type: C.TypeMixed, + Tag: "mixed-in", + MixedOptions: option.HTTPMixedInboundOptions{ + ListenOptions: option.ListenOptions{ + Listen: option.ListenAddress(netip.IPv4Unspecified()), + ListenPort: clientPort, + }, + }, + }, + { + Type: C.TypeTrojan, + TrojanOptions: option.TrojanInboundOptions{ + ListenOptions: option.ListenOptions{ + Listen: option.ListenAddress(netip.IPv4Unspecified()), + ListenPort: serverPort, + }, + Users: []option.TrojanUser{ + { + Name: "sekai", + Password: "password", + }, + }, + }, + }, + }, + Outbounds: []option.Outbound{ + { + Type: C.TypeDirect, + }, + { + Type: C.TypeTrojan, + Tag: "trojan-out", + TrojanOptions: option.TrojanOutboundOptions{ + ServerOptions: option.ServerOptions{ + Server: "127.0.0.1", + ServerPort: serverPort, + }, + Password: "password", + }, + }, + }, + Route: &option.RouteOptions{ + Rules: []option.Rule{ + { + DefaultOptions: option.DefaultRule{ + Inbound: []string{"mixed-in"}, + Outbound: "trojan-out", + }, + }, + }, + }, + }) + testSuit(t, clientPort, testPort) +} diff --git a/test/v2ray_grpc_test.go b/test/v2ray_grpc_test.go index 3569e0d8..94e810ad 100644 --- a/test/v2ray_grpc_test.go +++ b/test/v2ray_grpc_test.go @@ -161,7 +161,7 @@ func testV2RayGRPCOutbound(t *testing.T, forceLite bool) { }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuit(t, clientPort, testPort) } func TestV2RayGRPCLite(t *testing.T) { diff --git a/test/v2ray_transport_test.go b/test/v2ray_transport_test.go index d17cc890..f8848421 100644 --- a/test/v2ray_transport_test.go +++ b/test/v2ray_transport_test.go @@ -266,7 +266,7 @@ func TestVMessQUICSelf(t *testing.T) { }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuit(t, clientPort, testPort) } func testV2RayTransportNOTLSSelf(t *testing.T, transport *option.V2RayTransportOptions) { @@ -330,5 +330,5 @@ func testV2RayTransportNOTLSSelf(t *testing.T, transport *option.V2RayTransportO }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuit(t, clientPort, testPort) } diff --git a/test/v2ray_ws_test.go b/test/v2ray_ws_test.go index 4d6d03c1..7df2932e 100644 --- a/test/v2ray_ws_test.go +++ b/test/v2ray_ws_test.go @@ -193,5 +193,5 @@ func testV2RayWebsocketOutbound(t *testing.T, maxEarlyData uint32, earlyDataHead }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuit(t, clientPort, testPort) } diff --git a/test/vmess_test.go b/test/vmess_test.go index dd89b2ab..21d14248 100644 --- a/test/vmess_test.go +++ b/test/vmess_test.go @@ -256,7 +256,7 @@ func testVMessOutboundWithV2Ray(t *testing.T, security string, uuid uuid.UUID, g }, }, }) - testSuitSimple(t, clientPort, testPort) + testSuit(t, clientPort, testPort) } func testVMessSelf(t *testing.T, security string, uuid uuid.UUID, alterId int, globalPadding bool, authenticatedLength bool, packetAddr bool) { diff --git a/transport/v2raygrpclite/conn.go b/transport/v2raygrpclite/conn.go index 66fbadff..9d361e4b 100644 --- a/transport/v2raygrpclite/conn.go +++ b/transport/v2raygrpclite/conn.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "os" + "sync" "time" "github.com/sagernet/sing-box/common/baderror" @@ -28,6 +29,7 @@ type GunConn struct { create chan struct{} err error readRemaining int + writeAccess sync.Mutex } func newGunConn(reader io.Reader, writer io.Writer, flusher http.Flusher) *GunConn { @@ -100,7 +102,9 @@ func (c *GunConn) Write(b []byte) (n int, err error) { grpcHeader := buf.Get(5) grpcPayloadLen := uint32(1 + varuintLen + len(b)) binary.BigEndian.PutUint32(grpcHeader[1:5], grpcPayloadLen) + c.writeAccess.Lock() _, err = bufio.Copy(c.writer, io.MultiReader(bytes.NewReader(grpcHeader), bytes.NewReader(protobufHeader[:varuintLen+1]), bytes.NewReader(b))) + c.writeAccess.Unlock() buf.Put(grpcHeader) if c.flusher != nil { c.flusher.Flush() diff --git a/transport/v2raywebsocket/client.go b/transport/v2raywebsocket/client.go index d3b7a7a6..e47ededd 100644 --- a/transport/v2raywebsocket/client.go +++ b/transport/v2raywebsocket/client.go @@ -14,8 +14,7 @@ import ( E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" - - "github.com/gorilla/websocket" + "github.com/sagernet/websocket" ) var _ adapter.V2RayClientTransport = (*Client)(nil) diff --git a/transport/v2raywebsocket/conn.go b/transport/v2raywebsocket/conn.go index fa8ea04a..9985a65d 100644 --- a/transport/v2raywebsocket/conn.go +++ b/transport/v2raywebsocket/conn.go @@ -11,8 +11,7 @@ import ( C "github.com/sagernet/sing-box/constant" E "github.com/sagernet/sing/common/exceptions" - - "github.com/gorilla/websocket" + "github.com/sagernet/websocket" ) type WebsocketConn struct { diff --git a/transport/v2raywebsocket/server.go b/transport/v2raywebsocket/server.go index 673836d6..64f9286b 100644 --- a/transport/v2raywebsocket/server.go +++ b/transport/v2raywebsocket/server.go @@ -19,8 +19,7 @@ import ( M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" sHttp "github.com/sagernet/sing/protocol/http" - - "github.com/gorilla/websocket" + "github.com/sagernet/websocket" ) var _ adapter.V2RayServerTransport = (*Server)(nil)