diff --git a/common/sniff/dns.go b/common/sniff/dns.go index 0c41b924..ebb9d928 100644 --- a/common/sniff/dns.go +++ b/common/sniff/dns.go @@ -31,9 +31,11 @@ func StreamDomainNameQuery(readCtx context.Context, reader io.Reader) (*adapter. defer buffer.Release() readCtx, cancel := context.WithTimeout(readCtx, time.Millisecond*100) - err = task.Run(readCtx, func() error { + var readTask task.Group + readTask.Append0(func(ctx context.Context) error { return common.Error(buffer.ReadFullFrom(reader, buffer.FreeLen())) }) + err = readTask.Run(readCtx) cancel() if err != nil { return nil, err diff --git a/go.mod b/go.mod index e487f4f2..834e52a7 100644 --- a/go.mod +++ b/go.mod @@ -13,10 +13,10 @@ require ( github.com/hashicorp/yamux v0.1.1 github.com/logrusorgru/aurora v2.0.3+incompatible github.com/oschwald/maxminddb-golang v1.9.0 - github.com/sagernet/sing v0.0.0-20220802021749-842fd713ff94 + github.com/sagernet/sing v0.0.0-20220803094243-d9ca259bec6a github.com/sagernet/sing-dns v0.0.0-20220801112436-b9e99d83271e github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 - github.com/sagernet/sing-tun v0.0.0-20220803040044-e738a97f2f8a + github.com/sagernet/sing-tun v0.0.0-20220803073114-9fad6b0cf330 github.com/sagernet/sing-vmess v0.0.0-20220802053753-a38d3b22e6b9 github.com/spf13/cobra v1.5.0 github.com/stretchr/testify v1.8.0 @@ -43,7 +43,7 @@ require ( github.com/nxadm/tail v1.4.8 // indirect github.com/onsi/ginkgo v1.16.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sagernet/netlink v0.0.0-20220803035756-f7f6ab71902a // indirect + github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect golang.org/x/mod v0.5.1 // indirect diff --git a/go.sum b/go.sum index 538c7941..4bc19301 100644 --- a/go.sum +++ b/go.sum @@ -147,16 +147,16 @@ github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7q github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sagernet/netlink v0.0.0-20220803035756-f7f6ab71902a h1:mrdDelP9a4ueLd37MxSukDIF8D16OaoiGxJxYcQKqsM= -github.com/sagernet/netlink v0.0.0-20220803035756-f7f6ab71902a/go.mod h1:LA6AWv9hVXuxfnd5HDV5K1n6VEX3jPqFlWxu8PcOX5c= -github.com/sagernet/sing v0.0.0-20220802021749-842fd713ff94 h1:AEsJjN5cOy9/ILWPBQU2AljGqLjyeKHw4yJmp90rPI4= -github.com/sagernet/sing v0.0.0-20220802021749-842fd713ff94/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= +github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= +github.com/sagernet/sing v0.0.0-20220803094243-d9ca259bec6a h1:CHxuamnZEAxXoep7ycGSuMOiKzsRYuYa0ucnOCdUT9U= +github.com/sagernet/sing v0.0.0-20220803094243-d9ca259bec6a/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing-dns v0.0.0-20220801112436-b9e99d83271e h1:bkAQ07NmD2vvEg1r/VrKr63BeJXQqZP29waU1Lr1XBM= github.com/sagernet/sing-dns v0.0.0-20220801112436-b9e99d83271e/go.mod h1:4pEktidzm9Aq9QaN0TuwhOTYD2nFSPsoXckG4svKz/Q= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc= -github.com/sagernet/sing-tun v0.0.0-20220803040044-e738a97f2f8a h1:B5aNLbwdTYDP0JNgZIudomHvGfLYNKIYcN9dwUJ+CL8= -github.com/sagernet/sing-tun v0.0.0-20220803040044-e738a97f2f8a/go.mod h1:ECzpCm/GqBauEncEax10b4msJ2ILq7HUfRvyjonYGa8= +github.com/sagernet/sing-tun v0.0.0-20220803073114-9fad6b0cf330 h1:CtucdvCCxX57pibRy1Cucy104XR0XuVJqWoTIg42O0Q= +github.com/sagernet/sing-tun v0.0.0-20220803073114-9fad6b0cf330/go.mod h1:jNlPidQzZYkpmpQJ+sDN2YGrPsL4QImoqBpuauId9po= github.com/sagernet/sing-vmess v0.0.0-20220802053753-a38d3b22e6b9 h1:x+r8P5MKyQWGN3tcI5dmOM1Aei1mlWua2ciMBGz0/oM= github.com/sagernet/sing-vmess v0.0.0-20220802053753-a38d3b22e6b9/go.mod h1:ETvczg3TQzGa8zg0lYXj8cYTJr4+OFUmtQer9/c/cLU= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= diff --git a/outbound/dns.go b/outbound/dns.go index 50dc2f86..eb0355b3 100644 --- a/outbound/dns.go +++ b/outbound/dns.go @@ -100,11 +100,11 @@ func (d *DNS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter } func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { - defer conn.Close() ctx = adapter.WithContext(ctx, &metadata) fastClose, cancel := context.WithCancel(ctx) timeout := canceler.New(fastClose, cancel, C.DNSTimeout) - return task.Run(fastClose, func() error { + var group task.Group + group.Append0(func(ctx context.Context) error { defer cancel() _buffer := buf.StackNewSize(1024) defer common.KeepAlive(_buffer) @@ -145,8 +145,22 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada }() } }) + group.Cleanup(func() { + conn.Close() + }) + return group.Run(ctx) } func formatDNSQuestion(question dnsmessage.Question) string { - return string(question.Name.Data[:question.Name.Length-1]) + " " + question.Type.String()[4:] + " " + question.Class.String()[5:] + var qType string + qType = question.Type.String() + if len(qType) > 4 { + qType = qType[4:] + } + var qClass string + qClass = question.Class.String() + if len(qClass) > 5 { + qClass = qClass[5:] + } + return string(question.Name.Data[:question.Name.Length-1]) + " " + qType + " " + qClass } diff --git a/test/go.mod b/test/go.mod index 237145d6..01518997 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-20220802021749-842fd713ff94 + github.com/sagernet/sing v0.0.0-20220803094243-d9ca259bec6a github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 github.com/spyzhov/ajson v0.7.1 github.com/stretchr/testify v1.8.0 @@ -52,9 +52,9 @@ require ( github.com/oschwald/maxminddb-golang v1.9.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/sagernet/netlink v0.0.0-20220803035756-f7f6ab71902a // indirect + github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 // indirect github.com/sagernet/sing-dns v0.0.0-20220801112436-b9e99d83271e // indirect - github.com/sagernet/sing-tun v0.0.0-20220803040044-e738a97f2f8a // indirect + github.com/sagernet/sing-tun v0.0.0-20220803073114-9fad6b0cf330 // indirect github.com/sagernet/sing-vmess v0.0.0-20220802053753-a38d3b22e6b9 // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect diff --git a/test/go.sum b/test/go.sum index a91ccc78..67b0de91 100644 --- a/test/go.sum +++ b/test/go.sum @@ -172,16 +172,16 @@ github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1: github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= -github.com/sagernet/netlink v0.0.0-20220803035756-f7f6ab71902a h1:mrdDelP9a4ueLd37MxSukDIF8D16OaoiGxJxYcQKqsM= -github.com/sagernet/netlink v0.0.0-20220803035756-f7f6ab71902a/go.mod h1:LA6AWv9hVXuxfnd5HDV5K1n6VEX3jPqFlWxu8PcOX5c= -github.com/sagernet/sing v0.0.0-20220802021749-842fd713ff94 h1:AEsJjN5cOy9/ILWPBQU2AljGqLjyeKHw4yJmp90rPI4= -github.com/sagernet/sing v0.0.0-20220802021749-842fd713ff94/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= +github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805 h1:hE+vtsjBCCPmxkRz9jZA+CicHgVkDT6H+Av5ZzskVxs= +github.com/sagernet/netlink v0.0.0-20220803045538-bdac49abf805/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM= +github.com/sagernet/sing v0.0.0-20220803094243-d9ca259bec6a h1:CHxuamnZEAxXoep7ycGSuMOiKzsRYuYa0ucnOCdUT9U= +github.com/sagernet/sing v0.0.0-20220803094243-d9ca259bec6a/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing-dns v0.0.0-20220801112436-b9e99d83271e h1:bkAQ07NmD2vvEg1r/VrKr63BeJXQqZP29waU1Lr1XBM= github.com/sagernet/sing-dns v0.0.0-20220801112436-b9e99d83271e/go.mod h1:4pEktidzm9Aq9QaN0TuwhOTYD2nFSPsoXckG4svKz/Q= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1 h1:RYvOc69eSNMN0dwVugrDts41Nn7Ar/C/n/fvytvFcp4= github.com/sagernet/sing-shadowsocks v0.0.0-20220801112336-a91eacdd01e1/go.mod h1:NqZjiXszgVCMQ4gVDa2V+drhS8NMfGqUqDF86EacEFc= -github.com/sagernet/sing-tun v0.0.0-20220803040044-e738a97f2f8a h1:B5aNLbwdTYDP0JNgZIudomHvGfLYNKIYcN9dwUJ+CL8= -github.com/sagernet/sing-tun v0.0.0-20220803040044-e738a97f2f8a/go.mod h1:ECzpCm/GqBauEncEax10b4msJ2ILq7HUfRvyjonYGa8= +github.com/sagernet/sing-tun v0.0.0-20220803073114-9fad6b0cf330 h1:CtucdvCCxX57pibRy1Cucy104XR0XuVJqWoTIg42O0Q= +github.com/sagernet/sing-tun v0.0.0-20220803073114-9fad6b0cf330/go.mod h1:jNlPidQzZYkpmpQJ+sDN2YGrPsL4QImoqBpuauId9po= github.com/sagernet/sing-vmess v0.0.0-20220802053753-a38d3b22e6b9 h1:x+r8P5MKyQWGN3tcI5dmOM1Aei1mlWua2ciMBGz0/oM= github.com/sagernet/sing-vmess v0.0.0-20220802053753-a38d3b22e6b9/go.mod h1:ETvczg3TQzGa8zg0lYXj8cYTJr4+OFUmtQer9/c/cLU= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=