update tm config parsing
This commit is contained in:
1 parent
69de2bd8b7
commit
3545d53523
2 files changed
+79
-44
No files matched your search
+77
-40
@@ -1,81 +1,118 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/kulikov0/headless-client"
|
"github.com/kulikov0/headless-client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
fallbackAppVersion = "211.2.0"
|
||||||
|
fallbackSDKVersion = "5.28.0"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
globalParamsRe = regexp.MustCompile(`var globalParams\s*=\s*`)
|
||||||
|
appBundleRe = regexp.MustCompile(`src="([^"]+app\.js)"`)
|
||||||
|
|
||||||
|
sdkVersionPatterns = []*regexp.Regexp{
|
||||||
|
regexp.MustCompile(`buildInfo\s*=\s*\{version:"(\d+\.\d+\.\d+)"`),
|
||||||
|
regexp.MustCompile(`goloom-sdk@(\d+\.\d+\.\d+)`),
|
||||||
|
regexp.MustCompile(`goloom_sdk_version:"(\d+\.\d+\.\d+)"`),
|
||||||
|
regexp.MustCompile(`"@yandex-video-platform/goloom-sdk":"(\d+\.\d+\.\d+)"`),
|
||||||
|
regexp.MustCompile(`goloom-sdk\.(\d+\.\d+\.\d+)\.js`),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type TMConfig struct {
|
type TMConfig struct {
|
||||||
AppVersion string
|
AppVersion string
|
||||||
SDKVersion string
|
SDKVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchConfig() (TMConfig, error) {
|
func fetchConfig() TMConfig {
|
||||||
var cfg TMConfig
|
var cfg TMConfig
|
||||||
|
|
||||||
page, err := tmHttpGet("https://telemost.yandex.ru/", headless.DestDocument)
|
page, err := tmHttpGet("https://telemost.yandex.ru/", headless.DestDocument)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cfg, fmt.Errorf("failed to fetch telemost.yandex.ru: %w", err)
|
log.Printf("[config] failed to fetch telemost.yandex.ru: %v", err)
|
||||||
|
return finishConfig(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
stateRe := regexp.MustCompile(`<script[^>]*id="preloaded-state"[^>]*>([\s\S]*?)</script>`)
|
cfg.AppVersion = parseAppVersion(page)
|
||||||
stateMatch := stateRe.FindSubmatch(page)
|
cfg.SDKVersion = fetchSDKVersion(page)
|
||||||
if stateMatch == nil {
|
return finishConfig(cfg)
|
||||||
return cfg, fmt.Errorf("preloaded-state not found in page")
|
}
|
||||||
}
|
|
||||||
var state struct {
|
|
||||||
Config struct {
|
|
||||||
AppVersion string `json:"appVersion"`
|
|
||||||
} `json:"config"`
|
|
||||||
AppVersion string `json:"appVersion"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(stateMatch[1], &state); err != nil {
|
|
||||||
return cfg, fmt.Errorf("failed to parse preloaded-state: %w", err)
|
|
||||||
}
|
|
||||||
cfg.AppVersion = state.Config.AppVersion
|
|
||||||
if cfg.AppVersion == "" {
|
|
||||||
cfg.AppVersion = state.AppVersion
|
|
||||||
}
|
|
||||||
if cfg.AppVersion == "" {
|
|
||||||
return cfg, fmt.Errorf("appVersion not found in preloaded-state")
|
|
||||||
}
|
|
||||||
log.Printf("[config] appVersion=%s", cfg.AppVersion)
|
|
||||||
|
|
||||||
bundleRe := regexp.MustCompile(`https://telemost\.yastatic\.net/s3/telemost/_/main\.\w+\.[a-f0-9]+\.js`)
|
func parseAppVersion(page []byte) string {
|
||||||
bundleURL := bundleRe.FindString(string(page))
|
loc := globalParamsRe.FindIndex(page)
|
||||||
|
if loc == nil {
|
||||||
|
log.Println("[config] globalParams not found in page")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var params struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(bytes.NewReader(page[loc[1]:])).Decode(¶ms); err != nil {
|
||||||
|
log.Printf("[config] failed to parse globalParams: %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if params.Version == "" {
|
||||||
|
log.Println("[config] version not found in globalParams")
|
||||||
|
}
|
||||||
|
return params.Version
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchSDKVersion(page []byte) string {
|
||||||
|
bundleURL := parseBundleURL(page)
|
||||||
if bundleURL == "" {
|
if bundleURL == "" {
|
||||||
return cfg, fmt.Errorf("main bundle URL not found in page")
|
log.Println("[config] app bundle URL not found in page")
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
log.Printf("[config] Found bundle: %s", bundleURL)
|
log.Printf("[config] Found bundle: %s", bundleURL)
|
||||||
|
|
||||||
bundle, err := tmHttpGet(bundleURL, headless.DestScript)
|
bundle, err := tmHttpGet(bundleURL, headless.DestScript)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cfg, fmt.Errorf("failed to fetch bundle: %w", err)
|
log.Printf("[config] failed to fetch bundle: %v", err)
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
for _, pattern := range sdkVersionPatterns {
|
||||||
|
if match := pattern.FindSubmatch(bundle); match != nil {
|
||||||
|
return string(match[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Println("[config] goloom SDK version not found in bundle")
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
sdkVerPatterns := []*regexp.Regexp{
|
func parseBundleURL(page []byte) string {
|
||||||
regexp.MustCompile(`goloom_sdk_version:"(\d+\.\d+\.\d+)"`),
|
match := appBundleRe.FindSubmatch(page)
|
||||||
regexp.MustCompile(`"@yandex-video-platform/goloom-sdk":"(\d+\.\d+\.\d+)"`),
|
if match == nil {
|
||||||
regexp.MustCompile(`goloom-sdk\.(\d+\.\d+\.\d+)\.js`),
|
return ""
|
||||||
}
|
}
|
||||||
for _, re := range sdkVerPatterns {
|
bundleURL := string(match[1])
|
||||||
if m := re.FindSubmatch(bundle); m != nil {
|
if strings.HasPrefix(bundleURL, "//") {
|
||||||
cfg.SDKVersion = string(m[1])
|
bundleURL = "https:" + bundleURL
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
return bundleURL
|
||||||
|
}
|
||||||
|
|
||||||
|
func finishConfig(cfg TMConfig) TMConfig {
|
||||||
|
if cfg.AppVersion == "" {
|
||||||
|
cfg.AppVersion = fallbackAppVersion
|
||||||
|
log.Printf("[config] page scrape missed appVersion, using pinned %s", fallbackAppVersion)
|
||||||
}
|
}
|
||||||
if cfg.SDKVersion == "" {
|
if cfg.SDKVersion == "" {
|
||||||
return cfg, fmt.Errorf("goloom SDK version not found in bundle")
|
cfg.SDKVersion = fallbackSDKVersion
|
||||||
|
log.Printf("[config] bundle scrape missed sdkVersion, using pinned %s", fallbackSDKVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[config] app=%s sdk=%s", cfg.AppVersion, cfg.SDKVersion)
|
log.Printf("[config] app=%s sdk=%s", cfg.AppVersion, cfg.SDKVersion)
|
||||||
return cfg, nil
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func tmHttpGet(endpoint string, dest headless.RequestDest) ([]byte, error) {
|
func tmHttpGet(endpoint string, dest headless.RequestDest) ([]byte, error) {
|
||||||
|
|||||||
@@ -995,12 +995,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Println("[config] Fetching live config from Telemost bundle...")
|
log.Println("[config] Fetching live config from Telemost bundle...")
|
||||||
cfg, err := fetchConfig()
|
cfg := fetchConfig()
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("[config] %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var connInfo *ConnInfo
|
var connInfo *ConnInfo
|
||||||
|
var err error
|
||||||
if *tmLink != "" {
|
if *tmLink != "" {
|
||||||
connInfo, err = joinExistingConference(cookieStr, *tmLink, cfg)
|
connInfo, err = joinExistingConference(cookieStr, *tmLink, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user