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"
|
2019-09-14 12:00:40 +00:00
|
|
|
"context"
|
2018-11-01 03:54:45 +00:00
|
|
|
"crypto/tls"
|
2018-08-08 03:51:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2019-04-25 05:48:47 +00:00
|
|
|
"strconv"
|
2018-11-01 03:54:45 +00:00
|
|
|
"sync"
|
2018-08-08 03:51:06 +00:00
|
|
|
"time"
|
|
|
|
|
2019-04-25 05:48:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/socks5"
|
2018-08-08 03:51:06 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2019-06-28 16:58:59 +00:00
|
|
|
"github.com/Dreamacro/clash/dns"
|
2018-08-08 03:51:06 +00:00
|
|
|
)
|
|
|
|
|
2018-10-22 13:14:22 +00:00
|
|
|
const (
|
|
|
|
tcpTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-11-01 03:54:45 +00:00
|
|
|
var (
|
|
|
|
globalClientSessionCache tls.ClientSessionCache
|
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
2018-08-08 03:51:06 +00:00
|
|
|
u, err := url.Parse(rawURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
port := u.Port()
|
|
|
|
if port == "" {
|
2019-10-14 09:13:23 +00:00
|
|
|
switch u.Scheme {
|
|
|
|
case "https":
|
2018-08-08 03:51:06 +00:00
|
|
|
port = "443"
|
2019-10-14 09:13:23 +00:00
|
|
|
case "http":
|
2018-08-08 03:51:06 +00:00
|
|
|
port = "80"
|
2019-10-14 09:13:23 +00:00
|
|
|
default:
|
2018-08-08 03:51:06 +00:00
|
|
|
err = fmt.Errorf("%s scheme not Support", rawURL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
addr = C.Metadata{
|
2018-08-08 03:51:06 +00:00
|
|
|
AddrType: C.AtypDomainName,
|
|
|
|
Host: u.Hostname(),
|
2019-05-09 13:00:29 +00:00
|
|
|
DstIP: nil,
|
|
|
|
DstPort: port,
|
2018-08-08 03:51:06 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:00:12 +00:00
|
|
|
func tcpKeepAlive(c net.Conn) {
|
|
|
|
if tcp, ok := c.(*net.TCPConn); ok {
|
|
|
|
tcp.SetKeepAlive(true)
|
2018-10-01 11:42:15 +00:00
|
|
|
tcp.SetKeepAlivePeriod(30 * time.Second)
|
2018-08-29 07:00:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 03:54:45 +00:00
|
|
|
|
|
|
|
func getClientSessionCache() tls.ClientSessionCache {
|
|
|
|
once.Do(func() {
|
|
|
|
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
|
|
|
})
|
|
|
|
return globalClientSessionCache
|
|
|
|
}
|
2019-04-25 05:48:47 +00:00
|
|
|
|
|
|
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
|
|
|
var buf [][]byte
|
|
|
|
aType := uint8(metadata.AddrType)
|
2019-05-09 13:00:29 +00:00
|
|
|
p, _ := strconv.Atoi(metadata.DstPort)
|
2019-04-25 05:48:47 +00: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 13:00:29 +00:00
|
|
|
host := metadata.DstIP.To4()
|
2019-04-25 05:48:47 +00:00
|
|
|
buf = [][]byte{{aType}, host, port}
|
|
|
|
case socks5.AtypIPv6:
|
2019-05-09 13:00:29 +00:00
|
|
|
host := metadata.DstIP.To16()
|
2019-04-25 05:48:47 +00:00
|
|
|
buf = [][]byte{{aType}, host, port}
|
|
|
|
}
|
|
|
|
return bytes.Join(buf, nil)
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:55:39 +00:00
|
|
|
func dialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
2019-06-28 16:58:59 +00:00
|
|
|
host, port, err := net.SplitHostPort(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-14 12:00:40 +00:00
|
|
|
returned := make(chan struct{})
|
|
|
|
defer close(returned)
|
|
|
|
|
|
|
|
type dialResult struct {
|
|
|
|
net.Conn
|
|
|
|
error
|
2019-09-27 02:33:37 +00:00
|
|
|
resolved bool
|
|
|
|
ipv6 bool
|
|
|
|
done bool
|
2019-06-28 16:58:59 +00:00
|
|
|
}
|
2019-09-14 12:00:40 +00:00
|
|
|
results := make(chan dialResult)
|
|
|
|
var primary, fallback dialResult
|
|
|
|
|
|
|
|
startRacer := func(ctx context.Context, host string, ipv6 bool) {
|
2019-10-23 04:35:41 +00:00
|
|
|
dialer := net.Dialer{}
|
2019-09-20 08:04:06 +00:00
|
|
|
result := dialResult{ipv6: ipv6, done: true}
|
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case results <- result:
|
|
|
|
case <-returned:
|
|
|
|
if result.Conn != nil {
|
|
|
|
result.Conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-09-14 12:00:40 +00:00
|
|
|
|
|
|
|
var ip net.IP
|
|
|
|
if ipv6 {
|
2019-09-20 08:04:06 +00:00
|
|
|
ip, result.error = dns.ResolveIPv6(host)
|
2019-09-14 12:00:40 +00:00
|
|
|
} else {
|
2019-09-20 08:04:06 +00:00
|
|
|
ip, result.error = dns.ResolveIPv4(host)
|
2019-09-14 12:00:40 +00:00
|
|
|
}
|
2019-09-20 08:04:06 +00:00
|
|
|
if result.error != nil {
|
2019-09-14 12:00:40 +00:00
|
|
|
return
|
|
|
|
}
|
2019-09-27 02:33:37 +00:00
|
|
|
result.resolved = true
|
2019-06-28 16:58:59 +00:00
|
|
|
|
2019-09-14 12:00:40 +00:00
|
|
|
if ipv6 {
|
2019-09-20 08:04:06 +00:00
|
|
|
result.Conn, result.error = dialer.DialContext(ctx, "tcp6", net.JoinHostPort(ip.String(), port))
|
2019-09-14 12:00:40 +00:00
|
|
|
} else {
|
2019-09-20 08:04:06 +00:00
|
|
|
result.Conn, result.error = dialer.DialContext(ctx, "tcp4", net.JoinHostPort(ip.String(), port))
|
2019-09-14 12:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go startRacer(ctx, host, false)
|
|
|
|
go startRacer(ctx, host, true)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-results:
|
|
|
|
if res.error == nil {
|
|
|
|
return res.Conn, nil
|
|
|
|
}
|
|
|
|
|
2019-09-27 02:33:37 +00:00
|
|
|
if !res.ipv6 {
|
2019-09-14 12:00:40 +00:00
|
|
|
primary = res
|
|
|
|
} else {
|
|
|
|
fallback = res
|
|
|
|
}
|
|
|
|
|
|
|
|
if primary.done && fallback.done {
|
2019-09-27 02:33:37 +00:00
|
|
|
if primary.resolved {
|
|
|
|
return nil, primary.error
|
|
|
|
} else if fallback.resolved {
|
|
|
|
return nil, fallback.error
|
|
|
|
} else {
|
|
|
|
return nil, primary.error
|
|
|
|
}
|
2019-09-14 12:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 16:58:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|