Browse Source

Update task usage

世界 3 years ago
parent
commit
d4cce1b5b9
6 changed files with 38 additions and 22 deletions
  1. 3 1
      common/sniff/dns.go
  2. 3 3
      go.mod
  3. 6 6
      go.sum
  4. 17 3
      outbound/dns.go
  5. 3 3
      test/go.mod
  6. 6 6
      test/go.sum

+ 3 - 1
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

+ 3 - 3
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

+ 6 - 6
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=

+ 17 - 3
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
 }

+ 3 - 3
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

+ 6 - 6
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=