feat: 提交广电个人中心只读面板
This commit is contained in:
@@ -0,0 +1,162 @@
|
||||
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:"(cd_[0-9_]+)"`)
|
||||
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
|
||||
}
|
||||
|
||||
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 := channelRE.FindStringSubmatch(src)
|
||||
if ver == "" || ch == nil {
|
||||
return nil, errors.New("页面脚本里没有找到渠道或版本号")
|
||||
}
|
||||
return &siteProfile{
|
||||
ChannelID: ch[1],
|
||||
Version: ver,
|
||||
PublicB64: pubB64,
|
||||
PrivateB64: privB64,
|
||||
Public: pub,
|
||||
Private: priv,
|
||||
FetchedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func scriptURLs(html string) []string {
|
||||
matches := scriptRE.FindAllStringSubmatch(html, -1)
|
||||
var common, rest []string
|
||||
seen := map[string]bool{}
|
||||
for _, m := range matches {
|
||||
u := m[1]
|
||||
if seen[u] {
|
||||
continue
|
||||
}
|
||||
seen[u] = true
|
||||
if strings.Contains(u, "common-") {
|
||||
common = append(common, u)
|
||||
continue
|
||||
}
|
||||
rest = append(rest, u)
|
||||
}
|
||||
return append(common, rest...)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user