Skip to content

Commit

Permalink
simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 7, 2022
1 parent 3b63195 commit a3ace67
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
36 changes: 2 additions & 34 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down Expand Up @@ -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())
Expand Down
10 changes: 7 additions & 3 deletions ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit a3ace67

Please sign in to comment.