]>
Commit | Line | Data |
---|---|---|
43d991f6 MT |
1 | #!/usr/bin/python |
2 | ||
3 | import time | |
4 | ||
5 | import tornado.database | |
6 | ||
7 | ||
8 | def decode_hex(s): | |
9 | ret = [] | |
10 | for c in s: | |
11 | for i in range(256): | |
12 | if not c == chr(i): | |
13 | continue | |
14 | ||
15 | ret.append("%0x" % i) | |
16 | ||
17 | return "".join(ret) | |
18 | ||
19 | class Tracker(object): | |
20 | id = "The IPFire Torrent Tracker" | |
21 | ||
22 | # Intervals | |
23 | interval = 60*60 | |
24 | min_interval = 30*60 | |
25 | ||
26 | numwant = 50 | |
27 | ||
28 | def __init__(self): | |
29 | self.db = tornado.database.Connection( | |
30 | host="172.28.1.150", | |
31 | database="tracker", | |
32 | user="tracker", | |
33 | ) | |
34 | ||
e2afbd6a | 35 | def _fetch(self, hash, limit=None, random=False, completed=False, no_peer_id=False): |
43d991f6 MT |
36 | query = "SELECT * FROM peers WHERE last_update >= %d" % self.since |
37 | ||
38 | if hash: | |
39 | query += " AND hash = '%s'" % hash | |
40 | ||
41 | if completed: | |
42 | query += " AND left_data = 0" | |
43 | ||
44 | if random: | |
45 | query += " ORDER BY RAND()" | |
46 | ||
47 | if limit: | |
48 | query += " LIMIT %s" % limit | |
49 | ||
50 | peers = [] | |
51 | for peer in self.db.query(query): | |
52 | if not peer.ip or not peer.port: | |
53 | continue | |
54 | ||
e2afbd6a | 55 | peer_dict = { |
43d991f6 MT |
56 | "ip" : str(peer.ip), |
57 | "port" : int(peer.port), | |
e2afbd6a MT |
58 | } |
59 | ||
60 | if not no_peer_id: | |
61 | peer_dict["peer id"] = str(peer.id), | |
62 | ||
63 | peers.append(peer_dict) | |
43d991f6 MT |
64 | |
65 | return peers | |
66 | ||
67 | def get_peers(self, hash, **kwargs): | |
68 | return self._fetch(hash, **kwargs) | |
69 | ||
70 | def get_seeds(self, hash, **kwargs): | |
71 | kwargs.update({"completed" : True}) | |
72 | return self._fetch(hash, **kwargs) | |
73 | ||
74 | def complete(self, hash): | |
75 | return len(self.get_seeds(hash)) | |
76 | ||
77 | def incomplete(self, hash): | |
78 | return len(self.get_peers(hash)) | |
79 | ||
80 | def event_started(self, hash, peer_id): | |
81 | # Damn, mysql does not support INSERT IF NOT EXISTS... | |
82 | if not self.db.query("SELECT id FROM peers WHERE hash = '%s' AND peer_id = '%s'" % (hash, peer_id)): | |
83 | self.db.execute("INSERT INTO peers(hash, peer_id) VALUES('%s', '%s')" % (hash, peer_id)) | |
84 | ||
85 | if not hash in [h["hash"] for h in self.hashes]: | |
86 | self.db.execute("INSERT INTO hashes(hash) VALUES('%s')" % hash) | |
87 | ||
88 | def event_stopped(self, hash, peer_id): | |
89 | self.db.execute("DELETE FROM peers WHERE hash = '%s' AND peer_id = '%s'" % (hash, peer_id)) | |
90 | ||
91 | def event_completed(self, hash, peer_id): | |
92 | self.db.execute("UPDATE hashes SET completed=completed+1 WHERE hash = '%s'" % hash) | |
93 | ||
e2afbd6a MT |
94 | def scrape(self, hashes=[]): |
95 | ret = {} | |
96 | for hash in self.db.query("SELECT hash, completed FROM hashes"): | |
97 | hash, completed = hash.hash, hash.completed | |
98 | ||
99 | if hashes and hash not in hashes: | |
100 | continue | |
101 | ||
102 | ret[hash] = { | |
103 | "complete" : self.complete(hash), | |
104 | "downloaded" : completed or 0, | |
105 | "incomplete" : self.incomplete(hash), | |
106 | } | |
107 | ||
108 | return ret | |
109 | ||
43d991f6 MT |
110 | def update(self, hash, id, ip=None, port=None, downloaded=None, uploaded=None, left=None): |
111 | args = [ "last_update = '%s'" % self.now ] | |
112 | ||
113 | if ip: | |
114 | args.append("ip='%s'" % ip) | |
115 | ||
116 | if port: | |
117 | args.append("port='%s'" % port) | |
118 | ||
119 | if downloaded: | |
120 | args.append("downloaded='%s'" % downloaded) | |
121 | ||
122 | if uploaded: | |
123 | args.append("uploaded='%s'" % uploaded) | |
124 | ||
125 | if left: | |
126 | args.append("left_data='%s'" % left) | |
127 | ||
128 | if not args: | |
129 | return | |
130 | ||
131 | query = "UPDATE peers SET " + ", ".join(args) + \ | |
132 | " WHERE hash = '%s' AND peer_id = '%s'" % (hash, id) | |
133 | ||
134 | self.db.execute(query) | |
135 | ||
136 | @property | |
137 | def hashes(self): | |
138 | return self.db.query("SELECT * FROM hashes"); | |
139 | ||
140 | @property | |
141 | def now(self): | |
142 | return int(time.time()) | |
143 | ||
144 | @property | |
145 | def since(self): | |
146 | return int(time.time() - self.interval) | |
147 | ||
148 | ||
149 | tracker = Tracker() | |
150 | ||
151 | ||
152 | ##### This is borrowed from the bittorrent client libary ##### | |
153 | ||
154 | def decode_int(x, f): | |
155 | f += 1 | |
156 | newf = x.index('e', f) | |
157 | n = int(x[f:newf]) | |
158 | if x[f] == '-': | |
159 | if x[f + 1] == '0': | |
160 | raise ValueError | |
161 | elif x[f] == '0' and newf != f+1: | |
162 | raise ValueError | |
163 | return (n, newf+1) | |
164 | ||
165 | def decode_string(x, f): | |
166 | colon = x.index(':', f) | |
167 | n = int(x[f:colon]) | |
168 | if x[f] == '0' and colon != f+1: | |
169 | raise ValueError | |
170 | colon += 1 | |
171 | return (x[colon:colon+n], colon+n) | |
172 | ||
173 | def decode_list(x, f): | |
174 | r, f = [], f+1 | |
175 | while x[f] != 'e': | |
176 | v, f = decode_func[x[f]](x, f) | |
177 | r.append(v) | |
178 | return (r, f + 1) | |
179 | ||
180 | def decode_dict(x, f): | |
181 | r, f = {}, f+1 | |
182 | while x[f] != 'e': | |
183 | k, f = decode_string(x, f) | |
184 | r[k], f = decode_func[x[f]](x, f) | |
185 | return (r, f + 1) | |
186 | ||
187 | decode_func = {} | |
188 | decode_func['l'] = decode_list | |
189 | decode_func['d'] = decode_dict | |
190 | decode_func['i'] = decode_int | |
191 | decode_func['0'] = decode_string | |
192 | decode_func['1'] = decode_string | |
193 | decode_func['2'] = decode_string | |
194 | decode_func['3'] = decode_string | |
195 | decode_func['4'] = decode_string | |
196 | decode_func['5'] = decode_string | |
197 | decode_func['6'] = decode_string | |
198 | decode_func['7'] = decode_string | |
199 | decode_func['8'] = decode_string | |
200 | decode_func['9'] = decode_string | |
201 | ||
202 | def bdecode(x): | |
203 | try: | |
204 | r, l = decode_func[x[0]](x, 0) | |
205 | except (IndexError, KeyError, ValueError): | |
206 | raise Exception("not a valid bencoded string") | |
207 | if l != len(x): | |
208 | raise Exception("invalid bencoded value (data after valid prefix)") | |
209 | return r | |
210 | ||
211 | from types import StringType, IntType, LongType, DictType, ListType, TupleType | |
212 | ||
213 | ||
214 | class Bencached(object): | |
215 | ||
216 | __slots__ = ['bencoded'] | |
217 | ||
218 | def __init__(self, s): | |
219 | self.bencoded = s | |
220 | ||
221 | def encode_bencached(x,r): | |
222 | r.append(x.bencoded) | |
223 | ||
224 | def encode_int(x, r): | |
225 | r.extend(('i', str(x), 'e')) | |
226 | ||
227 | def encode_bool(x, r): | |
228 | if x: | |
229 | encode_int(1, r) | |
230 | else: | |
231 | encode_int(0, r) | |
232 | ||
233 | def encode_string(x, r): | |
234 | r.extend((str(len(x)), ':', x)) | |
235 | ||
236 | def encode_list(x, r): | |
237 | r.append('l') | |
238 | for i in x: | |
239 | encode_func[type(i)](i, r) | |
240 | r.append('e') | |
241 | ||
242 | def encode_dict(x,r): | |
243 | r.append('d') | |
244 | ilist = x.items() | |
245 | ilist.sort() | |
246 | for k, v in ilist: | |
247 | r.extend((str(len(k)), ':', k)) | |
248 | encode_func[type(v)](v, r) | |
249 | r.append('e') | |
250 | ||
251 | encode_func = {} | |
252 | encode_func[Bencached] = encode_bencached | |
253 | encode_func[IntType] = encode_int | |
254 | encode_func[LongType] = encode_int | |
255 | encode_func[StringType] = encode_string | |
256 | encode_func[ListType] = encode_list | |
257 | encode_func[TupleType] = encode_list | |
258 | encode_func[DictType] = encode_dict | |
259 | ||
260 | try: | |
261 | from types import BooleanType | |
262 | encode_func[BooleanType] = encode_bool | |
263 | except ImportError: | |
264 | pass | |
265 | ||
266 | def bencode(x): | |
267 | r = [] | |
268 | encode_func[type(x)](x, r) | |
269 | return ''.join(r) |