-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.py
executable file
·206 lines (160 loc) · 4.66 KB
/
request.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python
import getopt
import bencoding
import sys
import socket
import random
import urllib
import hashlib
BUFSIZ = 4096
alpha_num = [chr((ord('0')) + i) for i in range(0, 10)] + [chr((ord('A')) + i) for i in range(0, 26)]
def send_request(remote_host, remote_port, request_string):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((socket.gethostbyname(socket.gethostname()), 6881))
remote_addr = socket.gethostbyname(remote_host)
sock.settimeout(3.0)
print "Sending request to " + str(remote_addr) + " (" + remote_host + ") over port " + str(remote_port) + "\n"
sock.connect((remote_addr, remote_port))
sock.send(request_string)
resp = ""
s = sock.recv(BUFSIZ)
while s != "":
resp = resp + s
s = sock.recv(BUFSIZ)
return resp
def parse_tracker_response(resp):
p = resp
i = resp.index("\r\n")
while i != -1: #and p[:2] != "\r\n":
p = p[i+ 2:]
try:
i = p.index("\r\n")
except ValueError:
break
b = bencoding.bencode_parse_dict(p)
return b
# Generates a psuedo-random ID using Python's PRNG and SHA-1 to get an output of length 20
def generate_peer_id():
random.seed()
s = ""
for i in range(0, 13):
r = int(random.random() * 36)
a = alpha_num[r]
s += a
return '-UT2210' + s
def url_encode(s):
return urllib.quote_plus(s)
# Tracker should have protcol, host, and port in it
def generate_request(trackers, info_hash, peer_id, filename):
peer_id = urllib.quote_plus(peer_id)
request = "GET /announce?"
request = request + "info_hash=" + str(info_hash)
request = request + "&peer_id=" + peer_id
request = request + "&port=" + str(6881)
request = request + "&uploaded=0"
request = request + "&downloaded=0"
request = request + "&left=10000"
request = request + "&compact=1"
request = request + " HTTP/1.1\r\n\r\n"
print "Client Request: \n"
print request
for tracker in trackers:
tracker_proto, tracker_url = tracker.split("://")
if ':' in tracker_url:
tracker_host, tracker_end = tracker_url.split(":")
tmp = tracker_end.split("/")
if len(tmp) > 1:
tracker_port, tracker_path = tmp
else:
tracker_port = tmp[0]
else:
tracker_port = 80
tmp = tracker_url.split("/")
if len(tmp) > 1:
tracker_host, tracker_path = tmp
else:
tracker_host = tmp[0]
try:
socket.gethostbyname(tracker_host)
except Exception:
continue
try:
r = send_request(tracker_host, int(tracker_port), request)
return r
except Exception:
continue
raise socket.timeout
def tracker_request(filename, no_dns):
h = bencoding.url_hash(filename)
torrent_file = open(filename, "rb")
torrent_data = torrent_file.read()
(length, metadata) = bencoding.bencode_parse(torrent_data)
track = bencoding.http_trackers(metadata)
if len(track) == 0:
raise Exception("No HTTP trackers found (engine does not currently support UDP trackers)")
print "Using tracker at " + track[0]
peer_id = generate_peer_id()
resp = generate_request(track, h, peer_id, "")
print resp
(x, r) = parse_tracker_response(resp)
d = bencoding.bencode_dict_no_tod(r)
peers = ""
try:
peers = d["peers"]
except KeyError:
try:
tmp = d["peers6"]
except KeyError:
raise Exception("Didn't find peers")
raise Exception("IPv6 not supported!")
l = [None] * (len(peers) / 6)
for i in range(0, len(peers) / 6):
ip = str(ord(peers[i * 6 + 0])) + '.' \
+ str(ord(peers[i * 6 + 1])) + '.' \
+ str(ord(peers[i * 6 + 2])) + '.' \
+ str(ord(peers[i * 6 + 3]))
port = str(((ord(peers[i * 6 + 4]) << 8) + ord(peers[i * 6 + 5])))
l[i] = (ip, port)
# l.sort()
#for i in range(0, len(l)):
#addr = l[i]
#ip, port = addr.split(":")
#s = addr
#try:
#if not no_dns:
#name, alias, address = socket.gethostbyaddr(ip)
#l[i] = addr + " (" + name + ") "
#except Exception:
#pass
# print addr
return l
if __name__ == "__main__":
print alpha_num
no_dns = False
show_pieces = False
argv = sys.argv
opts, args = getopt.getopt(argv[1:], "h", ["no-dns", "show-pieces"])
filename = args[0]
for o, a in opts:
if o == "--no-dns":
no_dns = True
elif o == "--show-pieces":
show_pieces = True
elif o == "-h":
sha_hash = bencoding.info_hash(filename)
print "SHA-1 Info Hash of file " + filename + ": " + str(sha_hash) + '\n'
else:
assert False, "unhandled option"
l = tracker_request(filename, no_dns)
print 'List of peers (ip, port):\n'
for addr in l:
# Default is not to do reverse DNS lookups on the IP addresses given by the tracker
name = ""
(ip, port) = addr
if not no_dns:
try:
name, aliases, address = socket.gethostbyaddr(ip)
name = " (" + name + ") "
except Exception:
pass
print ip + ":" + port + name