-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
163 lines (140 loc) · 4.08 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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/hekmon/transmissionrpc"
)
var (
host string
port int
useHTTPS bool
username string
password string
debug bool
trackersSource string
)
const (
ngoSang = "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt"
ngoSangJsDelivrMirror = "https://cdn.jsdelivr.net/gh/ngosang/trackerslist/trackers_all.txt"
)
func parseFlag(fs *flag.FlagSet, args []string) error {
fs.StringVar(&host, "host", "127.0.0.1", "")
fs.IntVar(&port, "port", 9091, "")
fs.BoolVar(&useHTTPS, "use-https", false, "")
fs.StringVar(&username, "username", "rpcuser", "")
fs.StringVar(&password, "password", "rpcpass", "")
fs.BoolVar(&debug, "debug", false, "")
fs.StringVar(&trackersSource, "trackers-source", ngoSangJsDelivrMirror, "")
return fs.Parse(args)
}
func main() {
if err := parseFlag(flag.CommandLine, os.Args[1:]); err != nil {
log.Println("parse command line args failed", err)
return
}
client, err := connect(host, username, password, transmissionrpc.AdvancedConfig{
Port: uint16(port),
Debug: debug,
})
if err != nil {
log.Println("connect transmission failed:", err)
return
}
torrents, err := client.TorrentGetAll()
if err != nil {
log.Printf("get all torrents failed: %s", err)
return
}
if len(torrents) == 0 {
log.Println("no torrents found.")
return
}
trackers, err := getTrackers()
if err != nil {
log.Println("load trackers failed: ", err)
return
}
for _, t := range torrents {
log.Printf("torrend %d \"%s\"", *t.ID, *t.Name)
if err := addTrackers(client, t, trackers); err != nil {
log.Printf("add trackers to %d '%s' failed: %s\n", *t.ID, *t.Name, err)
return
}
}
}
func connect(
host, username, password string, conf transmissionrpc.AdvancedConfig,
) (*transmissionrpc.Client, error) {
client, err := transmissionrpc.New(host, username, password, &conf)
if err != nil {
return nil, fmt.Errorf("new transmission rpc failed: %w", err)
}
ok, serverVersion, serverMinimumVersion, err := client.RPCVersion()
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf(
"remote transmission RPC version (v%d) is incompatible with the transmission library (v%d): remote needs at least v%d",
serverVersion, transmissionrpc.RPCVersion, serverMinimumVersion)
}
log.Printf("remote transmission RPC version (v%d)", serverVersion)
return client, nil
}
func addTrackers(
client *transmissionrpc.Client, torrent *transmissionrpc.Torrent, newTrackers []string) error {
oldTrackers := make(map[string]bool)
for _, tracker := range torrent.Trackers {
oldTrackers[tracker.Announce] = true
}
var toAddTrackers []string
for _, nt := range newTrackers {
if !oldTrackers[nt] {
toAddTrackers = append(toAddTrackers, nt)
}
}
if len(toAddTrackers) == 0 {
log.Printf("torrent %d \"%s\" has all trackers",
*torrent.ID, *torrent.Name)
return nil
}
payload := transmissionrpc.TorrentSetPayload{
TrackerAdd: toAddTrackers,
IDs: []int64{*torrent.ID},
}
if err := client.TorrentSet(&payload); err != nil {
return fmt.Errorf("update torrent %d \"%s\" failed: %w",
*torrent.ID, *torrent.Name, err)
}
log.Printf("torrent %d \"%s\" added %d new trackers",
*torrent.ID, *torrent.Name, len(toAddTrackers))
return nil
}
func getTrackers() ([]string, error) {
log.Println("download trackers from ", trackersSource)
var trackers []string
resp, err := http.Get(trackersSource)
if err != nil {
return nil, fmt.Errorf("get %s failed: %w", trackersSource, err)
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
trackers = append(trackers, line)
}
if err := scanner.Err(); err != nil && err != io.EOF {
return nil, err
}
log.Printf("trackers: %+v", trackers)
return trackers, nil
}