Procházet zdrojové kódy

feat: set OS-specific user agents

Closes #495
ayn2op před 1 rokem
rodič
revize
edb0c255d4

+ 2 - 1
cmd/layout.go

@@ -4,6 +4,7 @@ import (
 	"log/slog"
 
 	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/consts"
 	"github.com/ayn2op/discordo/internal/login"
 	"github.com/gdamore/tcell/v2"
 	"github.com/rivo/tview"
@@ -113,7 +114,7 @@ func (l *Layout) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey {
 	case l.cfg.Keys.Logout:
 		l.app.Stop()
 
-		if err := keyring.Delete(config.Name, "token"); err != nil {
+		if err := keyring.Delete(consts.Name, "token"); err != nil {
 			slog.Error("failed to delete token from keyring", "err", err)
 			return nil
 		}

+ 2 - 1
cmd/message_input.go

@@ -8,6 +8,7 @@ import (
 
 	"github.com/atotto/clipboard"
 	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/consts"
 	"github.com/diamondburned/arikawa/v3/api"
 	"github.com/diamondburned/arikawa/v3/discord"
 	"github.com/diamondburned/arikawa/v3/utils/json/option"
@@ -15,7 +16,7 @@ import (
 	"github.com/rivo/tview"
 )
 
-const tmpFilePattern = config.Name + "_*.md"
+const tmpFilePattern = consts.Name + "_*.md"
 
 type MessageInput struct {
 	*tview.TextArea

+ 2 - 1
cmd/run.go

@@ -4,6 +4,7 @@ import (
 	"log/slog"
 
 	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/consts"
 	"github.com/ayn2op/discordo/internal/logger"
 	"github.com/zalando/go-keyring"
 )
@@ -20,7 +21,7 @@ func Run(token string) error {
 
 	// If no token was provided, look it up in the keyring.
 	if token == "" {
-		tok, err := keyring.Get(config.Name, "token")
+		tok, err := keyring.Get(consts.Name, "token")
 		if err != nil {
 			slog.Info("failed to get token from keyring", "err", err)
 		} else {

+ 5 - 12
internal/config/config.go

@@ -7,14 +7,7 @@ import (
 	"time"
 
 	"github.com/BurntSushi/toml"
-)
-
-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"
+	"github.com/ayn2op/discordo/internal/consts"
 )
 
 type Config struct {
@@ -44,9 +37,9 @@ func defaultConfig() *Config {
 		Editor:              "default",
 		TimestampsFormat:    time.Kitchen,
 
-		Browser:        Browser,
-		BrowserVersion: BrowserVersion,
-		UserAgent:      UserAgent,
+		Browser:        consts.Browser,
+		BrowserVersion: consts.BrowserVersion,
+		UserAgent:      consts.UserAgent,
 
 		Keys:  defaultKeys(),
 		Theme: defaultTheme(),
@@ -61,7 +54,7 @@ func Load() (*Config, error) {
 		path = "."
 	}
 
-	path = filepath.Join(path, Name, "config.toml")
+	path = filepath.Join(path, consts.Name, "config.toml")
 	f, err := os.Open(path)
 
 	cfg := defaultConfig()

+ 5 - 0
internal/consts/consts.go

@@ -0,0 +1,5 @@
+package consts
+
+const (
+	Name = "discordo"
+)

+ 7 - 0
internal/consts/consts_linux.go

@@ -0,0 +1,7 @@
+package consts
+
+const (
+	Browser        = "Chrome"
+	BrowserVersion = "133.0.0.0"
+	UserAgent      = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) " + Browser + "/" + BrowserVersion + " Safari/537.36"
+)

+ 7 - 0
internal/consts/consts_macos.go

@@ -0,0 +1,7 @@
+package consts
+
+const (
+	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"
+)

+ 7 - 0
internal/consts/consts_windows.go

@@ -0,0 +1,7 @@
+package consts
+
+const (
+	Browser        = "Chrome"
+	BrowserVersion = "133.0.0.0"
+	UserAgent      = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " + Browser + "/" + BrowserVersion + " Safari/537.36"
+)

+ 2 - 2
internal/logger/logger.go

@@ -5,7 +5,7 @@ import (
 	"os"
 	"path/filepath"
 
-	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/consts"
 )
 
 // Opens the log file and configures default logger.
@@ -15,7 +15,7 @@ func Load() error {
 		return err
 	}
 
-	path = filepath.Join(path, config.Name)
+	path = filepath.Join(path, consts.Name)
 	if err := os.MkdirAll(path, os.ModePerm); err != nil {
 		return err
 	}

+ 3 - 3
internal/login/form.go

@@ -4,7 +4,7 @@ import (
 	"errors"
 	"log/slog"
 
-	"github.com/ayn2op/discordo/internal/config"
+	"github.com/ayn2op/discordo/internal/consts"
 	"github.com/diamondburned/arikawa/v3/api"
 	"github.com/rivo/tview"
 	"github.com/zalando/go-keyring"
@@ -47,7 +47,7 @@ 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
+	client.UserAgent = consts.UserAgent
 
 	// Attempt to login using the email and password.
 	resp, err := client.Login(email, password)
@@ -76,7 +76,7 @@ func (self *Form) login() {
 		return
 	}
 
-	go keyring.Set(config.Name, "token", resp.Token)
+	go keyring.Set(consts.Name, "token", resp.Token)
 
 	if self.done != nil {
 		self.done(resp.Token)