ccs/cf-clearance-scraper.go

181 lines
4.4 KiB
Go
Raw Permalink Normal View History

2024-12-02 00:38:45 -05:00
package ccs
2024-12-02 00:33:44 -05:00
import (
"encoding/json"
"errors"
"time"
"github.com/Danny-Dasilva/CycleTLS/cycletls"
"github.com/go-resty/resty/v2"
)
type Request struct {
Url string `json:"url"`
Mode string `json:"mode"`
SiteKey string `json:"siteKey"`
}
2024-12-04 08:25:31 -05:00
type Cookie 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"`
}
2024-12-02 00:33:44 -05:00
type Session struct {
2024-12-04 08:25:31 -05:00
Cookies []Cookie `json:"cookies"`
2024-12-02 00:33:44 -05:00
Headers map[string]string `json:"headers"`
Code int `json:"code"`
}
var (
httpClient *resty.Client
cycletlsClient cycletls.CycleTLS
)
func init() {
httpClient = resty.New()
2024-12-18 03:03:06 -05:00
httpClient.SetRetryCount(3).SetTimeout(10 * time.Second).SetRetryWaitTime(3 * time.Second)
2024-12-02 00:33:44 -05:00
cycletlsClient = cycletls.Init()
}
func WAFSession(ccsUrl string, requestUrl string) (Session, error) {
data := Request{
Url: requestUrl,
Mode: "waf-session",
}
resp, err := httpClient.SetTimeout(60 * time.Second).R().SetBody(data).Post(ccsUrl)
if err != nil {
return Session{}, err
}
var response Session
err = json.Unmarshal(resp.Body(), &response)
if err != nil {
return Session{}, err
}
if response.Code != 200 {
return Session{}, errors.New("Failed to get WAF session")
}
return response, nil
}
func Source(ccsUrl string, requestUrl string) ([]byte, error) {
data := Request{
Url: requestUrl,
Mode: "source",
}
resp, err := httpClient.SetTimeout(60 * time.Second).R().SetBody(data).Post(ccsUrl)
if err != nil {
return nil, err
}
type response struct {
Source string `json:"source"`
Code int `json:"code"`
}
var ccsResp response
err = json.Unmarshal(resp.Body(), &ccsResp)
if err != nil {
return nil, err
}
if ccsResp.Code != 200 {
return nil, errors.New("Failed to get source")
}
return []byte(ccsResp.Source), nil
}
func TurnstileToken(ccsUrl string, requestUrl string, siteKey string) (string, error) {
data := Request{
Url: requestUrl,
Mode: "turnstile-min",
SiteKey: siteKey,
}
resp, err := httpClient.SetTimeout(60 * time.Second).R().SetBody(data).Post(ccsUrl)
if err != nil {
return "", err
}
var ccsResp struct {
Token string `json:"token"`
Code int `json:"code"`
}
err = json.Unmarshal(resp.Body(), &ccsResp)
if err != nil {
return "", err
}
if ccsResp.Code != 200 {
return "", errors.New("Failed to get source")
}
return ccsResp.Token, nil
}
func TurnstileMaxToken(ccsUrl string, requestUrl string) (string, error) {
data := Request{
Url: requestUrl,
Mode: "turnstile-max",
}
resp, err := httpClient.SetTimeout(60 * time.Second).R().SetBody(data).Post(ccsUrl)
if err != nil {
return "", err
}
var ccsResp struct {
Token string `json:"token"`
Code int `json:"code"`
}
err = json.Unmarshal(resp.Body(), &ccsResp)
if err != nil {
return "", err
}
if ccsResp.Code != 200 {
return "", errors.New("Failed to get source")
}
return ccsResp.Token, nil
}
2024-12-03 10:50:17 -05:00
func RequestWithWAFSession(method string, URL string, wafSession Session, options *cycletls.Options) (cycletls.Response, error) {
2024-12-03 10:54:27 -05:00
if options == nil {
options = &cycletls.Options{}
}
2024-12-03 10:47:30 -05:00
headers := map[string]string{}
2024-12-02 00:33:44 -05:00
for key, value := range wafSession.Headers {
headers[key] = value
}
2024-12-03 10:47:30 -05:00
options.Ja3 = "772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,23-27-65037-43-51-45-16-11-13-17513-5-18-65281-0-10-35,25497-29-23-24,0"
options.UserAgent = headers["user-agent"]
2024-12-03 10:56:55 -05:00
if options.Headers == nil {
options.Headers = map[string]string{}
}
2024-12-03 10:47:30 -05:00
for key, value := range headers {
options.Headers[key] = value
2024-12-02 00:33:44 -05:00
}
2024-12-13 08:04:42 -05:00
if options.Cookies == nil {
options.Cookies = []cycletls.Cookie{}
}
for _, cookie := range wafSession.Cookies {
options.Cookies = append(options.Cookies, cycletls.Cookie{
Name: cookie.Name,
Value: cookie.Value,
})
}
2024-12-04 08:50:23 -05:00
resp, err := cycletlsClient.Do(URL, *options, method)
if err != nil {
return resp, err
}
if resp.Status == 403 {
return resp, errors.New("Cloudflare blocked request")
}
return resp, nil
2024-12-02 00:33:44 -05:00
}