Changeset - f051c683d048
[Not reviewed]
default
0 3 0
Dennis Fink - 11 years ago 2014-07-15 00:28:38
dennis.fink@c3l.lu
Unified strings
3 files changed with 7 insertions and 7 deletions:
0 comments (0 inline, 0 general)
Scripts/update_server.py
Show inline comments
 
import subprocess
 
import configparser
 
import re
 
import json
 

	
 
import requests
 

	
 
from ast import literal_eval
 

	
 
OBFS_REGEX = re.compile(r'^ServerTransportPlugin (obfs\d+|scramblesuit)')
 

	
 
IP_REGEX = re.compile(r'^(OutboundBindAddress)\ (\d{1,3}\.\d{1,3}\.\d{1,3}\.'
 
                      r'\d{1,3})')
 

	
 

	
 
def read_tor_config(configfile="/etc/tor/torrc"):
 
def read_tor_config(configfile='/etc/tor/torrc'):
 

	
 
    with open(configfile) as fb:
 
        lines = {line[:-1] for line in fb if not line.startswith('#')}
 

	
 
    lines = {line for line in filter(None, lines)}
 
    return lines
 

	
 

	
 
def get_tor_status(name='tor'):
 

	
 
    try:
 
        pids = subprocess.check_output(['pidof', 'tor']).decode('utf-8')[:-1]
 
        pids = pids.split(' ')
 
        pid_file = '.'.join([name, 'pid'])
 
        pid = open('/'.join(['/var', 'run', 'tor', pid_file])).readline()[:-1]
 

	
 
        if str(pid) in pids:
 
            return "Online"
 
            return 'Online'
 
        else:
 
            return "Offline"
 
            return 'Offline'
 
    except subprocess.CalledProcessError:
 
        return "Offline"
 
        return 'Offline'
 

	
 

	
 
def get_tor_fingerprint(name='tor'):
 

	
 
    fingerprint_file = '/'.join(['/var', 'lib', name, 'fingerprint'])
 

	
 
    with open(fingerprint_file) as fb:
 
        line = fb.readline()[:-1]
 

	
 
    hostname, fingerprint = line.split(' ')
 
    return hostname, fingerprint
 

	
 

	
 
def get_server_type(tor_config):
 

	
 
    if 'BridgeRelay 1' in tor_config:
 
        if any(OBFS_REGEX.match(i) for i in tor_config):
 
            return 'Bridge', 'True'
 
        else:
 
            return 'Bridge', 'False'
 
    elif 'ExitPolicy reject *:*' in tor_config:
 
        return 'Relay', None
 
    else:
 
        return 'Exit', None
 

	
 

	
 
def get_ip(tor_config):
 

	
 
    for i in tor_config:
 
        match = IP_REGEX.match(i)
 

	
 
        if match is not None:
 
            return match.groups()[1]
 

	
 
    return None
 

	
 

	
 
def get_config():
 

	
 
    config = configparser.ConfigParser()
 
    config.read('/etc/ennstatus_updater.conf')
 

	
 
    return config
 

	
 

	
 
def create_server_json(tor_configfile='/etc/tor/torrc', name='tor'):
 

	
 
    tor_config = read_tor_config(tor_configfile)
 
    server_type, obfs = get_server_type(tor_config)
 
    hostname, fingerprint = get_tor_fingerprint(name)
 
    tor_status = get_tor_status(name)
 
    ip = get_ip(tor_config)
 

	
 
    dictionary = {'server_type': server_type, 'server_name': hostname,
 
                  'tor_status': tor_status, 'fingerprint': fingerprint}
 

	
 
    if ip is not None:
 
        dictionary['ip'] = ip
 

	
 
    if obfs is not None:
 
        dictionary['obfs'] = obfs
 

	
 
    return dictionary
 

	
 

	
 
def update_server(server_json, url):
 

	
 
    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
 

	
 
    response = requests.post(url + '/api/update', data=json.dumps(server_json),
 
                             headers=headers)
 
    return response.text
 

	
 

	
 
def main():
 

	
 
    config = get_config()
 
    ennstatus_url = config['main']['ennstatus_url']
 

	
 
    if 'servers' in config['main']:
 
        for server in literal_eval(config['main']['servers']):
 
            server_json = create_server_json(config[server]['configfile'],
 
                                             server)
 
            response = update_server(server_json, ennstatus_url)
 
            print(response, end='')
 
    else:
 
        server_json = create_server_json()
 
        response = update_server(server_json, ennstatus_url)
 
        print(response, end='')
 

	
 

	
 
if __name__ == '__main__':
 
    main()
manage.py
Show inline comments
 
import pprint
 
import subprocess
 
import os
 
import os.path
 

	
 
from flask import current_app
 
from flask.ext.script import Manager
 
from flask.ext.script.commands import Clean, ShowUrls
 

	
 
os.environ['ENNSTATUS_SETTINGS'] = '/home/dennis/Projects/Coding/Python/ennstatus/config.py'
 

	
 
from ennstatus import app
 

	
 

	
 
manager = Manager(app)
 

	
 

	
 
@manager.command
 
def dumpconfig():
 
    """Show config."""
 

	
 
    pprint.pprint(current_app.config)
 

	
 

	
 
def checkers(checker):
 

	
 
    def get_files(root, files):
 
        for f in files:
 
            if os.path.splitext(f)[1] == '.py':
 
                yield os.path.join(root, f)
 

	
 
    for root, dirs, files in os.walk('./'):
 
        for f in get_files(root, files):
 
            subprocess.call([checker, f])
 

	
 

	
 
@manager.command
 
def pep8():
 
    """Check all py files for pep8 compat."""
 

	
 
    checkers('pep8')
 

	
 

	
 
@manager.command
 
def pep257():
 
    """Check all py files for pep257 compat."""
 

	
 
    checkers('pep257')
 

	
 

	
 
manager.add_command("clean", Clean())
 
manager.add_command("urls", ShowUrls())
 
manager.add_command('clean', Clean())
 
manager.add_command('urls', ShowUrls())
 

	
 
if __name__ == "__main__":
 
    manager.run()
setup.py
Show inline comments
 
from setuptools import setup, find_packages
 

	
 

	
 
def _get_requirements():
 

	
 
    with open('requirements.in', encoding='utf-8') as f:
 
        lines = f.readlines()
 

	
 
    lines = [line[:-1] for line in lines if not line.startswith('#')]
 
    return lines
 

	
 

	
 
setup(name='Ennstatus',
 
      version='4.0.7',
 
      description=('Ennstatus provides the user with vital information about '
 
                   'the status of the organizations Tor servers.'),
 
      author='Frënn vun der Ënn',
 
      author_email='info@enn.lu',
 
      url='https://bitbucket.org/fvde/ennstatus',
 
      license="GPLv3+",
 
      license='GPLv3+',
 
      packages=find_packages(),
 
      package_data={'ennstatus': ['static/css/*.css',
 
                                  'static/css/flags/img/png/*.png',
 
                                  'static/css/flags/img/gif/*.gif',
 
                                  'static/css/flags/*.css',
 
                                  'static/files/*',
 
                                  'static/images/*',
 
                                  'static/videos/*',
 
                                  'templates/*.html',
 
                                  'templates/api/export/xml/*.xml',
 
                                  'templates/donate/*.html',
 
                                  'templates/root/*.html',
 
                                  'templates/status/*.html',
 
                                  'templates/stats/*.html',
 
                                  'templates/errorpages/*.html',
 
                                  ]},
 
      install_requires=_get_requirements(),
 
      classifiers=['Development Status :: 5 - Production/Stable',
 
                   'Environment :: Web Environment',
 
                   'Operating System :: POSIX',
 
                   'Programming Language :: Python',
 
                   'Programming Language :: Python :: 3',
 
                   'Programming Language :: Python :: 3.3',
 
                   'Topic :: Internet',
 
                   'Topic :: Internet :: WWW/HTTP',
 
                   'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
 
                   'Topic :: Internet :: WWW/HTTP :: WSGI',
 
                   'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
 
                   ('License :: OSI Approved :: '
 
                    'GNU General Public License v3 or later (GPLv3+)'),
 
                   ]
 
      )
0 comments (0 inline, 0 general)