Feat(Captcha): Initial captcha solver
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good

This commit is contained in:
2023-10-05 16:18:35 +08:00
parent e403ed6767
commit 828571312b
7 changed files with 240 additions and 5 deletions

View File

@@ -0,0 +1 @@
package captchasolver

View File

@@ -0,0 +1,103 @@
package captchasolver
import (
"io"
"log"
"net/http"
"time"
)
func Provider2CaptchaCreateCaptchaRequestV3(apiKey string, url string, googleKey string, googleAction string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://2captcha.com/in.php", nil)
if err != nil {
log.Print(err)
return ""
}
q := req.URL.Query()
q.Add("key", apiKey)
q.Add("method", "userrecaptcha")
q.Add("version", "3")
q.Add("action", googleAction)
q.Add("min_score", "0.3")
q.Add("googlekey", googleKey)
q.Add("pageurl", url)
req.URL.RawQuery = q.Encode()
res, err := client.Do(req)
if err != nil {
log.Print(err)
return ""
}
respBody, _ := io.ReadAll(res.Body)
respBodyStr := string(respBody)
return respBodyStr[3:]
}
func Provider2CaptchaCreateCaptchaRequestV2(apiKey string, url string, googleKey string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", "https://2captcha.com/in.php", nil)
if err != nil {
log.Print(err)
return ""
}
q := req.URL.Query()
q.Add("key", apiKey)
q.Add("method", "userrecaptcha")
q.Add("googlekey", googleKey)
q.Add("pageurl", url)
req.URL.RawQuery = q.Encode()
res, err := client.Do(req)
if err != nil {
log.Print(err)
return ""
}
respBody, _ := io.ReadAll(res.Body)
respBodyStr := string(respBody)
return respBodyStr[3:]
}
func Provider2CaptchaGetCaptchaResult(apiKey string, captchaID string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", "http://2captcha.com/res.php", nil)
if err != nil {
log.Print(err)
return ""
}
q := req.URL.Query()
q.Add("key", apiKey)
q.Add("action", "get")
q.Add("id", captchaID)
req.URL.RawQuery = q.Encode()
res, err := client.Do(req)
if err != nil {
log.Print(err)
return ""
}
respBody, _ := io.ReadAll(res.Body)
respBodyStr := string(respBody)
return respBodyStr
}
func Provider2CaptchaV2E2E(apiKey string, url string, dataSiteKey string) string {
captchaReqID := Provider2CaptchaCreateCaptchaRequestV2(apiKey, url, dataSiteKey)
log.Println(captchaReqID)
captchaAnswer := "CAPCHA_NOT_READY"
for captchaAnswer == "CAPCHA_NOT_READY" {
time.Sleep(500 * time.Millisecond)
captchaAnswer = Provider2CaptchaGetCaptchaResult(apiKey, captchaReqID)
}
log.Printf("Captcha ID: %s; Captcha Answer: %s\n", captchaReqID, captchaAnswer)
captchaAnswer = captchaAnswer[3:]
return captchaAnswer
}

View File

@@ -0,0 +1,102 @@
package captchasolver
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
func ProviderDeathByCaptchaCreateCaptchaRequestV2(deathByCaptchaUsername string, deathByCaptchaPassword string, captchaUrl string, googleKey string) string {
httpClient := http.Client{}
formValues := url.Values{
"username": {deathByCaptchaUsername},
"password": {deathByCaptchaPassword},
"type": {"4"},
"token_params": {fmt.Sprintf(`{"proxy": "", "proxytype": "", "googlekey": "%s","pageurl": "%s"}`, googleKey, captchaUrl)},
}
req, err := http.NewRequest("POST", "http://api.dbcapi.me/api/captcha", strings.NewReader(formValues.Encode()))
if err != nil {
log.Print(err)
return ""
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Accept", "application/json")
req.Header.Add("Expect", "")
resp, err := httpClient.Do(req)
if err != nil {
log.Print(err)
return ""
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Print(err)
return ""
}
var resBodyJson map[string]any
err = json.Unmarshal(respBody, &resBodyJson)
if err != nil {
log.Print(err)
return ""
}
if _, exists := resBodyJson["captcha"]; !exists {
return ""
}
captchaIDFloat := resBodyJson["captcha"].(float64)
captchaID := int(captchaIDFloat)
captchaIDStr := fmt.Sprintf("%d", captchaID)
return captchaIDStr
}
func ProviderDeathByCaptchaGetCaptchaResult(captchaID string) string {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("http://api.dbcapi.me/api/captcha/%s", captchaID), nil)
if err != nil {
log.Print(err)
return ""
}
req.Header.Add("Accept", "application/json")
res, err := client.Do(req)
if err != nil {
log.Print(err)
return ""
}
respBody, _ := io.ReadAll(res.Body)
var resBodyJson map[string]any
err = json.Unmarshal(respBody, &resBodyJson)
if err != nil {
log.Print(err)
return ""
}
if _, exists := resBodyJson["text"]; !exists {
return ""
}
return resBodyJson["text"].(string)
}
func ProviderDeathByCaptchaV2E2E(deathByCaptchaUsername string, deathByCaptchaPassword string, url string, dataSiteKey string) string {
captchaReqID := ProviderDeathByCaptchaCreateCaptchaRequestV2(deathByCaptchaUsername, deathByCaptchaPassword, url, dataSiteKey)
log.Println(captchaReqID)
captchaAnswer := ""
for captchaAnswer == "" {
time.Sleep(500 * time.Millisecond)
captchaAnswer = ProviderDeathByCaptchaGetCaptchaResult(captchaReqID)
}
log.Printf("Captcha ID: %s; Captcha Answer: %s\n", captchaReqID, captchaAnswer)
return captchaAnswer
}