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

View file

@ -202,9 +202,7 @@ func NewHTTP2Client(dialFn DialFn, tlsConfig *tls.Config, Fingerprint string) *T
if len(Fingerprint) != 0 { if len(Fingerprint) != 0 {
if fingerprint, exists := tlsC.GetFingerprint(Fingerprint); exists { if fingerprint, exists := tlsC.GetFingerprint(Fingerprint); exists {
utlsConn := tlsC.UClient(pconn, cfg, &tlsC.UClientHelloID{ utlsConn := tlsC.UClient(pconn, cfg, fingerprint)
ClientHelloID: fingerprint,
})
if err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx); err != nil { if err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx); err != nil {
pconn.Close() pconn.Close()
return nil, err 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) { func GetUtlsConnWithClientFingerprint(conn net.Conn, ClientFingerprint string, tlsConfig *tls.Config) (net.Conn, bool) {
if fingerprint, exists := tlsC.GetFingerprint(ClientFingerprint); exists { if fingerprint, exists := tlsC.GetFingerprint(ClientFingerprint); exists {
utlsConn := tlsC.UClient(conn, tlsConfig, &tlsC.UClientHelloID{ utlsConn := tlsC.UClient(conn, tlsConfig, fingerprint)
ClientHelloID: fingerprint,
})
return utlsConn, true 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 len(c.ClientFingerprint) != 0 {
if fingerprint, exists := tlsC.GetFingerprint(c.ClientFingerprint); exists { if fingerprint, exists := tlsC.GetFingerprint(c.ClientFingerprint); exists {
dialer.NetDialTLSContext = func(_ context.Context, _, addr string) (net.Conn, error) { dialer.NetDialTLSContext = func(_ context.Context, _, addr string) (net.Conn, error) {
utlsConn := tlsC.UClient(conn, c.TLSConfig, &tlsC.UClientHelloID{ utlsConn := tlsC.UClient(conn, c.TLSConfig, fingerprint)
ClientHelloID: fingerprint,
})
if err := utlsConn.(*tlsC.UConn).WebsocketHandshake(); err != nil { if err := utlsConn.(*tlsC.UConn).WebsocketHandshake(); err != nil {
return nil, fmt.Errorf("parse url %s error: %w", c.Path, err) return nil, fmt.Errorf("parse url %s error: %w", c.Path, err)