bake vk api versions

This commit is contained in:
kulikov0 committed 2026-05-12 20:58:13 +02:00
1 parent b27ad09138
commit 573771541a
5 files changed
+15 -264

No files matched your search

+6 -141
View File
@@ -9,7 +9,6 @@ import (
"net"
"net/http"
"net/url"
"regexp"
"strings"
"time"
@@ -17,28 +16,6 @@ import (
)
const vkConfigCacheKey = "vk_config"
func loadCachedConfig(cache CacheStore) *vkConfig {
data := cache.Load(vkConfigCacheKey)
if data == "" {
return nil
}
var cfg vkConfig
if json.Unmarshal([]byte(data), &cfg) != nil {
return nil
}
return &cfg
}
func saveCachedConfig(cache CacheStore, cfg *vkConfig) {
data, err := json.Marshal(cfg)
if err != nil {
return
}
cache.Save(vkConfigCacheKey, string(data))
}
type vkConfig struct {
AppID string `json:"appID"`
ApiVersion string `json:"apiVersion"`
@@ -69,16 +46,6 @@ func RunVKAuth(joinLink string, displayName string, logFn func(string, ...any),
}
client := &http.Client{Timeout: 60 * time.Second, Transport: transport}
httpGet := func(targetURL string) ([]byte, error) {
req, _ := http.NewRequest("GET", targetURL, nil)
req.Header.Set("User-Agent", common.UserAgent)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
httpPost := func(targetURL string, form url.Values, extraHeaders map[string]string) (map[string]interface{}, error) {
req, _ := http.NewRequest("POST", targetURL, strings.NewReader(form.Encode()))
@@ -105,20 +72,13 @@ func RunVKAuth(joinLink string, displayName string, logFn func(string, ...any),
return result, nil
}
// Use cached config if available, fetch only if missing
cfg := loadCachedConfig(cache)
if cfg != nil {
logFn("vk-auth: using cached config appID=%s api=%s", cfg.AppID, cfg.ApiVersion)
} else {
statusFn("Fetching config...")
logFn("vk-auth: fetching config")
var err error
cfg, err = fetchVKConfig(httpGet, logFn)
if err != nil {
return "", fmt.Errorf("fetchConfig: %w", err)
}
saveCachedConfig(cache, cfg)
cfg := &vkConfig{
AppID: "6287487",
ApiVersion: "5.276",
AppVersion: "1.1",
ProtocolVersion: "6",
}
logFn("vk-auth: appID=%s api=%s appVersion=%s proto=%s", cfg.AppID, cfg.ApiVersion, cfg.AppVersion, cfg.ProtocolVersion)
// Get anonymous token
statusFn("Getting anonymous token...")
@@ -312,101 +272,6 @@ func RunVKAuth(joinLink string, displayName string, logFn func(string, ...any),
return string(jsonBytes), nil
}
func fetchVKConfig(httpGet func(string) ([]byte, error), logFn func(string, ...any)) (*vkConfig, error) {
page, err := httpGet("https://vk.com")
if err != nil {
return nil, fmt.Errorf("fetch vk.com: %w", err)
}
bundleRe := regexp.MustCompile(`https://[a-z0-9.-]+/dist/core_spa/core_spa_vk\.[a-f0-9]+\.js`)
bundleURL := bundleRe.FindString(string(page))
if bundleURL == "" {
return nil, fmt.Errorf("bundle URL not found")
}
logFn("vk-auth: bundle=%s", bundleURL[strings.LastIndex(bundleURL, "/")+1:])
bundle, err := httpGet(bundleURL)
if err != nil {
return nil, fmt.Errorf("fetch bundle: %w", err)
}
bundleStr := string(bundle)
chunksBase := bundleURL[:strings.LastIndex(bundleURL, "core_spa_vk.")] + "chunks/"
cfg := &vkConfig{}
if m := regexp.MustCompile(`[,;]u=(\d{7,8}),[a-z]=\d{7,9}`).FindStringSubmatch(bundleStr); m != nil {
cfg.AppID = m[1]
} else {
return nil, fmt.Errorf("appID not found")
}
if m := regexp.MustCompile(`\d+:\(e,t,n\)=>\{"use strict";n\.d\(t,\{m:\(\)=>r\}\);const r="(5\.\d+)"\}`).FindStringSubmatch(bundleStr); m != nil {
cfg.ApiVersion = m[1]
} else {
return nil, fmt.Errorf("apiVersion not found")
}
logFn("vk-auth: appID=%s api=%s", cfg.AppID, cfg.ApiVersion)
bridgeRef := regexp.MustCompile(`core_spa/chunks/webCallsBridge\.([a-f0-9]+)\.js`).FindStringSubmatch(bundleStr)
if bridgeRef == nil {
return nil, fmt.Errorf("webCallsBridge not found")
}
bridgeURL := chunksBase + "webCallsBridge." + bridgeRef[1] + ".js"
bridgeData, err := httpGet(bridgeURL)
if err != nil {
return nil, fmt.Errorf("fetch bridge: %w", err)
}
requireRe := regexp.MustCompile(`i\((\d{4,6})\)`)
matches := requireRe.FindAllStringSubmatch(string(bridgeData), -1)
seen := map[string]bool{}
var moduleIds []string
for _, m := range matches {
if !seen[m[1]] {
seen[m[1]] = true
moduleIds = append(moduleIds, m[1])
}
}
chunkMap := map[string]string{}
chunkRe := regexp.MustCompile(`(\d+)===e\)return"core_spa/chunks/"\+e\+"\.([a-f0-9]+)\.js"`)
for _, m := range chunkRe.FindAllStringSubmatch(bundleStr, -1) {
chunkMap[m[1]] = m[2]
}
appVerRe := regexp.MustCompile(`appVersion.{0,40}return\s+([0-9.]+)`)
protoVerRe := regexp.MustCompile(`protocolVersion.{0,40}return.*?(\d+)`)
for _, modId := range moduleIds {
chunkId := modId
hash, ok := chunkMap[chunkId]
if !ok {
chunkId = modId[1:]
hash, ok = chunkMap[chunkId]
}
if !ok {
continue
}
chunkURL := fmt.Sprintf("%s%s.%s.js", chunksBase, chunkId, hash)
chunkData, err := httpGet(chunkURL)
if err != nil {
continue
}
chunkStr := string(chunkData)
if av := appVerRe.FindStringSubmatch(chunkStr); av != nil {
cfg.AppVersion = av[1]
if pv := protoVerRe.FindStringSubmatch(chunkStr); pv != nil {
cfg.ProtocolVersion = pv[1]
}
logFn("vk-auth: appVersion=%s proto=%s", cfg.AppVersion, cfg.ProtocolVersion)
break
}
}
if cfg.AppVersion == "" {
return nil, fmt.Errorf("appVersion not found")
}
return cfg, nil
}
func parseVKCaptchaError(errObj map[string]interface{}) *vkCaptchaError {
redirectURI, _ := errObj["redirect_uri"].(string)
if redirectURI == "" {