2022-08-29 11:43:13 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-04-08 10:00:48 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-08-29 11:43:13 +00:00
|
|
|
"github.com/sagernet/sing-box/log"
|
2024-04-08 10:00:48 +00:00
|
|
|
"github.com/sagernet/sing/common/control"
|
2022-08-29 11:43:13 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *myInboundAdapter) ListenTCP() (net.Listener, error) {
|
|
|
|
var err error
|
2023-03-19 12:46:22 +00:00
|
|
|
bindAddr := M.SocksaddrFrom(a.listenOptions.Listen.Build(), a.listenOptions.ListenPort)
|
2022-08-29 11:43:13 +00:00
|
|
|
var tcpListener net.Listener
|
2023-08-16 09:47:24 +00:00
|
|
|
var listenConfig net.ListenConfig
|
2024-04-08 10:00:48 +00:00
|
|
|
// TODO: Add an option to customize the keep alive period
|
|
|
|
listenConfig.KeepAlive = C.TCPKeepAliveInitial
|
|
|
|
listenConfig.Control = control.Append(listenConfig.Control, control.SetKeepAlivePeriod(C.TCPKeepAliveInitial, C.TCPKeepAliveInterval))
|
2023-08-16 09:47:24 +00:00
|
|
|
if a.listenOptions.TCPMultiPath {
|
|
|
|
if !go121Available {
|
|
|
|
return nil, E.New("MultiPath TCP requires go1.21, please recompile your binary.")
|
2023-08-08 08:14:03 +00:00
|
|
|
}
|
2023-08-16 09:47:24 +00:00
|
|
|
setMultiPathTCP(&listenConfig)
|
|
|
|
}
|
|
|
|
if a.listenOptions.TCPFastOpen {
|
|
|
|
if !go120Available {
|
|
|
|
return nil, E.New("TCP Fast Open requires go1.20, please recompile your binary.")
|
|
|
|
}
|
|
|
|
tcpListener, err = listenTFO(listenConfig, a.ctx, M.NetworkFromNetAddr(N.NetworkTCP, bindAddr.Addr), bindAddr.String())
|
2022-08-29 11:43:13 +00:00
|
|
|
} else {
|
2023-08-16 09:47:24 +00:00
|
|
|
tcpListener, err = listenConfig.Listen(a.ctx, M.NetworkFromNetAddr(N.NetworkTCP, bindAddr.Addr), bindAddr.String())
|
2022-08-29 11:43:13 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
a.logger.Info("tcp server started at ", tcpListener.Addr())
|
|
|
|
}
|
2023-10-25 04:00:00 +00:00
|
|
|
if a.listenOptions.ProxyProtocol || a.listenOptions.ProxyProtocolAcceptNoHeader {
|
|
|
|
return nil, E.New("Proxy Protocol is deprecated and removed in sing-box 1.6.0")
|
2022-08-29 11:43:13 +00:00
|
|
|
}
|
|
|
|
a.tcpListener = tcpListener
|
|
|
|
return tcpListener, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *myInboundAdapter) loopTCPIn() {
|
|
|
|
tcpListener := a.tcpListener
|
|
|
|
for {
|
|
|
|
conn, err := tcpListener.Accept()
|
|
|
|
if err != nil {
|
2023-03-01 16:18:35 +00:00
|
|
|
//goland:noinspection GoDeprecation
|
|
|
|
//nolint:staticcheck
|
|
|
|
if netError, isNetError := err.(net.Error); isNetError && netError.Temporary() {
|
|
|
|
a.logger.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if a.inShutdown.Load() && E.IsClosed(err) {
|
2022-09-06 15:16:25 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-01 16:18:35 +00:00
|
|
|
a.tcpListener.Close()
|
|
|
|
a.logger.Error("serve error: ", err)
|
2022-09-06 15:16:25 +00:00
|
|
|
continue
|
2022-08-29 11:43:13 +00:00
|
|
|
}
|
|
|
|
go a.injectTCP(conn, adapter.InboundContext{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *myInboundAdapter) injectTCP(conn net.Conn, metadata adapter.InboundContext) {
|
|
|
|
ctx := log.ContextWithNewID(a.ctx)
|
|
|
|
metadata = a.createMetadata(conn, metadata)
|
|
|
|
a.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
|
2024-10-21 15:38:34 +00:00
|
|
|
a.connHandler.NewConnectionEx(ctx, conn, metadata, nil)
|
2022-08-29 11:43:13 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (a *myInboundAdapter) routeTCP(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
metadata := a.createMetadata(conn, adapter.InboundContext{
|
|
|
|
Source: source,
|
|
|
|
Destination: destination,
|
|
|
|
})
|
2022-08-29 11:43:13 +00:00
|
|
|
a.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
|
2024-10-21 15:38:34 +00:00
|
|
|
a.connHandler.NewConnectionEx(ctx, conn, metadata, onClose)
|
2022-08-29 11:43:13 +00:00
|
|
|
}
|