sing-box/inbound/shadowsocks_relay.go

125 lines
4.3 KiB
Go
Raw 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"
"time"
2022-07-04 15:34:43 +08:00
2022-07-06 15:01:09 +08:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/mux"
"github.com/sagernet/sing-box/common/uot"
2022-07-06 15:01:09 +08:00
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"
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 = (*ShadowsocksRelay)(nil)
_ adapter.InjectableInbound = (*ShadowsocksRelay)(nil)
)
2022-07-04 15:34:43 +08:00
type ShadowsocksRelay struct {
myInboundAdapter
service *shadowaead_2022.RelayService[int]
destinations []option.ShadowsocksDestination
}
2022-07-12 15:17:29 +08:00
func newShadowsocksRelay(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*ShadowsocksRelay, error) {
2022-07-04 15:34:43 +08:00
inbound := &ShadowsocksRelay{
myInboundAdapter: myInboundAdapter{
protocol: C.TypeShadowsocks,
network: options.Network.Build(),
ctx: ctx,
router: uot.NewRouter(router, logger),
2022-07-04 15:34:43 +08:00
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
destinations: options.Destinations,
}
inbound.connHandler = inbound
inbound.packetHandler = inbound
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 15:34:43 +08:00
if options.UDPTimeout != 0 {
udpTimeout = time.Duration(options.UDPTimeout)
2022-07-04 15:34:43 +08:00
} else {
udpTimeout = C.UDPTimeout
2022-07-04 15:34:43 +08:00
}
service, err := shadowaead_2022.NewRelayServiceWithPassword[int](
options.Method,
options.Password,
int64(udpTimeout.Seconds()),
2022-07-04 15:34:43 +08:00
adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound),
)
if err != nil {
return nil, err
}
err = service.UpdateUsersWithPasswords(common.MapIndexed(options.Destinations, func(index int, user option.ShadowsocksDestination) int {
return index
}), common.Map(options.Destinations, func(user option.ShadowsocksDestination) string {
return user.Password
}), common.Map(options.Destinations, option.ShadowsocksDestination.Build))
if err != nil {
return nil, err
}
inbound.service = service
inbound.packetUpstream = service
return inbound, err
}
func (h *ShadowsocksRelay) 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 *ShadowsocksRelay) 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 *ShadowsocksRelay) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return os.ErrInvalid
}
2022-07-04 15:34:43 +08:00
func (h *ShadowsocksRelay) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-17 15:11:26 +08:00
destinationIndex, loaded := auth.UserFromContext[int](ctx)
if !loaded {
return os.ErrInvalid
}
destination := h.destinations[destinationIndex].Name
2022-07-04 15:34:43 +08:00
if destination == "" {
2022-07-17 15:11:26 +08:00
destination = F.ToString(destinationIndex)
} else {
metadata.User = destination
2022-07-04 15:34:43 +08:00
}
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "[", destination, "] inbound connection to ", metadata.Destination)
2022-07-04 15:34:43 +08:00
return h.router.RouteConnection(ctx, conn, metadata)
}
func (h *ShadowsocksRelay) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-17 15:11:26 +08:00
destinationIndex, loaded := auth.UserFromContext[int](ctx)
if !loaded {
return os.ErrInvalid
}
destination := h.destinations[destinationIndex].Name
2022-07-04 15:34:43 +08:00
if destination == "" {
2022-07-17 15:11:26 +08:00
destination = F.ToString(destinationIndex)
} else {
metadata.User = destination
2022-07-04 15:34:43 +08:00
}
2022-07-12 15:17:29 +08:00
ctx = log.ContextWithNewID(ctx)
h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection from ", metadata.Source)
h.logger.InfoContext(ctx, "[", destination, "] inbound packet connection to ", metadata.Destination)
2022-07-04 15:34:43 +08:00
return h.router.RoutePacketConnection(ctx, conn, metadata)
}