Browse Source

Rewrite py-i2phosts-checker to use BOB instead of SAM

pull/1/head
Hidden Z 11 years ago
parent
commit
37e3e4a43e
  1. 51
      bin/py-i2phosts-checker

51
bin/py-i2phosts-checker

@ -5,9 +5,7 @@ import sys
import argparse import argparse
import datetime import datetime
import configobj import configobj
import socket
from i2p import samclasses
from i2p import socket
# parse command line options # parse command line options
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -60,13 +58,27 @@ else:
log_file = config['log_file'] log_file = config['log_file']
log = get_logger(filename=log_file, log_level=log_level) log = get_logger(filename=log_file, log_level=log_level)
# determine SAM interface address # determine BOB interface address
if 'sam_addr' in config: if 'bob_addr' in config:
sam_addr = config['sam_addr'] bob_addr = config['bob_addr']
else: else:
log.warning('SAM address isn\'t specified in config, falling back to localhost') log.warning('BOB address isn\'t specified in config, falling back to localhost')
sam_addr = '127.0.0.1:7656' bob_addr = '127.0.0.1:2827'
S = samclasses.BaseSession(sam_addr)
# split bob_addr to ip and port
bob_ip, bob_port = bob_addr.split(':')
# connect to BOB
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((bob_ip, int(bob_port)))
# just receive BOB's greeting
data = s.recv(512)
# make file object
f = s.makefile('r')
except socket.error, e:
log.error('failed to connect to BOB: %s', e)
sys.exit(1)
all_hosts = i2phost.objects.all().order_by('-activated', '-last_seen') all_hosts = i2phost.objects.all().order_by('-activated', '-last_seen')
log.info('starting check') log.info('starting check')
@ -77,14 +89,17 @@ for host in all_hosts:
b32dest = get_b32(dest) b32dest = get_b32(dest)
# do name lookup query with b32 address # do name lookup query with b32 address
# it success only if host is alive # it success only if host is alive
try: s.send('lookup %s\n' % b32dest)
a = S._namelookup(b32dest) data = f.readline().rstrip('\n')
except socket.NetworkError, e: if data == 'ERROR Address Not found.':
log.debug('%s: %s', host.name, e.args[0][0]) log.debug('unable to resolve: %s', host.name)
continue continue
log.info('alive host: %s', host.name) elif data == 'OK ' + host.b64hash:
# update lastseen timestamp log.info('alive host: %s', host.name)
host.last_seen = datetime.datetime.utcnow() # update lastseen timestamp
host.save() host.last_seen = datetime.datetime.utcnow()
S.close() host.save()
else:
log.warning('unexpected reply: %s', data)
s.close()
log.info('check finished') log.info('check finished')

Loading…
Cancel
Save