2024-11-02 00:39:02 +08:00
|
|
|
package shadowsocks
|
2022-07-04 15:34:43 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2022-07-17 15:11:26 +08:00
|
|
|
"os"
|
2023-12-20 19:55:14 +08:00
|
|
|
"time"
|
2022-07-04 15:34:43 +08:00
|
|
|
|
2022-07-06 15:01:09 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter/inbound"
|
|
|
|
"github.com/sagernet/sing-box/common/listener"
|
2023-11-08 12:09:22 +08:00
|
|
|
"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"
|
2024-10-21 23:38:34 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2022-07-08 23:03:57 +08:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2024-10-21 23:38:34 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-07-08 23:03:57 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-04 15:34:43 +08:00
|
|
|
)
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
var _ adapter.TCPInjectableInbound = (*RelayInbound)(nil)
|
2022-07-04 15:34:43 +08:00
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
type RelayInbound struct {
|
|
|
|
inbound.Adapter
|
|
|
|
ctx context.Context
|
|
|
|
router adapter.ConnectionRouterEx
|
|
|
|
logger logger.ContextLogger
|
|
|
|
listener *listener.Listener
|
2022-07-04 15:34:43 +08:00
|
|
|
service *shadowaead_2022.RelayService[int]
|
|
|
|
destinations []option.ShadowsocksDestination
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
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,
|
2022-07-04 15:34:43 +08:00
|
|
|
destinations: options.Destinations,
|
|
|
|
}
|
2023-11-08 12:09:22 +08:00
|
|
|
var err error
|
|
|
|
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-20 19:55:14 +08:00
|
|
|
var udpTimeout time.Duration
|
2022-07-04 15:34:43 +08:00
|
|
|
if options.UDPTimeout != 0 {
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
2022-07-04 15:34:43 +08:00
|
|
|
} else {
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout = C.UDPTimeout
|
2022-07-04 15:34:43 +08:00
|
|
|
}
|
|
|
|
service, err := shadowaead_2022.NewRelayServiceWithPassword[int](
|
|
|
|
options.Method,
|
|
|
|
options.Password,
|
2023-12-20 19:55:14 +08:00
|
|
|
int64(udpTimeout.Seconds()),
|
2024-10-21 23:38:34 +08:00
|
|
|
adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound),
|
2022-07-04 15:34:43 +08:00
|
|
|
)
|
|
|
|
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
|
2024-11-02 00:39:02 +08: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 15:34:43 +08:00
|
|
|
return inbound, err
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
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) {
|
2024-10-21 23:38:34 +08:00
|
|
|
err := h.service.NewConnection(ctx, conn, adapter.UpstreamMetadata(metadata))
|
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
|
|
|
|
}
|
2022-07-04 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
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})
|
2024-10-21 23:38:34 +08:00
|
|
|
if err != nil {
|
|
|
|
h.logger.Error(E.Cause(err, "process packet from ", source))
|
|
|
|
}
|
2022-08-29 19:43:13 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *RelayInbound) 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)
|
2024-11-02 00:39:02 +08:00
|
|
|
metadata.Inbound = h.Tag()
|
|
|
|
metadata.InboundType = h.Type()
|
2024-11-15 17:21:33 +08:00
|
|
|
metadata.InboundDetour = h.listener.ListenOptions().Detour
|
|
|
|
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
|
2024-11-02 00:39:02 +08:00
|
|
|
return h.router.RouteConnection(ctx, conn, metadata)
|
2022-07-04 15:34:43 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *RelayInbound) 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)
|
2024-11-02 00:39:02 +08:00
|
|
|
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)
|
2022-07-04 15:34:43 +08:00
|
|
|
}
|