From 2b6ca35a6d553d758229bb66b853e68cec00aeae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sat, 18 May 2024 20:29:23 +0900 Subject: [PATCH] feat: customizable ua (#2) --- README.md | 2 ++ README_kr.md | 2 ++ README_sc.md | 2 ++ main.go | 5 ++--- net.go | 8 +++++--- ua.go | 5 +++++ ua_windows.go | 3 +++ 7 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 ua.go create mode 100644 ua_windows.go diff --git a/README.md b/README.md index 3b27467..4d267cf 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download' use standard TLS client -notui use plain text instead of TUI + -ua string + customize user agent -w uint connection waiting seconds (default 4) 'target/to/download' diff --git a/README_kr.md b/README_kr.md index 127127a..bfc81f2 100644 --- a/README_kr.md +++ b/README_kr.md @@ -98,6 +98,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download' use standard TLS client -notui use plain text instead of TUI + -ua string + customize user agent -w uint connection waiting seconds (default 4) 'target/to/download' diff --git a/README_sc.md b/README_sc.md index 5e7bd0b..52f03e4 100644 --- a/README_sc.md +++ b/README_sc.md @@ -96,6 +96,8 @@ Usage: rvcmd [-notrs] [-dns dns.yaml] 'target/to/download' use standard TLS client -notui use plain text instead of TUI + -ua string + customize user agent -w uint connection waiting seconds (default 4) 'target/to/download' diff --git a/main.go b/main.go index a59bb71..2e3b856 100644 --- a/main.go +++ b/main.go @@ -20,8 +20,6 @@ import ( //go:generate ./pckcfg.sh assets packs tools -const ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0" - var ( notui = false sc screen @@ -33,6 +31,7 @@ func main() { cust := flag.Bool("c", false, "use custom yaml instruction") force := flag.Bool("f", false, "force download even file exists") wait := flag.Uint("w", 4, "connection waiting seconds") + ua := flag.String("ua", defua, "customize user agent") flag.BoolVar(¬ui, "notui", false, "use plain text instead of TUI") flag.Parse() args := flag.Args() @@ -89,7 +88,7 @@ func main() { } ch := make(chan struct{}) go func() { - err := usercfg.download(args[0], "", time.Second*time.Duration(*wait), *cust, !*ntrs, *force) + err := usercfg.download(args[0], "", *ua, time.Second*time.Duration(*wait), *cust, !*ntrs, *force) ch <- struct{}{} if err != nil { errorln(err) diff --git a/net.go b/net.go index 8ffc3f2..b210203 100644 --- a/net.go +++ b/net.go @@ -15,7 +15,7 @@ import ( "github.com/pkg/errors" ) -func (c *config) download(path, prefix string, waits time.Duration, usecust, usetrs, force bool) error { +func (c *config) download(path, prefix, ua string, waits time.Duration, usecust, usetrs, force bool) error { for i, t := range c.Targets { if t.Refer != "" { refp := path[:strings.LastIndex(path, "/")+1] + t.Refer @@ -24,7 +24,7 @@ func (c *config) download(path, prefix string, waits time.Duration, usecust, use if err != nil { return err } - err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", waits, usecust, usetrs, force) + err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", ua, waits, usecust, usetrs, force) if err != nil { return err } @@ -70,7 +70,9 @@ func (c *config) download(path, prefix string, waits time.Duration, usecust, use return } infof("#%s%d get: %s", prefix, i+1, req.URL) - req.Header.Add("user-agent", ua) + if len(ua) > 0 { + req.Header.Add("user-agent", ua) + } var resp *http.Response if usetrs { resp, err = http2.DefaultClient.Do(req) diff --git a/ua.go b/ua.go new file mode 100644 index 0000000..96c26a4 --- /dev/null +++ b/ua.go @@ -0,0 +1,5 @@ +//go:build !windows + +package main + +const defua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0" diff --git a/ua_windows.go b/ua_windows.go new file mode 100644 index 0000000..9cc2d7e --- /dev/null +++ b/ua_windows.go @@ -0,0 +1,3 @@ +package main + +const defua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"