From a3ace678d699d1347ef83b8c9cf94cb7a372d281 Mon Sep 17 00:00:00 2001 From: Alain Gilbert Date: Sat, 6 Aug 2022 23:24:35 -0700 Subject: [PATCH] simplify code --- handlers.go | 36 ++---------------------------------- ogame.go | 10 +++++++--- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/handlers.go b/handlers.go index 99d69dd6..81db2ed2 100644 --- a/handlers.go +++ b/handlers.go @@ -4,17 +4,12 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/labstack/echo" "io" "net/http" "net/url" "strconv" "strings" - "time" - - "github.com/pquerna/otp" - "github.com/pquerna/otp/totp" - - "github.com/labstack/echo" ) // APIResp ... @@ -1320,38 +1315,11 @@ func GetCaptchaHandler(c echo.Context) error { return c.HTML(http.StatusOK, err.Error()) } - //var out postSessionsResponse - payload := url.Values{ - "autoGameAccountCreation": {"false"}, - "gameEnvironmentId": {gameEnvironmentID}, - "platformGameId": {platformGameID}, - "gfLang": {"en"}, - "locale": {"en_GB"}, - "identity": {bot.Username}, - "password": {bot.password}, - } - req, err := http.NewRequest("POST", "https://gameforge.com/api/v1/auth/thin/sessions", strings.NewReader(payload.Encode())) + req, err := postSessionsReq(gameEnvironmentID, platformGameID, bot.Username, bot.password, bot.otpSecret, "") if err != nil { return c.HTML(http.StatusOK, err.Error()) } - if bot.otpSecret != "" { - passcode, err := totp.GenerateCodeCustom(bot.otpSecret, time.Now(), totp.ValidateOpts{ - Period: 30, - Skew: 1, - Digits: otp.DigitsSix, - Algorithm: otp.AlgorithmSHA1, - }) - if err != nil { - return c.HTML(http.StatusOK, err.Error()) - } - req.Header.Add("tnt-2fa-code", passcode) - req.Header.Add("tnt-installation-id", "") - } - - req.Header.Add("Content-Type", "application/x-www-form-urlencoded") - req.Header.Add("Accept-Encoding", "gzip, deflate, br") - resp, err := bot.doReqWithLoginProxyTransport(req) if err != nil { return c.HTML(http.StatusOK, err.Error()) diff --git a/ogame.go b/ogame.go index 493657c5..4e8a3bc0 100644 --- a/ogame.go +++ b/ogame.go @@ -1107,7 +1107,7 @@ func solveChallenge(client IHttpClient, challengeID string, answer int64) error return nil } -func postSessionsReq(client IHttpClient, gameEnvironmentID, platformGameID, username, password, otpSecret, challengeID string) (*http.Response, error) { +func postSessionsReq(gameEnvironmentID, platformGameID, username, password, otpSecret, challengeID string) (*http.Request, error) { payload := url.Values{ "autoGameAccountCreation": {"false"}, "gameEnvironmentId": {gameEnvironmentID}, @@ -1141,7 +1141,7 @@ func postSessionsReq(client IHttpClient, gameEnvironmentID, platformGameID, user req.Header.Add("Content-Type", "application/x-www-form-urlencoded") req.Header.Add("Accept-Encoding", "gzip, deflate, br") - return client.Do(req) + return req, nil } type CaptchaRequiredError struct { @@ -1158,7 +1158,11 @@ func (e CaptchaRequiredError) Error() string { func postSessions2(client IHttpClient, gameEnvironmentID, platformGameID, username, password, otpSecret string) (postSessionsResponse, error) { var out postSessionsResponse - resp, err := postSessionsReq(client, gameEnvironmentID, platformGameID, username, password, otpSecret, "") + req, err := postSessionsReq(gameEnvironmentID, platformGameID, username, password, otpSecret, "") + if err != nil { + return out, err + } + resp, err := client.Do(req) if err != nil { return out, err }