Init commit

This commit is contained in:
世界
2022-06-30 21:27:56 +08:00
commit cfa35e5a92
39 changed files with 2202 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
package shadowsocks
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/config"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"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"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var ErrUnsupportedMethod = E.New("unsupported method")
var _ adapter.InboundHandler = (*Inbound)(nil)
type Inbound struct {
router adapter.Router
logger log.Logger
network []string
service shadowsocks.Service
}
func (i *Inbound) Network() []string {
return i.network
}
func NewInbound(router adapter.Router, logger log.Logger, options *config.ShadowsocksInboundOptions) (inbound *Inbound, err error) {
inbound = &Inbound{
router: router,
logger: logger,
network: options.Network.Build(),
}
handler := (*inboundHandler)(inbound)
var udpTimeout int64
if options.UDPTimeout != 0 {
udpTimeout = options.UDPTimeout
} else {
udpTimeout = 300
}
switch {
case options.Method == shadowsocks.MethodNone:
inbound.service = shadowsocks.NewNoneService(options.UDPTimeout, handler)
case common.Contains(shadowaead.List, options.Method):
inbound.service, err = shadowaead.NewService(options.Method, nil, options.Password, udpTimeout, handler)
case common.Contains(shadowaead_2022.List, options.Method):
inbound.service, err = shadowaead_2022.NewServiceWithPassword(options.Method, options.Password, udpTimeout, handler)
default:
err = E.Extend(ErrUnsupportedMethod, options.Method)
}
return
}
func (i *Inbound) Type() string {
return C.TypeShadowsocks
}
func (i *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
return i.service.NewConnection(&inboundContext{ctx, metadata}, conn, M.Metadata{
Source: M.SocksaddrFromNetIP(metadata.Source),
})
}
func (i *Inbound) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
return i.service.NewPacket(&inboundContext{ctx, metadata}, conn, buffer, M.Metadata{
Source: M.SocksaddrFromNetIP(metadata.Source),
})
}
func (i *Inbound) Upstream() any {
return i.service
}
type inboundContext struct {
context.Context
metadata adapter.InboundContext
}
type inboundHandler Inbound
func (h *inboundHandler) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
inboundCtx, _ := common.Cast[*inboundContext](ctx)
ctx = inboundCtx.Context
h.logger.WithContext(ctx).Info("inbound connection to ", metadata.Destination)
inboundCtx.metadata.Destination = metadata.Destination
return h.router.RouteConnection(ctx, conn, inboundCtx.metadata)
}
func (h *inboundHandler) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error {
inboundCtx, _ := common.Cast[*inboundContext](ctx)
ctx = log.ContextWithID(inboundCtx.Context)
h.logger.WithContext(ctx).Info("inbound packet connection from ", inboundCtx.metadata.Source)
h.logger.WithContext(ctx).Info("inbound packet connection to ", metadata.Destination)
inboundCtx.metadata.Destination = metadata.Destination
return h.router.RoutePacketConnection(ctx, conn, inboundCtx.metadata)
}
func (h *inboundHandler) NewError(ctx context.Context, err error) {
common.Close(err)
if E.IsClosed(err) {
return
}
h.logger.WithContext(ctx).Warn(err)
}

View File

@@ -0,0 +1,104 @@
package shadowsocks
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
"github.com/sagernet/sing-box/common/tunnel"
"github.com/sagernet/sing-box/config"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-shadowsocks"
"github.com/sagernet/sing-shadowsocks/shadowimpl"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.Outbound = (*Outbound)(nil)
type Outbound struct {
tag string
logger log.Logger
dialer N.Dialer
method shadowsocks.Method
serverAddr M.Socksaddr
}
func NewOutbound(tag string, router adapter.Router, logger log.Logger, options *config.ShadowsocksOutboundOptions) (outbound *Outbound, err error) {
outbound = &Outbound{
tag: tag,
logger: logger,
dialer: dialer.NewDialer(router, options.DialerOptions),
}
outbound.method, err = shadowimpl.FetchMethod(options.Method, options.Password)
if err != nil {
return
}
if options.Server == "" {
err = E.New("missing server address")
return
} else if options.ServerPort == 0 {
err = E.New("missing server port")
return
}
outbound.serverAddr = M.ParseSocksaddrHostPort(options.Server, options.ServerPort)
return
}
func (o *Outbound) Type() string {
return C.TypeShadowsocks
}
func (o *Outbound) Tag() string {
return o.tag
}
func (o *Outbound) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
serverConn, err := o.DialContext(ctx, "tcp", destination)
if err != nil {
return err
}
return tunnel.CopyEarlyConn(ctx, conn, serverConn)
}
func (o *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
serverConn, err := o.ListenPacket(ctx)
if err != nil {
return err
}
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(serverConn))
}
func (o *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
switch network {
case C.NetworkTCP:
o.logger.WithContext(ctx).Debug("outbound connection to ", destination)
outConn, err := o.dialer.DialContext(ctx, "tcp", o.serverAddr)
if err != nil {
return nil, err
}
return o.method.DialEarlyConn(outConn, destination), nil
case C.NetworkUDP:
o.logger.WithContext(ctx).Debug("outbound packet connection to ", destination)
outConn, err := o.dialer.DialContext(ctx, "udp", o.serverAddr)
if err != nil {
return nil, err
}
return &bufio.BindPacketConn{PacketConn: o.method.DialPacketConn(outConn), Addr: destination}, nil
default:
panic("unknown network " + network)
}
}
func (o *Outbound) ListenPacket(ctx context.Context) (net.PacketConn, error) {
o.logger.WithContext(ctx).Debug("outbound packet connection to ", o.serverAddr)
outConn, err := o.dialer.ListenPacket(ctx)
if err != nil {
return nil, err
}
return o.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: outConn, Addr: o.serverAddr.UDPAddr()}), nil
}