重构:完成架构一致性整改与生命周期收口
This commit is contained in:
1 parent
55904cc409
commit
9eb6be2875
344 files changed
+15961
-13926
No files matched your search
@@ -10,23 +10,23 @@ import (
|
||||
"xianyu-go/internal/xianyu/cookierefresh"
|
||||
)
|
||||
|
||||
// tokenExpirySafetyMargin 保存令牌ExpirySafetyMargin,供当前处理流程使用
|
||||
// tokenExpirySafetyMargin 用于本次流程后续判断的令牌ExpirySafetyMargin
|
||||
const tokenExpirySafetyMargin = time.Minute
|
||||
|
||||
// tokenFallbackLifetime 保存令牌FallbackLifetime,供当前处理流程使用
|
||||
// tokenFallbackLifetime 用于本次流程后续判断的令牌FallbackLifetime
|
||||
const (
|
||||
tokenFallbackLifetime = 30 * time.Minute
|
||||
tokenRefreshLeadTime = 10 * time.Minute
|
||||
)
|
||||
|
||||
// effectiveTokenExpireAt 负责effective令牌ExpireAt相关处理。
|
||||
// effectiveTokenExpireAt 封装effective令牌ExpireAt业务协调。
|
||||
func effectiveTokenExpireAt(serverExpireAt int64, now time.Time) int64 {
|
||||
// ttl 保存ttl,供当前处理流程使用
|
||||
// ttl 用于本次流程后续判断的ttl
|
||||
ttl := time.Unix(serverExpireAt, 0).Sub(now)
|
||||
if ttl <= 0 {
|
||||
return 0
|
||||
}
|
||||
// margin 保存margin,供当前处理流程使用
|
||||
// margin 用于本次流程后续判断的margin
|
||||
margin := tokenExpirySafetyMargin
|
||||
if ttl <= 2*margin {
|
||||
margin = ttl / 10
|
||||
@@ -34,15 +34,15 @@ func effectiveTokenExpireAt(serverExpireAt int64, now time.Time) int64 {
|
||||
return now.Add(ttl - margin).Unix()
|
||||
}
|
||||
|
||||
// tokenRotationSchedule 负责令牌RotationSchedule相关处理。
|
||||
// tokenRotationSchedule 封装令牌RotationSchedule业务协调。
|
||||
func tokenRotationSchedule(serverExpireAt int64, now time.Time) (expiresAt, refreshAt time.Time) {
|
||||
expiresAt = time.Unix(serverExpireAt, 0)
|
||||
if serverExpireAt <= now.Unix() {
|
||||
expiresAt = now.Add(tokenFallbackLifetime)
|
||||
}
|
||||
// ttl 保存ttl,供当前处理流程使用
|
||||
// ttl 用于本次流程后续判断的ttl
|
||||
ttl := expiresAt.Sub(now)
|
||||
// lead 保存lead,供当前处理流程使用
|
||||
// lead 用于本次流程后续判断的lead
|
||||
lead := ttl / 10
|
||||
if lead < tokenRefreshLeadTime {
|
||||
lead = tokenRefreshLeadTime
|
||||
@@ -54,13 +54,13 @@ func tokenRotationSchedule(serverExpireAt int64, now time.Time) (expiresAt, refr
|
||||
return expiresAt, refreshAt
|
||||
}
|
||||
|
||||
// credentialCookieFingerprint 负责credential登录凭证Fingerprint相关处理。
|
||||
// credentialCookieFingerprint 封装credential登录凭证Fingerprint业务协调。
|
||||
func credentialCookieFingerprint(cookieStr string) string {
|
||||
// canonical 保存canonical,供当前处理流程使用
|
||||
// canonical 用于本次流程后续判断的canonical
|
||||
var canonical strings.Builder
|
||||
// part 表示当前遍历过程中的part
|
||||
for _, part := range strings.Split(cookieStr, ";") {
|
||||
// key、value、ok 保存key、value、ok,供当前处理流程使用
|
||||
// key、value、ok 用于本次流程后续判断的key、value、ok
|
||||
key, value, ok := strings.Cut(strings.TrimSpace(part), "=")
|
||||
key = strings.TrimSpace(key)
|
||||
if !ok || key == "" {
|
||||
@@ -71,7 +71,7 @@ func credentialCookieFingerprint(cookieStr string) string {
|
||||
canonical.WriteString(strings.TrimSpace(value))
|
||||
canonical.WriteByte(0)
|
||||
}
|
||||
// sum 保存sum,供当前处理流程使用
|
||||
// sum 用于本次流程后续判断的sum
|
||||
sum := sha256.Sum256([]byte(canonical.String()))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -81,23 +81,23 @@ func credentialCookieFingerprint(cookieStr string) string {
|
||||
// fingerprint deliberately retains duplicate names and their order because
|
||||
// lib-mtop reads the first path-ordered _m_h5_tk entry. A present empty Jar is
|
||||
// distinct from legacy metadata that has no complete snapshot.
|
||||
// credentialStateFingerprint 负责credential状态Fingerprint相关处理。
|
||||
// credentialStateFingerprint 封装credential状态Fingerprint业务协调。
|
||||
func credentialStateFingerprint(cookieStr, metadataJSON string) string {
|
||||
// canonical 保存canonical,供当前处理流程使用
|
||||
// canonical 用于本次流程后续判断的canonical
|
||||
var canonical strings.Builder
|
||||
canonical.WriteString("flat\x00")
|
||||
canonical.WriteString(credentialCookieFingerprint(cookieStr))
|
||||
canonical.WriteString("\x00snapshot\x00")
|
||||
if // snapshot、complete 保存snapshot、complete,供当前处理流程使用
|
||||
if // snapshot、complete 用于本次流程后续判断的snapshot、complete
|
||||
snapshot, complete := cookierefresh.SnapshotFromMetadataOK(metadataJSON); complete {
|
||||
canonical.WriteByte('1')
|
||||
// raw 保存原始,供当前处理流程使用
|
||||
// raw 用于本次流程后续判断的原始
|
||||
raw, _ := json.Marshal(cookierefresh.NormalizeSnapshot(snapshot))
|
||||
canonical.Write(raw)
|
||||
} else {
|
||||
canonical.WriteByte('0')
|
||||
}
|
||||
// sum 保存sum,供当前处理流程使用
|
||||
// sum 用于本次流程后续判断的sum
|
||||
sum := sha256.Sum256([]byte(canonical.String()))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
Reference in new issue
Block a user