Feat(Captcha): Initial captcha solver
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.samuelpua.com/telboon/ktm-train-bot/backend/internal/captchasolver"
|
||||
"git.samuelpua.com/telboon/ktm-train-bot/backend/internal/user"
|
||||
"github.com/go-rod/rod"
|
||||
"github.com/go-rod/rod/lib/devices"
|
||||
@@ -38,6 +39,12 @@ func (env *Env) BackgroundJobRunner() {
|
||||
},
|
||||
)
|
||||
|
||||
forceStartBookingString := os.Getenv("FORCE_START_BOOKING")
|
||||
forceStartBooking := false
|
||||
if strings.ToUpper(forceStartBookingString) == "TRUE" {
|
||||
forceStartBooking = true
|
||||
}
|
||||
|
||||
tx := env.DB.Session(&gorm.Session{Logger: newLogger})
|
||||
for {
|
||||
|
||||
@@ -80,7 +87,7 @@ func (env *Env) BackgroundJobRunner() {
|
||||
startTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 00, 10, 0, 0, timeNow.Location())
|
||||
endTime := startTime.Add(15 * time.Minute)
|
||||
|
||||
if timeNow.After(startTime) && timeNow.Before(endTime) {
|
||||
if forceStartBooking || (timeNow.After(startTime) && timeNow.Before(endTime)) {
|
||||
err := env.DB.Where(&user.Profile{UserID: jobToDo.UserID}).First(&jobToDo.User.Profile).Error
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -425,6 +432,8 @@ func getBookingSlots(browser *rod.Browser, onwardDate string, reverse bool) *rod
|
||||
|
||||
func selectBookingSlot(ctx context.Context, page *rod.Page, timeCode string) *rod.Page {
|
||||
time.Sleep(5 * time.Second)
|
||||
twoCaptchaAPIKey := os.Getenv("TWOCAPTCHA_API_KEY")
|
||||
|
||||
// Initial closing of maintenance modal
|
||||
bodyText := page.MustElement("body").MustText()
|
||||
if strings.Contains(bodyText, "System maintenance scheduled at 23:00 to 00:15 (UTC+8)") {
|
||||
@@ -491,6 +500,22 @@ func selectBookingSlot(ctx context.Context, page *rod.Page, timeCode string) *ro
|
||||
time.Sleep(1000 * time.Millisecond)
|
||||
}
|
||||
|
||||
// Check if there is captcha
|
||||
if strings.Contains(bodyText, "Please complete the reCAPTCHA") {
|
||||
// Reset Body text
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
log.Println("Captcha detected")
|
||||
currURL := "https://shuttleonline.ktmb.com.my/ShuttleTrip"
|
||||
// Regex research for Google Captcha V2 API Key
|
||||
gcaptchaElement := page.MustElement(".g-recaptcha")
|
||||
googleRecaptchaKey := gcaptchaElement.MustAttribute("data-sitekey")
|
||||
log.Printf("Google CAPTCHA V2 Key: %s", *googleRecaptchaKey)
|
||||
captchaAnswer := captchasolver.Provider2CaptchaV2E2E(twoCaptchaAPIKey, currURL, *googleRecaptchaKey)
|
||||
page.MustElement("#g-recaptcha-response").Eval(`this.innerHTML = "` + captchaAnswer + `"`)
|
||||
page.Eval(`RecaptchaCallback()`)
|
||||
}
|
||||
|
||||
// Repeat if there's no seats -- seats might be released later
|
||||
if strings.Contains(bodyText, "Not enough seat for onward trip") {
|
||||
completed = false
|
||||
|
||||
Reference in New Issue
Block a user