-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
335 lines (299 loc) · 8.51 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"archive/tar"
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
logg "github.com/anacrolix/log"
"github.com/anacrolix/torrent"
"github.com/nektro/go-util/mbpp"
"github.com/nektro/go-util/util"
"github.com/spf13/pflag"
"golang.org/x/sync/semaphore"
)
var (
wg = new(sync.WaitGroup)
dones = map[string]bool{}
mtx = new(sync.Mutex)
wwg = new(sync.WaitGroup)
workingDir string
doneDir string
dropAfter int
dropAfterF int
doDownload bool
peerLog *os.File
seedFor int
includeHash bool
hashTrimLen int
packTar bool
ctx = context.TODO()
guard *semaphore.Weighted
)
func main() {
flagWD := pflag.StringP("working-dir", "w", "./", "Directory to store in-progress torrents.")
flagDD := pflag.StringP("done-dir", "d", "", "Optional directory to move completed torrents to. (default -w)")
flagTR := pflag.StringArrayP("torrent", "t", []string{}, "Path of the torrent file you wish to download. (Can be passed multiple times.)")
flagTD := pflag.String("torrent-dir", "", "Path to directory of torrent files. Will only pick up .torrents.")
flagMG := pflag.StringArrayP("magnet", "m", []string{}, "Magnet Link to download. (Can be passed multiple times.)")
flagCR := pflag.IntP("concurrency", "c", 10, "Maximum number of torrents to actively download at a time.")
flagDT := pflag.Int("drop-after", 35, "Minutes to drop torrents with no progress after. (disable -1)")
flagUA := pflag.String("user-agent", "rtorrent/0.9.2/0.13.2", "HTTP User Agent to use when contacting trackers.")
flagPI := pflag.String("peer-id", "-lt0D20-", "Bittorrent peer_id.")
flagDF := pflag.Int("drop-after-force", -1, "Minutes to drop torrents after regardless of progress. (disable -1)")
flagDL := pflag.Bool("do-download", true, "Setting this flag to false will make all torrents idle in client.")
flagPL := pflag.String("peers-log", "", "An optional path to log file that will list all peers per torrent.")
flagSF := pflag.IntP("seed-for", "s", 0, "When positive, minutes to seed torrents for. (-1: forever) (0: only leech)")
flagIH := pflag.BoolP("include-btih-in-dn", "i", false, "If true, folder name will be 'btih dn' instead of 'dn'.")
flagTB := pflag.Int("trim-btih", 40, "This will trim the length of the info hash used when --include-btih-in-dn is used.")
flagDH := pflag.Bool("disable-dht", false, "Setting this will disable DHT.")
flagMF := pflag.String("magnet-file", "", "Path to a text file with magnet links on each line.")
flagPT := pflag.Bool("pack-tar", false, "Enabling this will pack torrent folder into a .tar so that it only takes up a single file.")
pflag.Parse()
//
//
workingDir = *flagWD
workingDir, _ = filepath.Abs(workingDir)
util.DieOnError(util.Assert(util.DoesDirectoryExist(workingDir), "Path of --working-dir is '"+workingDir+"/' and must exist!"))
doneDir = *flagDD
if doneDir == "" {
doneDir = workingDir
}
doneDir, _ = filepath.Abs(doneDir)
util.DieOnError(util.Assert(util.DoesDirectoryExist(doneDir), "Path of --done-dir is '"+doneDir+"/' and must exist!"))
if len(*flagPL) > 0 {
plp := *flagPL
plp, _ = filepath.Abs(plp)
peerLog, _ = os.OpenFile(plp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
}
dropAfter = *flagDT
guard = semaphore.NewWeighted(int64(*flagCR))
dropAfterF = *flagDF
doDownload = *flagDL
seedFor = *flagSF
includeHash = *flagIH
hashTrimLen = *flagTB
packTar = *flagPT
//
//
util.Log("Working directory:\t", workingDir)
util.Log("Finish directory:\t", doneDir)
util.Log("--do-download:", doDownload)
//
//
mbpp.Init(*flagCR)
util.Log("Starting up client...")
cf := torrent.NewDefaultClientConfig()
cf.NoDHT = *flagDH
cf.DisablePEX = true
cf.Debug = false
cf.Bep20 = *flagPI + randomHex(20-len(*flagPI))
cf.ExtendedHandshakeClientVersion = "3.00+"
cf.DataDir = workingDir
cf.DisableIPv6 = true
cf.ListenPort = 0
cf.Logger = logg.Discard
cf.HTTPUserAgent = *flagUA
cf.Seed = *flagSF != 0
util.Log("peer_id:", cf.Bep20)
c, err := torrent.NewClient(cf)
util.DieOnError(err)
defer c.Close()
//
magFilePth := *flagMF
if len(magFilePth) > 0 {
magFilePth, _ := filepath.Abs(magFilePth)
util.DieOnError(util.Assert(util.DoesFileExist(magFilePth), "Path of --magnet-file is '"+magFilePth+"' and must exist!"))
util.ReadFileLines(magFilePth, func(s string) {
addT(c.AddMagnet, s)
})
}
torrentPths := *flagTR
if len(torrentPths) > 0 {
for _, item := range torrentPths {
torrentP, _ := filepath.Abs(item)
util.DieOnError(util.Assert(util.DoesFileExist(torrentP), "Path of --torrent is '"+torrentP+"' and must exist!"))
addT(c.AddTorrentFromFile, torrentP)
}
}
torrentDir := *flagTD
if len(torrentDir) > 0 {
if util.DoesDirectoryExist(torrentDir) {
filepath.Walk(torrentDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info == nil {
return nil
}
if strings.HasSuffix(info.Name(), ".torrent") {
addT(c.AddTorrentFromFile, path)
}
if strings.HasSuffix(info.Name(), ".magnet.txt") {
addT(c.AddMagnet, string(util.ReadFile(path)))
}
return nil
})
}
}
magnetLnks := *flagMG
if len(magnetLnks) > 0 {
for _, item := range magnetLnks {
addT(c.AddMagnet, item)
}
}
//
time.Sleep(time.Second)
c.WaitAll()
wg.Wait()
wwg.Wait()
util.Log("Completed after", len(dones), "torrents downloaded.")
}
func randomHex(n int) string {
bytes := make([]byte, n)
_, err := rand.Read(bytes)
if err != nil {
return ""
}
return hex.EncodeToString(bytes)[:n]
}
func addT(f func(string) (*torrent.Torrent, error), s string) {
wg.Add(1)
guard.Acquire(ctx, 1)
go func() {
t, err := f(s)
if err != nil {
wg.Done()
guard.Release(1)
return
}
if dropAfter > 0 {
go func() {
time.Sleep(time.Minute * time.Duration(dropAfter))
if t.BytesCompleted() == 0 {
closeT(t, true)
}
}()
}
if dropAfterF > 0 {
go func() {
time.Sleep(time.Minute * time.Duration(dropAfterF))
closeT(t, true)
}()
}
<-t.GotInfo()
if includeHash {
t.Info().Name = (t.InfoHash().HexString()[:hashTrimLen] + " " + t.Info().Name)
}
name := t.Info().Name
workName := workingDir + "/" + name
doneName := doneDir + "/" + name
if packTar {
if util.DoesFileExist(doneName + ".tar") {
closeT(t, true)
return
}
} else {
if util.DoesFileExist(doneName) {
go func() {
if seedFor > 0 {
os.Rename(doneName, workName)
t.VerifyData()
time.Sleep(time.Minute * time.Duration(seedFor))
os.Rename(workName, doneName)
}
closeT(t, true)
}()
return
}
}
if doDownload {
go func() {
mbpp.CreateJob(t.Name(), func(bar *mbpp.BarProxy) {
bar.AddToTotal(int64(t.NumPieces()))
prev_completed := 0
for {
this_completed := 0
for _, r := range t.PieceStateRuns() {
if r.Complete {
this_completed += r.Length
}
}
bar.Increment(this_completed - prev_completed)
if this_completed == t.NumPieces() {
break
}
prev_completed = this_completed
}
})
if seedFor >= 0 {
wwg.Add(1)
go func() {
time.Sleep(time.Minute * time.Duration(seedFor))
closeT(t, false)
if packTar {
stat, _ := os.Stat(workName)
out1, _ := os.Create(workName + ".tar")
out2 := tar.NewWriter(out1)
if stat.IsDir() {
filepath.Walk(workName, func(pathS string, info os.FileInfo, err error) error {
if util.DoesDirectoryExist(pathS) {
return nil
}
name := strings.TrimPrefix(pathS, workName+"/")
writeTarFile(out2, workName, name)
return nil
})
} else {
writeTarFile(out2, workingDir, name)
}
out2.Close()
os.RemoveAll(workName)
workName += ".tar"
doneName += ".tar"
}
if doneDir != workingDir {
os.Rename(workName, doneName)
}
wwg.Done()
}()
}
}()
t.DownloadAll()
}
}()
}
func closeT(t *torrent.Torrent, early bool) {
mtx.Lock()
defer mtx.Unlock()
h := t.InfoHash().HexString()
b, ok := dones[h]
if ok && b {
return
}
if peerLog != nil {
peers := []string{}
for _, p := range t.PeerConns() {
peers = append(peers, p.RemoteAddr.String())
}
fmt.Fprintln(peerLog, h, peers)
}
dones[h] = true
t.Drop()
wg.Done()
guard.Release(1)
}
func writeTarFile(w *tar.Writer, dir, name string) {
content := util.ReadFile(dir + "/" + name)
w.WriteHeader(&tar.Header{
Name: name,
Mode: 0600,
Size: int64(len(content)),
})
w.Write(content)
}