2018-08-08 11:51:06 +08:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
2018-11-01 11:54:45 +08:00
|
|
|
"crypto/tls"
|
2018-08-08 11:51:06 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2018-11-01 11:54:45 +08:00
|
|
|
"sync"
|
2018-08-08 11:51:06 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2018-10-22 21:14:22 +08:00
|
|
|
const (
|
|
|
|
tcpTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-11-01 11:54:45 +08:00
|
|
|
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(),
|
|
|
|
IP: nil,
|
|
|
|
Port: port,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-29 15:00:12 +08:00
|
|
|
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)
|
2018-08-29 15:00:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 11:54:45 +08:00
|
|
|
|
|
|
|
func getClientSessionCache() tls.ClientSessionCache {
|
|
|
|
once.Do(func() {
|
|
|
|
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
|
|
|
})
|
|
|
|
return globalClientSessionCache
|
|
|
|
}
|