package unicom import ( "errors" "regexp" "strings" ) type loginScript struct { exponent string modulus string captchaAppID string captchaScript string sendPath string loginPath string genQRPath string heartPath string logonPath string secCookie string piwCookie string pwdType string userType string redirectType string } type billAPI struct { URL string Origin string Referer string Version string Language string } var ( keyPairRE = regexp.MustCompile(`RSAUtils\.getKeyPair\(\s*"([0-9a-fA-F]+)"\s*,\s*"([0-9a-fA-F]*)"\s*,\s*"([0-9a-fA-F]+)"\s*\)`) captchaAppRE = regexp.MustCompile(`new\s+TencentCaptcha\(\s*['"](\d+)['"]`) captchaSrcRE = regexp.MustCompile(`src=["']([^"']*turing\.captcha[^"']+)["']`) sendPathRE = regexp.MustCompile(`(/portal/Service/SendMSG)`) loginPathRE = regexp.MustCompile(`(/portal/Service/MallLogin)`) genQRRE = regexp.MustCompile(`(/oauth2/genqr)`) heartRE = regexp.MustCompile(`(/qrcode/qrcode_hbt)`) logonRE = regexp.MustCompile(`(/portal/qrcode/qrcode_logon)`) secCookieRE = regexp.MustCompile(`secsnid\s*=\s*\$\.cookie\(["']([A-Za-z0-9_]+)["']\)`) piwCookieRE = regexp.MustCompile(`setCookie\(\s*["']([A-Za-z0-9_]+)["']\s*,\s*data\.piw`) pwdTypeRE = regexp.MustCompile(`PWD_TYPE_RND\s*=\s*["'](\d+)["']`) userTypeRE = regexp.MustCompile(`USER_TYPE_MOBILE\s*=\s*["'](\d+)["']`) loginFromRE = regexp.MustCompile(`(?is)]*id=["']loginFrom["'][^>]*>`) inputValueRE = regexp.MustCompile(`value=["']([^"']*)["']`) scriptSrcRE = regexp.MustCompile(`(?i)]*?\ssrc=["']?([^"'\s>]+)`) navURLRE = regexp.MustCompile(`https://www\.10010\.com/mall/service/query/QueryNavigations`) queryPrefixRE = regexp.MustCompile(`QueryRequestPrefix\s*:\s*function\s*\(\)\s*\{\s*return\s*["']([^"']+)["']`) xxBaseRE = regexp.MustCompile(`VUE_APP_XX_BASE_URL:"(https://[^"]+)"`) queryKeyRE = regexp.MustCompile(`(?:VUE_APP_KEY_QUERY|key_query):"([^"]+)"`) httpsHostRE = regexp.MustCompile(`"(https://[a-zA-Z0-9.-]+\.10010\.com)"`) versionRE = regexp.MustCompile(`version:"([^"]+)"`) languageRE = regexp.MustCompile(`(?:language|languageType)\s*[:=]\s*"([^"]+)"`) jsPathRE = regexp.MustCompile(`["']([^"']+\.js(?:\?[^"']*)?)["']`) ) func parseLoginBundle(js string) (loginScript, error) { var out loginScript key := keyPairRE.FindStringSubmatch(js) if key == nil { return out, errors.New("登录公钥没有在脚本里找到") } out.exponent = key[1] out.modulus = key[3] if app := captchaAppRE.FindStringSubmatch(js); app != nil { out.captchaAppID = app[1] } out.sendPath = first(sendPathRE, js) out.loginPath = first(loginPathRE, js) out.genQRPath = first(genQRRE, js) out.heartPath = first(heartRE, js) out.logonPath = first(logonRE, js) if m := secCookieRE.FindStringSubmatch(js); m != nil { out.secCookie = m[1] } if m := piwCookieRE.FindStringSubmatch(js); m != nil { out.piwCookie = m[1] } if m := pwdTypeRE.FindStringSubmatch(js); m != nil { out.pwdType = m[1] } if m := userTypeRE.FindStringSubmatch(js); m != nil { out.userType = m[1] } if out.sendPath == "" || out.loginPath == "" || out.captchaAppID == "" { return out, errors.New("登录脚本不完整") } return out, nil } func parseLoginFrom(html string) string { tag := loginFromRE.FindString(html) if tag == "" { return "" } if m := inputValueRE.FindStringSubmatch(tag); m != nil { return m[1] } return "" } func parseCaptchaScript(html string) string { if m := captchaSrcRE.FindStringSubmatch(html); m != nil { return m[1] } return "" } func scriptSrcs(html string) []string { all := scriptSrcRE.FindAllStringSubmatch(html, -1) out := make([]string, 0, len(all)) seen := map[string]bool{} for _, item := range all { src := strings.TrimSpace(item[1]) if src == "" || seen[src] { continue } seen[src] = true out = append(out, src) } return out } func findNavURL(text string) string { return navURLRE.FindString(text) } func resolveNav(texts []string) string { var prefix string called := false for _, text := range texts { if nav := findNavURL(text); nav != "" { return nav } if m := queryPrefixRE.FindStringSubmatch(text); m != nil { prefix = m[1] } if strings.Contains(text, "QueryNavigations") { called = true } } if prefix == "" || !called { return "" } if strings.HasPrefix(prefix, "//") { prefix = "https:" + prefix } return strings.TrimRight(prefix, "/") + "/QueryNavigations" } func parseBillAPI(js, pageURL, pathMarker string) (billAPI, bool) { if !strings.Contains(js, pathMarker) { return billAPI{}, false } base := firstGroup(xxBaseRE, js) key := firstGroup(queryKeyRE, js) if base == "" && key != "" { base = hostNear(js, key) } if base == "" || key == "" { return billAPI{}, false } path := pathMarker if i := strings.Index(js, pathMarker); i >= 0 { start := strings.LastIndex(js[:i], `"`) if start >= 0 && start < i { candidate := js[start+1 : i+len(pathMarker)] if strings.HasPrefix(candidate, "/") { path = candidate } } } full := strings.TrimRight(base, "/") + "/" + strings.Trim(key, "/") + path origin, referer := pageEndpoint(pageURL) return billAPI{ URL: full, Origin: origin, Referer: referer, Version: firstGroup(versionRE, js), Language: firstGroup(languageRE, js), }, true } func pageEndpoint(raw string) (origin, referer string) { raw = strings.TrimSpace(raw) if raw == "" { return "", "" } scheme := "https://" rest := raw if i := strings.Index(raw, "://"); i >= 0 { scheme = raw[:i+3] rest = raw[i+3:] } host := rest if i := strings.IndexAny(host, "/?#"); i >= 0 { host = host[:i] } return scheme + host, raw } func jsRefs(js string) []string { all := jsPathRE.FindAllStringSubmatch(js, -1) out := make([]string, 0, len(all)) seen := map[string]bool{} for _, item := range all { ref := item[1] if strings.Contains(ref, "://") && !strings.Contains(ref, "10010.com") { continue } if strings.HasPrefix(ref, "data:") || strings.Contains(ref, " ") { continue } if seen[ref] { continue } seen[ref] = true out = append(out, ref) } return out } func hostNear(js, key string) string { i := strings.Index(js, `key_query:"`+key+`"`) if i < 0 { i = strings.Index(js, key) } if i < 0 { return "" } start := i - 1500 if start < 0 { start = 0 } end := i + 200 if end > len(js) { end = len(js) } matches := httpsHostRE.FindAllStringSubmatch(js[start:end], -1) if len(matches) == 0 { return "" } return matches[len(matches)-1][1] } func first(re *regexp.Regexp, text string) string { m := re.FindStringSubmatch(text) if m == nil { return "" } if len(m) > 1 { return m[1] } return m[0] } func firstGroup(re *regexp.Regexp, text string) string { m := re.FindStringSubmatch(text) if m == nil || len(m) < 2 { return "" } return m[1] }