-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
229 lines (206 loc) · 5.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"encoding/gob"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
const VERSION = "1.0.3"
var queue, redo, finish chan int
var cor, size, length, timeout int
var hash, dst string
var verify, version, cache bool
func main() {
flag.IntVar(&cor, "c", 1, "coroutine num")
flag.IntVar(&size, "s", 0, "chunk size")
flag.IntVar(&size, "t", 0, "timeout")
flag.StringVar(&dst, "f", "", "file name")
flag.StringVar(&hash, "h", "sha1", "sha1 or md5 to verify the file")
flag.BoolVar(&verify, "v", false, "verify file, not download")
flag.BoolVar(&cache, "cache", false, "jump if cache exist, only verify the size")
flag.BoolVar(&version, "version", false, "show version")
flag.Parse()
url := os.Args[len(os.Args)-1]
if version || url == "version" {
fmt.Println("Fileload version:", VERSION)
return
}
if verify {
file, err := os.Open(url)
if err != nil {
log.Println(err)
return
}
if hash == "sha1" {
h := sha1.New()
io.Copy(h, file)
r := h.Sum(nil)
log.Printf("sha1 of file: %x\n", r)
} else if hash == "md5" {
h := md5.New()
io.Copy(h, file)
r := h.Sum(nil)
log.Printf("sha1 of file: %x\n", r)
}
return
}
if dst == "" {
_, dst = filepath.Split(url)
}
startTime := time.Now()
client := http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
response, err := client.Do(request)
response.Body.Close()
num := response.Header.Get("Content-Length")
length, _ = strconv.Atoi(num)
log.Println("Conetnt-Length", length)
ranges := response.Header.Get("Accept-Ranges")
log.Println("Ranges:", ranges)
if size <= 0 {
size = int(math.Ceil(float64(length) / float64(cor)))
}
fragment := int(math.Ceil(float64(length) / float64(size)))
queue = make(chan int, cor)
redo = make(chan int, int(math.Floor(float64(cor)/2)))
go func() {
for i := 0; i < fragment; i++ {
queue <- i
}
for {
j := <-redo
queue <- j
}
}()
finish = make(chan int, cor)
for j := 0; j < cor; j++ {
go Do(request, fragment, j)
}
for k := 0; k < fragment; k++ {
_ = <-finish
//log.Printf("[%s][%d]Finished\n", "-", i)
}
log.Println("Start to combine files...")
file, err := os.Create(dst)
if err != nil {
log.Println(err)
return
}
defer file.Close()
var offset int64 = 0
for x := 0; x < fragment; x++ {
filename := fmt.Sprintf("%s_%d", dst, x)
buf, err := ioutil.ReadFile(filename)
if err != nil {
log.Println(err)
continue
}
file.WriteAt(buf, offset)
offset += int64(len(buf))
os.Remove(filename)
}
log.Println("Written to ", dst)
//hash
if hash == "sha1" {
h := sha1.New()
io.Copy(h, file)
r := h.Sum(nil)
log.Printf("sha1 of file: %x\n", r)
} else if hash == "md5" {
h := md5.New()
io.Copy(h, file)
r := h.Sum(nil)
log.Printf("sha1 of file: %x\n", r)
}
finishTime := time.Now()
duration := finishTime.Sub(startTime).Seconds()
log.Printf("Time:%f Speed:%f Kb/s\n", duration, float64(length)/duration/1024)
}
func Do(request *http.Request, fragment, no int) {
var req http.Request
err := DeepCopy(&req, request)
if err != nil {
log.Println("ERROR|prepare request:", err)
log.Panic(err)
return
}
for {
cStartTime := time.Now()
i := <-queue
//log.Printf("[%d][%d]Start download\n",no, i)
start := i * size
var end int
if i < fragment-1 {
end = start + size - 1
} else {
end = length - 1
}
filename := fmt.Sprintf("%s_%d", dst, i)
if cache {
filesize := int64(end - start + 1)
file, err := os.Stat(filename)
if err == nil && file.Size() == filesize {
log.Printf("[%d][%d]Hint cached %s, size:%d\n", no, i, filename, filesize)
finish <- i
continue
}
}
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", start, end))
log.Printf("[%d][%d]Start download:%d-%d\n", no, i, start, end)
cli := http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
resp, err := cli.Do(&req)
if err != nil {
log.Printf("[%d][%d]ERROR|do request:%s\n", no, i, err.Error())
redo <- i
continue
}
//log.Printf("[%d]Content-Length:%s\n", i, resp.Header.Get("Content-Length"))
log.Printf("[%d][%d]Content-Range:%s\n", no, i, resp.Header.Get("Content-Range"))
file, err := os.Create(filename)
if err != nil {
log.Printf("[%d][%d]ERROR|create file %s:%s\n", no, i, filename, err.Error())
file.Close()
resp.Body.Close()
redo <- i
continue
}
log.Printf("[%d][%d]Writing to file %s\n", no, i, filename)
n, err := io.Copy(file, resp.Body)
if err != nil {
log.Printf("[%d][%d]ERROR|write to file %s:%s\n", no, i, filename, err.Error())
file.Close()
resp.Body.Close()
redo <- i
continue
}
cEndTime := time.Now()
duration := cEndTime.Sub(cStartTime).Seconds()
log.Printf("[%d][%d]Download successfully:%f Kb/s\n", no, i, float64(n)/duration/1024)
file.Close()
resp.Body.Close()
finish <- i
}
}
func DeepCopy(dst, src interface{}) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}