sing-box/protocol/shadowsocks/inbound_multi.go

184 lines
5.9 KiB
Go
Raw Normal View History

2024-11-01 16:39:02 +00:00
package shadowsocks
2022-07-04 07:34:43 +00:00
import (
"context"
"net"
2022-07-17 07:11:26 +00:00
"os"
"time"
2022-07-04 07:34:43 +00:00
2022-07-06 07:01:09 +00:00
"github.com/sagernet/sing-box/adapter"
2024-11-01 16:39:02 +00:00
"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/uot"
2022-07-06 07:01:09 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-shadowsocks"
"github.com/sagernet/sing-shadowsocks/shadowaead"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
"github.com/sagernet/sing/common"
2022-07-17 07:11:26 +00:00
"github.com/sagernet/sing/common/auth"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
2022-07-08 15:03:57 +00:00
F "github.com/sagernet/sing/common/format"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/common/logger"
2024-10-21 15:38:34 +00:00
M "github.com/sagernet/sing/common/metadata"
2022-07-08 15:03:57 +00:00
N "github.com/sagernet/sing/common/network"
2023-08-31 06:00:47 +00:00
"github.com/sagernet/sing/common/ntp"
2022-07-04 07:34:43 +00:00
)
2024-11-01 16:39:02 +00:00
var _ adapter.TCPInjectableInbound = (*MultiInbound)(nil)
2022-07-04 07:34:43 +00:00
2024-11-01 16:39:02 +00:00
type MultiInbound struct {
inbound.Adapter
ctx context.Context
router adapter.ConnectionRouterEx
logger logger.ContextLogger
listener *listener.Listener
service shadowsocks.MultiService[int]
users []option.ShadowsocksUser
2022-07-04 07:34:43 +00:00
}
2024-11-01 16:39:02 +00:00
func newMultiInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*MultiInbound, error) {
inbound := &MultiInbound{
Adapter: inbound.NewAdapter(C.TypeShadowsocks, tag),
ctx: ctx,
router: uot.NewRouter(router, logger),
logger: logger,
2022-07-04 07:34:43 +00:00
}
var err error
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
if err != nil {
return nil, err
}
var udpTimeout time.Duration
2022-07-04 07:34:43 +00:00
if options.UDPTimeout != 0 {
udpTimeout = time.Duration(options.UDPTimeout)
2022-07-04 07:34:43 +00:00
} else {
udpTimeout = C.UDPTimeout
2022-07-04 07:34:43 +00:00
}
var service shadowsocks.MultiService[int]
if common.Contains(shadowaead_2022.List, options.Method) {
service, err = shadowaead_2022.NewMultiServiceWithPassword[int](
options.Method,
options.Password,
int64(udpTimeout.Seconds()),
2024-10-21 15:38:34 +00:00
adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound),
2023-08-31 06:00:47 +00:00
ntp.TimeFuncFromContext(ctx),
)
} else if common.Contains(shadowaead.List, options.Method) {
service, err = shadowaead.NewMultiService[int](
options.Method,
int64(udpTimeout.Seconds()),
2024-10-21 15:38:34 +00:00
adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound),
)
} else {
return nil, E.New("unsupported method: " + options.Method)
}
2022-11-26 03:21:24 +00:00
if err != nil {
return nil, err
}
2022-11-13 03:30:48 +00:00
err = service.UpdateUsersWithPasswords(common.MapIndexed(options.Users, func(index int, user option.ShadowsocksUser) int {
2022-07-04 07:34:43 +00:00
return index
}), common.Map(options.Users, func(user option.ShadowsocksUser) string {
return user.Password
}))
if err != nil {
return nil, err
}
inbound.service = service
2022-11-13 03:30:48 +00:00
inbound.users = options.Users
2024-11-01 16:39:02 +00:00
inbound.listener = listener.New(listener.Options{
Context: ctx,
Logger: logger,
Network: options.Network.Build(),
Listen: options.ListenOptions,
ConnectionHandler: inbound,
PacketHandler: inbound,
ThreadUnsafePacketWriter: true,
})
2022-07-04 07:34:43 +00:00
return inbound, err
}
2024-11-21 10:10:41 +00:00
func (h *MultiInbound) Start(stage adapter.StartStage) error {
if stage != adapter.StartStateStart {
return nil
}
2024-11-01 16:39:02 +00:00
return h.listener.Start()
}
func (h *MultiInbound) Close() error {
return h.listener.Close()
}
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
2024-11-01 16:39:02 +00:00
func (h *MultiInbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
2024-10-21 15:38:34 +00:00
err := h.service.NewConnection(ctx, conn, adapter.UpstreamMetadata(metadata))
N.CloseOnHandshakeFailure(conn, onClose, err)
if err != nil {
if E.IsClosedOrCanceled(err) {
h.logger.DebugContext(ctx, "connection closed: ", err)
} else {
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
}
2024-10-21 15:38:34 +00:00
}
2022-07-04 07:34:43 +00:00
}
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
2024-11-01 16:39:02 +00:00
func (h *MultiInbound) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
err := h.service.NewPacket(h.ctx, &stubPacketConn{h.listener.PacketWriter()}, buffer, M.Metadata{Source: source})
2024-10-21 15:38:34 +00:00
if err != nil {
h.logger.Error(E.Cause(err, "process packet from ", source))
}
2022-08-29 11:43:13 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *MultiInbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-17 07:11:26 +00:00
userIndex, loaded := auth.UserFromContext[int](ctx)
if !loaded {
return os.ErrInvalid
}
user := h.users[userIndex].Name
2022-07-04 07:34:43 +00:00
if user == "" {
2022-07-17 07:11:26 +00:00
user = F.ToString(userIndex)
} else {
metadata.User = user
2022-07-04 07:34:43 +00:00
}
2022-07-12 07:17:29 +00:00
h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
2024-11-01 16:39:02 +00:00
metadata.Inbound = h.Tag()
metadata.InboundType = h.Type()
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
metadata.InboundDetour = h.listener.ListenOptions().Detour
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
2024-11-01 16:39:02 +00:00
return h.router.RouteConnection(ctx, conn, metadata)
2022-07-04 07:34:43 +00:00
}
2024-11-01 16:39:02 +00:00
func (h *MultiInbound) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-17 07:11:26 +00:00
userIndex, loaded := auth.UserFromContext[int](ctx)
if !loaded {
return os.ErrInvalid
}
user := h.users[userIndex].Name
2022-07-04 07:34:43 +00:00
if user == "" {
2022-07-17 07:11:26 +00:00
user = F.ToString(userIndex)
} else {
metadata.User = user
2022-07-04 07:34:43 +00:00
}
2022-07-12 07:17:29 +00:00
ctx = log.ContextWithNewID(ctx)
h.logger.InfoContext(ctx, "[", user, "] inbound packet connection from ", metadata.Source)
h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
2024-11-01 16:39:02 +00:00
metadata.Inbound = h.Tag()
metadata.InboundType = h.Type()
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
2024-11-01 16:39:02 +00:00
metadata.InboundDetour = h.listener.ListenOptions().Detour
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
2024-11-01 16:39:02 +00:00
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
return h.router.RoutePacketConnection(ctx, conn, metadata)
}
2024-11-23 14:34:02 +00:00
//nolint:staticcheck
2024-11-01 16:39:02 +00:00
func (h *MultiInbound) NewError(ctx context.Context, err error) {
NewError(h.logger, ctx, err)
2022-07-04 07:34:43 +00:00
}