chore: do not use extra pointer in UClient

This commit is contained in:
wwqgtxx 2023-02-07 17:51:37 +08:00
parent 967254d9ca
commit db54b438e6
4 changed files with 17 additions and 23 deletions

View file

@ -18,11 +18,11 @@ type UClientHelloID struct {
*utls.ClientHelloID
}
var initRandomFingerprint *utls.ClientHelloID
var initRandomFingerprint UClientHelloID
var initUtlsClient string
func UClient(c net.Conn, config *tls.Config, fingerprint *UClientHelloID) net.Conn {
utlsConn := utls.UClient(c, CopyConfig(config), utls.ClientHelloID{
func UClient(c net.Conn, config *tls.Config, fingerprint UClientHelloID) net.Conn {
utlsConn := utls.UClient(c, copyConfig(config), utls.ClientHelloID{
Client: fingerprint.Client,
Version: fingerprint.Version,
Seed: fingerprint.Seed,
@ -30,12 +30,12 @@ func UClient(c net.Conn, config *tls.Config, fingerprint *UClientHelloID) net.Co
return &UConn{UConn: utlsConn}
}
func GetFingerprint(ClientFingerprint string) (*utls.ClientHelloID, bool) {
func GetFingerprint(ClientFingerprint string) (UClientHelloID, bool) {
if ClientFingerprint == "none" {
return nil, false
return UClientHelloID{}, false
}
if initRandomFingerprint == nil {
if initRandomFingerprint.ClientHelloID == nil {
initRandomFingerprint, _ = RollFingerprint()
}
@ -49,7 +49,7 @@ func GetFingerprint(ClientFingerprint string) (*utls.ClientHelloID, bool) {
return fingerprint, ok
}
func RollFingerprint() (*utls.ClientHelloID, bool) {
func RollFingerprint() (UClientHelloID, bool) {
chooser, _ := weightedrand.NewChooser(
weightedrand.NewChoice("chrome", 6),
weightedrand.NewChoice("safari", 3),
@ -62,15 +62,15 @@ func RollFingerprint() (*utls.ClientHelloID, bool) {
return fingerprint, ok
}
var Fingerprints = map[string]*utls.ClientHelloID{
"chrome": &utls.HelloChrome_Auto,
"firefox": &utls.HelloFirefox_Auto,
"safari": &utls.HelloSafari_Auto,
"ios": &utls.HelloIOS_Auto,
"randomized": &utls.HelloRandomized,
var Fingerprints = map[string]UClientHelloID{
"chrome": {&utls.HelloChrome_Auto},
"firefox": {&utls.HelloFirefox_Auto},
"safari": {&utls.HelloSafari_Auto},
"ios": {&utls.HelloIOS_Auto},
"randomized": {&utls.HelloRandomized},
}
func CopyConfig(c *tls.Config) *utls.Config {
func copyConfig(c *tls.Config) *utls.Config {
return &utls.Config{
RootCAs: c.RootCAs,
ServerName: c.ServerName,

View file

@ -202,9 +202,7 @@ func NewHTTP2Client(dialFn DialFn, tlsConfig *tls.Config, Fingerprint string) *T
if len(Fingerprint) != 0 {
if fingerprint, exists := tlsC.GetFingerprint(Fingerprint); exists {
utlsConn := tlsC.UClient(pconn, cfg, &tlsC.UClientHelloID{
ClientHelloID: fingerprint,
})
utlsConn := tlsC.UClient(pconn, cfg, fingerprint)
if err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx); err != nil {
pconn.Close()
return nil, err

View file

@ -55,9 +55,7 @@ func StreamTLSConn(conn net.Conn, cfg *TLSConfig) (net.Conn, error) {
func GetUtlsConnWithClientFingerprint(conn net.Conn, ClientFingerprint string, tlsConfig *tls.Config) (net.Conn, bool) {
if fingerprint, exists := tlsC.GetFingerprint(ClientFingerprint); exists {
utlsConn := tlsC.UClient(conn, tlsConfig, &tlsC.UClientHelloID{
ClientHelloID: fingerprint,
})
utlsConn := tlsC.UClient(conn, tlsConfig, fingerprint)
return utlsConn, true
}

View file

@ -335,9 +335,7 @@ func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buf
if len(c.ClientFingerprint) != 0 {
if fingerprint, exists := tlsC.GetFingerprint(c.ClientFingerprint); exists {
dialer.NetDialTLSContext = func(_ context.Context, _, addr string) (net.Conn, error) {
utlsConn := tlsC.UClient(conn, c.TLSConfig, &tlsC.UClientHelloID{
ClientHelloID: fingerprint,
})
utlsConn := tlsC.UClient(conn, c.TLSConfig, fingerprint)
if err := utlsConn.(*tlsC.UConn).WebsocketHandshake(); err != nil {
return nil, fmt.Errorf("parse url %s error: %w", c.Path, err)