/
main.go
178 lines (152 loc) · 4.52 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
package main
import (
"flag"
"fmt"
"github.com/NebulousLabs/go-upnp"
"github.com/kr/pretty"
"github.com/stratospark/torro/bencoding"
"github.com/stratospark/torro/client"
"github.com/stratospark/torro/structure"
"io"
"io/ioutil"
"log"
"os"
"time"
)
func main() {
// Set up logging to file and stdout
f, err := os.OpenFile("torro.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("Error opening file: %v", err)
}
defer f.Close()
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
println("TORRO!\n\n\n")
// Read command line flags and arguments
pPrint := flag.String("print", "metainfo", "either tokens, parsed, or metainfo")
pUPNP := flag.Bool("upnp", false, "open port through UPNP")
pAnnounce := flag.Bool("announce", false, "send announce request to tracker")
flag.Parse()
var filename string
fmt.Println(flag.Args())
if len(flag.Args()) > 0 {
filename = flag.Args()[0]
} else {
filename = "testfiles/TheInternetsOwnBoyTheStoryOfAaronSwartz_archive.torrent"
}
if *pUPNP {
fmt.Println("Opening port through UPNP")
d, _ := upnp.Discover()
ip, _ := d.ExternalIP()
_ = d.Forward(55555, "torro")
defer d.Clear(55555)
log.Printf("Discovered: %q\n", d)
log.Printf("External IP: %q\n", ip)
log.Printf("Location: %q\n", d.Location())
}
// Read actual .torrent file
fmt.Println("Parsing: ", filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
// Lex and Parse .torrent file
torrentStr := string(data)
lex := bencoding.BeginLexing(".torrent", torrentStr, bencoding.LexBegin)
tokens := bencoding.Collect(lex)
output := bencoding.Parse(tokens)
result := output.Output.(map[string]interface{})
// Read .torrent metainfo and make request to the announce URL
metainfo := structure.NewMetainfo(filename)
c := client.NewTrackerClient()
req := structure.NewTrackerRequest(metainfo)
req.PeerID = "-qB3230-u~QGMmUs~yXH"
req.Port = 55555
req.Compact = true
req.NoPeerID = true
if *pAnnounce {
res, err := c.MakeAnnounceRequest(req, client.TrackerRequestStarted)
if err != nil {
fmt.Println(res.FailureReason)
panic(err.Error())
}
fmt.Println(res)
log.Println("StartListening")
port := 55555
peerId := []byte("-TR2840-nj5ovtkoz2ed")
s := client.NewBTService(port, peerId)
s.StartListening()
}
switch *pPrint {
case "tokens":
PrintTokens(&tokens)
case "parsed":
PrintParsedStructure(result)
case "metainfo":
PrintMetainfo(metainfo)
default:
PrintMetainfo(metainfo)
}
// time.Sleep(time.Second * 60)
}
func PrintTokens(tokens *[]bencoding.Token) {
fmt.Println("Printing Tokens:\n\n")
for _, token := range *tokens {
fmt.Println(token)
}
}
func PrintParsedStructure(result map[string]interface{}) {
conv := func(val interface{}) string {
b, _ := val.([]uint8)
return string(b)
}
pretty.Println("Announce: ", conv(result["announce"]))
pretty.Println("Announce-List", conv(result["annnounce-list"]))
if result["creation date"] != nil {
creationDate := int64(result["creation date"].(int))
t := time.Unix(creationDate, 0)
pretty.Println("Creation Date:", t.String())
}
pretty.Println("Comment:", conv(result["comment"]))
pretty.Println("Created by:", conv(result["created by"]))
pretty.Println("Encoding:", conv(result["encoding"]))
info := result["info"].(map[string]interface{})
pretty.Println("Info Piece Length:", info["piece length"])
pretty.Println("Info Private:", info["private"])
pretty.Println("Info/Name:", conv(info["name"]))
pretty.Println("Info/piece length:", info["piece length"])
pretty.Println("Info/pieces:", len(conv(info["pieces"])))
pretty.Println("Info/pieces/20:", len(conv(info["pieces"]))/20)
// pretty.Println("Info/pieces:", info["pieces"].(string)[:4])
pretty.Println("Info/Length:", info["length"])
pretty.Println("Info/md5sum:", info["md5sum"])
// pretty.Println("Info/files:", info["files"])
if info["files"] != nil {
files := info["files"].([]interface{})
for i, val := range files {
// pretty.Println(i, ": ", val)
pretty.Println("\n\n", i)
file := val.(map[string]interface{})
for key, val2 := range file {
switch val2.(type) {
case int:
pretty.Println(key, ": ", val2)
case []uint8:
pretty.Println(key, ": ", conv(val2))
case []interface{}:
path := val2.([]interface{})
pretty.Println("Paths:")
for _, val3 := range path {
pretty.Println("\t\t", conv(val3))
}
default:
pretty.Println(key, ": ", val2)
}
}
}
}
}
func PrintMetainfo(metainfo *structure.Metainfo) {
fmt.Println(metainfo.Announce)
}