3f73c82d00
- Bump go-shadowsocks2 to v0.1.3 (new API symbols) - Return error instead of nil when Target() is empty in TCP handler - Reset wbuf unconditionally on write error to prevent unbounded growth - Add nil guard on targetAddr before WriteTo to prevent panic - Remove duplicate NewClientConfig call and dead ClientConfig literal - Remove unused noDelay metadata field - Fix buffer-size guard to catch negative values (len(b)==0 → bufSize<=0) - Add address context to parse/session error messages
120 lines
2.7 KiB
Go
120 lines
2.7 KiB
Go
package ss
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"github.com/go-gost/core/connector"
|
|
md "github.com/go-gost/core/metadata"
|
|
"github.com/go-gost/go-shadowsocks2/core"
|
|
"github.com/go-gost/go-shadowsocks2/socks"
|
|
"github.com/go-gost/go-shadowsocks2/utils"
|
|
ctxvalue "github.com/go-gost/x/ctx"
|
|
"github.com/go-gost/x/internal/util/relay"
|
|
"github.com/go-gost/x/internal/util/ss"
|
|
"github.com/go-gost/x/registry"
|
|
)
|
|
|
|
func init() {
|
|
registry.ConnectorRegistry().Register("ssu", NewConnector)
|
|
}
|
|
|
|
type ssuConnector struct {
|
|
clientCfg core.ClientConfig
|
|
tcpClient core.TCPClient
|
|
md metadata
|
|
options connector.Options
|
|
}
|
|
|
|
func NewConnector(opts ...connector.Option) connector.Connector {
|
|
options := connector.Options{}
|
|
for _, opt := range opts {
|
|
opt(&options)
|
|
}
|
|
|
|
return &ssuConnector{
|
|
options: options,
|
|
}
|
|
}
|
|
|
|
func (c *ssuConnector) Init(md md.Metadata) (err error) {
|
|
if err = c.parseMetadata(md); err != nil {
|
|
return
|
|
}
|
|
|
|
if c.options.Auth == nil {
|
|
return errors.New("ss: auth is required")
|
|
}
|
|
|
|
method := c.options.Auth.Username()
|
|
password, _ := c.options.Auth.Password()
|
|
clientConfig, err := utils.NewClientConfig(method, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
clientConfig.UDPTimeout = time.Minute
|
|
|
|
c.clientCfg = clientConfig
|
|
c.tcpClient = core.NewTCPClient(clientConfig)
|
|
|
|
return
|
|
}
|
|
|
|
func (c *ssuConnector) Connect(ctx context.Context, conn net.Conn, network, address string, opts ...connector.ConnectOption) (net.Conn, error) {
|
|
log := c.options.Logger.WithFields(map[string]any{
|
|
"remote": conn.RemoteAddr().String(),
|
|
"local": conn.LocalAddr().String(),
|
|
"network": network,
|
|
"address": address,
|
|
"sid": string(ctxvalue.SidFromContext(ctx)),
|
|
})
|
|
log.Debugf("connect %s/%s", address, network)
|
|
|
|
switch network {
|
|
case "udp", "udp4", "udp6":
|
|
default:
|
|
err := fmt.Errorf("network %s is unsupported", network)
|
|
log.Error(err)
|
|
return nil, err
|
|
}
|
|
|
|
if c.md.connectTimeout > 0 {
|
|
conn.SetDeadline(time.Now().Add(c.md.connectTimeout))
|
|
defer conn.SetDeadline(time.Time{})
|
|
}
|
|
|
|
taddr, _ := net.ResolveUDPAddr(network, address)
|
|
if taddr == nil {
|
|
taddr = &net.UDPAddr{}
|
|
}
|
|
serverAddr, err := netip.ParseAddrPort(conn.RemoteAddr().String())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ss: parse remote addr %q: %w", conn.RemoteAddr().String(), err)
|
|
}
|
|
clientCfg := c.clientCfg
|
|
clientCfg.ServerAddr = serverAddr
|
|
client := core.NewUDPClient(clientCfg)
|
|
if err := client.Init(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pc, ok := conn.(net.PacketConn)
|
|
if ok {
|
|
// standard UDP relay
|
|
return ss.UDPClientConn(pc, taddr, &client), nil
|
|
}
|
|
|
|
target := socks.ParseAddr(taddr.String())
|
|
conn, err = c.tcpClient.WrapConn(conn, target)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// UDP over TCP
|
|
return relay.UDPTunClientConn(conn, taddr), nil
|
|
}
|