sing-box/inbound/default.go

195 lines
5.8 KiB
Go
Raw Normal View History

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