mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-01 19:06:43 +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/gomobile v0.0.0-20221130124640-349ebaa752ca
|
||||||
github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32
|
github.com/sagernet/quic-go v0.0.0-20230202071646-a8c8afb18b32
|
||||||
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
|
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-dns v0.1.5-0.20230408004833-5adaf486d440
|
||||||
github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b
|
github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b
|
||||||
github.com/sagernet/sing-shadowtls v0.1.1-0.20230409094821-9abef019436f
|
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/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.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.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.20230410085425-20b4148381c7 h1:ODs3QOkbKwkXwK0SrWxqbvuEdhr5sAAiUwtZHH+c+6w=
|
||||||
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/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 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-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=
|
github.com/sagernet/sing-shadowsocks v0.2.1-0.20230409094647-5c830455eb9b h1:nmP+V4nlc8lqEMpwjjbny8ISkrFIjvKWIETsjs7nSic=
|
||||||
|
|
|
@ -2,6 +2,7 @@ package ntp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
"github.com/sagernet/sing-box/common/settings"
|
"github.com/sagernet/sing-box/common/settings"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
C "github.com/sagernet/sing-box/constant"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
"github.com/sagernet/sing/common/logger"
|
"github.com/sagernet/sing/common/logger"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
@ -20,7 +22,7 @@ var _ adapter.TimeService = (*Service)(nil)
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel common.ContextCancelCauseFunc
|
||||||
server M.Socksaddr
|
server M.Socksaddr
|
||||||
writeToSystem bool
|
writeToSystem bool
|
||||||
dialer N.Dialer
|
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 {
|
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()
|
server := options.ServerOptions.Build()
|
||||||
if server.Port == 0 {
|
if server.Port == 0 {
|
||||||
server.Port = 123
|
server.Port = 123
|
||||||
|
@ -64,7 +66,7 @@ func (s *Service) Start() error {
|
||||||
|
|
||||||
func (s *Service) Close() error {
|
func (s *Service) Close() error {
|
||||||
s.ticker.Stop()
|
s.ticker.Stop()
|
||||||
s.cancel()
|
s.cancel(os.ErrClosed)
|
||||||
return nil
|
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 {
|
func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
||||||
ctx = adapter.WithContext(ctx, &metadata)
|
ctx = adapter.WithContext(ctx, &metadata)
|
||||||
fastClose, cancel := context.WithCancel(ctx)
|
fastClose, cancel := common.ContextWithCancelCause(ctx)
|
||||||
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
timeout := canceler.New(fastClose, cancel, C.DNSTimeout)
|
||||||
var group task.Group
|
var group task.Group
|
||||||
group.Append0(func(ctx context.Context) error {
|
group.Append0(func(ctx context.Context) error {
|
||||||
defer cancel()
|
|
||||||
_buffer := buf.StackNewSize(dns.FixedPacketSize)
|
_buffer := buf.StackNewSize(dns.FixedPacketSize)
|
||||||
defer common.KeepAlive(_buffer)
|
defer common.KeepAlive(_buffer)
|
||||||
buffer := common.Dup(_buffer)
|
buffer := common.Dup(_buffer)
|
||||||
|
@ -115,11 +114,13 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada
|
||||||
buffer.FullReset()
|
buffer.FullReset()
|
||||||
destination, err := conn.ReadPacket(buffer)
|
destination, err := conn.ReadPacket(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var message mDNS.Msg
|
var message mDNS.Msg
|
||||||
err = message.Unpack(buffer.Bytes())
|
err = message.Unpack(buffer.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
timeout.Update()
|
timeout.Update()
|
||||||
|
@ -127,17 +128,22 @@ func (d *DNS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metada
|
||||||
go func() error {
|
go func() error {
|
||||||
response, err := d.router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
|
response, err := d.router.Exchange(adapter.WithContext(ctx, &metadataInQuery), &message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
timeout.Update()
|
timeout.Update()
|
||||||
responseBuffer := buf.NewPacket()
|
responseBuffer := buf.NewPacket()
|
||||||
n, err := response.PackBuffer(responseBuffer.FreeBytes())
|
n, err := response.PackBuffer(responseBuffer.FreeBytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
responseBuffer.Release()
|
responseBuffer.Release()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
responseBuffer.Truncate(len(n))
|
responseBuffer.Truncate(len(n))
|
||||||
err = conn.WritePacket(responseBuffer, destination)
|
err = conn.WritePacket(responseBuffer, destination)
|
||||||
|
if err != nil {
|
||||||
|
cancel(err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
|
@ -102,10 +102,10 @@ func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client := NewGunServiceClient(clientConn).(GunServiceCustomNameClient)
|
client := NewGunServiceClient(clientConn).(GunServiceCustomNameClient)
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := common.ContextWithCancelCause(ctx)
|
||||||
stream, err := client.TunCustomName(ctx, c.serviceName)
|
stream, err := client.TunCustomName(ctx, c.serviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return deadline.NewConn(NewGRPCConn(stream, cancel)), nil
|
return deadline.NewConn(NewGRPCConn(stream, cancel)), nil
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package v2raygrpc
|
package v2raygrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/common/baderror"
|
"github.com/sagernet/sing-box/common/baderror"
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/rw"
|
"github.com/sagernet/sing/common/rw"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,11 +14,11 @@ var _ net.Conn = (*GRPCConn)(nil)
|
||||||
|
|
||||||
type GRPCConn struct {
|
type GRPCConn struct {
|
||||||
GunService
|
GunService
|
||||||
cancel context.CancelFunc
|
cancel common.ContextCancelCauseFunc
|
||||||
cache []byte
|
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 {
|
if client, isClient := service.(GunService_TunClient); isClient {
|
||||||
service = &clientConnWrapper{client}
|
service = &clientConnWrapper{client}
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,7 @@ func (c *GRPCConn) Read(b []byte) (n int, err error) {
|
||||||
hunk, err := c.Recv()
|
hunk, err := c.Recv()
|
||||||
err = baderror.WrapGRPC(err)
|
err = baderror.WrapGRPC(err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.cancel(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n = copy(b, hunk.Data)
|
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) {
|
func (c *GRPCConn) Write(b []byte) (n int, err error) {
|
||||||
err = baderror.WrapGRPC(c.Send(&Hunk{Data: b}))
|
err = baderror.WrapGRPC(c.Send(&Hunk{Data: b}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.cancel(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GRPCConn) Close() error {
|
func (c *GRPCConn) Close() error {
|
||||||
c.cancel()
|
c.cancel(net.ErrClosed)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
"github.com/sagernet/sing-box/common/tls"
|
"github.com/sagernet/sing-box/common/tls"
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/bufio/deadline"
|
"github.com/sagernet/sing/common/bufio/deadline"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
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 {
|
func (s *Server) Tun(server GunService_TunServer) error {
|
||||||
ctx, cancel := context.WithCancel(s.ctx)
|
ctx, cancel := common.ContextWithCancelCause(s.ctx)
|
||||||
conn := NewGRPCConn(server, cancel)
|
conn := NewGRPCConn(server, cancel)
|
||||||
var metadata M.Metadata
|
var metadata M.Metadata
|
||||||
if remotePeer, loaded := peer.FromContext(server.Context()); loaded {
|
if remotePeer, loaded := peer.FromContext(server.Context()); loaded {
|
||||||
|
|
Loading…
Reference in a new issue