Skip to content

Commit

Permalink
add endpoints for ip and captcha challenge as json
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Oct 1, 2022
1 parent 8f2cdcc commit 0be0742
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/ogamed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ func start(c *cli.Context) error {
// CAPTCHA Handler
e.GET("/bot/captcha", wrapper.GetCaptchaHandler)
e.POST("/bot/captcha/solve", wrapper.GetCaptchaSolverHandler)
e.GET("/bot/captcha/challenge", wrapper.GetCaptchaChallengeHandler)

e.GET("/bot/ip", wrapper.GetPublicIPHandler)
e.GET("/bot/server", wrapper.GetServerHandler)
e.GET("/bot/server-data", wrapper.GetServerDataHandler)
e.POST("/bot/set-user-agent", wrapper.SetUserAgentHandler)
Expand Down
40 changes: 40 additions & 0 deletions pkg/wrapper/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,3 +1405,43 @@ func GetCaptchaSolverHandler(c echo.Context) error {
}
return c.Redirect(http.StatusTemporaryRedirect, "/")
}

// CaptchaChallenge ...
type CaptchaChallenge struct {
ID string
Question string
Icons string
}

// GetCaptchaChallengeHandler ...
func GetCaptchaChallengeHandler(c echo.Context) error {
bot := c.Get("bot").(*OGame)
_, err := GFLogin(bot.client, bot.ctx, bot.lobby, bot.Username, bot.password, bot.otpSecret, "")
var captchaErr *CaptchaRequiredError
if errors.As(err, &captchaErr) {
questionRaw, iconsRaw, err := StartCaptchaChallenge(bot.GetClient(), bot.ctx, captchaErr.ChallengeID)
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResp(500, err.Error()))
}
questionB64 := base64.StdEncoding.EncodeToString(questionRaw)
iconsB64 := base64.StdEncoding.EncodeToString(iconsRaw)
return c.JSON(http.StatusOK, SuccessResp(CaptchaChallenge{
ID: captchaErr.ChallengeID,
Question: questionB64,
Icons: iconsB64,
}))
} else if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResp(500, err.Error()))
}
return c.JSON(http.StatusOK, SuccessResp(CaptchaChallenge{}))
}

// GetPublicIPHandler ...
func GetPublicIPHandler(c echo.Context) error {
bot := c.Get("bot").(*OGame)
ip, err := bot.GetPublicIP()
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResp(500, err.Error()))
}
return c.JSON(http.StatusOK, SuccessResp(ip))
}

0 comments on commit 0be0742

Please sign in to comment.