62 lines
1 KiB
Go
62 lines
1 KiB
Go
package adapters
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"sync"
|
|
"time"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
const (
|
|
tcpTimeout = 5 * time.Second
|
|
)
|
|
|
|
var (
|
|
globalClientSessionCache tls.ClientSessionCache
|
|
once sync.Once
|
|
)
|
|
|
|
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
|
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
|
|
}
|
|
}
|
|
|
|
addr = C.Metadata{
|
|
AddrType: C.AtypDomainName,
|
|
Host: u.Hostname(),
|
|
IP: nil,
|
|
Port: port,
|
|
}
|
|
return
|
|
}
|
|
|
|
func tcpKeepAlive(c net.Conn) {
|
|
if tcp, ok := c.(*net.TCPConn); ok {
|
|
tcp.SetKeepAlive(true)
|
|
tcp.SetKeepAlivePeriod(30 * time.Second)
|
|
}
|
|
}
|
|
|
|
func getClientSessionCache() tls.ClientSessionCache {
|
|
once.Do(func() {
|
|
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
|
})
|
|
return globalClientSessionCache
|
|
}
|