def _on_new_channel(self, splitted_line): if (len(splitted_line) != 7 or splitted_line[1] != 'OPEN' or splitted_line[3] != 'HASH'): raise SanitizeError, '? Invalid OPEN message' try: send = int(splitted_line[2]) except (ValueError): raise SanitizeError, '? Channel must be integer' key, lookup_mode, port_str = splitted_line[4:] try: info_hash = identifier.Id(key) except (identifier.IdError): raise SanitizeError, '? Invalid key (must be 40 HEX characters)' if lookup_mode != 'FAST': raise SanitizeError, '? Only FAST lookup supported' try: port = int(port_str) except (ValueError): raise SanitizeError, '? Port must be integer' if port > MAX_PORT: raise SanitizeError, '? Invalid port number' channel = self.open_channels.create(send) dht.get_peers(channel, info_hash, self._on_peers_found, port) return '%d OPEN %d' % (channel.send, channel.recv)
def main(options, args): my_addr = (options.ip, int(options.port)) logs_path = options.path logging_conf.setup(logs_path, logs_level) print 'Using the following plug-ins:' print '*', options.routing_m_file print '*', options.lookup_m_file routing_m_name = '.'.join(os.path.split(options.routing_m_file))[:-3] routing_m_mod = __import__(routing_m_name, fromlist=['']) lookup_m_name = '.'.join(os.path.split(options.lookup_m_file))[:-3] lookup_m_mod = __import__(lookup_m_name, fromlist=['']) dht = pymdht.Pymdht(my_addr, logs_path, routing_m_mod, lookup_m_mod) print '\nType "exit" to stop the DHT and exit' print 'Type an info_hash (in hex digits): ', while (1): input = sys.stdin.readline()[:-1] if input == 'exit': dht.stop() break try: info_hash = identifier.Id(input) except (identifier.IdError): print 'Invalid input (%s)' % input continue print 'Getting peers for info_hash %r' % info_hash global start_ts start_ts = time.time() dht.get_peers(None, info_hash, _on_peers_found)
def _on_new_channel(self, splitted_line): if (len(splitted_line) != 6 or splitted_line[1] != 'OPEN' or splitted_line[3] != 'HASH'): raise SanitizeError, '? Invalid OPEN message' try: send = int(splitted_line[2]) except (ValueError): raise SanitizeError, '? Channel must be integer' key, port_str = splitted_line[4:] try: info_hash = identifier.Id(key) except (identifier.IdError): raise SanitizeError, '? Invalid key (must be 40 HEX characters)' # if lookup_mode not in ('SLOW', 'FAST'): # raise SanitizeError, '? Only FAST and SLOW lookup supported' try: port = int(port_str) except (ValueError): raise SanitizeError, '? Port must be integer' if port > MAX_BT_PORT: raise SanitizeError, '? Invalid port number' channel = self.open_channels.create(send) self.wfile.write('%d OPEN %d\r\n' % (channel.send, channel.recv)) success = dht.get_peers(channel, info_hash, self._on_peers_found, port) if 0: #not success: print 'no success' self.open_channels.remove(channel) self.wfile.write('%d CLOSE\r\n' % (channel.send))
def command_user_interface(dht): print '\nType "exit" to stop the DHT and exit' print 'Type "help" if you need' while (1): input = sys.stdin.readline().strip().split() if not input: continue command = input[0] if command == 'help': print ''' Available commands are: - help - fast info_hash bt_port - exit - m Memory information - r Print routing table stats - rr Print routing table (full) ''' elif command == 'exit': dht.stop() break elif command == 'm': import guppy h = guppy.hpy() print h.heap() elif command == 'r': dht.print_routing_table_stats() elif command == 'rr': dht.print_routing_table() elif command == 'fast': if len(input) != 3: print 'usage: fast info_hash bt_port' continue try: info_hash = identifier.Id(input[1]) except (identifier.IdError): print 'Invalid info_hash (%s)' % input[1] continue try: bt_port = int(input[2]) except: print 'Invalid bt_port (%r)' % input[2] continue if 0 < bt_port < MIN_BT_PORT: print 'Mmmm, you are using reserved ports (<1024). Try again.' continue if bt_port > MAX_BT_PORT: print "I don't know about you, but I find difficult", print "to represent %d with only two bytes." % (bt_port), print "Try again." continue dht.get_peers(time.time(), info_hash, _on_peers_found, bt_port) else: print 'Invalid input: type help'
def main(options, args): my_addr = (options.ip, int(options.port)) logs_path = options.path print 'Using the following plug-ins:' print '*', options.routing_m_file print '*', options.lookup_m_file print 'Private DHT name:', options.private_dht_name routing_m_name = '.'.join(os.path.split(options.routing_m_file))[:-3] routing_m_mod = __import__(routing_m_name, fromlist=['']) lookup_m_name = '.'.join(os.path.split(options.lookup_m_file))[:-3] lookup_m_mod = __import__(lookup_m_name, fromlist=['']) dht = pymdht.Pymdht(my_addr, logs_path, routing_m_mod, lookup_m_mod, options.private_dht_name, logs_level) print '\nType "exit" to stop the DHT and exit' print 'Type "help" if you need' while (1): input = sys.stdin.readline().strip().split() if not input: continue command = input[0] if command == 'help': print ''' Available commands are: - help - fast info_hash bt_port - exit - m Memory information ''' elif command == 'exit': dht.stop() break elif command == 'm': import guppy h = guppy.hpy() print h.heap() elif command == 'fast': if len(input) != 3: print 'usage: fast info_hash bt_port' continue try: info_hash = identifier.Id(input[1]) except (identifier.IdError): print 'Invalid info_hash (%s)' % input[1] try: bt_port = int(input[2]) except: print 'Invalid bt_port (%r)' % input[2] continue success = dht.get_peers(time.time(), info_hash, _on_peers_found, bt_port) if not success: print 'Lookup failed'
def _on_new_channel(self, splitted_line): if (len(splitted_line) != 7 or splitted_line[1] != 'OPEN' or splitted_line[3] != 'HASH'): raise SanitizeError, '? Invalid OPEN message' try: send = int(splitted_line[2]) except (ValueError): raise SanitizeError, '? Channel must be integer' key, lookup_mode, port_str = splitted_line[4:] try: info_hash = identifier.Id(key) except (identifier.IdError): raise SanitizeError, '? Invalid key (must be 40 HEX characters)' if lookup_mode not in ('SLOW', 'FAST'): raise SanitizeError, '? Only FAST and SLOW lookup supported' try: port = int(port_str) except (ValueError): raise SanitizeError, '? Port must be integer' if port > MAX_PORT: raise SanitizeError, '? Invalid port number' channel = self.open_channels.create(send) self.wfile.write('%d OPEN %d\r\n' % (channel.send, channel.recv)) success = dht.get_peers(channel, info_hash, self._on_peers_found, port) # if peers: # for peer in peers: # if peer not in channel.peers: # channel.peers.add(peer) # peer_score = geo_score.score_peer(peer[0]) # self.wfile.write('%d PEER %s:%d SCORE %d\r\n' % (channel.send, # peer[0], peer[1], # peer_score)) if not success: print 'no success' self.open_channels.remove(channel) self.wfile.write('%d CLOSE\r\n' % (channel.send))
import time import sys import string import core.identifier as identifier import core.pymdht as pymdht import logging import plugins.lookup_a16 as lookup_m_mod import plugins.routing_nice_rtt as routing_m_mod #logs_path = './interactive_logs/' logs_path = '.' logs_level = logging.DEBUG # This generates HUGE (and useful) logs #logs_level = logging.INFO # This generates some (useful) logs #logs_level = logging.WARNING # This generates warning and error logs if len(sys.argv) == 4: my_addr = ('127.0.0.1', int(sys.argv[1])) id_ = identifier.Id(sys.argv[2]).generate_close_id(int(sys.argv[3])) dht = pymdht.Pymdht(my_addr, logs_path, routing_m_mod, lookup_m_mod, None, logs_level, id_) while (True): time.sleep(5) else: print 'usage: python incoming_traffic.py dht_port info_hash log_distance'
CONFIG = ( [pymdht, ('192.16.127.98', 7010), 'ns0', r_nice, l_a4], [pymdht, ('192.16.127.98', 7011), 'ns1', r_nice, l_a4], [pymdht, ('192.16.127.98', 7012), 'ns2', r_nice, l_a4], [pymdht, ('192.16.127.98', 7013), 'ns3', r_nice, l_a4], [pymdht, ('192.16.127.98', 7014), 'ns4', r_nice, l_a4], [pymdht, ('192.16.127.98', 7015), 'ns5', r_nice, l_a4], [pymdht, ('192.16.127.98', 7016), 'ns6', r_nice, l_a4], [pymdht, ('192.16.127.98', 7017), 'ns7', r_nice, l_a4], [pymdht, ('192.16.127.98', 7018), 'ns8', r_nice, l_a4], [pymdht, ('192.16.127.98', 7019), 'ns9', r_nice, l_a4], ) # Add a column with the node_id (close to info_hash) for i, line in enumerate(open('infohashes.dat')): infohash = identifier.Id(line.strip()) node_id = infohash.generate_close_id(131) CONFIG[i].append(node_id) def _on_peers_found(lookup_id, peers): if peers: print '[%.4f] %d peer(s)' % (time.time(), len(peers)) else: print '[%.4f] END OF LOOKUP' % (time.time()) def _randompopper(seq): """ Make a copy of the sequence and return a generator. This generator will yield a random element popped from the sequence until all the elements
def start_get_peers(self, dht, infohash, port): input = ['fast', infohash, port] dht.start_capture() dht.get_peers(time.time(), identifier.Id(input[1]), self._on_peers_found, int(input[2]))
def main(options, args): if not os.path.isdir(options.path): if os.path.exists(options.path): print >> sys.stderr, 'FATAL:', options.path, 'must be a directory' return print >> sys.stderr, options.path, 'does not exist. Creating directory...' os.mkdir(options.path) logs_path = options.path if options.lookup_delay and not options.daemon: print >> sys.stderr, 'Switching to DAEMON mode (no user interface)' if options.lookup_delay or options.daemon: # redirect output stdout_file = os.path.join(options.path, 'pymdht.stdout') stderr_file = os.path.join(options.path, 'pymdht.stderr') print >> sys.stderr, 'Redirecting output to %s and %s' % (stdout_file, stderr_file) sys.stdout = open(stdout_file, 'w') sys.stderr = open(stderr_file, 'w') my_addr = (options.ip, int(options.port)) my_id = None if options.node_id: base_id = identifier.Id(options.node_id) my_id = base_id.generate_close_id(options.log_distance) my_node = node.Node(my_addr, my_id, version=pymdht.VERSION_LABEL) if options.debug: logs_level = logging.DEBUG # This generates HUGE (and useful) logs else: # logs_level = logging.INFO # This generates some (useful) logs logs_level = logging.WARNING # This generates warning and error logs print 'Using the following plug-ins:' print '*', options.routing_m_file print '*', options.lookup_m_file print '*', options.experimental_m_file print 'Path:', options.path print 'Private DHT name:', options.private_dht_name print 'debug mode:', options.debug print 'bootstrap mode:', options.bootstrap_mode print 'Swift tracker port:', options.swift_port routing_m_name = '.'.join(os.path.split(options.routing_m_file))[:-3] routing_m_mod = __import__(routing_m_name, fromlist=['']) lookup_m_name = '.'.join(os.path.split(options.lookup_m_file))[:-3] lookup_m_mod = __import__(lookup_m_name, fromlist=['']) experimental_m_name = '.'.join(os.path.split( options.experimental_m_file))[:-3] experimental_m_mod = __import__(experimental_m_name, fromlist=['']) dht = pymdht.Pymdht(my_node, logs_path, routing_m_mod, lookup_m_mod, experimental_m_mod, options.private_dht_name, logs_level, options.bootstrap_mode, options.swift_port) if options.lookup_delay: loop_forever = not options.num_lookups remaining_lookups = options.num_lookups while loop_forever or remaining_lookups: time.sleep(options.lookup_delay) if options.lookup_target: target = identifier.Id(options.lookup_target) else: target = identifier.RandomId() print 'lookup', target dht.get_peers(None, target, None, options.announce_port) remaining_lookups = remaining_lookups - 1 time.sleep(options.stop_delay) dht.stop() elif options.ttl: stop_timestamp = time.time() + int(options.ttl) while time.time() < stop_timestamp: time.sleep(1) dht.stop() elif options.daemon: # Just loop for ever while True: time.sleep(10) elif options.gui: import wx import ui.gui app = wx.PySimpleApp() frame = ui.gui.Interactive_GUI(None, "Interactive Look@MDHT", None, (1440, 900), dht, logs_path) frame.Show(True) app.MainLoop() elif options.telnet_port: import ui.telnet telnet_ui = ui.telnet.Telnet(dht, options.telnet_port) telnet_ui.start() elif options.cli: import ui.cli ui.cli.command_user_interface(dht)
def on_press(self): dht.get_peers( self, identifier.Id('e936e73881ee1920b8edbd263d001fffed424c5f'), _on_peers_handler)
def main(): now = datetime.datetime.now() timestamp_str = now.strftime("%Y%m%d-%H%M%S") results_path = timestamp_str + '_tag' os.mkdir(results_path) shutil.copy('infohashes.dat', results_path) shutil.copy('conductor.py', results_path) shutil.copy('parser.py', results_path) shutil.copy('plotter.py', results_path) shutil.copytree('parsers', os.path.join(results_path, 'parsers')) shutil.copytree('plotters', os.path.join(results_path, 'plotters')) captures_path = os.path.abspath( os.path.join(results_path, timestamp_str + '.pcap')) print 'Now, you need to start capturing netwok traffic' print 'Windows:\nWinDump.exe -C 500 -s 0 -w %s udp' % (captures_path) print 'Linux:\nsudo tcpdump -C 500 -s 0 -w %s udp' % (captures_path) print '-' * 70 print 'Press ENTER to continue' sys.stdin.readline() nodes = [] for mod, addr, node_name, r_mod, l_mod in _randompopper(CONFIG): # Create infohash generator infohash_gen = _randompopper(INFOHASHES) print 'Starting %s %r, %r %r...' % (node_name, addr, r_mod.__file__, l_mod.__file__), sys.stdout.flush() node_path = os.path.join(results_path, node_name) os.mkdir(node_path) node = mod.Pymdht(addr, node_path, r_mod, l_mod, None, logs_level) nodes.append((node_name, node, infohash_gen)) print 'DONE' time.sleep(STARTUP_DELAY) #Leave them some time to bootstrap time.sleep(BOOTSTRAP_DELAY) running = True for round_number in xrange(len(INFOHASHES)): # In every round, DHT nodes are randomly ordered for node_name, node, infohash_gen in _randompopper(nodes): # Every DHT node performs a lookup infohash = identifier.Id(infohash_gen.next()) print '%d %s getting peers for info_hash %r' % ( round_number, node_name, infohash) node.get_peers((node_name, time.time()), infohash, _on_peers_found, 0) time.sleep(REMOVE_TORRENT_DELAY) try: remove_torrent_f = node.remove_torrent except: pass else: remove_torrent_f(infohash) time.sleep(LOOKUP_DELAY - REMOVE_TORRENT_DELAY) time.sleep(ROUND_DELAY) for node_name, node, _ in nodes: node.stop() print 'Stopping %s ...' % node_name, sys.stdout.flush() time.sleep(STOPPING_DELAY) print 'DONE' print '-' * 70 print 'Now, stop the network capturing with Ctr-C'