feat: isolate TLS fingerprint for each extension lifecycle (#1135)

This commit is contained in:
Levi authored and GitHub committed 2025-10-09 20:37:21 +08:00
1 parent 20a70d7f91
commit 90f70d1bf6
3 files changed
+27 -14

No files matched your search

+6 -4
View File
@@ -131,6 +131,7 @@ type XMLHttpRequest struct {
responseHeaders http.Header responseHeaders http.Header
aborted bool aborted bool
client *req.Client client *req.Client
fingerprint string
WithCredentials bool `json:"withCredentials"` WithCredentials bool `json:"withCredentials"`
Upload *XMLHttpRequestUpload `json:"upload"` Upload *XMLHttpRequestUpload `json:"upload"`
@@ -164,7 +165,7 @@ func (xhr *XMLHttpRequest) SetRequestHeader(key, value string) {
} }
func (xhr *XMLHttpRequest) Send(data goja.Value) { func (xhr *XMLHttpRequest) Send(data goja.Value) {
setFingerprint(xhr.client) setFingerprint(xhr.client, xhr.fingerprint)
d := xhr.parseData(data) d := xhr.parseData(data)
var ( var (
@@ -381,7 +382,8 @@ func Enable(runtime *goja.Runtime, proxyHandler func(r *http.Request) (*url.URL,
} }
instance := &XMLHttpRequest{ instance := &XMLHttpRequest{
client: client, client: client,
fingerprint: util.SafeGet[string](runtime, FingerprintMagicKey),
Upload: &XMLHttpRequestUpload{ Upload: &XMLHttpRequestUpload{
EventProp: &EventProp{ EventProp: &EventProp{
eventListeners: make(map[string]func(event *ProgressEvent)), eventListeners: make(map[string]func(event *ProgressEvent)),
@@ -395,8 +397,8 @@ func Enable(runtime *goja.Runtime, proxyHandler func(r *http.Request) (*url.URL,
instanceValue.SetPrototype(call.This.Prototype()) instanceValue.SetPrototype(call.This.Prototype())
return instanceValue return instanceValue
}) })
runtime.Set("__gopeed_setFingerprint", func(fingerprint Fingerprint) { runtime.Set("__gopeed_setFingerprint", func(fingerprint string) {
currentFingerprint = fingerprint runtime.Set(FingerprintMagicKey, fingerprint)
}) })
if err := runtime.Set("ProgressEvent", progressEvent); err != nil { if err := runtime.Set("ProgressEvent", progressEvent); err != nil {
return err return err
@@ -1,21 +1,19 @@
package xhr package xhr
import ( import "github.com/imroc/req/v3"
"github.com/imroc/req/v3"
)
type Fingerprint string type Fingerprint string
const ( const (
fingerprintChrome Fingerprint = "chrome" FingerprintMagicKey = "__gopeed_xhr_fingerprint"
fingerprintFirefox Fingerprint = "firefox"
fingerprintSafari Fingerprint = "safari" fingerprintChrome = "chrome"
fingerprintFirefox = "firefox"
fingerprintSafari = "safari"
) )
var currentFingerprint Fingerprint = "" func setFingerprint(client *req.Client, fingerprint string) {
switch fingerprint {
func setFingerprint(client *req.Client) {
switch currentFingerprint {
case fingerprintChrome: case fingerprintChrome:
client.ImpersonateChrome() client.ImpersonateChrome()
case fingerprintFirefox: case fingerprintFirefox:
+13
View File
@@ -22,3 +22,16 @@ func AssertError[T error](err error) (t T, r bool) {
} }
return return
} }
func SafeGet[T any](vm *goja.Runtime, name string) T {
v := vm.Get(name)
if v == nil {
var init T
return init
}
if e, ok := v.Export().(T); ok {
return e
}
var init T
return init
}