mihomo/adapters/outbound/util.go

188 lines
3.6 KiB
Go
Raw Normal View History

2018-08-08 11:51:06 +08:00
package adapters
import (
2019-04-25 13:48:47 +08:00
"bytes"
2019-09-14 20:00:40 +08:00
"context"
"crypto/tls"
2018-08-08 11:51:06 +08:00
"fmt"
"net"
"net/url"
2019-04-25 13:48:47 +08:00
"strconv"
"sync"
2018-08-08 11:51:06 +08:00
"time"
2019-04-25 13:48:47 +08:00
"github.com/Dreamacro/clash/component/socks5"
2018-08-08 11:51:06 +08:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/dns"
2018-08-08 11:51:06 +08:00
)
2018-10-22 21:14:22 +08:00
const (
tcpTimeout = 5 * time.Second
)
var (
globalClientSessionCache tls.ClientSessionCache
once sync.Once
)
2018-09-30 12:25:52 +08:00
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
2018-08-08 11:51:06 +08:00
u, err := url.Parse(rawURL)
if err != nil {
return
}
port := u.Port()
if port == "" {
if u.Scheme == "https" {
port = "443"
} else if u.Scheme == "http" {
port = "80"
} else {
err = fmt.Errorf("%s scheme not Support", rawURL)
return
}
}
2018-09-30 12:25:52 +08:00
addr = C.Metadata{
2018-08-08 11:51:06 +08:00
AddrType: C.AtypDomainName,
Host: u.Hostname(),
2019-05-09 21:00:29 +08:00
DstIP: nil,
DstPort: port,
2018-08-08 11:51:06 +08:00
}
return
}
func tcpKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
2018-10-01 19:42:15 +08:00
tcp.SetKeepAlivePeriod(30 * time.Second)
}
}
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
aType := uint8(metadata.AddrType)
2019-05-09 21:00:29 +08:00
p, _ := strconv.Atoi(metadata.DstPort)
2019-04-25 13:48:47 +08:00
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
switch metadata.AddrType {
case socks5.AtypDomainName:
len := uint8(len(metadata.Host))
host := []byte(metadata.Host)
buf = [][]byte{{aType, len}, host, port}
case socks5.AtypIPv4:
2019-05-09 21:00:29 +08:00
host := metadata.DstIP.To4()
2019-04-25 13:48:47 +08:00
buf = [][]byte{{aType}, host, port}
case socks5.AtypIPv6:
2019-05-09 21:00:29 +08:00
host := metadata.DstIP.To16()
2019-04-25 13:48:47 +08:00
buf = [][]byte{{aType}, host, port}
}
return bytes.Join(buf, nil)
}
type fakeUDPConn struct {
net.Conn
}
func (fuc *fakeUDPConn) WriteTo(b []byte, addr net.Addr) (int, error) {
return fuc.Conn.Write(b)
}
func (fuc *fakeUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
n, err := fuc.Conn.Read(b)
return n, fuc.RemoteAddr(), err
}
func dialTimeout(network, address string, timeout time.Duration) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
2019-09-14 20:00:40 +08:00
dialer := net.Dialer{Timeout: timeout}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
returned := make(chan struct{})
defer close(returned)
type dialResult struct {
net.Conn
error
ipv6 bool
done bool
}
2019-09-14 20:00:40 +08:00
results := make(chan dialResult)
var primary, fallback dialResult
startRacer := func(ctx context.Context, host string, ipv6 bool) {
var err error
var ip net.IP
if ipv6 {
ip, err = dns.ResolveIPv6(host)
} else {
ip, err = dns.ResolveIPv4(host)
}
if err != nil {
return
}
2019-09-14 20:00:40 +08:00
var c net.Conn
if ipv6 {
c, err = dialer.DialContext(ctx, "tcp6", net.JoinHostPort(ip.String(), port))
} else {
c, err = dialer.DialContext(ctx, "tcp4", net.JoinHostPort(ip.String(), port))
}
select {
case results <- dialResult{Conn: c, error: err, ipv6: ipv6, done: true}:
2019-09-14 20:00:40 +08:00
case <-returned:
if c != nil {
c.Close()
}
}
}
go startRacer(ctx, host, false)
go startRacer(ctx, host, true)
for {
select {
case res := <-results:
if res.error == nil {
return res.Conn, nil
}
if res.ipv6 {
primary = res
} else {
fallback = res
}
if primary.done && fallback.done {
return nil, primary.error
}
}
}
}
func resolveUDPAddr(network, address string) (*net.UDPAddr, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
ip, err := dns.ResolveIP(host)
if err != nil {
return nil, err
}
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
}