/
main.go
65 lines (51 loc) · 1.26 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
// Utility to list torrent file contents
package main
import (
"fmt"
"github.com/marksamman/bencode"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: lstorrent [files]")
return
}
for _, f := range os.Args[1:] {
lstorrent(f)
}
}
func lstorrent(path string) {
fmt.Printf("%s\n", path)
defer fmt.Printf("\n")
f, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
fmt.Printf("Failed opening file: ", err)
return
}
defer f.Close()
datb, err := bencode.Decode(f)
if err != nil {
fmt.Printf("Failed decoding:", err)
return
}
tor_info, ok := datb["info"].(map[string]interface{})
if !ok {
}
// first look for files key
files, ok := tor_info["files"].([]interface{})
if ok {
for _, v := range files {
file := v.(map[string]interface{})
path := file["path"].([]interface{})[0].(string)
fmt.Printf("%s\n", path)
}
return // finished printing multifile torrent
}
// second look for name key
file, ok := tor_info["name"].(string)
if ok {
fmt.Printf("%s\n", file)
return
}
fmt.Printf("couldnt find files in torrent: %s", path)
}