From af9f8ced805509c3efd264fc5be2a82ea1a000f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=BCnger?= Date: Thu, 29 Aug 2024 19:25:08 +0200 Subject: [PATCH] yandex: implement custom user agent to help with upload speeds --- backend/yandex/yandex.go | 23 +++++++++++++++++++---- docs/content/yandex.md | 11 +++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/backend/yandex/yandex.go b/backend/yandex/yandex.go index 798cccd6c..aabb2f8db 100644 --- a/backend/yandex/yandex.go +++ b/backend/yandex/yandex.go @@ -26,6 +26,7 @@ import ( "github.com/rclone/rclone/lib/encoder" "github.com/rclone/rclone/lib/oauthutil" "github.com/rclone/rclone/lib/pacer" + "github.com/rclone/rclone/lib/random" "github.com/rclone/rclone/lib/readers" "github.com/rclone/rclone/lib/rest" "golang.org/x/oauth2" @@ -39,6 +40,8 @@ const ( minSleep = 10 * time.Millisecond maxSleep = 2 * time.Second // may needs to be increased, testing needed decayConstant = 2 // bigger for slower decay, exponential + + userAgentTemplae = `Yandex.Disk {"os":"windows","dtype":"ydisk3","vsn":"3.2.37.4977","id":"6BD01244C7A94456BBCEE7EEC990AEAD","id2":"0F370CD40C594A4783BC839C846B999C","session_id":"%s"}` ) // Globals @@ -79,15 +82,22 @@ func init() { // it doesn't seem worth making an exception for this Default: (encoder.Display | encoder.EncodeInvalidUtf8), + }, { + Name: "spoof_ua", + Help: "Set the user agent to match an official version of the yandex disk client. May help with upload performance.", + Default: true, + Advanced: true, + Hide: fs.OptionHideConfigurator, }}...), }) } // Options defines the configuration for this backend type Options struct { - Token string `config:"token"` - HardDelete bool `config:"hard_delete"` - Enc encoder.MultiEncoder `config:"encoding"` + Token string `config:"token"` + HardDelete bool `config:"hard_delete"` + Enc encoder.MultiEncoder `config:"encoding"` + SpoofUserAgent bool `config:"spoof_ua"` } // Fs represents a remote yandex @@ -254,6 +264,12 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e return nil, err } + ctx, ci := fs.AddConfig(ctx) + if fs.ConfigOptionsInfo.Get("user_agent").IsDefault() && opt.SpoofUserAgent { + randomSessionID, _ := random.Password(128) + ci.UserAgent = fmt.Sprintf(userAgentTemplae, randomSessionID) + } + token, err := oauthutil.GetToken(name, m) if err != nil { return nil, fmt.Errorf("couldn't read OAuth token: %w", err) @@ -274,7 +290,6 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, e return nil, fmt.Errorf("failed to configure Yandex: %w", err) } - ci := fs.GetConfig(ctx) f := &Fs{ name: name, opt: *opt, diff --git a/docs/content/yandex.md b/docs/content/yandex.md index 45039f0d7..6c65b1adc 100644 --- a/docs/content/yandex.md +++ b/docs/content/yandex.md @@ -210,6 +210,17 @@ Properties: - Type: Encoding - Default: Slash,Del,Ctl,InvalidUtf8,Dot +#### --yandex-spoof-ua + +Set the user agent to match an official version of the yandex disk client. May help with upload performance. + +Properties: + +- Config: spoof_ua +- Env Var: RCLONE_YANDEX_SPOOF_UA +- Type: bool +- Default: true + #### --yandex-description Description of the remote.