2021-07-06 23:55:34 +08:00
|
|
|
package vless
|
|
|
|
|
|
|
|
import (
|
2022-01-18 10:05:06 +08:00
|
|
|
"context"
|
2023-02-25 13:12:19 +08:00
|
|
|
"errors"
|
2021-07-06 23:55:34 +08:00
|
|
|
"net"
|
|
|
|
|
2023-01-13 09:55:01 +08:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2021-07-06 23:55:34 +08:00
|
|
|
xtls "github.com/xtls/go"
|
|
|
|
)
|
|
|
|
|
2023-02-25 13:12:19 +08:00
|
|
|
var (
|
|
|
|
ErrNotTLS13 = errors.New("XTLS Vision based on TLS 1.3 outer connection")
|
|
|
|
)
|
|
|
|
|
2021-07-06 23:55:34 +08:00
|
|
|
type XTLSConfig struct {
|
|
|
|
Host string
|
|
|
|
SkipCertVerify bool
|
2023-01-11 22:01:15 +08:00
|
|
|
Fingerprint string
|
2021-07-06 23:55:34 +08:00
|
|
|
NextProtos []string
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:15:08 +08:00
|
|
|
func StreamXTLSConn(ctx context.Context, conn net.Conn, cfg *XTLSConfig) (net.Conn, error) {
|
2021-07-06 23:55:34 +08:00
|
|
|
xtlsConfig := &xtls.Config{
|
|
|
|
ServerName: cfg.Host,
|
|
|
|
InsecureSkipVerify: cfg.SkipCertVerify,
|
|
|
|
NextProtos: cfg.NextProtos,
|
|
|
|
}
|
2023-01-11 22:01:15 +08:00
|
|
|
if len(cfg.Fingerprint) == 0 {
|
2023-01-14 21:08:06 +08:00
|
|
|
xtlsConfig = tlsC.GetGlobalXTLSConfig(xtlsConfig)
|
2022-07-11 13:42:28 +08:00
|
|
|
} else {
|
|
|
|
var err error
|
2023-01-11 22:01:15 +08:00
|
|
|
if xtlsConfig, err = tlsC.GetSpecifiedFingerprintXTLSConfig(xtlsConfig, cfg.Fingerprint); err != nil {
|
2022-07-11 13:42:28 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 23:55:34 +08:00
|
|
|
|
|
|
|
xtlsConn := xtls.Client(conn, xtlsConfig)
|
2022-01-18 10:05:06 +08:00
|
|
|
|
|
|
|
err := xtlsConn.HandshakeContext(ctx)
|
2021-07-06 23:55:34 +08:00
|
|
|
return xtlsConn, err
|
|
|
|
}
|