github.com/sagernet/sing-box@v1.9.0-rc.20/inbound/socks.go (about)

     1  package inbound
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"os"
     7  
     8  	"github.com/sagernet/sing-box/adapter"
     9  	"github.com/sagernet/sing-box/common/uot"
    10  	C "github.com/sagernet/sing-box/constant"
    11  	"github.com/sagernet/sing-box/log"
    12  	"github.com/sagernet/sing-box/option"
    13  	"github.com/sagernet/sing/common/auth"
    14  	N "github.com/sagernet/sing/common/network"
    15  	"github.com/sagernet/sing/protocol/socks"
    16  )
    17  
    18  var (
    19  	_ adapter.Inbound           = (*Socks)(nil)
    20  	_ adapter.InjectableInbound = (*Socks)(nil)
    21  )
    22  
    23  type Socks struct {
    24  	myInboundAdapter
    25  	authenticator *auth.Authenticator
    26  }
    27  
    28  func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
    29  	inbound := &Socks{
    30  		myInboundAdapter{
    31  			protocol:      C.TypeSOCKS,
    32  			network:       []string{N.NetworkTCP},
    33  			ctx:           ctx,
    34  			router:        uot.NewRouter(router, logger),
    35  			logger:        logger,
    36  			tag:           tag,
    37  			listenOptions: options.ListenOptions,
    38  		},
    39  		auth.NewAuthenticator(options.Users),
    40  	}
    41  	inbound.connHandler = inbound
    42  	return inbound
    43  }
    44  
    45  func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
    46  	return socks.HandleConnection(ctx, conn, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
    47  }
    48  
    49  func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
    50  	return os.ErrInvalid
    51  }