blob: 5eecb204d3864296832ef28f8460b36ac38bfa90 (
plain)
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
|
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"os"
"strings"
bencode "github.com/jackpal/bencode-go" // Available if you need it!
)
func main() {
command := os.Args[1]
switch command {
case "decode":
bencodedValue := os.Args[2]
decoded, err := bencode.Decode(strings.NewReader(bencodedValue))
if err != nil {
fmt.Println(err)
return
}
jsonOutput, _ := json.Marshal(decoded)
fmt.Println(string(jsonOutput))
case "info":
f, err := os.Open(os.Args[2])
if err != nil {
fmt.Println("Failed to open file: ", os.Args[2])
}
var meta Meta
if err = bencode.Unmarshal(f, &meta); err != nil {
panic(err)
}
fmt.Printf("Tracker URL: %s\n", meta.Announce)
fmt.Printf("Length: %d\n", meta.Info.Length)
sha := sha1.New()
if err = bencode.Marshal(sha, meta.Info); err != nil {
panic(err)
}
fmt.Printf("Info Hash: %x", sha.Sum(nil))
fmt.Printf("Piece Length: %d\n", meta.Info.PieceLength)
fmt.Println("Piece Hashes:")
for i := 0; i < len(meta.Info.Pieces); i += 20 {
fmt.Printf("%x\n", meta.Info.Pieces[i:i+20])
}
default:
fmt.Println("Unknown command: " + command)
os.Exit(1)
}
}
|