From 765ec6fb3229de1a30559be9181942831ea94649 Mon Sep 17 00:00:00 2001 From: sugarBecomer Date: Mon, 17 Nov 2025 17:14:49 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8Dcookie=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/cookiejar/all_save_jar.go | 83 +++++++++++++++++-- internal/application/cookiejar/jar.go | 82 ++++++++++++++++-- internal/application/cookiejar/jar_test.go | 7 ++ 3 files changed, 162 insertions(+), 10 deletions(-) diff --git a/internal/application/cookiejar/all_save_jar.go b/internal/application/cookiejar/all_save_jar.go index f2666ab..9808bae 100644 --- a/internal/application/cookiejar/all_save_jar.go +++ b/internal/application/cookiejar/all_save_jar.go @@ -158,7 +158,30 @@ func (a *AllSavePersistCookieJar) Deserialize(content string) { var domainMap domainMap err := sonic.Unmarshal([]byte(content), &domainMap) - utils.OkOrPanic(err) + + // 如果反序列化失败,尝试从旧格式转换 + if err != nil { + // 旧格式: map[string]map[string]*http.Cookie + type oldCookieMap map[string]*http.Cookie + type oldDomainMap map[string]oldCookieMap + + var oldMap oldDomainMap + oldErr := sonic.Unmarshal([]byte(content), &oldMap) + if oldErr != nil { + // 两种格式都失败,抛出原始错误 + utils.OkOrPanic(err) + return + } + + // 转换旧格式到新格式 + domainMap = make(map[string]cookieMap) + for domain, cookies := range oldMap { + domainMap[domain] = make(map[string][]*http.Cookie) + for name, cookie := range cookies { + domainMap[domain][name] = []*http.Cookie{cookie} + } + } + } a.domainMap = domainMap } @@ -191,7 +214,31 @@ func (a *AllSavePersistCookieJar) DeserializeBytes(content []byte) { var domainMap domainMap err := gob.NewDecoder(retReader). Decode(&domainMap) - utils.OkOrPanic(err) + + // 如果反序列化失败,尝试从旧格式转换 + if err != nil { + // 旧格式: map[string]map[string]*http.Cookie + type oldCookieMap map[string]*http.Cookie + type oldDomainMap map[string]oldCookieMap + + retReader = bytes.NewBuffer(content) + var oldMap oldDomainMap + oldErr := gob.NewDecoder(retReader).Decode(&oldMap) + if oldErr != nil { + // 两种格式都失败,抛出原始错误 + utils.OkOrPanic(err) + return + } + + // 转换旧格式到新格式 + domainMap = make(map[string]cookieMap) + for domain, cookies := range oldMap { + domainMap[domain] = make(map[string][]*http.Cookie) + for name, cookie := range cookies { + domainMap[domain][name] = []*http.Cookie{cookie} + } + } + } a.domainMap = domainMap } @@ -200,10 +247,36 @@ func (a *AllSavePersistCookieJar) DeserializeReader(reader io.Reader) { a.Lock() defer a.Unlock() - var domainMap domainMap - err := gob.NewDecoder(reader). - Decode(&domainMap) + // 先读取所有数据,以便在失败时可以重试 + data, err := io.ReadAll(reader) utils.OkOrPanic(err) + var domainMap domainMap + err = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&domainMap) + + // 如果反序列化失败,尝试从旧格式转换 + if err != nil { + // 旧格式: map[string]map[string]*http.Cookie + type oldCookieMap map[string]*http.Cookie + type oldDomainMap map[string]oldCookieMap + + var oldMap oldDomainMap + oldErr := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&oldMap) + if oldErr != nil { + // 两种格式都失败,抛出原始错误 + utils.OkOrPanic(err) + return + } + + // 转换旧格式到新格式 + domainMap = make(map[string]cookieMap) + for domain, cookies := range oldMap { + domainMap[domain] = make(map[string][]*http.Cookie) + for name, cookie := range cookies { + domainMap[domain][name] = []*http.Cookie{cookie} + } + } + } + a.domainMap = domainMap } diff --git a/internal/application/cookiejar/jar.go b/internal/application/cookiejar/jar.go index 058863d..b8006da 100644 --- a/internal/application/cookiejar/jar.go +++ b/internal/application/cookiejar/jar.go @@ -154,7 +154,29 @@ func (p *PersistCookieJar) Deserialize(content string) { var hostMap hostMap err := sonic.Unmarshal([]byte(content), &hostMap) - utils.OkOrPanic(err) + // 如果反序列化失败,尝试从旧格式转换 + if err != nil { + // 旧格式: map[string]map[string]*http.Cookie + type oldCookieMap map[string]*http.Cookie + type oldHostMap map[string]oldCookieMap + + var oldMap oldHostMap + oldErr := sonic.Unmarshal([]byte(content), &oldMap) + if oldErr != nil { + // 两种格式都失败,抛出原始错误 + utils.OkOrPanic(err) + return + } + + // 转换旧格式到新格式 + hostMap = make(map[string]cookieMap) + for host, cookies := range oldMap { + hostMap[host] = make(map[string][]*http.Cookie) + for name, cookie := range cookies { + hostMap[host][name] = []*http.Cookie{cookie} + } + } + } p.hostMap = hostMap } @@ -187,7 +209,31 @@ func (p *PersistCookieJar) DeserializeBytes(content []byte) { var hostMap hostMap err := gob.NewDecoder(retReader). Decode(&hostMap) - utils.OkOrPanic(err) + + // 如果反序列化失败,尝试从旧格式转换 + if err != nil { + // 旧格式: map[string]map[string]*http.Cookie + type oldCookieMap map[string]*http.Cookie + type oldHostMap map[string]oldCookieMap + + retReader = bytes.NewBuffer(content) + var oldMap oldHostMap + oldErr := gob.NewDecoder(retReader).Decode(&oldMap) + if oldErr != nil { + // 两种格式都失败,抛出原始错误 + utils.OkOrPanic(err) + return + } + + // 转换旧格式到新格式 + hostMap = make(map[string]cookieMap) + for host, cookies := range oldMap { + hostMap[host] = make(map[string][]*http.Cookie) + for name, cookie := range cookies { + hostMap[host][name] = []*http.Cookie{cookie} + } + } + } p.hostMap = hostMap } @@ -196,10 +242,36 @@ func (p *PersistCookieJar) DeserializeReader(reader io.Reader) { p.Lock() defer p.Unlock() - var hostMap hostMap - err := gob.NewDecoder(reader). - Decode(&hostMap) + // 先读取所有数据,以便在失败时可以重试 + data, err := io.ReadAll(reader) utils.OkOrPanic(err) + var hostMap hostMap + err = gob.NewDecoder(bytes.NewBuffer(data)).Decode(&hostMap) + + // 如果反序列化失败,尝试从旧格式转换 + if err != nil { + // 旧格式: map[string]map[string]*http.Cookie + type oldCookieMap map[string]*http.Cookie + type oldHostMap map[string]oldCookieMap + + var oldMap oldHostMap + oldErr := gob.NewDecoder(bytes.NewBuffer(data)).Decode(&oldMap) + if oldErr != nil { + // 两种格式都失败,抛出原始错误 + utils.OkOrPanic(err) + return + } + + // 转换旧格式到新格式 + hostMap = make(map[string]cookieMap) + for host, cookies := range oldMap { + hostMap[host] = make(map[string][]*http.Cookie) + for name, cookie := range cookies { + hostMap[host][name] = []*http.Cookie{cookie} + } + } + } + p.hostMap = hostMap } diff --git a/internal/application/cookiejar/jar_test.go b/internal/application/cookiejar/jar_test.go index 81a182c..40ad291 100644 --- a/internal/application/cookiejar/jar_test.go +++ b/internal/application/cookiejar/jar_test.go @@ -114,3 +114,10 @@ func TestPersistCookieJar_DeserializeReader(t *testing.T) { } } } + +func TestDeseralizeCookie(t *testing.T) { + var str = `{"cdpc.campusphere.net":{"MOD_AUTH_CAS":{"Name":"MOD_AUTH_CAS","Value":"ST-iap:1021988086918169:ST:8d7fe60b-dc7d-4fcd-b26b-5517e3ca1926:20251113162807","Quoted":false,"Path":"/","Domain":"","Expires":"0001-01-01T00:00:00Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":true,"SameSite":0,"Partitioned":false,"Raw":"MOD_AUTH_CAS=ST-iap:1021988086918169:ST:8d7fe60b-dc7d-4fcd-b26b-5517e3ca1926:20251113162807; path=/; Httponly","Unparsed":null},"HWWAFSESID":{"Name":"HWWAFSESID","Value":"88d84731cf506df0bf","Quoted":false,"Path":"/","Domain":"","Expires":"0001-01-01T00:00:00Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":false,"SameSite":0,"Partitioned":false,"Raw":"HWWAFSESID=88d84731cf506df0bf; path=/","Unparsed":null},"HWWAFSESTIME":{"Name":"HWWAFSESTIME","Value":"1763022486728","Quoted":false,"Path":"/","Domain":"","Expires":"0001-01-01T00:00:00Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":false,"SameSite":0,"Partitioned":false,"Raw":"HWWAFSESTIME=1763022486728; path=/","Unparsed":null},"CONVERSATION":{"Name":"CONVERSATION","Value":"iap-1021988086918169-CONV-83bee566-7010-4ef6-ad17-4b9eebcccdf8","Quoted":false,"Path":"/iap","Domain":"","Expires":"0001-01-01T00:00:00Z","RawExpires":"","MaxAge":0,"Secure":false,"HttpOnly":true,"SameSite":0,"Partitioned":false,"Raw":"CONVERSATION=iap-1021988086918169-CONV-83bee566-7010-4ef6-ad17-4b9eebcccdf8; Path=/iap; HttpOnly","Unparsed":null},"CASTGC":{"Name":"CASTGC","Value":"iap-1021988086918169-TGT-717aa5db-92cb-46ac-a178-64b630946f43","Quoted":false,"Path":"/iap","Domain":"","Expires":"0001-01-01T00:00:00Z","RawExpires":"","MaxAge":0,"Secure":true,"HttpOnly":true,"SameSite":4,"Partitioned":false,"Raw":"CASTGC=iap-1021988086918169-TGT-717aa5db-92cb-46ac-a178-64b630946f43; Path=/iap; HttpOnly; Secure; SameSite=None","Unparsed":null}}}` + jar := NewPersistCookieJar() + jar.Deserialize(str) + t.Log(jar) +}