package main import ( "context" "encoding/json" "errors" "net/http" "net/url" "time" "git.ipao.vip/rogee/creator-hub/internal/douyin" "git.ipao.vip/rogee/creator-hub/internal/hub" ) type douyinGatewayBrowser struct { gateway hub.Gateway environment hub.EnvironmentContext } type douyinGatewayRequest struct { BindingVersion int64 `json:"binding_version"` RuntimeID string `json:"runtime_id"` NetworkID string `json:"network_id"` NetworkExitID string `json:"network_exit_id"` URL string `json:"url,omitempty"` } func (browser douyinGatewayBrowser) Get(ctx context.Context, target string) (douyin.Response, error) { request, err := browser.request() if err != nil { return douyin.Response{}, err } request.URL = target status, body, err := gatewayCall(ctx, browser.gateway, http.MethodPost, "/v1/browsers/"+url.PathEscape(browser.environment.Alias)+"/douyin/get", request, 30*time.Second) if err != nil || status != http.StatusOK { return douyin.Response{}, errors.New("restricted browser operation failed") } var response struct { Status int `json:"status"` Body string `json:"body"` Challenge douyin.Challenge `json:"challenge"` } if json.Unmarshal(body, &response) != nil || response.Status < 100 || response.Status > 599 || (response.Challenge != douyin.ChallengeNone && response.Challenge != douyin.ChallengeCaptcha && response.Challenge != douyin.ChallengeDevice) { return douyin.Response{}, errors.New("restricted browser returned an invalid response") } return douyin.Response{Status: response.Status, Body: []byte(response.Body), Challenge: response.Challenge}, nil } func (browser douyinGatewayBrowser) request() (douyinGatewayRequest, error) { environment := browser.environment if browser.gateway.Endpoint == "" || browser.gateway.Token == "" || !accountRunnable(environment) || !gatewayGenerationIDPattern.MatchString(environment.AccountID) || !gatewayGenerationIDPattern.MatchString(environment.Alias) || !gatewayGenerationIDPattern.MatchString(environment.BindingID) || !gatewayGenerationIDPattern.MatchString(environment.Exit.ID) || environment.Exit.HealthStatus != "healthy" || environment.RuntimeCleanupPending || !gatewayGenerationIDPattern.MatchString(environment.RuntimeInstanceID) || !gatewayGenerationIDPattern.MatchString(environment.RuntimeID) || !gatewayGenerationIDPattern.MatchString(environment.RuntimeNetworkID) || environment.BindingVersion < 1 { return douyinGatewayRequest{}, errors.New("restricted browser is not ready") } return douyinGatewayRequest{BindingVersion: environment.BindingVersion, RuntimeID: environment.RuntimeID, NetworkID: environment.RuntimeNetworkID, NetworkExitID: environment.Exit.ID}, nil }