feat: 广电加密参数改为按脚本内容读取
This commit is contained in:
@@ -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
|
||||
|
||||
+45
-14
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/dop251/goja"
|
||||
)
|
||||
|
||||
// 只在返回人机校验页时执行官网自己的脚本,平时的查询走 Go,不常驻解释器。
|
||||
// 只在返回人机校验页时执行官网自己的脚本。业务加解密在密钥和原生结果对应上之后走 Go。
|
||||
func solveACW(html string) (string, error) {
|
||||
parts := extractScripts(html)
|
||||
if len(parts) < 2 {
|
||||
|
||||
Reference in New Issue
Block a user