From c18c545798c1cf2407073fff1a36b0beda00b4a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 21 Sep 2022 18:46:20 +0800 Subject: [PATCH] Add stdio test --- Makefile | 12 +++++++++--- go.mod | 2 +- go.sum | 4 ++-- transport/hysteria/protocol.go | 27 +++++++++++++++++++-------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index df5ee6e2..3f42aa40 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ NAME = sing-box COMMIT = $(shell git rev-parse --short HEAD) TAGS ?= with_gvisor,with_quic,with_wireguard,with_clash_api -PARAMS = -v -trimpath -tags '$(TAGS)' -ldflags '-s -w -buildid=' +TAGS_TEST ?= with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_shadowsocksr +PARAMS = -v -trimpath -tags "$(TAGS)" -ldflags "-s -w -buildid=" MAIN = ./cmd/sing-box .PHONY: test release @@ -59,10 +60,15 @@ release_install: go install -v github.com/tcnksm/ghr@latest test: - @go test -v . && \ + @go test -v ./... && \ cd test && \ go mod tidy && \ - go test -v -tags with_gvisor,with_quic,with_wireguard,with_grpc,with_ech,with_utls,with_shadowsocksr . + go test -v -tags "$(TAGS_TEST)" . +test_stdio: + @go test -v ./... && \ + cd test && \ + go mod tidy && \ + go test -v -tags "$(TAGS_TEST),force_stdio" . clean: rm -rf bin dist diff --git a/go.mod b/go.mod index ce83aafd..0db1d8b9 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/pires/go-proxyproto v0.6.2 github.com/refraction-networking/utls v1.1.2 github.com/sagernet/quic-go v0.0.0-20220818150011-de611ab3e2bb - github.com/sagernet/sing v0.0.0-20220921090219-b2828dac5f86 + github.com/sagernet/sing v0.0.0-20220921101604-86d7d510231f github.com/sagernet/sing-dns v0.0.0-20220915084601-812e0864b45b github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 github.com/sagernet/sing-tun v0.0.0-20220916073459-0032242c9617 diff --git a/go.sum b/go.sum index 28a5e1cd..5d1baba8 100644 --- a/go.sum +++ b/go.sum @@ -145,8 +145,8 @@ 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-20220921090219-b2828dac5f86 h1:cjEloP/20kV1p6RBNvNQWaOMulgmlwHUyB9S7KikZzw= -github.com/sagernet/sing v0.0.0-20220921090219-b2828dac5f86/go.mod h1:x3NHUeJBQwV75L51zwmLKQdLtRvR+M4PmXkfQtU1vIY= +github.com/sagernet/sing v0.0.0-20220921101604-86d7d510231f h1:GX416thAwyc0vHBOal/qplvdhFgYO2dHD5GqADCJ0Ig= +github.com/sagernet/sing v0.0.0-20220921101604-86d7d510231f/go.mod h1:x3NHUeJBQwV75L51zwmLKQdLtRvR+M4PmXkfQtU1vIY= github.com/sagernet/sing-dns v0.0.0-20220915084601-812e0864b45b h1:cXCMNJ9heZ+c6l+qUcku60x9KyXo4SOAaJfg/6spOmU= github.com/sagernet/sing-dns v0.0.0-20220915084601-812e0864b45b/go.mod h1:SrvWLfOSlnFmH32CWXicfilAGgIXR0VjrH6yRbuXYww= github.com/sagernet/sing-shadowsocks v0.0.0-20220819002358-7461bb09a8f6 h1:JJfDeYYhWunvtxsU/mOVNTmFQmnzGx9dY034qG6G3g4= diff --git a/transport/hysteria/protocol.go b/transport/hysteria/protocol.go index 3a92d194..f7730dad 100644 --- a/transport/hysteria/protocol.go +++ b/transport/hysteria/protocol.go @@ -487,6 +487,25 @@ func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) er }) } +func (c *PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { + msg := <-c.msgCh + if msg == nil { + err = net.ErrClosed + return + } + n = copy(p, msg.Data) + addr = M.ParseSocksaddrHostPort(msg.Host, msg.Port).UDPAddr() + return +} + +func (c *PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { + err = c.WritePacket(buf.As(p), M.SocksaddrFromNet(addr)) + if err == nil { + n = len(p) + } + return +} + func (c *PacketConn) LocalAddr() net.Addr { return nil } @@ -507,14 +526,6 @@ func (c *PacketConn) SetWriteDeadline(t time.Time) error { return os.ErrInvalid } -func (c *PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) { - return 0, nil, os.ErrInvalid -} - -func (c *PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { - return 0, os.ErrInvalid -} - func (c *PacketConn) Read(b []byte) (n int, err error) { return 0, os.ErrInvalid }