pcgamedb/utils/cf-clearance-scraper.go

148 lines
3.2 KiB
Go
Raw Normal View History

package utils
import (
"encoding/json"
"errors"
"time"
)
// https://github.com/ZFC-Digital/cf-clearance-scraper
type ccsRequest struct {
Url string `json:"url"`
Mode string `json:"mode"`
SiteKey string `json:"siteKey"`
}
type WAFSession struct {
Cookies []struct {
Name string `json:"name"`
Value string `json:"value"`
Domain string `json:"domain"`
Path string `json:"path"`
Expires float64 `json:"expires"`
Size int `json:"size"`
HTTPOnly bool `json:"httpOnly"`
Secure bool `json:"secure"`
Session bool `json:"session"`
SameSite string `json:"sameSite"`
Priority string `json:"priority"`
SameParty bool `json:"sameParty"`
SourceScheme string `json:"sourceScheme"`
PartitionKey string `json:"partitionKey"`
} `json:"cookies"`
Headers map[string]string `json:"headers"`
Code int `json:"code"`
}
func CCSWAFSession(ccsUrl string, requestUrl string) (*WAFSession, error) {
data := ccsRequest{
Url: requestUrl,
Mode: "waf-session",
}
resp, err := Fetch(FetchConfig{
Url: ccsUrl,
Method: "POST",
Data: data,
Timeout: 60 * time.Second,
})
if err != nil {
return nil, err
}
var response WAFSession
err = json.Unmarshal(resp.Data, &response)
if err != nil {
return nil, err
}
if response.Code != 200 {
return nil, errors.New("Failed to get WAF session")
}
return &response, nil
}
func CCSSource(ccsUrl string, requestUrl string) (string, error) {
data := ccsRequest{
Url: requestUrl,
Mode: "source",
}
resp, err := Fetch(FetchConfig{
Url: ccsUrl,
Method: "POST",
Data: data,
Timeout: 60 * time.Second,
})
if err != nil {
return "", err
}
type response struct {
Source string `json:"source"`
Code int `json:"code"`
}
var ccsResp response
err = json.Unmarshal(resp.Data, &ccsResp)
if err != nil {
return "", err
}
if ccsResp.Code != 200 {
return "", errors.New("Failed to get source")
}
return ccsResp.Source, nil
}
func CCSTurnstileToken(ccsUrl string, requestUrl string, siteKey string) (string, error) {
data := ccsRequest{
Url: requestUrl,
Mode: "turnstile-min",
SiteKey: siteKey,
}
resp, err := Fetch(FetchConfig{
Url: ccsUrl,
Method: "POST",
Data: data,
Timeout: 60 * time.Second,
})
if err != nil {
return "", err
}
var ccsResp struct {
Token string `json:"token"`
Code int `json:"code"`
}
err = json.Unmarshal(resp.Data, &ccsResp)
if err != nil {
return "", err
}
if ccsResp.Code != 200 {
return "", errors.New("Failed to get source")
}
return ccsResp.Token, nil
}
func CCSTurnstileMaxToken(ccsUrl string, requestUrl string) (string, error) {
data := ccsRequest{
Url: requestUrl,
Mode: "turnstile-max",
}
resp, err := Fetch(FetchConfig{
Url: ccsUrl,
Method: "POST",
Data: data,
Timeout: 60 * time.Second,
})
if err != nil {
return "", err
}
var ccsResp struct {
Token string `json:"token"`
Code int `json:"code"`
}
err = json.Unmarshal(resp.Data, &ccsResp)
if err != nil {
return "", err
}
if ccsResp.Code != 200 {
return "", errors.New("Failed to get source")
}
return ccsResp.Token, nil
}