Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 6504116

Browse files
committed
修复更新下载
1 parent e279c2d commit 6504116

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

cmd/iploc-fetch/iploc-fetch.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import (
99
"io/ioutil"
1010
"net/http"
1111
"os"
12-
"strconv"
1312

1413
flag "github.com/spf13/pflag"
1514
"golang.org/x/text/encoding/simplifiedchinese"
1615
"golang.org/x/text/transform"
1716
)
1817

1918
const (
19+
userAgent = "Mozilla/3.0 (compatible; Indy Library)"
2020
urlCopywrite = "http://update.cz88.net/ip/copywrite.rar"
2121
urlQqwryDat = "http://update.cz88.net/ip/qqwry.rar"
2222
)
2323

2424
var (
25-
outputFile string
25+
outputFile string
2626
quiet, help bool
2727

2828
fileSize uint64
@@ -45,16 +45,13 @@ func (w *FetchWriter) progress() {
4545
}
4646

4747
func fetch(key uint32) error {
48-
resp, err := http.Get(urlQqwryDat)
48+
resp, err := newRequest(urlQqwryDat)
4949
if err != nil {
5050
return err
5151
}
5252
defer resp.Body.Close()
53-
size, err := strconv.ParseUint(resp.Header.Get("Content-Length"), 10, 32)
54-
if err != nil {
55-
return err
56-
}
57-
if size != fileSize {
53+
54+
if uint64(resp.ContentLength) != fileSize {
5855
return fmt.Errorf("the file size of the agreement is different")
5956
}
6057

@@ -113,7 +110,7 @@ func init() {
113110
}
114111

115112
func main() {
116-
resp, err := http.Get(urlCopywrite)
113+
resp, err := newRequest(urlCopywrite)
117114
if err != nil {
118115
fmt.Fprintf(os.Stderr, err.Error())
119116
os.Exit(1)
@@ -137,6 +134,16 @@ func main() {
137134
fatal(fetch(key))
138135
}
139136

137+
func newRequest(urls string) (*http.Response, error) {
138+
req, err := http.NewRequest("GET", urls, nil)
139+
if err != nil {
140+
return nil, err
141+
}
142+
req.Header.Set("User-Agent", userAgent)
143+
client := &http.Client{}
144+
return client.Do(req)
145+
}
146+
140147
func readVersion(p []byte) []byte {
141148
var start = 24
142149
var end int
@@ -170,7 +177,7 @@ func toUTF8(s []byte) (b []byte) {
170177

171178
func fatal(err error) {
172179
if err != nil {
173-
fmt.Fprintf(os.Stderr, err.Error() + "\n")
180+
fmt.Fprintf(os.Stderr, err.Error()+"\n")
174181
os.Exit(1)
175182
}
176183
}

0 commit comments

Comments
 (0)