Refactor dialer

This commit is contained in:
世界 2022-07-01 13:30:04 +08:00
parent 719a27fc42
commit e85528b42f
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 16 additions and 18 deletions

View file

@ -8,11 +8,12 @@ import (
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
) )
type ContextDialer interface { type Dialer interface {
DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error)
ListenPacket(ctx context.Context) (net.PacketConn, error)
} }
var SystemDialer ContextDialer = &DefaultDialer{ var SystemDialer Dialer = &DefaultDialer{
Dialer: net.Dialer{ Dialer: net.Dialer{
Timeout: 5 * time.Second, Timeout: 5 * time.Second,
}, },
@ -20,19 +21,13 @@ var SystemDialer ContextDialer = &DefaultDialer{
type DefaultDialer struct { type DefaultDialer struct {
net.Dialer net.Dialer
net.ListenConfig
} }
func (d *DefaultDialer) ListenUDP(network string, laddr *net.UDPAddr) (*net.UDPConn, error) { func (d *DefaultDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
return net.ListenUDP(network, laddr) return d.Dialer.DialContext(ctx, network, destination.String())
} }
func (d *DefaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) { func (d *DefaultDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) {
return d.Dialer.DialContext(ctx, network, address.String()) return d.ListenConfig.ListenPacket(ctx, "udp", "")
} }
type Listener interface {
Listen(ctx context.Context, network, address string) (net.Listener, error)
ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error)
}
var SystemListener Listener = &net.ListenConfig{}

View file

@ -24,13 +24,13 @@ const (
type Client struct { type Client struct {
version Version version Version
dialer N.ContextDialer dialer N.Dialer
serverAddr M.Socksaddr serverAddr M.Socksaddr
username string username string
password string password string
} }
func NewClient(dialer N.ContextDialer, serverAddr M.Socksaddr, version Version, username string, password string) *Client { func NewClient(dialer N.Dialer, serverAddr M.Socksaddr, version Version, username string, password string) *Client {
return &Client{ return &Client{
version: version, version: version,
dialer: dialer, dialer: dialer,
@ -40,7 +40,7 @@ func NewClient(dialer N.ContextDialer, serverAddr M.Socksaddr, version Version,
} }
} }
func NewClientFromURL(dialer N.ContextDialer, rawURL string) (*Client, error) { func NewClientFromURL(dialer N.Dialer, rawURL string) (*Client, error) {
var client Client var client Client
if !strings.Contains(rawURL, "://") { if !strings.Contains(rawURL, "://") {
rawURL = "socks://" + rawURL rawURL = "socks://" + rawURL

View file

@ -217,11 +217,14 @@ func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authent
metadata.Protocol = "socks5" metadata.Protocol = "socks5"
metadata.Destination = request.Destination metadata.Destination = request.Destination
var innerError error var innerError error
done := make(chan struct{})
go func() { go func() {
defer conn.Close() defer conn.Close()
innerError = handler.NewPacketConnection(ctx, NewAssociatePacketConn(udpConn, request.Destination, conn), metadata) innerError = handler.NewPacketConnection(ctx, NewAssociatePacketConn(udpConn, request.Destination, conn), metadata)
close(done)
}() }()
return common.AnyError(innerError, common.Error(io.Copy(io.Discard, conn))) err = common.Error(io.Copy(io.Discard, conn))
return common.AnyError(innerError, err)
default: default:
err = socks5.WriteResponse(conn, socks5.Response{ err = socks5.WriteResponse(conn, socks5.Response{
ReplyCode: socks5.ReplyCodeUnsupported, ReplyCode: socks5.ReplyCodeUnsupported,