mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-01 10:56:38 +00:00
Update cancel context usage
This commit is contained in:
parent
da82a41697
commit
1e39196bc9
2
go.mod
2
go.mod
|
@ -25,7 +25,7 @@ require (
|
|||
github.com/sagernet/gomobile v0.0.0-20221130124640-349ebaa752ca
|
||||
github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32
|
||||
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
|
||||
github.com/sagernet/sing v0.2.3-0.20230409145127-a82d82e55914
|
||||
github.com/sagernet/sing v0.2.3-0.20230410085425-20b4148381c7
|
||||
github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440
|
||||
github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b
|
||||
github.com/sagernet/sing-shadowtls v0.1.1-0.20230409094821-9abef019436f
|
||||
|
|
4
go.sum
4
go.sum
|
@ -111,8 +111,8 @@ github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byL
|
|||
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
|
||||
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
|
||||
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
|
||||
github.com/sagernet/sing v0.2.3-0.20230409145127-a82d82e55914 h1:ReHDfZgEMKNVh0xvgvOc5j7M2kX9J+3nNQ8DrSwx0Hs=
|
||||
github.com/sagernet/sing v0.2.3-0.20230409145127-a82d82e55914/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
||||
github.com/sagernet/sing v0.2.3-0.20230410085425-20b4148381c7 h1:ODs3QOkbKwkXwK0SrWxqbvuEdhr5sAAiUwtZHH+c+6w=
|
||||
github.com/sagernet/sing v0.2.3-0.20230410085425-20b4148381c7/go.mod h1:Ta8nHnDLAwqySzKhGoKk4ZIB+vJ3GTKj7UPrWYvM+4w=
|
||||
github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440 h1:VH8/BcOVuApHtS+vKP+khxlGRcXH7KKhgkTDtNynqSQ=
|
||||
github.com/sagernet/sing-dns v0.1.5-0.20230408004833-5adaf486d440/go.mod h1:69PNSHyEmXdjf6C+bXBOdr2GQnPeEyWjIzo/MV8gmz8=
|
||||
github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b h1:nmP+V4nlc8lqEMpwjjbny8ISkrFIjvKWIETsjs7nSic=
|
||||
|
|
|
@ -2,6 +2,7 @@ package ntp
|
|||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
|
@ -9,6 +10,7 @@ import (
|
|||
"github.com/sagernet/sing-box/common/settings"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
|
@ -20,7 +22,7 @@ var _ adapter.TimeService = (*Service)(nil)
|
|||
|
||||
type Service struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
cancel common.ContextCancelCauseFunc
|
||||
server M.Socksaddr
|
||||
writeToSystem bool
|
||||
dialer N.Dialer
|
||||
|
@ -30,7 +32,7 @@ type Service struct {
|
|||
}
|
||||
|
||||
func NewService(ctx context.Context, router adapter.Router, logger logger.Logger, options option.NTPOptions) *Service {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
ctx, cancel := common.ContextWithCancelCause(ctx)
|
||||
server := options.ServerOptions.Build()
|
||||
if server.Port == 0 {
|
||||
server.Port = 123
|
||||
|
@ -64,7 +66,7 @@ func (s *Service) Start() error {
|
|||
|
||||
func (s *Service) Close() error {
|
||||
s.ticker.Stop()
|
||||
s.cancel()
|
||||
s.cancel(os.ErrClosed)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -102,11 +102,10 @@ func (d *DNS) handleConnection(ctx context.Context, conn net.Conn, metadata adap
|
|||
|
||||
func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
||||
ctx = adapter.WithContext(ctx, &metadata)
|
||||
fastClose, cancel := context.WithCancel(ctx)
|
||||
fastClose, cancel := common.ContextWithCancelCause(ctx)
|
||||
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
||||
var group task.Group
|
||||
group.Append0(func(ctx context.Context) error {
|
||||
defer cancel()
|
||||
_buffer := buf.StackNewSize(dns.FixedPacketSize)
|
||||
defer common.KeepAlive(_buffer)
|
||||
buffer := common.Dup(_buffer)
|
||||
|
@ -115,11 +114,13 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada
|
|||
buffer.FullReset()
|
||||
destination, err := conn.ReadPacket(buffer)
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
return err
|
||||
}
|
||||
var message mDNS.Msg
|
||||
err = message.Unpack(buffer.Bytes())
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
return err
|
||||
}
|
||||
timeout.Update()
|
||||
|
@ -127,17 +128,22 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada
|
|||
go func() error {
|
||||
response, err := d.router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
return err
|
||||
}
|
||||
timeout.Update()
|
||||
responseBuffer := buf.NewPacket()
|
||||
n, err := response.PackBuffer(responseBuffer.FreeBytes())
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
responseBuffer.Release()
|
||||
return err
|
||||
}
|
||||
responseBuffer.Truncate(len(n))
|
||||
err = conn.WritePacket(responseBuffer, destination)
|
||||
if err != nil {
|
||||
cancel(err)
|
||||
}
|
||||
return err
|
||||
}()
|
||||
}
|
||||
|
|
|
@ -102,10 +102,10 @@ func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
|||
return nil, err
|
||||
}
|
||||
client := NewGunServiceClient(clientConn).(GunServiceCustomNameClient)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
ctx, cancel := common.ContextWithCancelCause(ctx)
|
||||
stream, err := client.TunCustomName(ctx, c.serviceName)
|
||||
if err != nil {
|
||||
cancel()
|
||||
cancel(err)
|
||||
return nil, err
|
||||
}
|
||||
return deadline.NewConn(NewGRPCConn(stream, cancel)), nil
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package v2raygrpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/common/baderror"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/rw"
|
||||
)
|
||||
|
||||
|
@ -14,11 +14,11 @@ var _ net.Conn = (*GRPCConn)(nil)
|
|||
|
||||
type GRPCConn struct {
|
||||
GunService
|
||||
cancel context.CancelFunc
|
||||
cancel common.ContextCancelCauseFunc
|
||||
cache []byte
|
||||
}
|
||||
|
||||
func NewGRPCConn(service GunService, cancel context.CancelFunc) *GRPCConn {
|
||||
func NewGRPCConn(service GunService, cancel common.ContextCancelCauseFunc) *GRPCConn {
|
||||
if client, isClient := service.(GunService_TunClient); isClient {
|
||||
service = &clientConnWrapper{client}
|
||||
}
|
||||
|
@ -37,6 +37,7 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
|
|||
hunk, err := c.Recv()
|
||||
err = baderror.WrapGRPC(err)
|
||||
if err != nil {
|
||||
c.cancel(err)
|
||||
return
|
||||
}
|
||||
n = copy(b, hunk.Data)
|
||||
|
@ -49,13 +50,14 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
|
|||
func (c *GRPCConn) Write(b []byte) (n int, err error) {
|
||||
err = baderror.WrapGRPC(c.Send(&Hunk{Data: b}))
|
||||
if err != nil {
|
||||
c.cancel(err)
|
||||
return
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
func (c *GRPCConn) Close() error {
|
||||
c.cancel()
|
||||
c.cancel(net.ErrClosed)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/common/tls"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/bufio/deadline"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
|
@ -46,7 +47,7 @@ func NewServer(ctx context.Context, options option.V2RayGRPCOptions, tlsConfig t
|
|||
}
|
||||
|
||||
func (s *Server) Tun(server GunService_TunServer) error {
|
||||
ctx, cancel := context.WithCancel(s.ctx)
|
||||
ctx, cancel := common.ContextWithCancelCause(s.ctx)
|
||||
conn := NewGRPCConn(server, cancel)
|
||||
var metadata M.Metadata
|
||||
if remotePeer, loaded := peer.FromContext(server.Context()); loaded {
|
||||
|
|
Loading…
Reference in a new issue