Files
v2ray_simple/tlsLayer/client.go
2022-03-31 18:28:57 +08:00

87 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tlsLayer
import (
"crypto/tls"
"net"
"unsafe"
"github.com/hahahrfool/v2ray_simple/utils"
utls "github.com/refraction-networking/utls"
"go.uber.org/zap"
)
// 关于utls的简单分析可参考
//https://github.com/hahahrfool/v2ray_simple/discussions/7
type Client struct {
tlsConfig *tls.Config
uTlsConfig utls.Config
use_uTls bool
alpnList []string
}
func NewClient(host string, insecure bool, use_uTls bool, alpnList []string) *Client {
c := &Client{
use_uTls: use_uTls,
}
c.alpnList = alpnList
if use_uTls {
c.uTlsConfig = utls.Config{
InsecureSkipVerify: insecure,
ServerName: host,
NextProtos: alpnList,
}
if ce := utils.CanLogInfo("using utls and Chrome fingerprint for"); ce != nil {
//log.Println("using utls and Chrome fingerprint for", host)
ce.Write(zap.String("host", host))
}
} else {
c.tlsConfig = &tls.Config{
InsecureSkipVerify: insecure,
ServerName: host,
NextProtos: alpnList,
}
}
return c
}
func (c *Client) Handshake(underlay net.Conn) (tlsConn *Conn, err error) {
if c.use_uTls {
configCopy := c.uTlsConfig //发现uTlsConfig竟然没法使用指针握手一次后配置文件就会被污染只能拷贝
//否则的话接下来的握手客户端会报错: tls: CurvePreferences includes unsupported curve
utlsConn := utls.UClient(underlay, &configCopy, utls.HelloChrome_Auto)
err = utlsConn.Handshake()
if err != nil {
return
}
tlsConn = &Conn{
Conn: utlsConn,
ptr: unsafe.Pointer(utlsConn.Conn),
tlsPackageType: utlsPackage,
}
} else {
officialConn := tls.Client(underlay, c.tlsConfig)
err = officialConn.Handshake()
if err != nil {
return
}
tlsConn = &Conn{
Conn: officialConn,
ptr: unsafe.Pointer(officialConn),
tlsPackageType: official,
}
}
return
}