sing-box/inbound/shadowsocks_multi.go

122 lines
3.9 KiB
Go
Raw Permalink Normal View History

2022-07-04 15:34:43 +08:00
package inbound
import (
"context"
"net"
2022-07-17 15:11:26 +08:00
"os"
2022-07-04 15:34:43 +08:00
2022-07-06 15:01:09 +08:00
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
"github.com/sagernet/sing/common"
2022-07-17 15:11:26 +08:00
"github.com/sagernet/sing/common/auth"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
2022-07-08 23:03:57 +08:00
F "github.com/sagernet/sing/common/format"
N "github.com/sagernet/sing/common/network"
2022-07-04 15:34:43 +08:00
)
2022-08-29 19:43:13 +08:00
var (
_ adapter.Inbound = (*ShadowsocksMulti)(nil)
_ adapter.InjectableInbound = (*ShadowsocksMulti)(nil)
)
2022-07-04 15:34:43 +08:00
type ShadowsocksMulti struct {
myInboundAdapter
2022-11-13 11:30:48 +08:00
service *shadowaead_2022.MultiService[int]
users []option.ShadowsocksUser
2022-07-04 15:34:43 +08:00
}
2022-07-12 15:17:29 +08:00
func newShadowsocksMulti(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*ShadowsocksMulti, error) {
2022-07-04 15:34:43 +08:00
inbound := &ShadowsocksMulti{
myInboundAdapter: myInboundAdapter{
protocol: C.TypeShadowsocks,
network: options.Network.Build(),
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
}
inbound.connHandler = inbound
inbound.packetHandler = inbound
var udpTimeout int64
if options.UDPTimeout != 0 {
udpTimeout = options.UDPTimeout
} else {
udpTimeout = int64(C.UDPTimeout.Seconds())
2022-07-04 15:34:43 +08:00
}
if !common.Contains(shadowaead_2022.List, options.Method) {
return nil, E.New("unsupported method: " + options.Method)
}
2022-07-04 15:34:43 +08:00
service, err := shadowaead_2022.NewMultiServiceWithPassword[int](
options.Method,
options.Password,
udpTimeout,
adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound),
2023-02-21 14:53:00 +08:00
router.TimeFunc(),
2022-07-04 15:34:43 +08:00
)
2022-11-26 11:21:24 +08:00
if err != nil {
return nil, err
}
2022-11-13 11:30:48 +08:00
err = service.UpdateUsersWithPasswords(common.MapIndexed(options.Users, func(index int, user option.ShadowsocksUser) int {
2022-07-04 15:34:43 +08:00
return index
}), common.Map(options.Users, func(user option.ShadowsocksUser) string {
return user.Password
}))
if err != nil {
return nil, err
}
inbound.service = service
inbound.packetUpstream = service
2022-11-13 11:30:48 +08:00
inbound.users = options.Users
2022-07-04 15:34:43 +08:00
return inbound, err
}
func (h *ShadowsocksMulti) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-12 15:17:29 +08:00
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
2022-07-04 15:34:43 +08:00
}
func (h *ShadowsocksMulti) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
2022-07-17 15:11:26 +08:00
return h.service.NewPacket(adapter.WithContext(ctx, &metadata), conn, buffer, adapter.UpstreamMetadata(metadata))
2022-07-04 15:34:43 +08:00
}
2022-08-29 19:43:13 +08:00
func (h *ShadowsocksMulti) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return os.ErrInvalid
}
2022-07-04 15:34:43 +08:00
func (h *ShadowsocksMulti) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-17 15:11:26 +08:00
userIndex, loaded := auth.UserFromContext[int](ctx)
if !loaded {
return os.ErrInvalid
}
user := h.users[userIndex].Name
2022-07-04 15:34:43 +08:00
if user == "" {
2022-07-17 15:11:26 +08:00
user = F.ToString(userIndex)
} else {
metadata.User = user
2022-07-04 15:34:43 +08:00
}
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
2022-07-04 15:34:43 +08:00
return h.router.RouteConnection(ctx, conn, metadata)
}
func (h *ShadowsocksMulti) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-17 15:11:26 +08:00
userIndex, loaded := auth.UserFromContext[int](ctx)
if !loaded {
return os.ErrInvalid
}
user := h.users[userIndex].Name
2022-07-04 15:34:43 +08:00
if user == "" {
2022-07-17 15:11:26 +08:00
user = F.ToString(userIndex)
} else {
metadata.User = user
2022-07-04 15:34:43 +08:00
}
2022-07-12 15:17:29 +08: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)
2022-07-04 15:34:43 +08:00
return h.router.RoutePacketConnection(ctx, conn, metadata)
}