clash/adapters/outbound/util.go

63 lines
1 KiB
Go
Raw Normal View History

2018-08-08 03:51:06 +00:00
package adapters
import (
"crypto/tls"
2018-08-08 03:51:06 +00:00
"fmt"
"net"
"net/url"
"sync"
2018-08-08 03:51:06 +00:00
"time"
C "github.com/Dreamacro/clash/constant"
)
2018-10-22 13:14:22 +00:00
const (
tcpTimeout = 5 * time.Second
)
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
}
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)
}
}
func getClientSessionCache() tls.ClientSessionCache {
once.Do(func() {
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
})
return globalClientSessionCache
}