mirror of https://github.com/r4sas/py-i2phosts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.2 KiB
103 lines
3.2 KiB
14 years ago
|
#!/usr/bin/python
|
||
|
|
||
|
import os
|
||
|
import sys
|
||
14 years ago
|
import argparse
|
||
14 years ago
|
import configobj
|
||
14 years ago
|
import datetime
|
||
14 years ago
|
|
||
14 years ago
|
from django.core.exceptions import ValidationError
|
||
14 years ago
|
|
||
14 years ago
|
# parse command line options
|
||
|
parser = argparse.ArgumentParser(
|
||
|
description='Hosts injector for py-i2phosts.',
|
||
|
epilog='Report bugs to http://zzz.i2p/topics/733')
|
||
14 years ago
|
parser.add_argument('-c', '--config', default='/etc/py-i2phosts/injector.conf', dest='config_file',
|
||
|
help='config file to use')
|
||
14 years ago
|
parser.add_argument('-f', '--file', dest='hostsfile',
|
||
14 years ago
|
help='hosts.txt for parsing')
|
||
14 years ago
|
parser.add_argument('-d', '--description', default='Auto-added from external hosts.txt',
|
||
|
help='provide custom description message')
|
||
14 years ago
|
parser.add_argument('-a', '--approve', action='store_true',
|
||
|
help='add hosts as approved')
|
||
14 years ago
|
parser.add_argument('-s', '--supress', action='store_true',
|
||
14 years ago
|
help='supress warnings about already existed hostnames'),
|
||
|
parser.add_argument('-q', '--quiet', action='store_true',
|
||
|
help='be completely quiet, print only errors')
|
||
14 years ago
|
args = parser.parse_args()
|
||
14 years ago
|
|
||
14 years ago
|
# read config
|
||
|
config = configobj.ConfigObj(args.config_file)
|
||
|
if 'include' in config:
|
||
|
config_included = configobj.ConfigObj(config['include'])
|
||
|
config.merge(config_included)
|
||
|
|
||
14 years ago
|
# django setup
|
||
|
DJANGO_SETTINGS_MODULE = 'settings'
|
||
|
if 'DJANGO_PROJECT_PATH' in config:
|
||
|
DJANGO_PROJECT_PATH = config['DJANGO_PROJECT_PATH']
|
||
|
else:
|
||
|
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
|
||
14 years ago
|
from pyi2phosts.postkey.models import i2phost
|
||
|
from pyi2phosts.lib.validation import validate_hostname
|
||
|
from pyi2phosts.lib.validation import validate_b64hash
|
||
14 years ago
|
|
||
14 years ago
|
# determine approve hosts or not
|
||
14 years ago
|
if args.approve or config.as_bool('approve'):
|
||
14 years ago
|
approved = True
|
||
|
else:
|
||
|
approved = False
|
||
|
|
||
14 years ago
|
# turn on output supressing if quiet
|
||
14 years ago
|
if args.quiet:
|
||
|
args.supress = True
|
||
|
|
||
14 years ago
|
# determine what hosts.txt file we should parse
|
||
|
if args.hostsfile:
|
||
|
hostsfile = args.hostsfile
|
||
|
else:
|
||
|
env = os.environ
|
||
|
if 'HOME' in env:
|
||
|
hostsfile = os.environ['HOME'] + '/.i2p/hosts.txt'
|
||
|
else:
|
||
|
sys.stderr.write('unable to determine hosts file for parsing\n')
|
||
|
sys.exit(1)
|
||
|
|
||
14 years ago
|
f = open(args.hostsfile, 'r')
|
||
14 years ago
|
for line in f:
|
||
14 years ago
|
# ignore comments and empty lines
|
||
|
if line.startswith('#') or line.isspace():
|
||
|
continue
|
||
14 years ago
|
if line.find('=') == -1:
|
||
14 years ago
|
sys.stdout.write('Invalid line: %s\n' % line)
|
||
14 years ago
|
continue
|
||
14 years ago
|
# strip trailing '\n'
|
||
|
line = line.rstrip('\n')
|
||
14 years ago
|
entry = line.split('=')
|
||
14 years ago
|
try:
|
||
|
hostname = validate_hostname(entry[0])
|
||
|
base64 = validate_b64hash(entry[1], check_uniq=False) # don't require uniqueness
|
||
|
except ValidationError, e:
|
||
14 years ago
|
sys.stdout.write('validation error: %s: %s\n\n' % (e, line))
|
||
14 years ago
|
else:
|
||
14 years ago
|
# Check for already existed hosts in database to avoid unneeded INSERTs
|
||
|
# beacuse they will fail anyway.
|
||
14 years ago
|
try:
|
||
|
h = i2phost.objects.get(name=hostname)
|
||
|
except i2phost.DoesNotExist:
|
||
14 years ago
|
if not args.quiet:
|
||
14 years ago
|
sys.stdout.write('Adding %s\n' % hostname)
|
||
14 years ago
|
host = i2phost(name=hostname, b64hash=base64,
|
||
14 years ago
|
description=args.description,
|
||
14 years ago
|
date_added=datetime.datetime.utcnow(),
|
||
14 years ago
|
activated=False, external=True, approved=approved)
|
||
14 years ago
|
host.save()
|
||
14 years ago
|
else:
|
||
|
if not args.supress:
|
||
14 years ago
|
sys.stdout.write('Host %s already exists\n' % hostname)
|
||
14 years ago
|
if h.b64hash != base64:
|
||
14 years ago
|
sys.stdout.write('Key conflict for host: %s\n' % hostname)
|
||
14 years ago
|
f.close()
|