sing-box/outbound/shadowsocks.go

90 lines
2.8 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package outbound
2022-06-30 21:27:56 +08:00
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
2022-07-07 21:47:21 +08:00
"github.com/sagernet/sing-box/common/dialer"
2022-06-30 21:27:56 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing-shadowsocks"
"github.com/sagernet/sing-shadowsocks/shadowimpl"
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-06-30 21:27:56 +08:00
)
2022-07-01 19:34:02 +08:00
var _ adapter.Outbound = (*Shadowsocks)(nil)
2022-06-30 21:27:56 +08:00
2022-07-01 19:34:02 +08:00
type Shadowsocks struct {
myOutboundAdapter
dialer N.Dialer
2022-06-30 21:27:56 +08:00
method shadowsocks.Method
serverAddr M.Socksaddr
}
2022-07-12 15:17:29 +08:00
func NewShadowsocks(router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
2022-07-07 21:47:21 +08:00
method, err := shadowimpl.FetchMethod(options.Method, options.Password)
if err != nil {
return nil, err
}
return &Shadowsocks{
myOutboundAdapter{
2022-07-21 21:03:41 +08:00
protocol: C.TypeShadowsocks,
2022-07-23 09:15:47 +08:00
network: options.Network.Build(),
router: router,
2022-07-01 19:34:02 +08:00
logger: logger,
tag: tag,
},
2022-07-07 23:36:32 +08:00
dialer.NewOutbound(router, options.OutboundDialerOptions),
2022-07-07 21:47:21 +08:00
method,
options.ServerOptions.Build(),
}, nil
2022-06-30 21:27:56 +08:00
}
2022-07-07 21:47:21 +08:00
func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
2022-07-07 23:36:32 +08:00
metadata.Destination = destination
2022-06-30 21:27:56 +08:00
switch network {
case C.NetworkTCP:
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "outbound connection to ", destination)
2022-07-07 21:47:21 +08:00
outConn, err := h.dialer.DialContext(ctx, C.NetworkTCP, h.serverAddr)
2022-06-30 21:27:56 +08:00
if err != nil {
return nil, err
}
2022-07-07 21:47:21 +08:00
return h.method.DialEarlyConn(outConn, destination), nil
2022-06-30 21:27:56 +08:00
case C.NetworkUDP:
2022-07-12 15:17:29 +08:00
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2022-07-07 21:47:21 +08:00
outConn, err := h.dialer.DialContext(ctx, C.NetworkUDP, h.serverAddr)
2022-06-30 21:27:56 +08:00
if err != nil {
return nil, err
}
2022-07-07 21:47:21 +08:00
return &bufio.BindPacketConn{PacketConn: h.method.DialPacketConn(outConn), Addr: destination}, nil
2022-06-30 21:27:56 +08:00
default:
panic("unknown network " + network)
}
}
2022-07-07 21:47:21 +08:00
func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
ctx, metadata := adapter.AppendContext(ctx)
metadata.Outbound = h.tag
2022-07-07 23:36:32 +08:00
metadata.Destination = destination
2022-07-18 12:32:31 +08:00
h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
2022-07-10 14:22:28 +08:00
outConn, err := h.dialer.DialContext(ctx, "udp", h.serverAddr)
2022-06-30 21:27:56 +08:00
if err != nil {
return nil, err
}
2022-07-10 14:22:28 +08:00
return h.method.DialPacketConn(outConn), nil
2022-06-30 21:27:56 +08:00
}
2022-07-03 13:14:49 +08:00
2022-07-07 23:36:32 +08:00
func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
return NewEarlyConnection(ctx, h, conn, metadata)
2022-07-03 13:14:49 +08:00
}
2022-07-07 23:36:32 +08:00
func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return NewPacketConnection(ctx, h, conn, metadata)
2022-07-03 13:14:49 +08:00
}