2021-07-06 23:55:34 +08:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
2022-01-18 10:05:06 +08:00
|
|
|
"context"
|
2022-07-11 13:42:28 +08:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2021-07-06 23:55:34 +08:00
|
|
|
"net"
|
|
|
|
|
2022-01-18 10:05:06 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-06 23:55:34 +08:00
|
|
|
xtls "github.com/xtls/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type XTLSConfig struct {
|
|
|
|
Host string
|
|
|
|
SkipCertVerify bool
|
2022-07-11 13:42:28 +08:00
|
|
|
FingerPrint string
|
2021-07-06 23:55:34 +08:00
|
|
|
NextProtos []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func StreamXTLSConn(conn net.Conn, cfg *XTLSConfig) (net.Conn, error) {
|
|
|
|
xtlsConfig := &xtls.Config{
|
|
|
|
ServerName: cfg.Host,
|
|
|
|
InsecureSkipVerify: cfg.SkipCertVerify,
|
|
|
|
NextProtos: cfg.NextProtos,
|
|
|
|
}
|
2022-07-11 13:42:28 +08:00
|
|
|
if len(cfg.FingerPrint) == 0 {
|
|
|
|
xtlsConfig = tlsC.GetGlobalFingerprintXTLCConfig(xtlsConfig)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
if xtlsConfig, err = tlsC.GetSpecifiedFingerprintXTLSConfig(xtlsConfig, cfg.FingerPrint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 23:55:34 +08:00
|
|
|
|
|
|
|
xtlsConn := xtls.Client(conn, xtlsConfig)
|
2022-01-18 10:05:06 +08:00
|
|
|
|
2022-02-23 01:00:27 +08:00
|
|
|
// fix xtls handshake not timeout
|
2022-01-18 10:05:06 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err := xtlsConn.HandshakeContext(ctx)
|
2021-07-06 23:55:34 +08:00
|
|
|
return xtlsConn, err
|
|
|
|
}
|