sing-box/inbound/default.go

197 lines
5.8 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package inbound
import (
"context"
"net"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/settings"
2022-07-08 23:03:57 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-07-01 19:34:02 +08:00
"github.com/sagernet/sing/common"
2023-04-14 20:55:05 +08:00
"github.com/sagernet/sing/common/atomic"
2022-07-01 19:34:02 +08:00
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.Inbound = (*myInboundAdapter)(nil)
type myInboundAdapter struct {
2022-07-15 08:42:02 +08:00
protocol string
network []string
ctx context.Context
router adapter.ConnectionRouter
2022-07-15 08:42:02 +08:00
logger log.ContextLogger
tag string
listenOptions option.ListenOptions
connHandler adapter.ConnectionHandler
packetHandler adapter.PacketHandler
oobPacketHandler adapter.OOBPacketHandler
packetUpstream any
2022-07-01 19:34:02 +08:00
// http mixed
2023-09-03 14:29:37 +08:00
setSystemProxy bool
systemProxy settings.SystemProxy
2022-07-01 19:34:02 +08:00
// internal
2022-08-23 21:07:35 +08:00
tcpListener net.Listener
2022-07-01 19:34:02 +08:00
udpConn *net.UDPConn
2022-08-20 10:38:12 +08:00
udpAddr M.Socksaddr
2022-07-01 19:34:02 +08:00
packetOutboundClosed chan struct{}
packetOutbound chan *myInboundPacket
2023-03-02 00:18:35 +08:00
inShutdown atomic.Bool
2022-07-01 19:34:02 +08:00
}
func (a *myInboundAdapter) Type() string {
return a.protocol
}
func (a *myInboundAdapter) Tag() string {
return a.tag
}
2022-08-29 19:43:13 +08:00
func (a *myInboundAdapter) Network() []string {
return a.network
}
2022-07-01 19:34:02 +08:00
func (a *myInboundAdapter) Start() error {
2022-08-04 22:01:20 +08:00
var err error
2022-07-30 00:29:22 +08:00
if common.Contains(a.network, N.NetworkTCP) {
2022-08-23 19:44:40 +08:00
_, err = a.ListenTCP()
2022-07-01 19:34:02 +08:00
if err != nil {
return err
}
go a.loopTCPIn()
}
2022-07-30 00:29:22 +08:00
if common.Contains(a.network, N.NetworkUDP) {
2022-08-23 19:44:40 +08:00
_, err = a.ListenUDP()
2022-07-01 19:34:02 +08:00
if err != nil {
return err
}
a.packetOutboundClosed = make(chan struct{})
a.packetOutbound = make(chan *myInboundPacket)
2022-07-15 08:42:02 +08:00
if a.oobPacketHandler != nil {
if _, threadUnsafeHandler := common.Cast[N.ThreadUnsafeWriter](a.packetUpstream); !threadUnsafeHandler {
go a.loopUDPOOBIn()
} else {
go a.loopUDPOOBInThreadSafe()
}
2022-07-01 19:34:02 +08:00
} else {
2022-07-15 08:42:02 +08:00
if _, threadUnsafeHandler := common.Cast[N.ThreadUnsafeWriter](a.packetUpstream); !threadUnsafeHandler {
go a.loopUDPIn()
} else {
go a.loopUDPInThreadSafe()
}
go a.loopUDPOut()
2022-07-01 19:34:02 +08:00
}
}
if a.setSystemProxy {
2023-09-03 14:29:37 +08:00
listenPort := M.SocksaddrFromNet(a.tcpListener.Addr()).Port
var listenAddrString string
listenAddr := a.listenOptions.Listen.Build()
if listenAddr.IsUnspecified() {
listenAddrString = "127.0.0.1"
} else {
listenAddrString = listenAddr.String()
}
var systemProxy settings.SystemProxy
systemProxy, err = settings.NewSystemProxy(a.ctx, M.ParseSocksaddrHostPort(listenAddrString, listenPort), a.protocol == C.TypeMixed)
2023-09-03 14:29:37 +08:00
if err != nil {
return E.Cause(err, "initialize system proxy")
}
err = systemProxy.Enable()
if err != nil {
return E.Cause(err, "set system proxy")
}
a.systemProxy = systemProxy
}
2022-07-01 19:34:02 +08:00
return nil
}
func (a *myInboundAdapter) Close() error {
2023-03-02 00:18:35 +08:00
a.inShutdown.Store(true)
var err error
2023-09-03 14:29:37 +08:00
if a.systemProxy != nil && a.systemProxy.IsEnabled() {
err = a.systemProxy.Disable()
}
return E.Errors(err, common.Close(
2022-08-23 21:07:35 +08:00
a.tcpListener,
2022-07-01 19:34:02 +08:00
common.PtrOrNil(a.udpConn),
))
2022-07-01 19:34:02 +08:00
}
func (a *myInboundAdapter) upstreamHandler(metadata adapter.InboundContext) adapter.UpstreamHandlerAdapter {
2022-07-02 14:07:50 +08:00
return adapter.NewUpstreamHandler(metadata, a.newConnection, a.streamPacketConnection, a)
2022-07-01 19:34:02 +08:00
}
func (a *myInboundAdapter) upstreamContextHandler() adapter.UpstreamHandlerAdapter {
return adapter.NewUpstreamContextHandler(a.newConnection, a.newPacketConnection, a)
}
func (a *myInboundAdapter) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-12 15:17:29 +08:00
a.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
2022-07-01 19:34:02 +08:00
return a.router.RouteConnection(ctx, conn, metadata)
}
2022-07-02 14:07:50 +08:00
func (a *myInboundAdapter) streamPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-12 15:17:29 +08:00
a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
2022-07-02 14:07:50 +08:00
return a.router.RoutePacketConnection(ctx, conn, metadata)
}
2022-07-01 19:34:02 +08:00
func (a *myInboundAdapter) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-12 15:17:29 +08:00
ctx = log.ContextWithNewID(ctx)
a.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
a.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
2022-07-01 19:34:02 +08:00
return a.router.RoutePacketConnection(ctx, conn, metadata)
}
2022-08-23 19:44:40 +08:00
func (a *myInboundAdapter) createMetadata(conn net.Conn, metadata adapter.InboundContext) adapter.InboundContext {
2022-08-22 18:53:47 +08:00
metadata.Inbound = a.tag
metadata.InboundType = a.protocol
2022-08-29 19:43:13 +08:00
metadata.InboundDetour = a.listenOptions.Detour
2022-10-07 20:30:27 +08:00
metadata.InboundOptions = a.listenOptions.InboundOptions
2022-08-23 21:07:35 +08:00
if !metadata.Source.IsValid() {
2022-09-25 22:16:24 +08:00
metadata.Source = M.SocksaddrFromNet(conn.RemoteAddr()).Unwrap()
2022-08-23 21:07:35 +08:00
}
if !metadata.Destination.IsValid() {
2022-09-25 22:16:24 +08:00
metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
2022-08-23 21:07:35 +08:00
}
if tcpConn, isTCP := common.Cast[*net.TCPConn](conn); isTCP {
2022-09-25 22:16:24 +08:00
metadata.OriginDestination = M.SocksaddrFromNet(tcpConn.LocalAddr()).Unwrap()
2022-08-23 21:07:35 +08:00
}
2022-08-22 18:53:47 +08:00
return metadata
}
2023-07-23 14:42:19 +08:00
func (a *myInboundAdapter) createPacketMetadata(conn N.PacketConn, metadata adapter.InboundContext) adapter.InboundContext {
metadata.Inbound = a.tag
metadata.InboundType = a.protocol
metadata.InboundDetour = a.listenOptions.Detour
metadata.InboundOptions = a.listenOptions.InboundOptions
if !metadata.Destination.IsValid() {
metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
}
return metadata
}
2022-07-01 19:34:02 +08:00
func (a *myInboundAdapter) newError(err error) {
2022-07-02 14:07:50 +08:00
a.logger.Error(err)
2022-07-01 19:34:02 +08:00
}
func (a *myInboundAdapter) NewError(ctx context.Context, err error) {
2022-07-09 19:18:37 +08:00
NewError(a.logger, ctx, err)
}
2022-07-12 15:17:29 +08:00
func NewError(logger log.ContextLogger, ctx context.Context, err error) {
2022-07-01 19:34:02 +08:00
common.Close(err)
2022-07-20 09:41:44 +08:00
if E.IsClosedOrCanceled(err) {
2022-08-16 23:37:51 +08:00
logger.DebugContext(ctx, "connection closed: ", err)
2022-07-01 19:34:02 +08:00
return
}
2022-07-12 15:17:29 +08:00
logger.ErrorContext(ctx, err)
2022-07-01 19:34:02 +08:00
}