Skip to content

Commit

Permalink
feat: customizable ua (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed May 18, 2024
1 parent e6b2709 commit 2b6ca35
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions README_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions README_sc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(&notui, "notui", false, "use plain text instead of TUI")
flag.Parse()
args := flag.Args()
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions ua.go
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions ua_windows.go
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 2b6ca35

Please sign in to comment.