2021-07-06 15:55:34 +00:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
2022-01-18 02:05:06 +00:00
|
|
|
"context"
|
2021-07-06 15:55:34 +00:00
|
|
|
"net"
|
|
|
|
|
2023-01-13 01:55:01 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2022-01-18 02:05:06 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2021-07-06 15:55:34 +00:00
|
|
|
xtls "github.com/xtls/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type XTLSConfig struct {
|
|
|
|
Host string
|
|
|
|
SkipCertVerify bool
|
2023-01-11 14:01:15 +00:00
|
|
|
Fingerprint string
|
2021-07-06 15:55:34 +00: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,
|
|
|
|
}
|
2023-01-11 14:01:15 +00:00
|
|
|
if len(cfg.Fingerprint) == 0 {
|
2023-01-13 01:55:01 +00:00
|
|
|
xtlsConfig = tlsC.GetGlobalFingerprintXTLSConfig(xtlsConfig)
|
2022-07-11 05:42:28 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
2023-01-11 14:01:15 +00:00
|
|
|
if xtlsConfig, err = tlsC.GetSpecifiedFingerprintXTLSConfig(xtlsConfig, cfg.Fingerprint); err != nil {
|
2022-07-11 05:42:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 15:55:34 +00:00
|
|
|
|
|
|
|
xtlsConn := xtls.Client(conn, xtlsConfig)
|
2022-01-18 02:05:06 +00:00
|
|
|
|
2022-02-22 17:00:27 +00:00
|
|
|
// fix xtls handshake not timeout
|
2022-01-18 02:05:06 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err := xtlsConn.HandshakeContext(ctx)
|
2021-07-06 15:55:34 +00:00
|
|
|
return xtlsConn, err
|
|
|
|
}
|