2019-12-08 04:17:24 +00:00
|
|
|
package outbound
|
2018-08-08 03:51:06 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-25 05:48:47 +00:00
|
|
|
"bytes"
|
2022-11-12 05:18:36 +00:00
|
|
|
"context"
|
2022-02-04 15:33:36 +00:00
|
|
|
"crypto/tls"
|
|
|
|
xtls "github.com/xtls/go"
|
2018-08-08 03:51:06 +00:00
|
|
|
"net"
|
2022-08-28 05:41:19 +00:00
|
|
|
"net/netip"
|
2019-04-25 05:48:47 +00:00
|
|
|
"strconv"
|
2022-02-04 15:33:36 +00:00
|
|
|
"sync"
|
2018-08-08 03:51:06 +00:00
|
|
|
"time"
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2018-08-08 03:51:06 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-05-13 14:18:49 +00:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
2018-08-08 03:51:06 +00:00
|
|
|
)
|
|
|
|
|
2022-02-04 15:33:36 +00:00
|
|
|
var (
|
|
|
|
globalClientSessionCache tls.ClientSessionCache
|
|
|
|
globalClientXSessionCache xtls.ClientSessionCache
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
2018-08-29 07:00:12 +00:00
|
|
|
func tcpKeepAlive(c net.Conn) {
|
|
|
|
if tcp, ok := c.(*net.TCPConn); ok {
|
2022-04-19 17:52:51 +00:00
|
|
|
_ = tcp.SetKeepAlive(true)
|
|
|
|
_ = tcp.SetKeepAlivePeriod(30 * time.Second)
|
2018-08-29 07:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 03:54:45 +00:00
|
|
|
|
2022-02-04 15:33:36 +00:00
|
|
|
func getClientSessionCache() tls.ClientSessionCache {
|
|
|
|
once.Do(func() {
|
|
|
|
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
|
|
|
})
|
|
|
|
return globalClientSessionCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClientXSessionCache() xtls.ClientSessionCache {
|
|
|
|
once.Do(func() {
|
|
|
|
globalClientXSessionCache = xtls.NewLRUClientSessionCache(128)
|
|
|
|
})
|
|
|
|
return globalClientXSessionCache
|
|
|
|
}
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
|
|
|
var buf [][]byte
|
2022-11-11 01:19:28 +00:00
|
|
|
addrType := metadata.AddrType()
|
|
|
|
aType := uint8(addrType)
|
2021-11-07 16:31:08 +00:00
|
|
|
p, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
2019-04-25 05:48:47 +00:00
|
|
|
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
2022-11-11 01:19:28 +00:00
|
|
|
switch addrType {
|
2019-04-25 05:48:47 +00:00
|
|
|
case socks5.AtypDomainName:
|
2022-04-19 17:52:51 +00:00
|
|
|
lenM := uint8(len(metadata.Host))
|
2019-04-25 05:48:47 +00:00
|
|
|
host := []byte(metadata.Host)
|
2022-04-19 17:52:51 +00:00
|
|
|
buf = [][]byte{{aType, lenM}, host, port}
|
2019-04-25 05:48:47 +00:00
|
|
|
case socks5.AtypIPv4:
|
2022-04-19 17:52:51 +00:00
|
|
|
host := metadata.DstIP.AsSlice()
|
2019-04-25 05:48:47 +00:00
|
|
|
buf = [][]byte{{aType}, host, port}
|
|
|
|
case socks5.AtypIPv6:
|
2022-04-19 17:52:51 +00:00
|
|
|
host := metadata.DstIP.AsSlice()
|
2019-04-25 05:48:47 +00:00
|
|
|
buf = [][]byte{{aType}, host, port}
|
|
|
|
}
|
|
|
|
return bytes.Join(buf, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-12 05:18:36 +00:00
|
|
|
func resolveUDPAddr(ctx context.Context, network, address string) (*net.UDPAddr, error) {
|
2019-06-28 16:58:59 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-12 05:18:36 +00:00
|
|
|
ip, err := resolver.ResolveProxyServerHost(ctx, host)
|
2019-06-28 16:58:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
|
|
|
}
|
2021-03-22 15:26:20 +00:00
|
|
|
|
2022-11-12 05:18:36 +00:00
|
|
|
func resolveUDPAddrWithPrefer(ctx context.Context, network, address string, prefer C.DNSPrefer) (*net.UDPAddr, error) {
|
2022-08-28 05:41:19 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ip netip.Addr
|
2022-11-25 03:12:22 +00:00
|
|
|
var fallback netip.Addr
|
2022-08-28 05:41:19 +00:00
|
|
|
switch prefer {
|
|
|
|
case C.IPv4Only:
|
2022-11-12 05:18:36 +00:00
|
|
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
2022-08-28 05:41:19 +00:00
|
|
|
case C.IPv6Only:
|
2022-11-12 05:18:36 +00:00
|
|
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
2022-08-28 05:41:19 +00:00
|
|
|
case C.IPv6Prefer:
|
|
|
|
var ips []netip.Addr
|
2022-11-12 05:18:36 +00:00
|
|
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
2022-08-28 05:41:19 +00:00
|
|
|
var fallback netip.Addr
|
|
|
|
if err == nil {
|
|
|
|
for _, addr := range ips {
|
|
|
|
if addr.Is6() {
|
|
|
|
ip = addr
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if !fallback.IsValid() {
|
|
|
|
fallback = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 07:57:10 +00:00
|
|
|
default:
|
2022-08-29 05:04:48 +00:00
|
|
|
// C.IPv4Prefer, C.DualStack and other
|
2022-08-28 05:41:19 +00:00
|
|
|
var ips []netip.Addr
|
2022-11-12 05:18:36 +00:00
|
|
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
2022-08-28 05:41:19 +00:00
|
|
|
if err == nil {
|
|
|
|
for _, addr := range ips {
|
|
|
|
if addr.Is4() {
|
|
|
|
ip = addr
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if !fallback.IsValid() {
|
|
|
|
fallback = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 05:04:48 +00:00
|
|
|
|
2022-08-28 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 03:12:22 +00:00
|
|
|
if !ip.IsValid() && fallback.IsValid() {
|
|
|
|
ip = fallback
|
|
|
|
}
|
|
|
|
|
2022-08-28 05:41:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
|
|
|
}
|
|
|
|
|
2021-03-22 15:26:20 +00:00
|
|
|
func safeConnClose(c net.Conn, err error) {
|
|
|
|
if err != nil {
|
2022-04-19 17:52:51 +00:00
|
|
|
_ = c.Close()
|
2021-03-22 15:26:20 +00:00
|
|
|
}
|
|
|
|
}
|