|
@@ -1,4 +1,4 @@
|
|
-package inbound
|
|
|
|
|
|
+package shadowsocks
|
|
|
|
|
|
import (
|
|
import (
|
|
"context"
|
|
"context"
|
|
@@ -7,6 +7,8 @@ import (
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
+ "github.com/sagernet/sing-box/adapter/inbound"
|
|
|
|
+ "github.com/sagernet/sing-box/common/listener"
|
|
"github.com/sagernet/sing-box/common/mux"
|
|
"github.com/sagernet/sing-box/common/mux"
|
|
"github.com/sagernet/sing-box/common/uot"
|
|
"github.com/sagernet/sing-box/common/uot"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
@@ -18,36 +20,31 @@ import (
|
|
"github.com/sagernet/sing/common/buf"
|
|
"github.com/sagernet/sing/common/buf"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
F "github.com/sagernet/sing/common/format"
|
|
F "github.com/sagernet/sing/common/format"
|
|
|
|
+ "github.com/sagernet/sing/common/logger"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
)
|
|
|
|
|
|
-var (
|
|
|
|
- _ adapter.Inbound = (*ShadowsocksRelay)(nil)
|
|
|
|
- _ adapter.TCPInjectableInbound = (*ShadowsocksRelay)(nil)
|
|
|
|
-)
|
|
|
|
|
|
+var _ adapter.TCPInjectableInbound = (*RelayInbound)(nil)
|
|
|
|
|
|
-type ShadowsocksRelay struct {
|
|
|
|
- myInboundAdapter
|
|
|
|
|
|
+type RelayInbound struct {
|
|
|
|
+ inbound.Adapter
|
|
|
|
+ ctx context.Context
|
|
|
|
+ router adapter.ConnectionRouterEx
|
|
|
|
+ logger logger.ContextLogger
|
|
|
|
+ listener *listener.Listener
|
|
service *shadowaead_2022.RelayService[int]
|
|
service *shadowaead_2022.RelayService[int]
|
|
destinations []option.ShadowsocksDestination
|
|
destinations []option.ShadowsocksDestination
|
|
}
|
|
}
|
|
|
|
|
|
-func newShadowsocksRelay(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*ShadowsocksRelay, error) {
|
|
|
|
- inbound := &ShadowsocksRelay{
|
|
|
|
- myInboundAdapter: myInboundAdapter{
|
|
|
|
- protocol: C.TypeShadowsocks,
|
|
|
|
- network: options.Network.Build(),
|
|
|
|
- ctx: ctx,
|
|
|
|
- router: uot.NewRouter(router, logger),
|
|
|
|
- logger: logger,
|
|
|
|
- tag: tag,
|
|
|
|
- listenOptions: options.ListenOptions,
|
|
|
|
- },
|
|
|
|
|
|
+func newRelayInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*RelayInbound, error) {
|
|
|
|
+ inbound := &RelayInbound{
|
|
|
|
+ Adapter: inbound.NewAdapter(C.TypeShadowsocks, tag),
|
|
|
|
+ ctx: ctx,
|
|
|
|
+ router: uot.NewRouter(router, logger),
|
|
|
|
+ logger: logger,
|
|
destinations: options.Destinations,
|
|
destinations: options.Destinations,
|
|
}
|
|
}
|
|
- inbound.connHandler = inbound
|
|
|
|
- inbound.packetHandler = inbound
|
|
|
|
var err error
|
|
var err error
|
|
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
|
|
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -77,11 +74,27 @@ func newShadowsocksRelay(ctx context.Context, router adapter.Router, logger log.
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
inbound.service = service
|
|
inbound.service = service
|
|
- inbound.packetUpstream = service
|
|
|
|
|
|
+ inbound.listener = listener.New(listener.Options{
|
|
|
|
+ Context: ctx,
|
|
|
|
+ Logger: logger,
|
|
|
|
+ Network: options.Network.Build(),
|
|
|
|
+ Listen: options.ListenOptions,
|
|
|
|
+ ConnectionHandler: inbound,
|
|
|
|
+ PacketHandler: inbound,
|
|
|
|
+ ThreadUnsafePacketWriter: true,
|
|
|
|
+ })
|
|
return inbound, err
|
|
return inbound, err
|
|
}
|
|
}
|
|
|
|
|
|
-func (h *ShadowsocksRelay) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
|
|
|
|
+func (h *RelayInbound) Start() error {
|
|
|
|
+ return h.listener.Start()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h *RelayInbound) Close() error {
|
|
|
|
+ return h.listener.Close()
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h *RelayInbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
err := h.service.NewConnection(ctx, conn, adapter.UpstreamMetadata(metadata))
|
|
err := h.service.NewConnection(ctx, conn, adapter.UpstreamMetadata(metadata))
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -89,14 +102,14 @@ func (h *ShadowsocksRelay) NewConnectionEx(ctx context.Context, conn net.Conn, m
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (h *ShadowsocksRelay) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
|
|
|
|
- err := h.service.NewPacket(h.ctx, h.packetConn(), buffer, M.Metadata{Source: source})
|
|
|
|
|
|
+func (h *RelayInbound) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
|
|
|
|
+ err := h.service.NewPacket(h.ctx, &stubPacketConn{h.listener.PacketWriter()}, buffer, M.Metadata{Source: source})
|
|
if err != nil {
|
|
if err != nil {
|
|
h.logger.Error(E.Cause(err, "process packet from ", source))
|
|
h.logger.Error(E.Cause(err, "process packet from ", source))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (h *ShadowsocksRelay) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
|
|
+func (h *RelayInbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
destinationIndex, loaded := auth.UserFromContext[int](ctx)
|
|
destinationIndex, loaded := auth.UserFromContext[int](ctx)
|
|
if !loaded {
|
|
if !loaded {
|
|
return os.ErrInvalid
|
|
return os.ErrInvalid
|
|
@@ -108,10 +121,12 @@ func (h *ShadowsocksRelay) newConnection(ctx context.Context, conn net.Conn, met
|
|
metadata.User = destination
|
|
metadata.User = destination
|
|
}
|
|
}
|
|
h.logger.InfoContext(ctx, "[", destination, "] inbound connection to ", metadata.Destination)
|
|
h.logger.InfoContext(ctx, "[", destination, "] inbound connection to ", metadata.Destination)
|
|
- return h.router.RouteConnection(ctx, conn, h.createMetadata(conn, metadata))
|
|
|
|
|
|
+ metadata.Inbound = h.Tag()
|
|
|
|
+ metadata.InboundType = h.Type()
|
|
|
|
+ return h.router.RouteConnection(ctx, conn, metadata)
|
|
}
|
|
}
|
|
|
|
|
|
-func (h *ShadowsocksRelay) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
|
|
+func (h *RelayInbound) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
destinationIndex, loaded := auth.UserFromContext[int](ctx)
|
|
destinationIndex, loaded := auth.UserFromContext[int](ctx)
|
|
if !loaded {
|
|
if !loaded {
|
|
return os.ErrInvalid
|
|
return os.ErrInvalid
|
|
@@ -125,5 +140,13 @@ func (h *ShadowsocksRelay) newPacketConnection(ctx context.Context, conn N.Packe
|
|
ctx = log.ContextWithNewID(ctx)
|
|
ctx = log.ContextWithNewID(ctx)
|
|
h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection from ", metadata.Source)
|
|
h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection from ", metadata.Source)
|
|
h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection to ", metadata.Destination)
|
|
h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection to ", metadata.Destination)
|
|
- return h.router.RoutePacketConnection(ctx, conn, h.createPacketMetadata(conn, metadata))
|
|
|
|
|
|
+ metadata.Inbound = h.Tag()
|
|
|
|
+ metadata.InboundType = h.Type()
|
|
|
|
+ metadata.InboundDetour = h.listener.ListenOptions().Detour
|
|
|
|
+ metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
|
|
|
|
+ return h.router.RoutePacketConnection(ctx, conn, metadata)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (h *RelayInbound) NewError(ctx context.Context, err error) {
|
|
|
|
+ NewError(h.logger, ctx, err)
|
|
}
|
|
}
|