Skip to content

Commit 6901ee3

Browse files
committed
feat: code cleanup + resume bug fix
Signed-off-by: abzcoding <[email protected]>
1 parent 614694c commit 6901ee3

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

Makefile

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
COMMIT = $$(git describe --always)
1+
COMMIT := $(shell git describe --always)
2+
BINARY := hget
3+
BINDIR := bin
4+
INSTALL_PATH := /usr/local/bin
5+
6+
.PHONY: all clean build install test deps
7+
8+
all: build
29

310
deps:
4-
@echo "====> Install dependencies..."
5-
go get github.com/fatih/color
6-
go get github.com/mattn/go-colorable
7-
go get github.com/mattn/go-isatty
8-
go get github.com/fatih/color
9-
go get gopkg.in/cheggaaa/pb.v1
10-
go get github.com/mattn/go-isatty
11-
go get github.com/imkira/go-task
12-
go get github.com/fujiwara/shapeio
13-
go get github.com/alecthomas/units
11+
@echo "====> Updating dependencies..."
12+
go mod tidy
1413

1514
clean:
16-
@echo "====> Remove installed binary"
17-
rm -f bin/hget
15+
@echo "====> Removing installed binary"
16+
rm -f $(BINDIR)/$(BINARY)
17+
18+
test:
19+
@echo "====> Running tests..."
20+
go test -v ./...
1821

1922
build: deps
20-
@echo "====> Build hget in ./bin "
21-
go build -ldflags "-X main.GitCommit=\"$(COMMIT)\"" -o bin/hget
23+
@echo "====> Building $(BINARY) in ./$(BINDIR)"
24+
mkdir -p $(BINDIR)
25+
go build -ldflags "-X main.GitCommit=\"$(COMMIT)\"" -o $(BINDIR)/$(BINARY)
2226

2327
install: build
24-
@echo "====> Installing hget in /usr/local/bin/hget"
25-
chmod +x ./bin/hget
26-
sudo mv ./bin/hget /usr/local/bin/hget
28+
@echo "====> Installing $(BINARY) in $(INSTALL_PATH)/$(BINARY)"
29+
chmod +x ./$(BINDIR)/$(BINARY)
30+
sudo mv ./$(BINDIR)/$(BINARY) $(INSTALL_PATH)/$(BINARY)

http.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ import (
2121
pb "gopkg.in/cheggaaa/pb.v1"
2222
)
2323

24-
var (
25-
tr = &http.Transport{
26-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
27-
}
28-
client = &http.Client{Transport: tr}
29-
)
30-
3124
var (
3225
acceptRangeHeader = "Accept-Ranges"
3326
contentLengthHeader = "Content-Length"
@@ -50,7 +43,7 @@ type HTTPDownloader struct {
5043
// NewHTTPDownloader returns a ProxyAwareHttpClient with given configurations.
5144
func NewHTTPDownloader(url string, par int, skipTLS bool, proxyServer string, bwLimit string) *HTTPDownloader {
5245
var resumable = true
53-
client := ProxyAwareHTTPClient(proxyServer)
46+
client := ProxyAwareHTTPClient(proxyServer, skipTLS)
5447

5548
parsed, err := stdurl.Parse(url)
5649
FatalCheck(err)
@@ -146,8 +139,10 @@ func partCalculate(par int64, len int64, url string) []Part {
146139
}
147140

148141
// ProxyAwareHTTPClient returns an HTTP client that may use an HTTP or SOCKS5 proxy.
149-
func ProxyAwareHTTPClient(proxyServer string) *http.Client {
150-
httpTransport := &http.Transport{}
142+
func ProxyAwareHTTPClient(proxyServer string, skipTLS bool) *http.Client {
143+
httpTransport := &http.Transport{
144+
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipTLS},
145+
}
151146
httpClient := &http.Client{Transport: httpTransport}
152147
var dialer proxy.Dialer = proxy.Direct
153148

@@ -234,7 +229,7 @@ func (d *HTTPDownloader) downloadPart(part Part, bar *pb.ProgressBar, wg *sync.W
234229
return
235230
}
236231

237-
client := ProxyAwareHTTPClient(d.proxy)
232+
client := ProxyAwareHTTPClient(d.proxy, d.skipTLS)
238233
resp, err := client.Do(req)
239234
if err != nil {
240235
errorChan <- err

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main() {
1919
var proxy, filePath, bwLimit, resumeTask string
2020

2121
conn := flag.Int("n", runtime.NumCPU(), "number of connections")
22-
skiptls := flag.Bool("skip-tls", true, "skip certificate verification for https")
22+
skiptls := flag.Bool("skip-tls", false, "skip certificate verification for https")
2323
flag.StringVar(&proxy, "proxy", "", "proxy for downloading, e.g. -proxy '127.0.0.1:12345' for socks5 or -proxy 'http://proxy.com:8080' for http proxy")
2424
flag.StringVar(&filePath, "file", "", "path to a file that contains one URL per line")
2525
flag.StringVar(&bwLimit, "rate", "", "bandwidth limit during download, e.g. -rate 10kB or -rate 10MiB")
@@ -161,7 +161,7 @@ hget [options] --resume=TaskName
161161
162162
Options:
163163
-n int number of connections (default number of CPUs)
164-
-skip-tls bool skip certificate verification for https (default true)
164+
-skip-tls bool skip certificate verification for https (default false)
165165
-proxy string proxy address (e.g., '127.0.0.1:12345' for socks5 or 'http://proxy.com:8080')
166166
-file string file path containing URLs (one per line)
167167
-rate string bandwidth limit during download (e.g., 10kB, 10MiB)

resume.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ import (
44
"fmt"
55
"io/ioutil"
66
"os"
7+
"os/user"
78
"path/filepath"
89
"strings"
910
)
1011

1112
// TaskPrint reads and prints data about current download jobs.
1213
func TaskPrint() error {
13-
downloadingPath := filepath.Join(os.Getenv("HOME"), dataFolder)
14+
usr, err := user.Current()
15+
FatalCheck(err)
16+
homeDir := usr.HomeDir
17+
18+
downloadingPath := filepath.Join(homeDir, dataFolder)
1419
downloading, err := ioutil.ReadDir(downloadingPath)
1520
if err != nil {
1621
return err

state.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"io/ioutil"
66
"os"
7+
"os/user"
78
"path/filepath"
89
)
910

@@ -37,7 +38,10 @@ func (s *State) Save() error {
3738

3839
//move current downloading file to data folder
3940
for _, part := range s.Parts {
40-
os.Rename(part.Path, filepath.Join(folder, filepath.Base(part.Path)))
41+
err := os.Rename(part.Path, filepath.Join(folder, filepath.Base(part.Path)))
42+
if err != nil {
43+
return err
44+
}
4145
}
4246

4347
//save state file
@@ -50,7 +54,14 @@ func (s *State) Save() error {
5054

5155
// Read loads data about the state of downloaded files
5256
func Read(task string) (*State, error) {
53-
file := filepath.Join(os.Getenv("HOME"), dataFolder, task, stateFileName)
57+
usr, err := user.Current()
58+
FatalCheck(err)
59+
homeDir := usr.HomeDir
60+
61+
// extract filename from task
62+
taskName := TaskFromURL(task)
63+
64+
file := filepath.Join(homeDir, dataFolder, taskName, stateFileName)
5465
Printf("Getting data from %s\n", file)
5566
bytes, err := ioutil.ReadFile(file)
5667
if err != nil {

util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66
"net/url"
77
"os"
8+
"os/user"
89
"path/filepath"
910
"strings"
1011

@@ -53,13 +54,16 @@ func DisplayProgressBar() bool {
5354

5455
// FolderOf makes sure you won't get LFI
5556
func FolderOf(urlStr string) string {
56-
safePath := filepath.Join(os.Getenv("HOME"), dataFolder)
57+
usr, err := user.Current()
58+
FatalCheck(err)
59+
homeDir := usr.HomeDir
60+
safePath := filepath.Join(homeDir, dataFolder)
5761

5862
// Extract the last path from the URL, excluding parameters.
5963
// eg: URL_ADDRESS.com/path/to/file?param=value -> file
6064
cleanPath := TaskFromURL(urlStr)
6165

62-
fullQualifyPath, err := filepath.Abs(filepath.Join(os.Getenv("HOME"), dataFolder, cleanPath))
66+
fullQualifyPath, err := filepath.Abs(filepath.Join(homeDir, dataFolder, cleanPath))
6367
FatalCheck(err)
6468

6569
//must ensure full qualify path is CHILD of safe path

0 commit comments

Comments
 (0)