premiere-libtorrent/tools/dht_flood.py

79 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
2009-04-13 21:16:14 +02:00
import socket
import sys
from types import StringType, IntType, LongType, DictType, ListType, TupleType
import random
port = int(sys.argv[1])
# from BitTorrent 4.3.0
def encode_bencached(x, r):
2009-04-13 21:16:14 +02:00
r.append(x.bencoded)
2009-04-13 21:16:14 +02:00
def encode_int(x, r):
r.extend(('i', str(x), 'e'))
2009-04-13 21:16:14 +02:00
def encode_string(x, r):
r.extend((str(len(x)), ':', x))
2009-04-13 21:16:14 +02:00
def encode_list(x, r):
r.append('l')
for i in x:
encode_func[type(i)](i, r)
r.append('e')
def encode_dict(x, r):
2009-04-13 21:16:14 +02:00
r.append('d')
ilist = sorted(x.items())
2009-04-13 21:16:14 +02:00
for k, v in ilist:
r.extend((str(len(k)), ':', k))
encode_func[type(v)](v, r)
r.append('e')
2009-04-13 21:16:14 +02:00
encode_func = {}
encode_func[IntType] = encode_int
encode_func[LongType] = encode_int
encode_func[StringType] = encode_string
encode_func[ListType] = encode_list
encode_func[TupleType] = encode_list
encode_func[DictType] = encode_dict
2009-04-13 21:16:14 +02:00
def bencode(x):
r = []
encode_func[type(x)](x, r)
return ''.join(r)
2009-04-13 21:16:14 +02:00
def send_dht_message(msg):
s.sendto(bencode(msg), 0, ('127.0.0.1', port))
2009-04-13 21:16:14 +02:00
def random_key():
ret = ''
for i in range(0, 20):
ret += chr(random.randint(0, 255))
return ret
2009-04-13 21:16:14 +02:00
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
node_id = '1' * 20
2009-04-13 21:16:14 +02:00
query = 'get_peers'
print('test random info-hashes')
for i in range(1, 30000):
send_dht_message({'a': {'id': node_id, 'info_hash': random_key()}, 'q': query, 'y': 'q', 't': '%d' % i})
2009-04-13 21:16:14 +02:00
print('test random peer-ids')
for i in range(1, 30000):
send_dht_message({'a': {'id': random_key(), 'info_hash': random_key()}, 'q': query, 'y': 'q', 't': '%d' % i})