sing-box/inbound/shadowsocks.go

115 lines
4.5 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package inbound
import (
"context"
"net"
"time"
2022-07-01 19:34:02 +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"
"github.com/sagernet/sing-shadowsocks/shadowaead"
"github.com/sagernet/sing-shadowsocks/shadowaead_2022"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
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"
2023-08-31 14:00:47 +08:00
"github.com/sagernet/sing/common/ntp"
2022-07-01 19:34:02 +08:00
)
2022-07-12 15:17:29 +08:00
func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (adapter.Inbound, error) {
2022-07-04 15:34:43 +08:00
if len(options.Users) > 0 && len(options.Destinations) > 0 {
return nil, E.New("users and destinations options must not be combined")
}
if len(options.Users) > 0 {
return newShadowsocksMulti(ctx, router, logger, tag, options)
} else if len(options.Destinations) > 0 {
return newShadowsocksRelay(ctx, router, logger, tag, options)
} else {
return newShadowsocks(ctx, router, logger, tag, options)
}
}
2022-08-29 19:43:13 +08:00
var (
2024-10-21 23:38:34 +08:00
_ adapter.Inbound = (*Shadowsocks)(nil)
_ adapter.TCPInjectableInbound = (*Shadowsocks)(nil)
2022-08-29 19:43:13 +08:00
)
2022-07-01 19:34:02 +08:00
type Shadowsocks struct {
myInboundAdapter
service shadowsocks.Service
}
2022-07-12 15:17:29 +08:00
func newShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksInboundOptions) (*Shadowsocks, error) {
2022-07-01 19:34:02 +08:00
inbound := &Shadowsocks{
myInboundAdapter: myInboundAdapter{
protocol: C.TypeShadowsocks,
network: options.Network.Build(),
ctx: ctx,
router: uot.NewRouter(router, logger),
2022-07-01 19:34:02 +08:00
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
}
2022-07-01 19:34:02 +08:00
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-01 19:34:02 +08:00
if options.UDPTimeout != 0 {
udpTimeout = time.Duration(options.UDPTimeout)
2022-07-01 19:34:02 +08:00
} else {
udpTimeout = C.UDPTimeout
2022-07-01 19:34:02 +08:00
}
switch {
case options.Method == shadowsocks.MethodNone:
2024-10-21 23:38:34 +08:00
inbound.service = shadowsocks.NewNoneService(int64(udpTimeout.Seconds()), adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound))
2022-07-01 19:34:02 +08:00
case common.Contains(shadowaead.List, options.Method):
2024-10-21 23:38:34 +08:00
inbound.service, err = shadowaead.NewService(options.Method, nil, options.Password, int64(udpTimeout.Seconds()), adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound))
2022-07-01 19:34:02 +08:00
case common.Contains(shadowaead_2022.List, options.Method):
2024-10-21 23:38:34 +08:00
inbound.service, err = shadowaead_2022.NewServiceWithPassword(options.Method, options.Password, int64(udpTimeout.Seconds()), adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newConnection, inbound.newPacketConnection, inbound), ntp.TimeFuncFromContext(ctx))
2022-07-01 19:34:02 +08:00
default:
err = E.New("unsupported method: ", options.Method)
2022-07-01 19:34:02 +08:00
}
2022-07-02 14:07:50 +08:00
inbound.packetUpstream = inbound.service
2022-07-01 19:34:02 +08:00
return inbound, err
}
2024-10-21 23:38:34 +08:00
func (h *Shadowsocks) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
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))
}
}
func (h *Shadowsocks) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
err := h.service.NewPacket(h.ctx, h.packetConn(), buffer, M.Metadata{Source: source})
if err != nil {
h.logger.Error(E.Cause(err, "process packet from ", source))
}
2022-07-01 19:34:02 +08:00
}
2024-10-21 23:38:34 +08:00
func (h *Shadowsocks) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
return h.router.RouteConnection(ctx, conn, h.createMetadata(conn, metadata))
2022-07-17 15:11:26 +08:00
}
2024-10-21 23:38:34 +08:00
func (h *Shadowsocks) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
ctx = log.ContextWithNewID(ctx)
h.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
h.logger.InfoContext(ctx, "inbound packet connection to ", metadata.Destination)
return h.router.RoutePacketConnection(ctx, conn, h.createPacketMetadata(conn, metadata))
2022-08-29 19:43:13 +08:00
}