-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (107 loc) · 3.03 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
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"runtime"
bencode "github.com/anacrolix/torrent/bencode"
)
type bdecodedInfo struct {
Name string `bencode:"name"`
Length int `bencode:"length"`
PieceLength int `bencode:"piece length"`
Pieces string `bencode:"pieces"`
}
type bdecodedFile struct {
Announce string `bencode:"announce"`
PublisherUrl string `bencode:"publisher-url"`
Info bdecodedInfo `bencode:"info"`
InfoHash [20]byte
PeerId [20]byte
}
// USAGE: exileum [path to torrent file] [output path]
func main() {
// Setup
args := os.Args[1:]
var torStruct bdecodedFile
var pts = &torStruct
// rawTorrentfile := make(map[string]interface{}) //t
if !isInputValid() {
log.Fatal("Input invalid, check if input and output filepaths are valid")
}
// build torrent filepath
torrentFilePath := getFilePath(args[0])
// read torrent file into memory
openedFilepath, err := os.ReadFile(torrentFilePath)
ErrCheck(err)
// unmarshal into rawTorrentfile empty interface basically
// if err := bencode.Unmarshal(openedFilepath, &rawTorrentfile); err != nil {
// panic(err)
// }
// unmarshal into main struct
if err1 := bencode.Unmarshal(openedFilepath, &torStruct); err1 != nil {
panic(err1)
}
pts.computeInfoHash()
pts.computePeerId()
fmt.Println("\n\n INFOHASH \n\n", torStruct.InfoHash)
prettyprint, _ := json.MarshalIndent(torStruct, "", " ")
fmt.Println(string(prettyprint))
// fmt.Println(hex.EncodeToString(torStruct.InfoHash[:]))
pts.getPeerlistRequestURL()
}
func isInputValid() bool {
if len(os.Args) < 2 {
return false
} else {
return true
}
}
func getFilePath(file string) (filepath string) {
_, fullWorkingDir, _, _ := runtime.Caller(0)
workingDir, _ := path.Split(fullWorkingDir)
return workingDir + file
}
// adds the InfoHash key to the bdecodedFile struct
func (i *bdecodedFile) computeInfoHash() {
data, err := bencode.Marshal(i.Info)
if err != nil {
panic(err) // panic v fatal
}
i.InfoHash = sha1.Sum(data)
}
func (i *bdecodedFile) computePeerId() {
r := make([]byte, 1)
rand.Read(r)
i.PeerId = sha1.Sum(r)
}
// returns the tracker URL with params included
func (i *bdecodedFile) getPeerlistRequestURL() {
base, err := url.Parse(i.Announce)
fmt.Println(base)
ErrCheck(err)
v := url.Values{}
v.Set("info_hash", string(i.InfoHash[:]))
v.Set("peer_id", string(i.PeerId[:]))
base.RawQuery = v.Encode()
fmt.Println(base.String())
fmt.Println(base.RawQuery)
req, _ := http.NewRequest("GET", base.String(), nil)
res, err := http.DefaultClient.Do(req)
ErrCheck(err)
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
res.Body.Close()
// url := "http://bt3.t-ru.org/ann?info_hash=ef8c5b770da5119f584eacb61c024251fc112d03&left=5854908&peer_id=fdcb4c5d2e905e1526c373e34663204d65d0777e"
// http://bt3.t-ru.org/ann
// ?info_hash=ef8c5b770da5119f584eacb61c024251fc112d03
// &peer_id=fdcb4c5d2e905e1526c373e34663204d65d0777e
}