From 2f1d16408c06647bd88b9dfe8860f66d177025f6 Mon Sep 17 00:00:00 2001 From: zu1k Date: Mon, 4 Jul 2022 22:01:03 +0800 Subject: [PATCH] fix: common download save to file Signed-off-by: zu1k --- go.mod | 4 ++-- go.sum | 4 ++++ pkg/common/httpclient.go | 9 +++++---- pkg/download/download.go | 6 ++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 843ee5f..b315bc6 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/ip2location/ip2location-go/v9 v9.4.0 github.com/ipipdotnet/ipdb-go v1.3.1 github.com/lionsoul2014/ip2region v2.2.0-release+incompatible - github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220624075035-53e6777abd9d + github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220704023600-9376c56688c8 github.com/oschwald/geoip2-golang v1.7.0 github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda github.com/spf13/cobra v1.5.0 @@ -35,7 +35,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.0 // indirect github.com/ulikunitz/xz v0.5.10 // indirect - golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect + golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect golang.org/x/tools v0.1.7 // indirect gopkg.in/ini.v1 v1.66.6 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 575b543..cf29a93 100644 --- a/go.sum +++ b/go.sum @@ -146,6 +146,8 @@ github.com/lionsoul2014/ip2region v2.2.0-release+incompatible h1:1qp9iks+69h7IGL github.com/lionsoul2014/ip2region v2.2.0-release+incompatible/go.mod h1:+ZBN7PBoh5gG6/y0ZQ85vJDBe21WnfbRrQQwTfliJJI= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220624075035-53e6777abd9d h1:E9eY3SL8JWS+mJds+Uk0OGAbLMLvMvEiebAGmo2fp1U= github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220624075035-53e6777abd9d/go.mod h1:bChUKvbKVC3zL/lLLIcu6alhQaL8uWD/DA+jRdyggdI= +github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220704023600-9376c56688c8 h1:aHOC8Mcbd3TiCb+L9cSbU/NdZinWuaKSoL4qD4I3MQo= +github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20220704023600-9376c56688c8/go.mod h1:bChUKvbKVC3zL/lLLIcu6alhQaL8uWD/DA+jRdyggdI= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= @@ -344,6 +346,8 @@ golang.org/x/sys v0.0.0-20220624220833-87e55d714810 h1:rHZQSjJdAI4Xf5Qzeh2bBc5YJ golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8= golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e h1:CsOuNlbOuf0mzxJIefr6Q4uAUetRUwZE4qt7VfzP+xo= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/pkg/common/httpclient.go b/pkg/common/httpclient.go index b869746..7844ab2 100644 --- a/pkg/common/httpclient.go +++ b/pkg/common/httpclient.go @@ -2,6 +2,7 @@ package common import ( "io/ioutil" + "log" "net/http" "time" ) @@ -16,11 +17,11 @@ var httpClient *HttpClient func init() { httpClient = &HttpClient{http.DefaultClient} - httpClient.Timeout = time.Second * 30 + httpClient.Timeout = time.Second * 60 httpClient.Transport = &http.Transport{ TLSHandshakeTimeout: time.Second * 5, - IdleConnTimeout: time.Second * 20, - ResponseHeaderTimeout: time.Second * 20, + IdleConnTimeout: time.Second * 10, + ResponseHeaderTimeout: time.Second * 10, ExpectContinueTimeout: time.Second * 20, } } @@ -37,9 +38,9 @@ func (c *HttpClient) Get(urls ...string) (body []byte, err error) { for _, url := range urls { req, err = http.NewRequest(http.MethodGet, url, nil) if err != nil { + log.Println(err) continue } - req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") req.Header.Set("User-Agent", UserAgent) resp, err = c.Do(req) diff --git a/pkg/download/download.go b/pkg/download/download.go index 7907716..5719c45 100644 --- a/pkg/download/download.go +++ b/pkg/download/download.go @@ -16,10 +16,8 @@ func Download(filePath string, urls ...string) (data []byte, err error) { return } - if len(filePath) == 1 { - if err := common.SaveFile(filePath, data); err == nil { - log.Println("文件下载成功:", filePath) - } + if err := common.SaveFile(filePath, data); err == nil { + log.Println("文件下载成功:", filePath) } return }