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

     1  package inbound
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/sagernet/sing-box/adapter"
     8  	"github.com/sagernet/sing-box/common/dialer"
     9  	C "github.com/sagernet/sing-box/constant"
    10  	"github.com/sagernet/sing-box/log"
    11  	"github.com/sagernet/sing-box/option"
    12  	"github.com/sagernet/sing-shadowtls"
    13  	"github.com/sagernet/sing/common"
    14  	"github.com/sagernet/sing/common/auth"
    15  	N "github.com/sagernet/sing/common/network"
    16  )
    17  
    18  type ShadowTLS struct {
    19  	myInboundAdapter
    20  	service *shadowtls.Service
    21  }
    22  
    23  func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowTLSInboundOptions) (*ShadowTLS, error) {
    24  	inbound := &ShadowTLS{
    25  		myInboundAdapter: myInboundAdapter{
    26  			protocol:      C.TypeShadowTLS,
    27  			network:       []string{N.NetworkTCP},
    28  			ctx:           ctx,
    29  			router:        router,
    30  			logger:        logger,
    31  			tag:           tag,
    32  			listenOptions: options.ListenOptions,
    33  		},
    34  	}
    35  
    36  	if options.Version == 0 {
    37  		options.Version = 1
    38  	}
    39  
    40  	var handshakeForServerName map[string]shadowtls.HandshakeConfig
    41  	if options.Version > 1 {
    42  		handshakeForServerName = make(map[string]shadowtls.HandshakeConfig)
    43  		for serverName, serverOptions := range options.HandshakeForServerName {
    44  			handshakeDialer, err := dialer.New(router, serverOptions.DialerOptions)
    45  			if err != nil {
    46  				return nil, err
    47  			}
    48  			handshakeForServerName[serverName] = shadowtls.HandshakeConfig{
    49  				Server: serverOptions.ServerOptions.Build(),
    50  				Dialer: handshakeDialer,
    51  			}
    52  		}
    53  	}
    54  	handshakeDialer, err := dialer.New(router, options.Handshake.DialerOptions)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	service, err := shadowtls.NewService(shadowtls.ServiceConfig{
    59  		Version:  options.Version,
    60  		Password: options.Password,
    61  		Users: common.Map(options.Users, func(it option.ShadowTLSUser) shadowtls.User {
    62  			return (shadowtls.User)(it)
    63  		}),
    64  		Handshake: shadowtls.HandshakeConfig{
    65  			Server: options.Handshake.ServerOptions.Build(),
    66  			Dialer: handshakeDialer,
    67  		},
    68  		HandshakeForServerName: handshakeForServerName,
    69  		StrictMode:             options.StrictMode,
    70  		Handler:                adapter.NewUpstreamContextHandler(inbound.newConnection, nil, inbound),
    71  		Logger:                 logger,
    72  	})
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	inbound.service = service
    77  	inbound.connHandler = inbound
    78  	return inbound, nil
    79  }
    80  
    81  func (h *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
    82  	return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
    83  }
    84  
    85  func (h *ShadowTLS) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
    86  	if userName, _ := auth.UserFromContext[string](ctx); userName != "" {
    87  		metadata.User = userName
    88  		h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination)
    89  	} else {
    90  		h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
    91  	}
    92  	return h.router.RouteConnection(ctx, conn, metadata)
    93  }