Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alaingilbert committed Aug 21, 2022
1 parent 86a7322 commit fab0090
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
40 changes: 20 additions & 20 deletions pkg/wrapper/client.go → pkg/httpclient/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wrapper
package httpclient

import (
"bytes"
Expand All @@ -16,8 +16,8 @@ type IHttpClient interface {
Get(url string) (*http.Response, error)
}

// OGameClient ...
type OGameClient struct {
// Client ...
type Client struct {
sync.Mutex
*http.Client
userAgent string
Expand All @@ -29,21 +29,21 @@ type OGameClient struct {
bytesUploaded int64
}

func (c *OGameClient) BytesDownloaded() int64 {
func (c *Client) BytesDownloaded() int64 {
c.Lock()
defer c.Unlock()
return c.bytesDownloaded
}

func (c *OGameClient) BytesUploaded() int64 {
func (c *Client) BytesUploaded() int64 {
c.Lock()
defer c.Unlock()
return c.bytesUploaded
}

// NewOGameClient ...
func NewOGameClient() *OGameClient {
client := &OGameClient{
// NewClient ...
func NewClient() *Client {
client := &Client{
Client: &http.Client{
Timeout: 30 * time.Second,
},
Expand All @@ -65,11 +65,11 @@ func NewOGameClient() *OGameClient {
}

// SetMaxRPS ...
func (c *OGameClient) SetMaxRPS(maxRPS int32) {
func (c *Client) SetMaxRPS(maxRPS int32) {
atomic.StoreInt32(&c.maxRPS, maxRPS)
}

func (c *OGameClient) incrRPS() {
func (c *Client) incrRPS() {
newRPS := atomic.AddInt32(&c.rpsCounter, 1)
maxRPS := atomic.LoadInt32(&c.maxRPS)
if maxRPS > 0 && newRPS > maxRPS {
Expand All @@ -79,11 +79,11 @@ func (c *OGameClient) incrRPS() {
}
}

func (c *OGameClient) Get(url string) (*http.Response, error) {
func (c *Client) Get(url string) (*http.Response, error) {
return c.get(url)
}

func (c *OGameClient) get(url string) (*http.Response, error) {
func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
Expand All @@ -92,11 +92,11 @@ func (c *OGameClient) get(url string) (*http.Response, error) {
}

// Do executes a request
func (c *OGameClient) Do(req *http.Request) (*http.Response, error) {
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.do(req)
}

func (c *OGameClient) do(req *http.Request) (*http.Response, error) {
func (c *Client) do(req *http.Request) (*http.Response, error) {
c.incrRPS()
req.Header.Add("User-Agent", c.userAgent)
resp, err := c.Client.Do(req)
Expand All @@ -112,7 +112,7 @@ func (c *OGameClient) do(req *http.Request) (*http.Response, error) {
return resp, err
}

func (c *OGameClient) WithTransport(tr http.RoundTripper, clb func(IHttpClient) error) error {
func (c *Client) WithTransport(tr http.RoundTripper, clb func(IHttpClient) error) error {
c.Lock()
defer c.Unlock()
if tr != nil {
Expand All @@ -123,31 +123,31 @@ func (c *OGameClient) WithTransport(tr http.RoundTripper, clb func(IHttpClient)
return clb(c)
}

func (c *OGameClient) SetTransport(tr http.RoundTripper) {
func (c *Client) SetTransport(tr http.RoundTripper) {
c.Lock()
defer c.Unlock()
c.Transport = tr
}

func (c *OGameClient) UserAgent() string {
func (c *Client) UserAgent() string {
c.Lock()
defer c.Unlock()
return c.userAgent
}

func (c *OGameClient) SetUserAgent(userAgent string) {
func (c *Client) SetUserAgent(userAgent string) {
c.Lock()
defer c.Unlock()
c.userAgent = userAgent
}

// FakeDo for testing purposes
func (c *OGameClient) FakeDo() {
func (c *Client) FakeDo() {
c.incrRPS()
fmt.Println("FakeDo")
}

// GetRPS gets the current client RPS
func (c *OGameClient) GetRPS() int32 {
func (c *Client) GetRPS() int32 {
return atomic.LoadInt32(&c.rps)
}
6 changes: 3 additions & 3 deletions pkg/wrapper/client_test.go → pkg/httpclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wrapper
package httpclient

import (
"bytes"
Expand All @@ -18,7 +18,7 @@ func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
}

func TestOgameClient_Do(t *testing.T) {
c := OGameClient{userAgent: "test", Client: &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
c := Client{userAgent: "test", Client: &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
// Test request parameters
return &http.Response{
StatusCode: 200,
Expand All @@ -35,7 +35,7 @@ func TestOgameClient_Do(t *testing.T) {
}

func TestOgameClient_SetUserAgent(t *testing.T) {
c := OGameClient{userAgent: "test", Client: &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
c := Client{userAgent: "test", Client: &http.Client{Transport: RoundTripFunc(func(req *http.Request) *http.Response {
// Test request parameters
return &http.Response{
StatusCode: 200,
Expand Down
21 changes: 11 additions & 10 deletions pkg/wrapper/gameforge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"github.com/alaingilbert/ogame/pkg/httpclient"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/utils"
"github.com/pquerna/otp"
Expand Down Expand Up @@ -112,7 +113,7 @@ func Register(client *http.Client, ctx context.Context, lobby, email, password,
}

// ValidateAccount validate a gameforge account
func ValidateAccount(client IHttpClient, ctx context.Context, lobby, code string) error {
func ValidateAccount(client httpclient.IHttpClient, ctx context.Context, lobby, code string) error {
if len(code) != 36 {
return errors.New("invalid validation code")
}
Expand Down Expand Up @@ -208,7 +209,7 @@ type AddAccountRes struct {

func (r AddAccountRes) GetBearerToken() string { return r.BearerToken }

func AddAccount(client IHttpClient, ctx context.Context, lobby, accountGroup, sessionToken string) (*AddAccountRes, error) {
func AddAccount(client httpclient.IHttpClient, ctx context.Context, lobby, accountGroup, sessionToken string) (*AddAccountRes, error) {
var payload struct {
AccountGroup string `json:"accountGroup"`
Locale string `json:"locale"`
Expand Down Expand Up @@ -262,7 +263,7 @@ type GFLoginRes struct {

func (r GFLoginRes) GetBearerToken() string { return r.Token }

func GFLogin(client IHttpClient, ctx context.Context, lobby, username, password, otpSecret, challengeID string) (out *GFLoginRes, err error) {
func GFLogin(client httpclient.IHttpClient, ctx context.Context, lobby, username, password, otpSecret, challengeID string) (out *GFLoginRes, err error) {
gameEnvironmentID, platformGameID, err := getConfiguration(client, ctx, lobby)
if err != nil {
return out, err
Expand Down Expand Up @@ -319,7 +320,7 @@ func GFLogin(client IHttpClient, ctx context.Context, lobby, username, password,
return out, nil
}

func getConfiguration(client IHttpClient, ctx context.Context, lobby string) (string, string, error) {
func getConfiguration(client httpclient.IHttpClient, ctx context.Context, lobby string) (string, string, error) {
ogURL := "https://" + lobby + ".ogame.gameforge.com/config/configuration.js"
req, err := http.NewRequest(http.MethodGet, ogURL, nil)
if err != nil {
Expand Down Expand Up @@ -392,7 +393,7 @@ func postSessionsReq(gameEnvironmentID, platformGameID, username, password, otpS
return req, nil
}

func StartCaptchaChallenge(client IHttpClient, ctx context.Context, challengeID string) (questionRaw, iconsRaw []byte, err error) {
func StartCaptchaChallenge(client httpclient.IHttpClient, ctx context.Context, challengeID string) (questionRaw, iconsRaw []byte, err error) {
req, err := http.NewRequest(http.MethodGet, "https://challenge.gameforge.com/challenge/"+challengeID, nil)
if err != nil {
return
Expand Down Expand Up @@ -445,7 +446,7 @@ func StartCaptchaChallenge(client IHttpClient, ctx context.Context, challengeID
return
}

func SolveChallenge(client IHttpClient, ctx context.Context, challengeID string, answer int64) error {
func SolveChallenge(client httpclient.IHttpClient, ctx context.Context, challengeID string, answer int64) error {
challengeURL := "https://image-drop-challenge.gameforge.com/challenge/" + challengeID + "/en-GB"
body := strings.NewReader(`{"answer":` + utils.FI64(answer) + `}`)
req, _ := http.NewRequest(http.MethodPost, challengeURL, body)
Expand Down Expand Up @@ -493,7 +494,7 @@ type Server struct {
}
}

func GetServers(lobby string, client IHttpClient, ctx context.Context) ([]Server, error) {
func GetServers(lobby string, client httpclient.IHttpClient, ctx context.Context) ([]Server, error) {
var servers []Server
req, err := http.NewRequest(http.MethodGet, "https://"+lobby+".ogame.gameforge.com/api/servers", nil)
if err != nil {
Expand Down Expand Up @@ -557,7 +558,7 @@ type ServerData struct {
}

// GetServerData gets the server data from xml api
func GetServerData(client IHttpClient, ctx context.Context, serverNumber int64, serverLang string) (ServerData, error) {
func GetServerData(client httpclient.IHttpClient, ctx context.Context, serverNumber int64, serverLang string) (ServerData, error) {
var serverData ServerData
req, err := http.NewRequest(http.MethodGet, "https://s"+utils.FI64(serverNumber)+"-"+serverLang+".ogame.gameforge.com/api/serverData.xml", nil)
if err != nil {
Expand Down Expand Up @@ -601,7 +602,7 @@ type Account struct {
}
}

func GetUserAccounts(client IHttpClient, ctx context.Context, lobby, bearerToken string) ([]Account, error) {
func GetUserAccounts(client httpclient.IHttpClient, ctx context.Context, lobby, bearerToken string) ([]Account, error) {
var userAccounts []Account
req, err := http.NewRequest(http.MethodGet, "https://"+lobby+".ogame.gameforge.com/api/users/me/accounts", nil)
if err != nil {
Expand All @@ -625,7 +626,7 @@ func GetUserAccounts(client IHttpClient, ctx context.Context, lobby, bearerToken
return userAccounts, nil
}

func GetLoginLink(client IHttpClient, ctx context.Context, lobby string, userAccount Account, bearerToken string) (string, error) {
func GetLoginLink(client httpclient.IHttpClient, ctx context.Context, lobby string, userAccount Account, bearerToken string) (string, error) {
ogURL := fmt.Sprintf("https://%s.ogame.gameforge.com/api/users/me/loginLink?id=%d&server[language]=%s&server[number]=%d&clickedButton=account_list",
lobby, userAccount.ID, userAccount.Server.Language, userAccount.Server.Number)
req, err := http.NewRequest(http.MethodGet, ogURL, nil)
Expand Down
5 changes: 3 additions & 2 deletions pkg/wrapper/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wrapper
import (
"crypto/tls"
"github.com/alaingilbert/ogame/pkg/extractor"
"github.com/alaingilbert/ogame/pkg/httpclient"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/taskRunner"
"net/http"
Expand Down Expand Up @@ -158,7 +159,7 @@ type Wrapper interface {
GetCachedPlanets() []Planet
GetCachedPlayer() ogame.UserInfos
GetCachedPreferences() ogame.Preferences
GetClient() *OGameClient
GetClient() *httpclient.Client
GetExtractor() extractor.Extractor
GetLanguage() string
GetNbSystems() int64
Expand Down Expand Up @@ -194,7 +195,7 @@ type Wrapper interface {
RemoveWSCallback(string)
ServerURL() string
ServerVersion() string
SetClient(*OGameClient)
SetClient(*httpclient.Client)
SetGetServerDataWrapper(func(func() (ServerData, error)) (ServerData, error))
SetLoginWrapper(func(func() (bool, error)) error)
SetOGameCredentials(username, password, otpSecret, bearerToken string)
Expand Down
21 changes: 11 additions & 10 deletions pkg/wrapper/ogame.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/alaingilbert/ogame/pkg/extractor/v8"
"github.com/alaingilbert/ogame/pkg/extractor/v874"
"github.com/alaingilbert/ogame/pkg/extractor/v9"
"github.com/alaingilbert/ogame/pkg/httpclient"
"github.com/alaingilbert/ogame/pkg/ogame"
"github.com/alaingilbert/ogame/pkg/parser"
"github.com/alaingilbert/ogame/pkg/taskRunner"
Expand Down Expand Up @@ -85,7 +86,7 @@ type OGame struct {
serverData ServerData
location *time.Location
serverURL string
client *OGameClient
client *httpclient.Client
logger *log.Logger
chatCallbacks []func(msg ogame.ChatMsg)
wsCallbacks map[string]func(msg []byte)
Expand Down Expand Up @@ -136,7 +137,7 @@ type Params struct {
Lobby string
APINewHostname string
CookiesFilename string
Client *OGameClient
Client *httpclient.Client
CaptchaCallback CaptchaCallback
}

Expand All @@ -158,7 +159,7 @@ func GetClientWithProxy(proxyAddr, proxyUsername, proxyPassword, proxyType strin
}

func (b *OGame) validateAccount(code string) error {
return b.client.WithTransport(b.loginProxyTransport, func(client IHttpClient) error {
return b.client.WithTransport(b.loginProxyTransport, func(client httpclient.IHttpClient) error {
return ValidateAccount(client, b.ctx, b.lobby, code)
})
}
Expand Down Expand Up @@ -204,7 +205,7 @@ func NewWithParams(params Params) (*OGame, error) {
}

// NewNoLogin does not auto login.
func NewNoLogin(username, password, otpSecret, bearerToken, universe, lang, cookiesFilename string, playerID int64, client *OGameClient) (*OGame, error) {
func NewNoLogin(username, password, otpSecret, bearerToken, universe, lang, cookiesFilename string, playerID int64, client *httpclient.Client) (*OGame, error) {
b := new(OGame)
b.getServerDataWrapper = DefaultGetServerDataWrapper
b.loginWrapper = DefaultLoginWrapper
Expand Down Expand Up @@ -237,7 +238,7 @@ func NewNoLogin(username, password, otpSecret, bearerToken, universe, lang, cook
}
}

b.client = NewOGameClient()
b.client = httpclient.NewClient()
b.client.Jar = jar
b.client.SetUserAgent(defaultUserAgent)
} else {
Expand Down Expand Up @@ -468,7 +469,7 @@ func NinjaSolver(apiKey string) CaptchaCallback {
}

func postSessions(b *OGame, lobby, username, password, otpSecret string) (out *GFLoginRes, err error) {
if err := b.client.WithTransport(b.loginProxyTransport, func(client IHttpClient) error {
if err := b.client.WithTransport(b.loginProxyTransport, func(client httpclient.IHttpClient) error {
var challengeID string
tried := false
for {
Expand Down Expand Up @@ -840,7 +841,7 @@ func (b *OGame) SetLoginWrapper(newWrapper func(func() (bool, error)) error) {
// execute a request using the login proxy transport if set
func (b *OGame) doReqWithLoginProxyTransport(req *http.Request) (resp *http.Response, err error) {
req = req.WithContext(b.ctx)
_ = b.client.WithTransport(b.loginProxyTransport, func(client IHttpClient) error {
_ = b.client.WithTransport(b.loginProxyTransport, func(client httpclient.IHttpClient) error {
resp, err = client.Do(req)
return nil
})
Expand Down Expand Up @@ -4029,17 +4030,17 @@ func (b *OGame) IsConnected() bool {
}

// GetClient get the http client used by the bot
func (b *OGame) GetClient() *OGameClient {
func (b *OGame) GetClient() *httpclient.Client {
return b.client
}

// SetClient set the http client used by the bot
func (b *OGame) SetClient(client *OGameClient) {
func (b *OGame) SetClient(client *httpclient.Client) {
b.client = client
}

// GetLoginClient get the http client used by the bot for login operations
func (b *OGame) GetLoginClient() *OGameClient {
func (b *OGame) GetLoginClient() *httpclient.Client {
return b.client
}

Expand Down

0 comments on commit fab0090

Please sign in to comment.