Clash.Meta/adapter/outbound/util.go

123 lines
2.7 KiB
Go
Raw Normal View History

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