refactor: Modular inbounds/outbounds

This commit is contained in:
世界 2024-11-02 00:39:02 +08:00
parent 9f7683818f
commit e233fd4fe5
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
152 changed files with 3116 additions and 2926 deletions

View file

@ -8,12 +8,13 @@ import (
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/common/task"
"github.com/sagernet/smux"
)
func HandleMuxConnection(ctx context.Context, conn net.Conn, metadata M.Metadata, handler Handler) error {
func HandleMuxConnection(ctx context.Context, conn net.Conn, metadata M.Metadata, handler Handler, logger logger.ContextLogger) error {
session, err := smux.Server(conn, smuxConfig())
if err != nil {
return err
@ -26,7 +27,7 @@ func HandleMuxConnection(ctx context.Context, conn net.Conn, metadata M.Metadata
if err != nil {
return err
}
go newMuxConnection(ctx, stream, metadata, handler)
go newMuxConnection(ctx, stream, metadata, handler, logger)
}
})
group.Cleanup(func() {
@ -35,10 +36,10 @@ func HandleMuxConnection(ctx context.Context, conn net.Conn, metadata M.Metadata
return group.Run(ctx)
}
func newMuxConnection(ctx context.Context, conn net.Conn, metadata M.Metadata, handler Handler) {
func newMuxConnection(ctx context.Context, conn net.Conn, metadata M.Metadata, handler Handler, logger logger.ContextLogger) {
err := newMuxConnection0(ctx, conn, metadata, handler)
if err != nil {
handler.NewError(ctx, E.Cause(err, "process trojan-go multiplex connection"))
logger.ErrorContext(ctx, E.Cause(err, "process trojan-go multiplex connection"))
}
}

View file

@ -9,6 +9,7 @@ import (
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/rw"
@ -17,7 +18,6 @@ import (
type Handler interface {
N.TCPConnectionHandler
N.UDPConnectionHandler
E.Handler
}
type Service[K comparable] struct {
@ -25,14 +25,16 @@ type Service[K comparable] struct {
keys map[[56]byte]K
handler Handler
fallbackHandler N.TCPConnectionHandler
logger logger.ContextLogger
}
func NewService[K comparable](handler Handler, fallbackHandler N.TCPConnectionHandler) *Service[K] {
func NewService[K comparable](handler Handler, fallbackHandler N.TCPConnectionHandler, logger logger.ContextLogger) *Service[K] {
return &Service[K]{
users: make(map[K][56]byte),
keys: make(map[[56]byte]K),
handler: handler,
fallbackHandler: fallbackHandler,
logger: logger,
}
}
@ -110,7 +112,7 @@ func (s *Service[K]) NewConnection(ctx context.Context, conn net.Conn, metadata
return s.handler.NewPacketConnection(ctx, &PacketConn{Conn: conn}, metadata)
// case CommandMux:
default:
return HandleMuxConnection(ctx, conn, metadata, s.handler)
return HandleMuxConnection(ctx, conn, metadata, s.handler, s.logger)
}
}

View file

@ -10,6 +10,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
@ -21,10 +22,10 @@ var _ conn.Bind = (*ClientBind)(nil)
type ClientBind struct {
ctx context.Context
logger logger.Logger
pauseManager pause.Manager
bindCtx context.Context
bindDone context.CancelFunc
errorHandler E.Handler
dialer N.Dialer
reservedForEndpoint map[netip.AddrPort][3]uint8
connAccess sync.Mutex
@ -35,11 +36,11 @@ type ClientBind struct {
reserved [3]uint8
}
func NewClientBind(ctx context.Context, errorHandler E.Handler, dialer N.Dialer, isConnect bool, connectAddr netip.AddrPort, reserved [3]uint8) *ClientBind {
func NewClientBind(ctx context.Context, logger logger.Logger, dialer N.Dialer, isConnect bool, connectAddr netip.AddrPort, reserved [3]uint8) *ClientBind {
return &ClientBind{
ctx: ctx,
logger: logger,
pauseManager: service.FromContext[pause.Manager](ctx),
errorHandler: errorHandler,
dialer: dialer,
reservedForEndpoint: make(map[netip.AddrPort][3]uint8),
done: make(chan struct{}),
@ -115,7 +116,7 @@ func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint)
return
default:
}
c.errorHandler.NewError(context.Background(), E.Cause(err, "connect to server"))
c.logger.Error(E.Cause(err, "connect to server"))
err = nil
c.pauseManager.WaitActive()
time.Sleep(time.Second)
@ -127,7 +128,7 @@ func (c *ClientBind) receive(packets [][]byte, sizes []int, eps []conn.Endpoint)
select {
case <-c.done:
default:
c.errorHandler.NewError(context.Background(), E.Cause(err, "read packet"))
c.logger.Error(context.Background(), E.Cause(err, "read packet"))
err = nil
}
return

View file

@ -8,7 +8,7 @@ import (
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
dns "github.com/sagernet/sing-dns"
"github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
)