2020-02-09 09:02:48 +00:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-15 13:42:46 +00:00
|
|
|
"errors"
|
2022-04-22 16:30:25 +00:00
|
|
|
"fmt"
|
2022-06-25 01:00:35 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2020-02-09 09:02:48 +00:00
|
|
|
"net"
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2022-04-27 13:37:20 +00:00
|
|
|
"sync"
|
2020-02-09 09:02:48 +00:00
|
|
|
)
|
|
|
|
|
2022-04-27 13:37:20 +00:00
|
|
|
var (
|
|
|
|
dialMux sync.Mutex
|
|
|
|
actualSingleDialContext = singleDialContext
|
|
|
|
actualDualStackDialContext = dualStackDialContext
|
2022-05-26 11:49:12 +00:00
|
|
|
tcpConcurrent = false
|
2022-04-27 13:37:20 +00:00
|
|
|
DisableIPv6 = false
|
|
|
|
)
|
2022-04-22 16:27:22 +00:00
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
func DialContext(ctx context.Context, network, address string, options ...Option) (net.Conn, error) {
|
2022-02-22 18:38:50 +00:00
|
|
|
opt := &option{
|
|
|
|
interfaceName: DefaultInterface.Load(),
|
|
|
|
routingMark: int(DefaultRoutingMark.Load()),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range DefaultOptions {
|
|
|
|
o(opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(opt)
|
|
|
|
}
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
switch network {
|
|
|
|
case "tcp4", "tcp6", "udp4", "udp6":
|
2022-04-27 13:37:20 +00:00
|
|
|
return actualSingleDialContext(ctx, network, address, opt)
|
2020-02-15 13:42:46 +00:00
|
|
|
case "tcp", "udp":
|
2022-04-27 13:37:20 +00:00
|
|
|
return actualDualStackDialContext(ctx, network, address, opt)
|
2020-02-15 13:42:46 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("network invalid")
|
|
|
|
}
|
2020-02-09 09:02:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
func ListenPacket(ctx context.Context, network, address string, options ...Option) (net.PacketConn, error) {
|
2021-11-07 08:48:51 +00:00
|
|
|
cfg := &option{
|
|
|
|
interfaceName: DefaultInterface.Load(),
|
2022-02-17 06:23:47 +00:00
|
|
|
routingMark: int(DefaultRoutingMark.Load()),
|
2021-11-07 08:48:51 +00:00
|
|
|
}
|
2021-09-06 15:07:34 +00:00
|
|
|
|
2021-11-07 08:48:51 +00:00
|
|
|
for _, o := range DefaultOptions {
|
|
|
|
o(cfg)
|
2021-09-06 15:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(cfg)
|
|
|
|
}
|
|
|
|
|
2022-06-14 04:35:24 +00:00
|
|
|
if DisableIPv6 {
|
|
|
|
network = "udp4"
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
lc := &net.ListenConfig{}
|
|
|
|
if cfg.interfaceName != "" {
|
|
|
|
addr, err := bindIfaceToListenConfig(cfg.interfaceName, lc, network, address)
|
2020-04-24 15:48:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
2021-09-06 15:07:34 +00:00
|
|
|
address = addr
|
|
|
|
}
|
|
|
|
if cfg.addrReuse {
|
|
|
|
addrReuseToListenConfig(lc)
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
2021-11-08 08:59:48 +00:00
|
|
|
if cfg.routingMark != 0 {
|
|
|
|
bindMarkToListenConfig(cfg.routingMark, lc, network, address)
|
|
|
|
}
|
2020-10-21 16:11:49 +00:00
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
return lc.ListenPacket(ctx, network, address)
|
2020-02-09 09:02:48 +00:00
|
|
|
}
|
2020-02-15 13:42:46 +00:00
|
|
|
|
2022-04-27 13:37:20 +00:00
|
|
|
func SetDial(concurrent bool) {
|
|
|
|
dialMux.Lock()
|
2022-05-26 11:49:12 +00:00
|
|
|
tcpConcurrent = concurrent
|
2022-04-27 13:37:20 +00:00
|
|
|
if concurrent {
|
|
|
|
actualSingleDialContext = concurrentSingleDialContext
|
|
|
|
actualDualStackDialContext = concurrentDualStackDialContext
|
|
|
|
} else {
|
|
|
|
actualSingleDialContext = singleDialContext
|
2022-05-27 12:43:39 +00:00
|
|
|
actualDualStackDialContext = dualStackDialContext
|
2022-04-27 13:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dialMux.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-05-26 11:49:12 +00:00
|
|
|
func GetDial() bool {
|
|
|
|
return tcpConcurrent
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt *option) (net.Conn, error) {
|
2021-09-06 15:07:34 +00:00
|
|
|
dialer := &net.Dialer{}
|
|
|
|
if opt.interfaceName != "" {
|
|
|
|
if err := bindIfaceToDialer(opt.interfaceName, dialer, network, destination); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 08:59:48 +00:00
|
|
|
if opt.routingMark != 0 {
|
|
|
|
bindMarkToDialer(opt.routingMark, dialer, network, destination)
|
|
|
|
}
|
2021-09-06 15:07:34 +00:00
|
|
|
|
2022-04-22 16:30:25 +00:00
|
|
|
if DisableIPv6 && destination.Is6() {
|
|
|
|
return nil, fmt.Errorf("IPv6 is diabled, dialer cancel")
|
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
return dialer.DialContext(ctx, network, net.JoinHostPort(destination.String(), port))
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:38:50 +00:00
|
|
|
func dualStackDialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
2020-02-15 13:42:46 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
net.Conn
|
|
|
|
error
|
|
|
|
resolved bool
|
|
|
|
ipv6 bool
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
results := make(chan dialResult)
|
|
|
|
var primary, fallback dialResult
|
|
|
|
|
2022-03-27 16:44:13 +00:00
|
|
|
startRacer := func(ctx context.Context, network, host string, direct bool, ipv6 bool) {
|
2020-02-15 13:42:46 +00:00
|
|
|
result := dialResult{ipv6: ipv6, done: true}
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
|
|
|
if result.Conn != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
_ = result.Conn.Close()
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
var ip netip.Addr
|
2020-02-15 13:42:46 +00:00
|
|
|
if ipv6 {
|
2022-03-27 16:44:13 +00:00
|
|
|
if !direct {
|
|
|
|
ip, result.error = resolver.ResolveIPv6ProxyServerHost(host)
|
2022-02-22 18:38:50 +00:00
|
|
|
} else {
|
|
|
|
ip, result.error = resolver.ResolveIPv6(host)
|
|
|
|
}
|
2020-02-15 13:42:46 +00:00
|
|
|
} else {
|
2022-03-27 16:44:13 +00:00
|
|
|
if !direct {
|
|
|
|
ip, result.error = resolver.ResolveIPv4ProxyServerHost(host)
|
2022-02-22 18:38:50 +00:00
|
|
|
} else {
|
|
|
|
ip, result.error = resolver.ResolveIPv4(host)
|
|
|
|
}
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
if result.error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result.resolved = true
|
|
|
|
|
2022-02-22 18:38:50 +00:00
|
|
|
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 16:44:13 +00:00
|
|
|
go startRacer(ctx, network+"4", host, opt.direct, false)
|
|
|
|
go startRacer(ctx, network+"6", host, opt.direct, true)
|
2020-02-15 13:42:46 +00:00
|
|
|
|
2022-06-25 01:00:35 +00:00
|
|
|
count := 2
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
select {
|
|
|
|
case res := <-results:
|
|
|
|
if res.error == nil {
|
|
|
|
return res.Conn, nil
|
|
|
|
}
|
2020-02-15 13:42:46 +00:00
|
|
|
|
2022-06-25 01:00:35 +00:00
|
|
|
if !res.ipv6 {
|
|
|
|
primary = res
|
2020-08-25 14:19:59 +00:00
|
|
|
} else {
|
2022-06-25 01:00:35 +00:00
|
|
|
fallback = res
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
2022-06-25 01:00:35 +00:00
|
|
|
|
|
|
|
if primary.done && fallback.done {
|
|
|
|
if primary.resolved {
|
|
|
|
return nil, primary.error
|
|
|
|
} else if fallback.resolved {
|
|
|
|
return nil, fallback.error
|
|
|
|
} else {
|
|
|
|
return nil, primary.error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
break
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 14:19:59 +00:00
|
|
|
|
|
|
|
return nil, errors.New("never touched")
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
2022-04-22 16:27:22 +00:00
|
|
|
|
2022-04-27 13:37:20 +00:00
|
|
|
func concurrentDualStackDialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-27 13:37:20 +00:00
|
|
|
var ips []netip.Addr
|
|
|
|
|
|
|
|
if opt.direct {
|
|
|
|
ips, err = resolver.ResolveAllIP(host)
|
|
|
|
} else {
|
|
|
|
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
|
|
|
}
|
|
|
|
|
|
|
|
return concurrentDialContext(ctx, network, ips, port, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func concurrentDialContext(ctx context.Context, network string, ips []netip.Addr, port string, opt *option) (net.Conn, error) {
|
2022-04-22 16:27:22 +00:00
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
ip netip.Addr
|
|
|
|
net.Conn
|
|
|
|
error
|
|
|
|
resolved bool
|
|
|
|
}
|
|
|
|
|
|
|
|
results := make(chan dialResult)
|
|
|
|
tcpRacer := func(ctx context.Context, ip netip.Addr) {
|
|
|
|
result := dialResult{ip: ip}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
|
|
|
if result.Conn != nil {
|
|
|
|
result.Conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
v := "4"
|
|
|
|
if ip.Is6() {
|
|
|
|
v = "6"
|
|
|
|
}
|
2022-04-27 13:37:20 +00:00
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
result.Conn, result.error = dialContext(ctx, network+v, ip, port, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
go tcpRacer(ctx, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
connCount := len(ips)
|
2022-06-25 01:00:35 +00:00
|
|
|
for i := 0; i < connCount; i++ {
|
|
|
|
select {
|
|
|
|
case res := <-results:
|
|
|
|
if res.error == nil {
|
|
|
|
return res.Conn, nil
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
2022-04-22 16:27:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 13:37:20 +00:00
|
|
|
|
2022-05-13 13:43:42 +00:00
|
|
|
return nil, fmt.Errorf("all ips %v tcp shake hands failed", ips)
|
2022-04-27 13:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func singleDialContext(ctx context.Context, network string, address string, opt *option) (net.Conn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ip netip.Addr
|
|
|
|
switch network {
|
|
|
|
case "tcp4", "udp4":
|
|
|
|
if !opt.direct {
|
|
|
|
ip, err = resolver.ResolveIPv4ProxyServerHost(host)
|
|
|
|
} else {
|
|
|
|
ip, err = resolver.ResolveIPv4(host)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !opt.direct {
|
|
|
|
ip, err = resolver.ResolveIPv6ProxyServerHost(host)
|
|
|
|
} else {
|
|
|
|
ip, err = resolver.ResolveIPv6(host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dialContext(ctx, network, ip, port, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func concurrentSingleDialContext(ctx context.Context, network string, address string, opt *option) (net.Conn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ips []netip.Addr
|
|
|
|
switch network {
|
|
|
|
case "tcp4", "udp4":
|
|
|
|
if !opt.direct {
|
|
|
|
ips, err = resolver.ResolveAllIPv4ProxyServerHost(host)
|
|
|
|
} else {
|
|
|
|
ips, err = resolver.ResolveAllIPv4(host)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if !opt.direct {
|
|
|
|
ips, err = resolver.ResolveAllIPv6ProxyServerHost(host)
|
|
|
|
} else {
|
|
|
|
ips, err = resolver.ResolveAllIPv6(host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return concurrentDialContext(ctx, network, ips, port, opt)
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|