2023-04-10 08:54:10 +08:00
|
|
|
package proxydialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2023-04-11 12:51:24 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2023-04-10 08:54:10 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
2023-04-11 21:42:16 +08:00
|
|
|
"github.com/Dreamacro/clash/tunnel/statistic"
|
2023-04-10 08:54:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type proxyDialer struct {
|
2023-04-11 21:42:16 +08:00
|
|
|
proxy C.ProxyAdapter
|
|
|
|
dialer C.Dialer
|
|
|
|
statistic bool
|
2023-04-10 08:54:10 +08:00
|
|
|
}
|
|
|
|
|
2023-04-11 21:42:16 +08:00
|
|
|
func New(proxy C.ProxyAdapter, dialer C.Dialer, statistic bool) C.Dialer {
|
|
|
|
return proxyDialer{proxy: proxy, dialer: dialer, statistic: statistic}
|
2023-04-10 08:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewByName(proxyName string, dialer C.Dialer) (C.Dialer, error) {
|
|
|
|
proxies := tunnel.Proxies()
|
|
|
|
if proxy, ok := proxies[proxyName]; ok {
|
2023-04-11 21:42:16 +08:00
|
|
|
return New(proxy, dialer, true), nil
|
2023-04-10 08:54:10 +08:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("proxyName[%s] not found", proxyName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p proxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
|
currentMeta, err := addrToMetadata(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if strings.Contains(network, "udp") { // using in wireguard outbound
|
2023-04-11 12:51:24 +08:00
|
|
|
pc, err := p.listenPacket(ctx, currentMeta)
|
2023-04-10 08:54:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return N.NewBindPacketConn(pc, currentMeta.UDPAddr()), nil
|
|
|
|
}
|
2023-04-11 21:42:16 +08:00
|
|
|
var conn C.Conn
|
2023-04-11 12:51:24 +08:00
|
|
|
switch p.proxy.SupportWithDialer() {
|
|
|
|
case C.ALLNet:
|
|
|
|
fallthrough
|
|
|
|
case C.TCP:
|
2023-04-11 21:42:16 +08:00
|
|
|
conn, err = p.proxy.DialContextWithDialer(ctx, p.dialer, currentMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 12:51:24 +08:00
|
|
|
default: // fallback to old function
|
|
|
|
if d, ok := p.dialer.(dialer.Dialer); ok { // fallback to old function
|
2023-04-11 21:42:16 +08:00
|
|
|
conn, err = p.proxy.DialContext(ctx, currentMeta, dialer.WithOption(d.Opt))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, C.ErrNotSupport
|
2023-04-11 12:51:24 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 21:42:16 +08:00
|
|
|
if p.statistic {
|
|
|
|
conn = statistic.NewTCPTracker(conn, statistic.DefaultManager, currentMeta, nil, 0, 0)
|
|
|
|
}
|
|
|
|
return conn, err
|
2023-04-10 08:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p proxyDialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
|
|
|
currentMeta, err := addrToMetadata(rAddrPort.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 12:51:24 +08:00
|
|
|
return p.listenPacket(ctx, currentMeta)
|
|
|
|
}
|
|
|
|
|
2023-04-11 21:42:16 +08:00
|
|
|
func (p proxyDialer) listenPacket(ctx context.Context, currentMeta *C.Metadata) (C.PacketConn, error) {
|
|
|
|
var pc C.PacketConn
|
|
|
|
var err error
|
2023-04-10 08:54:10 +08:00
|
|
|
currentMeta.NetWork = C.UDP
|
2023-04-11 12:51:24 +08:00
|
|
|
switch p.proxy.SupportWithDialer() {
|
|
|
|
case C.ALLNet:
|
|
|
|
fallthrough
|
|
|
|
case C.UDP:
|
2023-04-11 21:42:16 +08:00
|
|
|
pc, err = p.proxy.ListenPacketWithDialer(ctx, p.dialer, currentMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 12:51:24 +08:00
|
|
|
default: // fallback to old function
|
|
|
|
if d, ok := p.dialer.(dialer.Dialer); ok { // fallback to old function
|
2023-04-11 21:42:16 +08:00
|
|
|
pc, err = p.proxy.ListenPacketContext(ctx, currentMeta, dialer.WithOption(d.Opt))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, C.ErrNotSupport
|
2023-04-11 12:51:24 +08:00
|
|
|
}
|
|
|
|
}
|
2023-04-11 21:42:16 +08:00
|
|
|
if p.statistic {
|
|
|
|
pc = statistic.NewUDPTracker(pc, statistic.DefaultManager, currentMeta, nil, 0, 0)
|
|
|
|
}
|
|
|
|
return pc, nil
|
2023-04-10 08:54:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
|
|
|
|
host, port, err := net.SplitHostPort(rawAddress)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("addrToMetadata failed: %w", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip, err := netip.ParseAddr(host); err != nil {
|
|
|
|
addr = &C.Metadata{
|
|
|
|
Host: host,
|
|
|
|
DstPort: port,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addr = &C.Metadata{
|
|
|
|
Host: "",
|
|
|
|
DstIP: ip.Unmap(),
|
|
|
|
DstPort: port,
|
|
|
|
}
|
|
|
|
}
|
2023-04-11 21:42:16 +08:00
|
|
|
addr.Type = C.INNER
|
2023-04-10 08:54:10 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|