From fafa9925074bbb0bb89917b4de111352f566e7df Mon Sep 17 00:00:00 2001 From: Hidden Z Date: Wed, 20 Oct 2010 17:12:29 +0000 Subject: [PATCH] py-i2phosts-checker: simple availability checker for i2p It uses b32->b64 lookup test through SAM interface for determining hosts availability. For communicating with SAM it uses python i2p library from SAM contrib (i2p-0.8/apps/sam/python/src/i2p). --- py-i2phosts-checker | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 py-i2phosts-checker diff --git a/py-i2phosts-checker b/py-i2phosts-checker new file mode 100755 index 0000000..8b85986 --- /dev/null +++ b/py-i2phosts-checker @@ -0,0 +1,61 @@ +#!/usr/bin/python + +import os +import sys +import argparse +import logging +import datetime +import time +import hashlib +import base64 + +from i2p import samclasses +from i2p import socket + +# django setup +DJANGO_SETTINGS_MODULE = 'settings' +DJANGO_PROJECT_PATH = os.path.dirname(sys.argv[0]) + '/web' +sys.path.insert(1, DJANGO_PROJECT_PATH) +os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE +from web.postkey.models import i2phost + +# parse command line options +parser = argparse.ArgumentParser( + description='Hosts checker for py-i2phosts.', + epilog='Report bugs to http://zzz.i2p/topics/733') +parser.add_argument('-d', '--debug', action='store_true', + help='enable debug messages') +args = parser.parse_args() + +# configure logger +log = logging.getLogger(sys.argv[0]) +handler = logging.StreamHandler() +log.addHandler(handler) +if args.debug: + log.setLevel(logging.DEBUG) +else: + log.setLevel(logging.INFO) + +all_hosts = i2phost.objects.all() + +S = samclasses.BaseSession('127.0.0.1:7656') +for host in all_hosts: + log.debug('testing %s', host.name) + # get b32 address from full dest key + dest = host.b64hash + raw_key = base64.b64decode(dest.encode('utf-8'), '-~') + hash = hashlib.sha256(raw_key) + b32dest = base64.b32encode(hash.digest()).lower().replace('=', '')+'.b32.i2p' + # do name lookup query with b32 address + # it success only if host is alive + try: + a = S._namelookup(b32dest) + except socket.NetworkError, e: + log.debug('%s: %s', host.name, e.args[0][0]) + continue + log.info('alive host: %s', host.name) + # update lastseen timestamp + host.last_seen = datetime.datetime.now() + host.save() +S.close() +