forked from jackpal/Taipei-Torrent
/
metainfo.go
566 lines (516 loc) · 12 KB
/
metainfo.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package torrent
import (
"bytes"
"crypto/md5"
"crypto/sha1"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
bencode "github.com/jackpal/bencode-go"
"github.com/nictuku/dht"
)
type FileDict struct {
Length int64
Path []string
Md5sum string
}
type InfoDict struct {
PieceLength int64 `bencode:"piece length"`
Pieces string
Private int64
Name string
// Single File Mode
Length int64
Md5sum string
// Multiple File mode
Files []FileDict
}
type MetaInfo struct {
Info InfoDict
InfoHash string
Announce string
AnnounceList [][]string `bencode:"announce-list"`
CreationDate string `bencode:"creation date"`
Comment string
CreatedBy string `bencode:"created by"`
Encoding string
}
func getString(m map[string]interface{}, k string) string {
if v, ok := m[k]; ok {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
// Parse a list of list of strings structure, filtering out anything that's
// not a string, and filtering out empty lists. May return nil.
func getSliceSliceString(m map[string]interface{}, k string) (aas [][]string) {
if a, ok := m[k]; ok {
if b, ok := a.([]interface{}); ok {
for _, c := range b {
if d, ok := c.([]interface{}); ok {
var sliceOfStrings []string
for _, e := range d {
if f, ok := e.(string); ok {
sliceOfStrings = append(sliceOfStrings, f)
}
}
if len(sliceOfStrings) > 0 {
aas = append(aas, sliceOfStrings)
}
}
}
}
}
return
}
func GetMetaInfo(dialer Dialer, torrent string) (metaInfo *MetaInfo, err error) {
var input io.ReadCloser
if strings.HasPrefix(torrent, "http:") {
r, err := proxyHttpGet(dialer, torrent)
if err != nil {
return nil, err
}
input = r.Body
} else if strings.HasPrefix(torrent, "magnet:") {
magnet, err := parseMagnet(torrent)
if err != nil {
log.Println("Couldn't parse magnet: ", err)
return nil, err
}
ih, err := dht.DecodeInfoHash(magnet.InfoHashes[0])
if err != nil {
return nil, err
}
metaInfo = &MetaInfo{InfoHash: string(ih), AnnounceList: magnet.Trackers}
return metaInfo, err
} else {
if input, err = os.Open(torrent); err != nil {
return
}
}
// We need to calcuate the sha1 of the Info map, including every value in the
// map. The easiest way to do this is to read the data using the Decode
// API, and then pick through it manually.
var m interface{}
m, err = bencode.Decode(input)
input.Close()
if err != nil {
err = errors.New("Couldn't parse torrent file phase 1: " + err.Error())
return
}
topMap, ok := m.(map[string]interface{})
if !ok {
err = errors.New("Couldn't parse torrent file phase 2.")
return
}
infoMap, ok := topMap["info"]
if !ok {
err = errors.New("Couldn't parse torrent file. info")
return
}
var b bytes.Buffer
if err = bencode.Marshal(&b, infoMap); err != nil {
return
}
hash := sha1.New()
hash.Write(b.Bytes())
var m2 MetaInfo
err = bencode.Unmarshal(&b, &m2.Info)
if err != nil {
return
}
m2.InfoHash = string(hash.Sum(nil))
m2.Announce = getString(topMap, "announce")
m2.AnnounceList = getSliceSliceString(topMap, "announce-list")
m2.CreationDate = getString(topMap, "creation date")
m2.Comment = getString(topMap, "comment")
m2.CreatedBy = getString(topMap, "created by")
m2.Encoding = getString(topMap, "encoding")
metaInfo = &m2
return
}
type MetaInfoFileSystem interface {
Open(name string) (MetaInfoFile, error)
Stat(name string) (os.FileInfo, error)
}
type MetaInfoFile interface {
io.Closer
io.Reader
io.ReaderAt
Readdirnames(n int) (names []string, err error)
Stat() (os.FileInfo, error)
}
type OSMetaInfoFileSystem struct {
dir string
}
func (o *OSMetaInfoFileSystem) Open(name string) (MetaInfoFile, error) {
return os.Open(path.Join(o.dir, name))
}
func (o *OSMetaInfoFileSystem) Stat(name string) (os.FileInfo, error) {
return os.Stat(path.Join(o.dir, name))
}
// Adapt a MetaInfoFileSystem into a torrent file store FileSystem
type FileStoreFileSystemAdapter struct {
m MetaInfoFileSystem
}
type FileStoreFileAdapter struct {
f MetaInfoFile
}
func (f *FileStoreFileSystemAdapter) Open(name []string, length int64) (file File, err error) {
var ff MetaInfoFile
ff, err = f.m.Open(path.Join(name...))
if err != nil {
return
}
stat, err := ff.Stat()
if err != nil {
return
}
actualSize := stat.Size()
if actualSize != length {
err = fmt.Errorf("Unexpected file size %v. Expected %v", actualSize, length)
return
}
file = &FileStoreFileAdapter{ff}
return
}
func (f *FileStoreFileAdapter) ReadAt(p []byte, off int64) (n int, err error) {
return f.f.ReadAt(p, off)
}
func (f *FileStoreFileAdapter) WriteAt(p []byte, off int64) (n int, err error) {
// Writes must match existing data exactly.
q := make([]byte, len(p))
_, err = f.ReadAt(q, off)
if err != nil {
return
}
if bytes.Compare(p, q) != 0 {
err = fmt.Errorf("New data does not match original data.")
}
return
}
func (f *FileStoreFileAdapter) Close() (err error) {
return f.f.Close()
}
// Create a MetaInfo for a given file and file system.
// If fs is nil then the OSMetaInfoFileSystem will be used.
// If pieceLength is 0 then an optimal piece length will be chosen.
func CreateMetaInfoFromFileSystem(fs MetaInfoFileSystem, root string, pieceLength int64, wantMD5Sum bool) (metaInfo *MetaInfo, err error) {
if fs == nil {
dir, file := path.Split(root)
fs = &OSMetaInfoFileSystem{dir}
root = file
}
var m *MetaInfo = &MetaInfo{}
var fileInfo os.FileInfo
fileInfo, err = fs.Stat(root)
if err != nil {
return
}
var totalLength int64
if fileInfo.IsDir() {
err = m.addFiles(fs, root)
if err != nil {
return
}
for i := range m.Info.Files {
totalLength += m.Info.Files[i].Length
}
if wantMD5Sum {
for i := range m.Info.Files {
fd := &m.Info.Files[i]
fd.Md5sum, err = md5Sum(fs, path.Join(fd.Path...))
if err != nil {
return
}
}
}
} else {
m.Info.Name = path.Base(root)
totalLength = fileInfo.Size()
m.Info.Length = totalLength
if wantMD5Sum {
m.Info.Md5sum, err = md5Sum(fs, root)
if err != nil {
return
}
}
}
if pieceLength == 0 {
pieceLength = choosePieceLength(totalLength)
}
m.Info.PieceLength = int64(pieceLength)
fileStoreFS := &FileStoreFileSystemAdapter{fs}
var fileStore FileStore
var fileStoreLength int64
fileStore, fileStoreLength, err = NewFileStore(&m.Info, fileStoreFS)
if err != nil {
return
}
if fileStoreLength != totalLength {
err = fmt.Errorf("Filestore total length %v, expected %v", fileStoreLength, totalLength)
return
}
var sums []byte
sums, err = computeSums(fileStore, totalLength, int64(pieceLength))
if err != nil {
return
}
m.Info.Pieces = string(sums)
m.UpdateInfoHash(metaInfo)
metaInfo = m
return
}
const MinimumPieceLength = 16 * 1024
const TargetPieceCountLog2 = 10
const TargetPieceCountMin = 1 << TargetPieceCountLog2
// Target piece count should be < TargetPieceCountMax
const TargetPieceCountMax = TargetPieceCountMin << 1
// Choose a good piecelength.
func choosePieceLength(totalLength int64) (pieceLength int64) {
// Must be a power of 2.
// Must be a multiple of 16KB
// Prefer to provide around 1024..2048 pieces.
pieceLength = MinimumPieceLength
pieces := totalLength / pieceLength
for pieces >= TargetPieceCountMax {
pieceLength <<= 1
pieces >>= 1
}
return
}
func roundUpToPowerOfTwo(v uint64) uint64 {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v |= v >> 32
v++
return v
}
func WriteMetaInfoBytes(root string, w io.Writer) (err error) {
var m *MetaInfo
m, err = CreateMetaInfoFromFileSystem(nil, root, 0, true)
if err != nil {
return
}
// log.Printf("Metainfo: %#v", m)
err = m.Bencode(w)
if err != nil {
return
}
return
}
func md5Sum(fs MetaInfoFileSystem, file string) (sum string, err error) {
var f MetaInfoFile
f, err = fs.Open(file)
if err != nil {
return
}
defer f.Close()
hash := md5.New()
_, err = io.Copy(hash, f)
if err != nil {
return
}
sum = string(hash.Sum(nil))
return
}
func (m *MetaInfo) addFiles(fs MetaInfoFileSystem, file string) (err error) {
var fileInfo os.FileInfo
fileInfo, err = fs.Stat(file)
if err != nil {
return
}
if fileInfo.IsDir() {
var f MetaInfoFile
f, err = fs.Open(file)
if err != nil {
return
}
var fi []string
fi, err = f.Readdirnames(0)
if err != nil {
return
}
for _, name := range fi {
err = m.addFiles(fs, path.Join(file, name))
if err != nil {
return
}
}
} else {
fileDict := FileDict{Length: fileInfo.Size()}
cleanFile := path.Clean(file)
parts := strings.Split(cleanFile, string(os.PathSeparator))
fileDict.Path = parts
m.Info.Files = append(m.Info.Files, fileDict)
}
return
}
// Updates the InfoHash field. Call this after manually changing the Info data.
func (m *MetaInfo) UpdateInfoHash(metaInfo *MetaInfo) (err error) {
var b bytes.Buffer
infoMap := m.Info.toMap()
if len(infoMap) > 0 {
err = bencode.Marshal(&b, infoMap)
if err != nil {
return
}
}
hash := sha1.New()
hash.Write(b.Bytes())
m.InfoHash = string(hash.Sum(nil))
return
}
// Copy the non-default values from an InfoDict to a map.
func (i *InfoDict) toMap() (m map[string]interface{}) {
id := map[string]interface{}{}
// InfoDict
if i.PieceLength != 0 {
id["piece length"] = i.PieceLength
}
if i.Pieces != "" {
id["pieces"] = i.Pieces
}
if i.Private != 0 {
id["private"] = i.Private
}
if i.Name != "" {
id["name"] = i.Name
}
if i.Length != 0 {
id["length"] = i.Length
}
if i.Md5sum != "" {
id["md5sum"] = i.Md5sum
}
if len(i.Files) > 0 {
var fi []map[string]interface{}
for ii := range i.Files {
f := &i.Files[ii]
fd := map[string]interface{}{}
if f.Length > 0 {
fd["length"] = f.Length
}
if len(f.Path) > 0 {
fd["path"] = f.Path
}
if f.Md5sum != "" {
fd["md5sum"] = f.Md5sum
}
if len(fd) > 0 {
fi = append(fi, fd)
}
}
if len(fi) > 0 {
id["files"] = fi
}
}
if len(id) > 0 {
m = id
}
return
}
// Encode to Bencode, but only encode non-default values.
func (m *MetaInfo) Bencode(w io.Writer) (err error) {
var mi map[string]interface{} = map[string]interface{}{}
id := m.Info.toMap()
if len(id) > 0 {
mi["info"] = id
}
// Do not encode InfoHash. Clients are supposed to calculate it themselves.
if m.Announce != "" {
mi["announce"] = m.Announce
}
if len(m.AnnounceList) > 0 {
mi["announce-list"] = m.AnnounceList
}
if m.CreationDate != "" {
mi["creation date"] = m.CreationDate
}
if m.Comment != "" {
mi["comment"] = m.Comment
}
if m.CreatedBy != "" {
mi["created by"] = m.CreatedBy
}
if m.Encoding != "" {
mi["encoding"] = m.Encoding
}
bencode.Marshal(w, mi)
return
}
type TrackerResponse struct {
FailureReason string `bencode:"failure reason"`
WarningMessage string `bencode:"warning message"`
Interval uint
MinInterval uint `bencode:"min interval"`
TrackerId string `bencode:"tracker id"`
Complete uint
Incomplete uint
Peers string
Peers6 string
}
type SessionInfo struct {
PeerId string
Port uint16
Uploaded uint64
Downloaded uint64
Left uint64
UseDHT bool
FromMagnet bool
HaveTorrent bool
OurExtensions map[int]string
ME *MetaDataExchange
}
type MetaDataExchange struct {
Transferring bool
Pieces [][]byte
}
func getTrackerInfo(dialer Dialer, url string) (tr *TrackerResponse, err error) {
r, err := proxyHttpGet(dialer, url)
if err != nil {
return
}
defer r.Body.Close()
if r.StatusCode >= 400 {
data, _ := ioutil.ReadAll(r.Body)
reason := "Bad Request " + string(data)
log.Println(reason)
err = errors.New(reason)
return
}
var tr2 TrackerResponse
err = bencode.Unmarshal(r.Body, &tr2)
r.Body.Close()
if err != nil {
return
}
tr = &tr2
return
}
func saveMetaInfo(metadata string) (err error) {
var info InfoDict
err = bencode.Unmarshal(bytes.NewReader([]byte(metadata)), &info)
if err != nil {
return
}
f, err := os.Create(info.Name + ".torrent")
if err != nil {
log.Println("Error when opening file for creation: ", err)
return
}
defer f.Close()
_, err = f.WriteString(metadata)
return
}