2018-08-08 03:51:06 +00:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2019-04-25 05:48:47 +00:00
|
|
|
"bytes"
|
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"
|
|
|
|
)
|
|
|
|
|
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 == "" {
|
|
|
|
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 04:25:52 +00:00
|
|
|
addr = C.Metadata{
|
2018-08-08 03:51:06 +00:00
|
|
|
AddrType: C.AtypDomainName,
|
|
|
|
Host: u.Hostname(),
|
|
|
|
IP: nil,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
p, _ := strconv.Atoi(metadata.Port)
|
|
|
|
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:
|
|
|
|
host := metadata.IP.To4()
|
|
|
|
buf = [][]byte{{aType}, host, port}
|
|
|
|
case socks5.AtypIPv6:
|
|
|
|
host := metadata.IP.To16()
|
|
|
|
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
|
|
|
|
}
|