From f973a7bee5ddc51280eea9883b545eefdcc3a7f7 Mon Sep 17 00:00:00 2001 From: adotkhan <61702862+adotkhan@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:03:27 -0400 Subject: [PATCH] fix: generate ClientHelloSpec only once (#306) * fix: generate ClientHelloSpec only once * chore: remove empty line in u_parrots.go Co-authored-by: Gaukas Wang Signed-off-by: adotkhan <61702862+adotkhan@users.noreply.github.com> --------- Signed-off-by: adotkhan <61702862+adotkhan@users.noreply.github.com> Co-authored-by: Gaukas Wang --- u_conn.go | 1 + u_parrots.go | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/u_conn.go b/u_conn.go index 49cc0f0e..753decdd 100644 --- a/u_conn.go +++ b/u_conn.go @@ -32,6 +32,7 @@ type UConn struct { sessionController *sessionController clientHelloBuildStatus ClientHelloBuildStatus + clientHelloSpec *ClientHelloSpec HandshakeState PubClientHandshakeState diff --git a/u_parrots.go b/u_parrots.go index 8ac1ca07..885b7431 100644 --- a/u_parrots.go +++ b/u_parrots.go @@ -2588,25 +2588,31 @@ func ShuffleChromeTLSExtensions(exts []TLSExtension) []TLSExtension { } func (uconn *UConn) applyPresetByID(id ClientHelloID) (err error) { - var spec ClientHelloSpec - uconn.ClientHelloID = id - // choose/generate the spec - switch id.Client { - case helloRandomized, helloRandomizedNoALPN, helloRandomizedALPN: - spec, err = uconn.generateRandomizedSpec() - if err != nil { - return err - } - case helloCustom: - return nil - default: - spec, err = UTLSIdToSpec(id) - if err != nil { - return err + + if uconn.clientHelloSpec == nil { + var spec ClientHelloSpec + uconn.ClientHelloID = id + + // choose/generate the spec + switch id.Client { + case helloRandomized, helloRandomizedNoALPN, helloRandomizedALPN: + spec, err = uconn.generateRandomizedSpec() + if err != nil { + return err + } + case helloCustom: + return nil + default: + spec, err = UTLSIdToSpec(id) + if err != nil { + return err + } } + + uconn.clientHelloSpec = &spec } - return uconn.ApplyPreset(&spec) + return uconn.ApplyPreset(uconn.clientHelloSpec) } // ApplyPreset should only be used in conjunction with HelloCustom to apply custom specs.