github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/inbound/default.go (about)

     1  package inbound
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/inazumav/sing-box/adapter"
     8  	"github.com/inazumav/sing-box/common/settings"
     9  	C "github.com/inazumav/sing-box/constant"
    10  	"github.com/inazumav/sing-box/log"
    11  	"github.com/inazumav/sing-box/option"
    12  	"github.com/sagernet/sing/common"
    13  	"github.com/sagernet/sing/common/atomic"
    14  	E "github.com/sagernet/sing/common/exceptions"
    15  	M "github.com/sagernet/sing/common/metadata"
    16  	N "github.com/sagernet/sing/common/network"
    17  )
    18  
    19  var _ adapter.Inbound = (*myInboundAdapter)(nil)
    20  
    21  type myInboundAdapter struct {
    22  	protocol         string
    23  	network          []string
    24  	ctx              context.Context
    25  	router           adapter.Router
    26  	logger           log.ContextLogger
    27  	tag              string
    28  	listenOptions    option.ListenOptions
    29  	connHandler      adapter.ConnectionHandler
    30  	packetHandler    adapter.PacketHandler
    31  	oobPacketHandler adapter.OOBPacketHandler
    32  	packetUpstream   any
    33  
    34  	// http mixed
    35  
    36  	setSystemProxy bool
    37  	systemProxy    settings.SystemProxy
    38  
    39  	// internal
    40  
    41  	tcpListener          net.Listener
    42  	udpConn              *net.UDPConn
    43  	udpAddr              M.Socksaddr
    44  	packetOutboundClosed chan struct{}
    45  	packetOutbound       chan *myInboundPacket
    46  
    47  	inShutdown atomic.Bool
    48  }
    49  
    50  func (a *myInboundAdapter) Type() string {
    51  	return a.protocol
    52  }
    53  
    54  func (a *myInboundAdapter) Tag() string {
    55  	return a.tag
    56  }
    57  
    58  func (a *myInboundAdapter) Network() []string {
    59  	return a.network
    60  }
    61  
    62  func (a *myInboundAdapter) Start() error {
    63  	var err error
    64  	if common.Contains(a.network, N.NetworkTCP) {
    65  		_, err = a.ListenTCP()
    66  		if err != nil {
    67  			return err
    68  		}
    69  		go a.loopTCPIn()
    70  	}
    71  	if common.Contains(a.network, N.NetworkUDP) {
    72  		_, err = a.ListenUDP()
    73  		if err != nil {
    74  			return err
    75  		}
    76  		a.packetOutboundClosed = make(chan struct{})
    77  		a.packetOutbound = make(chan *myInboundPacket)
    78  		if a.oobPacketHandler != nil {
    79  			if _, threadUnsafeHandler := common.Cast[N.ThreadUnsafeWriter](a.packetUpstream); !threadUnsafeHandler {
    80  				go a.loopUDPOOBIn()
    81  			} else {
    82  				go a.loopUDPOOBInThreadSafe()
    83  			}
    84  		} else {
    85  			if _, threadUnsafeHandler := common.Cast[N.ThreadUnsafeWriter](a.packetUpstream); !threadUnsafeHandler {
    86  				go a.loopUDPIn()
    87  			} else {
    88  				go a.loopUDPInThreadSafe()
    89  			}
    90  			go a.loopUDPOut()
    91  		}
    92  	}
    93  	if a.setSystemProxy {
    94  		listenPort := M.SocksaddrFromNet(a.tcpListener.Addr()).Port
    95  		var listenAddrString string
    96  		listenAddr := a.listenOptions.Listen.Build()
    97  		if listenAddr.IsUnspecified() {
    98  			listenAddrString = "127.0.0.1"
    99  		} else {
   100  			listenAddrString = listenAddr.String()
   101  		}
   102  		a.systemProxy, err = settings.NewSystemProxy(a.ctx, M.ParseSocksaddrHostPort(listenAddrString, listenPort), a.protocol == C.TypeMixed)
   103  		if err != nil {
   104  			return E.Cause(err, "initialize system proxy")
   105  		}
   106  		err = a.systemProxy.Enable()
   107  		if err != nil {
   108  			return E.Cause(err, "set system proxy")
   109  		}
   110  	}
   111  	return nil
   112  }
   113  
   114  func (a *myInboundAdapter) Close() error {
   115  	a.inShutdown.Store(true)
   116  	var err error
   117  	if a.systemProxy != nil && a.systemProxy.IsEnabled() {
   118  		err = a.systemProxy.Disable()
   119  	}
   120  	return E.Errors(err, common.Close(
   121  		a.tcpListener,
   122  		common.PtrOrNil(a.udpConn),
   123  	))
   124  }
   125  
   126  func (a *myInboundAdapter) upstreamHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {
   127  	return adapter.NewUpstreamHandler(metadata, a.newConnection, a.streamPacketConnection, a)
   128  }
   129  
   130  func (a *myInboundAdapter) upstreamContextHandler() adapter.UpstreamHandlerAdapter {
   131  	return adapter.NewUpstreamContextHandler(a.newConnection, a.newPacketConnection, a)
   132  }
   133  
   134  func (a *myInboundAdapter) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
   135  	a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
   136  	return a.router.RouteConnection(ctx, conn, metadata)
   137  }
   138  
   139  func (a *myInboundAdapter) streamPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
   140  	a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
   141  	return a.router.RoutePacketConnection(ctx, conn, metadata)
   142  }
   143  
   144  func (a *myInboundAdapter) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
   145  	ctx = log.ContextWithNewID(ctx)
   146  	a.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
   147  	a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
   148  	return a.router.RoutePacketConnection(ctx, conn, metadata)
   149  }
   150  
   151  func (a *myInboundAdapter) createMetadata(conn net.Conn, metadata adapter.InboundContext) adapter.InboundContext {
   152  	metadata.Inbound = a.tag
   153  	metadata.InboundType = a.protocol
   154  	metadata.InboundDetour = a.listenOptions.Detour
   155  	metadata.InboundOptions = a.listenOptions.InboundOptions
   156  	if !metadata.Source.IsValid() {
   157  		metadata.Source = M.SocksaddrFromNet(conn.RemoteAddr()).Unwrap()
   158  	}
   159  	if !metadata.Destination.IsValid() {
   160  		metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
   161  	}
   162  	if tcpConn, isTCP := common.Cast[*net.TCPConn](conn); isTCP {
   163  		metadata.OriginDestination = M.SocksaddrFromNet(tcpConn.LocalAddr()).Unwrap()
   164  	}
   165  	return metadata
   166  }
   167  
   168  func (a *myInboundAdapter) createPacketMetadata(conn N.PacketConn, metadata adapter.InboundContext) adapter.InboundContext {
   169  	metadata.Inbound = a.tag
   170  	metadata.InboundType = a.protocol
   171  	metadata.InboundDetour = a.listenOptions.Detour
   172  	metadata.InboundOptions = a.listenOptions.InboundOptions
   173  	if !metadata.Destination.IsValid() {
   174  		metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
   175  	}
   176  	return metadata
   177  }
   178  
   179  func (a *myInboundAdapter) newError(err error) {
   180  	a.logger.Error(err)
   181  }
   182  
   183  func (a *myInboundAdapter) NewError(ctx context.Context, err error) {
   184  	NewError(a.logger, ctx, err)
   185  }
   186  
   187  func NewError(logger log.ContextLogger, ctx context.Context, err error) {
   188  	common.Close(err)
   189  	if E.IsClosedOrCanceled(err) {
   190  		logger.DebugContext(ctx, "connection closed: ", err)
   191  		return
   192  	}
   193  	logger.ErrorContext(ctx, err)
   194  }