-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
78 lines (72 loc) · 1.43 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
package main
import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"text/template"
)
var listUrl = "https://ngosang.github.io/trackerslist/trackers_best.txt"
var tmpl = `bt-tracker={{range $k, $v := .}}{{if eq $k 0}}{{$v}}{{else}},{{$v}}{{end}}{{end}}
enable-dht=true
bt-enable-lpd=true
enable-peer-exchange=true
`
func downloadList() ([]string, error) {
resp, err := http.Get(listUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
rd := bufio.NewReader(resp.Body)
res := []string{}
for {
line1, _, err := rd.ReadLine()
if err != nil {
break
}
//fmt.Println("url:", string(line1), len(line1))
if len(line1) > 0 {
res = append(res, string(line1))
}
}
return res, nil
}
func writeConf(data []string) error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
dir1 := filepath.Join(home, ".aria2")
os.Mkdir(dir1, os.ModePerm)
confPath := filepath.Join(home, ".aria2", "aria2.conf")
fp, err := os.Create(confPath)
if err != nil {
return err
}
defer fp.Close()
t := template.New("")
t, err = t.Parse(tmpl)
if err != nil {
return err
}
return t.Execute(fp, data)
}
func main() {
flag.Parse()
if len(flag.Arg(0)) > 0 {
listUrl = flag.Arg(0)
} else {
fmt.Print("Usage:\n\t./aria2-trackers\nOr:\n\t./aria2-trackers [url of list]\n")
}
list1, err := downloadList()
if err != nil {
panic(err.Error())
}
err = writeConf(list1)
if err != nil {
panic(err.Error())
}
}