From 76294270bfe3c24f498b8ecb39d4d16f591a3dec Mon Sep 17 00:00:00 2001 From: Nixevol Date: Fri, 16 Oct 2026 00:11:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B9=BF=E7=94=B5=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=94=B9=E4=B8=BA=E6=8C=89=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cbn/client.go | 11 ++++---- internal/cbn/site.go | 59 +++++++++++++++++++++++++++++---------- internal/cbn/site_test.go | 52 ++++++++++++++++++++++++++++++++++ internal/cbn/waf.go | 2 +- 4 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 internal/cbn/site_test.go diff --git a/internal/cbn/client.go b/internal/cbn/client.go index 610eb08..36be91d 100644 --- a/internal/cbn/client.go +++ b/internal/cbn/client.go @@ -425,17 +425,18 @@ func (c *Client) fetchProfile(ctx context.Context) (*siteProfile, error) { last = err continue } - if isWAF(body) || !bytes.Contains(body, []byte("MII")) { + if isWAF(body) { continue } - p, err := parseSiteJS(string(body)) + src := string(body) + if !strings.Contains(src, "encryptLong") || !strings.Contains(src, "setPublicKey") { + continue + } + p, err := parseSiteJS(src) if err == nil { return p, nil } last = err - if strings.Contains(ref, "common-") { - break - } } if last != nil { return nil, last diff --git a/internal/cbn/site.go b/internal/cbn/site.go index d7efeba..77e1ab5 100644 --- a/internal/cbn/site.go +++ b/internal/cbn/site.go @@ -17,7 +17,7 @@ const siteCacheTTL = 6 * time.Hour var ( keyRE = regexp.MustCompile(`MII[A-Za-z0-9+/]{200,}={0,2}`) versionRE = regexp.MustCompile(`\d+\.\d+\.\d+\.\d+_[A-Za-z0-9]+`) - channelRE = regexp.MustCompile(`channelId:"(cd_[0-9_]+)"`) + channelRE = regexp.MustCompile(`channelId\s*:\s*"([^"]+)"`) scriptRE = regexp.MustCompile(`(?i)src=["']([^"']+\.js)["']`) ) @@ -29,6 +29,7 @@ type siteProfile struct { Public *rsa.PublicKey Private *rsa.PrivateKey FetchedAt time.Time + useNative bool } type siteFile struct { @@ -75,38 +76,68 @@ func parseSiteJS(src string) (*siteProfile, error) { return nil, errors.New("页面脚本里没有找到加密密钥") } ver := versionRE.FindString(src) - ch := channelRE.FindStringSubmatch(src) - if ver == "" || ch == nil { + ch := chooseChannel(channelRE.FindAllStringSubmatch(src, -1)) + if ver == "" || ch == "" { return nil, errors.New("页面脚本里没有找到渠道或版本号") } - return &siteProfile{ - ChannelID: ch[1], + p := &siteProfile{ + ChannelID: ch, Version: ver, PublicB64: pubB64, PrivateB64: privB64, Public: pub, Private: priv, FetchedAt: time.Now(), - }, nil + } + p.useNative = p.roundtrip() + if !p.useNative { + return nil, errors.New("页面脚本里的密钥无法对应") + } + return p, nil +} + +func chooseChannel(matches [][]string) string { + first := "" + for _, m := range matches { + if len(m) < 2 || m[1] == "" { + continue + } + if first == "" { + first = m[1] + } + if strings.HasPrefix(m[1], "cd_") { + return m[1] + } + } + return first +} + +func (p *siteProfile) roundtrip() bool { + if p == nil || p.Public == nil || p.Private == nil { + return false + } + const probe = "probe" + enc, err := encryptLong(p.Public, probe) + if err != nil { + return false + } + back, err := decryptLong(p.Private, enc) + return err == nil && back == probe } func scriptURLs(html string) []string { matches := scriptRE.FindAllStringSubmatch(html, -1) - var common, rest []string + var out []string seen := map[string]bool{} for _, m := range matches { u := m[1] - if seen[u] { + if u == "" || seen[u] { continue } seen[u] = true - if strings.Contains(u, "common-") { - common = append(common, u) - continue - } - rest = append(rest, u) + out = append(out, u) } - return append(common, rest...) + return out } func resolveURL(ref string) string { diff --git a/internal/cbn/site_test.go b/internal/cbn/site_test.go new file mode 100644 index 0000000..48d4022 --- /dev/null +++ b/internal/cbn/site_test.go @@ -0,0 +1,52 @@ +package cbn + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/base64" + "testing" +) + +func TestParseSiteJSReadsScriptValues(t *testing.T) { + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + pubDER, err := x509.MarshalPKIXPublicKey(&key.PublicKey) + if err != nil { + t.Fatal(err) + } + privDER, err := x509.MarshalPKCS8PrivateKey(key) + if err != nil { + t.Fatal(err) + } + pub := base64.StdEncoding.EncodeToString(pubDER) + priv := base64.StdEncoding.EncodeToString(privDER) + src := `channelId:"other" channelId:"cd_9_1" something 1.2.3.4_ab encryptLong setPublicKey "` + pub + `" "` + priv + `"` + p, err := parseSiteJS(src) + if err != nil { + t.Fatal(err) + } + if p.ChannelID != "cd_9_1" || p.Version != "1.2.3.4_ab" || !p.useNative { + t.Fatalf("channel %s version %s native %v", p.ChannelID, p.Version, p.useNative) + } + plain := encodeURIComponent(`{"accessNum":"13800000000"}`) + enc, err := encryptLong(p.Public, plain) + if err != nil { + t.Fatal(err) + } + back, err := decryptLong(p.Private, enc) + if err != nil || back != plain { + t.Fatalf("roundtrip %q %v", back, err) + } + + renamed := `channelId:"shop_8" 9.8.7.6_zz "` + pub + `" "` + priv + `"` + p, err = parseSiteJS(renamed) + if err != nil { + t.Fatal(err) + } + if p.ChannelID != "shop_8" { + t.Fatalf("channel %s", p.ChannelID) + } +} diff --git a/internal/cbn/waf.go b/internal/cbn/waf.go index edd0004..76d98be 100644 --- a/internal/cbn/waf.go +++ b/internal/cbn/waf.go @@ -8,7 +8,7 @@ import ( "github.com/dop251/goja" ) -// 只在返回人机校验页时执行官网自己的脚本,平时的查询走 Go,不常驻解释器。 +// 只在返回人机校验页时执行官网自己的脚本。业务加解密在密钥和原生结果对应上之后走 Go。 func solveACW(html string) (string, error) { parts := extractScripts(html) if len(parts) < 2 {