Skip to content

Commit

Permalink
login fix part alaingilbert#2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xE232FE committed Apr 14, 2023
1 parent ae9ebb4 commit b4304e1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/ogamed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

"github.com/alaingilbert/ogame/pkg/device"
"github.com/alaingilbert/ogame/pkg/tlsclientconfig"
"github.com/alaingilbert/ogame/pkg/wrapper"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
Expand Down Expand Up @@ -213,6 +214,12 @@ func main() {
Value: "",
EnvVars: []string{"NJA_API_KEY"},
},
// cookies-filename DEPRECATED Only for compatibility TBot this parameter have to be removed soon!
&cli.StringFlag{
Name: "cookies-filename",
Usage: "[DEPRECATED] Path to Cookie File",
EnvVars: []string{"OGAMED_COOKIEFILENAME"},
},
}
app.Action = start
if err := app.Run(os.Args); err != nil {
Expand Down Expand Up @@ -303,6 +310,8 @@ func start(c *cli.Context) error {
panic(err)
}

tlsclientconfig.AddRoundTripper(deviceInst.GetClient().Transport)

params := wrapper.Params{
Device: deviceInst,
Universe: universe,
Expand Down
67 changes: 67 additions & 0 deletions pkg/tlsclientconfig/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tlsclientconfig

import (
"crypto/tls"
"net/http"
)

type OwnRoundTripper struct {
inner http.RoundTripper
options Options
}

type Options struct {
AddMissingHeaders bool
Headers map[string]string
}

func AddRoundTripper(inner http.RoundTripper, options ...Options) http.RoundTripper {
if trans, ok := inner.(*http.Transport); ok {
trans.TLSClientConfig = getTLSConfiguration()
}
roundTripper := &OwnRoundTripper{
inner: inner,
}
if options != nil {
roundTripper.options = options[0]
} else {
roundTripper.options = GetDefaultOptions()
}
return roundTripper
}

func (ug *OwnRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
if ug.options.AddMissingHeaders {
for header, value := range ug.options.Headers {
if _, ok := r.Header[header]; !ok {
r.Header.Set(header, value)
}
}
}
if ug.inner == nil {
return (&http.Transport{
TLSClientConfig: getTLSConfiguration(),
}).RoundTrip(r)
}
return ug.inner.RoundTrip(r)
}

func getTLSConfiguration() *tls.Config {
return &tls.Config{
CipherSuites: []uint16{tls.TLS_AES_128_GCM_SHA256, tls.TLS_CHACHA20_POLY1305_SHA256, tls.TLS_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
},
CurvePreferences: []tls.CurveID{tls.CurveP256, tls.CurveP384, tls.CurveP521, tls.X25519},
}
}

func GetDefaultOptions() Options {
return Options{
AddMissingHeaders: true,
Headers: map[string]string{
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0",
},
}
}

0 comments on commit b4304e1

Please sign in to comment.