194 lines
4.2 KiB
Go
194 lines
4.2 KiB
Go
package cbn
|
|
|
|
import (
|
|
"crypto/rsa"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
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\s*:\s*"([^"]+)"`)
|
|
scriptRE = regexp.MustCompile(`(?i)src=["']([^"']+\.js)["']`)
|
|
)
|
|
|
|
type siteProfile struct {
|
|
ChannelID string
|
|
Version string
|
|
PublicB64 string
|
|
PrivateB64 string
|
|
Public *rsa.PublicKey
|
|
Private *rsa.PrivateKey
|
|
FetchedAt time.Time
|
|
useNative bool
|
|
}
|
|
|
|
type siteFile struct {
|
|
ChannelID string `json:"channelId"`
|
|
Version string `json:"version"`
|
|
PublicB64 string `json:"publicKey"`
|
|
PrivateB64 string `json:"privateKey"`
|
|
FetchedAt time.Time `json:"fetchedAt"`
|
|
}
|
|
|
|
func parseSiteJS(src string) (*siteProfile, error) {
|
|
var pub *rsa.PublicKey
|
|
var priv *rsa.PrivateKey
|
|
var pubB64, privB64 string
|
|
for _, b64 := range keyRE.FindAllString(src, -1) {
|
|
der, err := base64.StdEncoding.DecodeString(b64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if pub == nil {
|
|
if parsed, err := x509.ParsePKIXPublicKey(der); err == nil {
|
|
if rk, ok := parsed.(*rsa.PublicKey); ok {
|
|
pub = rk
|
|
pubB64 = b64
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
if priv == nil {
|
|
if parsed, err := x509.ParsePKCS8PrivateKey(der); err == nil {
|
|
if rk, ok := parsed.(*rsa.PrivateKey); ok {
|
|
priv = rk
|
|
privB64 = b64
|
|
continue
|
|
}
|
|
}
|
|
if rk, err := x509.ParsePKCS1PrivateKey(der); err == nil {
|
|
priv = rk
|
|
privB64 = b64
|
|
}
|
|
}
|
|
}
|
|
if pub == nil || priv == nil {
|
|
return nil, errors.New("页面脚本里没有找到加密密钥")
|
|
}
|
|
ver := versionRE.FindString(src)
|
|
ch := chooseChannel(channelRE.FindAllStringSubmatch(src, -1))
|
|
if ver == "" || ch == "" {
|
|
return nil, errors.New("页面脚本里没有找到渠道或版本号")
|
|
}
|
|
p := &siteProfile{
|
|
ChannelID: ch,
|
|
Version: ver,
|
|
PublicB64: pubB64,
|
|
PrivateB64: privB64,
|
|
Public: pub,
|
|
Private: priv,
|
|
FetchedAt: time.Now(),
|
|
}
|
|
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 out []string
|
|
seen := map[string]bool{}
|
|
for _, m := range matches {
|
|
u := m[1]
|
|
if u == "" || seen[u] {
|
|
continue
|
|
}
|
|
seen[u] = true
|
|
out = append(out, u)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func resolveURL(ref string) string {
|
|
if strings.HasPrefix(ref, "https://") || strings.HasPrefix(ref, "http://") {
|
|
return ref
|
|
}
|
|
if strings.HasPrefix(ref, "//") {
|
|
return "https:" + ref
|
|
}
|
|
if strings.HasPrefix(ref, "/") {
|
|
return origin + ref
|
|
}
|
|
return origin + "/" + ref
|
|
}
|
|
|
|
func loadSiteCache(path string) (*siteProfile, error) {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var file siteFile
|
|
if err := json.Unmarshal(b, &file); err != nil {
|
|
return nil, err
|
|
}
|
|
p, err := parseSiteJS(`channelId:"` + file.ChannelID + `" v="` + file.Version + `" "` + file.PublicB64 + `" "` + file.PrivateB64 + `"`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
p.FetchedAt = file.FetchedAt
|
|
return p, nil
|
|
}
|
|
|
|
func saveSiteCache(path string, p *siteProfile) error {
|
|
buf, err := json.MarshalIndent(siteFile{
|
|
ChannelID: p.ChannelID,
|
|
Version: p.Version,
|
|
PublicB64: p.PublicB64,
|
|
PrivateB64: p.PrivateB64,
|
|
FetchedAt: p.FetchedAt,
|
|
}, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buf = append(buf, '\n')
|
|
if err := os.MkdirAll("data", 0755); err != nil {
|
|
return err
|
|
}
|
|
tmp := path + ".tmp"
|
|
if err := os.WriteFile(tmp, buf, 0600); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp, path)
|
|
}
|