Răsfoiți Sursa

feat(config): add user_agent field (#486)

Co-authored-by: ayn2op <ayn2op@mac.local>
Ayyan 1 an în urmă
părinte
comite
f2e8a05bac
3 a modificat fișierele cu 40 adăugiri și 32 ștergeri
  1. 10 13
      cmd/state.go
  2. 28 19
      internal/config/config.go
  3. 2 0
      internal/login/form.go

+ 10 - 13
cmd/state.go

@@ -16,19 +16,6 @@ import (
 	"github.com/rivo/tview"
 )
 
-const userAgent = config.Name + "/0.1 (https://github.com/diamondburned/arikawa, v3)"
-
-func init() {
-	api.UserAgent = userAgent
-	gateway.DefaultIdentity = gateway.IdentifyProperties{
-		OS:     runtime.GOOS,
-		Device: "",
-
-		Browser:          config.Name,
-		BrowserUserAgent: userAgent,
-	}
-}
-
 type State struct {
 	*ningen.State
 	cfg *config.Config
@@ -36,6 +23,16 @@ type State struct {
 }
 
 func openState(token string, app *tview.Application, cfg *config.Config) error {
+	api.UserAgent = cfg.UserAgent
+	gateway.DefaultIdentity = gateway.IdentifyProperties{
+		OS:     runtime.GOOS,
+		Device: "",
+
+		Browser:          cfg.Browser,
+		BrowserVersion:   cfg.BrowserVersion,
+		BrowserUserAgent: cfg.UserAgent,
+	}
+
 	discordState = &State{
 		State: ningen.New(token),
 		cfg:   cfg,

+ 28 - 19
internal/config/config.go

@@ -9,35 +9,44 @@ import (
 	"github.com/BurntSushi/toml"
 )
 
-const Name = "discordo"
+const (
+	Name = "discordo"
+
+	Browser        = "Chrome"
+	BrowserVersion = "132.0.0.0"
+	UserAgent      = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) " + Browser + "/" + BrowserVersion + " Safari/537.36"
+)
 
 type Config struct {
-	Mouse            bool `toml:"mouse"`
-	HideBlockedUsers bool `toml:"hide_blocked_users"`
+	Mouse               bool   `toml:"mouse"`
+	HideBlockedUsers    bool   `toml:"hide_blocked_users"`
+	Timestamps          bool   `toml:"timestamps"`
+	ShowAttachmentLinks bool   `toml:"show_attachment_links"`
+	MessagesLimit       uint8  `toml:"messages_limit"`
+	Editor              string `toml:"editor"`
 
-	MessagesLimit uint8  `toml:"messages_limit"`
-	Editor        string `toml:"editor"`
+	Browser        string `toml:"browser"`
+	BrowserVersion string `toml:"browser_version"`
+	UserAgent      string `toml:"user_agent"`
 
-	Timestamps       bool   `toml:"timestamps"`
 	TimestampsFormat string `toml:"timestamps_format"`
-
-	ShowAttachmentLinks bool `toml:"show_attachment_links"`
-
-	Keys  Keys  `toml:"keys"`
-	Theme Theme `toml:"theme"`
+	Keys             Keys   `toml:"keys"`
+	Theme            Theme  `toml:"theme"`
 }
 
 func defaultConfig() *Config {
 	return &Config{
-		Mouse:            true,
-		HideBlockedUsers: true,
-		MessagesLimit:    50,
-		Editor:           "default",
-
-		Timestamps:       false,
-		TimestampsFormat: time.Kitchen,
-
+		Mouse:               true,
+		HideBlockedUsers:    true,
+		Timestamps:          false,
 		ShowAttachmentLinks: true,
+		MessagesLimit:       50,
+		Editor:              "default",
+		TimestampsFormat:    time.Kitchen,
+
+		Browser:        Browser,
+		BrowserVersion: BrowserVersion,
+		UserAgent:      UserAgent,
 
 		Keys:  defaultKeys(),
 		Theme: defaultTheme(),

+ 2 - 0
internal/login/form.go

@@ -108,6 +108,8 @@ func (self *Form) login() {
 
 	// Create an API client without an authentication token.
 	client := api.NewClient("")
+	// Spoof the user agent of a web browser.
+	client.UserAgent = config.UserAgent
 
 	// Attempt to login using the email and password.
 	resp, err := client.Login(email, password)