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"
|
2022-11-12 13:18:36 +08:00
|
|
|
"context"
|
2022-02-04 23:33:36 +08:00
|
|
|
"crypto/tls"
|
2023-09-21 10:28:28 +08:00
|
|
|
"fmt"
|
2018-08-08 11:51:06 +08:00
|
|
|
"net"
|
2022-08-28 13:41:19 +08:00
|
|
|
"net/netip"
|
2023-09-21 10:43:45 +08:00
|
|
|
"regexp"
|
2023-09-21 10:28:28 +08:00
|
|
|
"strconv"
|
2022-02-04 23:33:36 +08:00
|
|
|
"sync"
|
2018-08-08 11:51:06 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/transport/socks5"
|
2018-08-08 11:51:06 +08:00
|
|
|
)
|
|
|
|
|
2022-02-04 23:33:36 +08:00
|
|
|
var (
|
2023-07-16 23:26:07 +08:00
|
|
|
globalClientSessionCache tls.ClientSessionCache
|
|
|
|
once sync.Once
|
2022-02-04 23:33:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2023-08-09 13:51:02 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
func resolveUDPAddr(ctx context.Context, network, address string) (*net.UDPAddr, error) {
|
2019-06-29 00:58:59 +08:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-04 13:19:41 +08:00
|
|
|
ip, err := resolver.ResolveIPWithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
2019-06-29 00:58:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
|
|
|
}
|
2021-03-22 23:26:20 +08:00
|
|
|
|
2022-11-12 13:18:36 +08:00
|
|
|
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:
|
2024-10-04 13:19:41 +08:00
|
|
|
ip, err = resolver.ResolveIPv4WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
2022-08-28 13:41:19 +08:00
|
|
|
case C.IPv6Only:
|
2024-10-04 13:19:41 +08:00
|
|
|
ip, err = resolver.ResolveIPv6WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
2022-08-28 13:41:19 +08:00
|
|
|
case C.IPv6Prefer:
|
|
|
|
var ips []netip.Addr
|
2024-10-04 13:19:41 +08:00
|
|
|
ips, err = resolver.LookupIPWithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
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
|
2024-10-04 13:19:41 +08:00
|
|
|
ips, err = resolver.LookupIPWithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
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-12-13 18:09:19 +08:00
|
|
|
|
2022-08-28 13:41:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
|
|
|
}
|
|
|
|
|
2021-03-22 23:26:20 +08:00
|
|
|
func safeConnClose(c net.Conn, err error) {
|
2022-12-13 18:09:19 +08:00
|
|
|
if err != nil && c != nil {
|
2022-04-20 01:52:51 +08:00
|
|
|
_ = c.Close()
|
2021-03-22 23:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-21 10:28:28 +08:00
|
|
|
|
2023-09-21 10:43:45 +08:00
|
|
|
var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`)
|
|
|
|
|
2023-09-21 14:52:26 +08:00
|
|
|
func StringToBps(s string) uint64 {
|
2023-09-21 10:28:28 +08:00
|
|
|
if s == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// when have not unit, use Mbps
|
|
|
|
if v, err := strconv.Atoi(s); err == nil {
|
2023-09-21 14:52:26 +08:00
|
|
|
return StringToBps(fmt.Sprintf("%d Mbps", v))
|
2023-09-21 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m := rateStringRegexp.FindStringSubmatch(s)
|
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2023-12-08 21:22:20 +08:00
|
|
|
var n uint64 = 1
|
2023-09-21 10:28:28 +08:00
|
|
|
switch m[2] {
|
|
|
|
case "T":
|
2023-12-08 21:22:20 +08:00
|
|
|
n *= 1000
|
|
|
|
fallthrough
|
|
|
|
case "G":
|
|
|
|
n *= 1000
|
|
|
|
fallthrough
|
|
|
|
case "M":
|
|
|
|
n *= 1000
|
|
|
|
fallthrough
|
|
|
|
case "K":
|
|
|
|
n *= 1000
|
2023-09-21 10:28:28 +08:00
|
|
|
}
|
|
|
|
v, _ := strconv.ParseUint(m[1], 10, 64)
|
2023-12-08 21:22:20 +08:00
|
|
|
n *= v
|
2023-09-21 10:28:28 +08:00
|
|
|
if m[3] == "b" {
|
|
|
|
// Bits, need to convert to bytes
|
2023-12-08 21:22:20 +08:00
|
|
|
n /= 8
|
2023-09-21 10:28:28 +08:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|