aboutsummaryrefslogtreecommitdiff
path: root/cmd/mybittorrent/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/mybittorrent/main.go')
-rw-r--r--cmd/mybittorrent/main.go22
1 files changed, 13 insertions, 9 deletions
diff --git a/cmd/mybittorrent/main.go b/cmd/mybittorrent/main.go
index 7905e01..d8014e7 100644
--- a/cmd/mybittorrent/main.go
+++ b/cmd/mybittorrent/main.go
@@ -1,10 +1,13 @@
package main
import (
+ "crypto/sha1"
"encoding/json"
"fmt"
"os"
- // bencode "github.com/jackpal/bencode-go" // Available if you need it!
+ "strings"
+
+ bencode "github.com/jackpal/bencode-go" // Available if you need it!
)
func main() {
@@ -13,8 +16,7 @@ func main() {
switch command {
case "decode":
bencodedValue := os.Args[2]
-
- decoded, err := NewDecoder(bencodedValue).Decode()
+ decoded, err := bencode.Decode(strings.NewReader(bencodedValue))
if err != nil {
fmt.Println(err)
return
@@ -26,18 +28,20 @@ func main() {
case "info":
f, err := os.Open(os.Args[2])
if err != nil {
- fmt.Println("Failed to open file: " + os.Args[2])
+ fmt.Println("Failed to open file: ", os.Args[2])
}
- tf := NewFile()
+ var meta Meta
+ if err = bencode.Unmarshal(f, &meta); err != nil {
+ panic(err)
+ }
- err = tf.ReadFrom(f)
- if err != nil {
- fmt.Println("Failed to parse file: " + os.Args[2])
+ sha := sha1.New()
+ if err = bencode.Marshal(sha, meta.Info); err != nil {
panic(err)
}
- fmt.Printf("Tracker URL: %s\nLength: %d", tf.Announce, tf.Info.Length)
+ fmt.Printf("Tracker URL: %s\nLength: %d\nInfo Hash: %x", meta.Announce, meta.Info.Length, sha.Sum(nil))
default:
fmt.Println("Unknown command: " + command)