Changeset - c34648b1a4d6
[Not reviewed]
default
0 5 0
Dennis Fink - 11 years ago 2014-07-16 01:23:27
dennis.fink@c3l.lu
Add support for flashproxy an meek an version bump
5 files changed with 29 insertions and 4 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)')
 

	
 
FTEPROXY_REGEX = re.compile(r'^ServerTransportPlugin fte')
 
FLASHPROXY_REGEX = re.compile(r'^ServerTransportPlugin webproxy')
 
MEEKPROXY_REGEX = re.compile(r'^ServerTransportPlugin meek')
 

	
 
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'):
 

	
 
    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'
 
@@ -50,94 +51,114 @@ def get_tor_fingerprint(name='tor'):
 
    hostname, fingerprint = line.split(' ')
 
    return hostname, fingerprint
 

	
 

	
 
def get_server_type(tor_config):
 

	
 
    if 'BridgeRelay 1' in tor_config:
 
        return 'Bridge'
 
    elif 'ExitPolicy reject *:*' in tor_config:
 
        return 'Relay', None
 
    else:
 
        return 'Exit', None
 

	
 

	
 
def get_obfs_proxy(tor_config):
 

	
 
    return any(OBFS_REGEX.match(i) for i in tor_config)
 

	
 

	
 
def get_fte_proxy(tor_config):
 

	
 
    return any(FTEPROXY_REGEX.match(i) for i in tor_config)
 

	
 

	
 
def get_flash_proxy(tor_config):
 

	
 
    return any(FLASHPROXY_REGEX.match(i) for i in tor_config)
 

	
 

	
 
def get_meek_proxy(tor_config):
 

	
 
    return any(MEEKPROXY_REGEX.match(i) for i in tor_config)
 

	
 

	
 
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 = get_server_type(tor_config)
 
    hostname, fingerprint = get_tor_fingerprint(name)
 
    tor_status = get_tor_status(name)
 
    ip = get_ip(tor_config)
 

	
 
    obfs = None
 
    fte = None
 
    flash = None
 
    meek = None
 

	
 
    if server_type == 'Bridge':
 
        obfs = get_obfs_proxy(tor_config)
 
        fte = get_fte_proxy(tor_config)
 
        flash = get_flash_proxy(tor_config)
 
        meek = get_meek_proxy(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
 

	
 
    if fte is not None:
 
        dictionary['fteproxy'] = fte
 

	
 
    if flash is not None:
 
        dictionary['flashproxy'] = flash
 

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

	
 
    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:
ennstatus/api/functions.py
Show inline comments
 
@@ -34,49 +34,49 @@ def check_bridge(key, server):
 
            raise ValueError(error_message)
 

	
 

	
 
def check_json_format(server):
 

	
 
    for key in ('server_type', 'server_name', 'tor_status', 'fingerprint'):
 
        if key not in server:
 
            raise ValueError('%s key not present!\n' % key)
 

	
 
    if server['server_type'] not in ('Exit', 'Relay', 'Bridge'):
 
        error_message = ('server_type has not the right content!'
 
                         ' is: %s must be one of: Exit, Relay or Bridge\n') \
 
            % server['server_type']
 
        raise ValueError(error_message)
 

	
 
    if not server['tor_status'] in ('Online', 'Offline'):
 
        error_message = ('tor_status has not the right content!'
 
                         ' is: %s must be one of: Online or Offline\n') \
 
            % server['tor_status']
 

	
 
    if FINGERPRINT_REGEX.match(server['fingerprint']) is None:
 
        raise ValueError('fingerprint has not the right format!\n')
 

	
 
    if server['server_type'] == 'bridge':
 
        for key in ('obfs', 'fteproxy'):
 
        for key in ('obfs', 'fteproxy', 'flashproxy', 'meek'):
 
            check_bridge(key, server)
 

	
 
    if 'ip' in server:
 
        if IP_REGEX.match(server['ip']) is None:
 
            raise ValueError('ip is not the right format!\n')
 
        elif PRIVATE_IP_REGEX.match(server['ip']) is not None:
 
            raise ValueError('ip is not accepted!\n')
 

	
 
    return True
 

	
 

	
 
def update_server(server, ip):
 

	
 
    server['last_updated'] = datetime.utcnow().strftime(DATE_FORMAT)
 
    server['server_status'] = 'Online'
 
    server['country'] = gi4.country_name_by_addr(ip)
 

	
 
    if server['server_type'] == 'Bridge':
 
        if 'ip' in server:
 
            del server['ip']
 
    else:
 
        if 'obfs' in server:
 
            del server['obfs']
 

	
ennstatus/templates/api/export/xml/server.xml
Show inline comments
 
<server>
 
    <server_type>{{ server['server_type'] }}</server_type>
 
    <server_name>{{ server['server_name'] }}</server_name>
 
    <server_status>{{ server['server_status'] }}</server_status>
 
    <tor_status>{{ server['tor_status'] }}</tor_status>
 
    <country>{{ server['country'] }}</country>
 
    <last_updated>{{ server['last_updated'] }}</last_updated>
 
    <fingerprint>{{ server['fingerprint'] }}</fingerprint>
 
    {% if server['server_type'] == 'bridge' %}
 
    <obfs>{{ server['obfs'] }}</obfs>
 
    <fteproxy>{{ server['fteproxy'] }}</fteproxy>
 
    <flashproxy>{{ server['flashproxy'] }}</flashproxy>
 
    <meek>{{ server['meek'] }}</meek>
 
    {% else %}
 
    <ip>{{ server['ip'] }}</ip>
 
    {% endif %}
 
  </server>
ennstatus/templates/status/macros.html
Show inline comments
 
@@ -19,60 +19,62 @@
 
{% endmacro %}
 

	
 
{% macro create_country(country) %}
 
  {% set country_class = "flag-" + country|lower|replace(' ', '-') %}
 
  <i class={{ country_class }}></i> {{ country|title }}
 
{% endmacro %}
 

	
 
{% macro create_fingerprint(fingerprint, server_type) %}
 
  {% if server_type in ('Exit', 'Relay') %}
 
    {% set url_type = 'relay' %}
 
  {% else %}
 
    {% set url_type = 'bridge' %}
 
  {% endif %}
 
  <a href="http://globe.enn.lu/#/{{ url_type }}/{{ fingerprint }}">{{ fingerprint|upper}}</a>
 
{% endmacro %}
 

	
 
{% macro create_name(name) %}
 
  <a href="http://{{ name|lower }}.enn.lu">{{ name }}</a>
 
{% endmacro %}
 

	
 
{% macro create_server_table(server_type, servers) %}
 
  {% if server_type in ('Exit', 'Relay') %}
 
    {% set headers = ('#', 'Name', 'IP', 'Server Status', 'Tor Status', 'Country', 'Fingerprint', 'Last Updated (UTC)') %}
 
  {% else %}
 
    {% set headers = ('#', 'Name', 'Server Status', 'Tor Status', 'Country', 'Fingerprint', 'OBFS', 'FTEProxy', 'Last Updated (UTC)') %}
 
    {% set headers = ('#', 'Name', 'Server Status', 'Tor Status', 'Country', 'Fingerprint', 'OBFS', 'FTEProxy', 'Flashproxy', 'meek', 'Last Updated (UTC)') %}
 
  {% endif %}
 
  <h2>{{ server_type }}</h2>
 
  <table class="table table-bordered table-striped">
 
    <thead>
 
      <tr>
 
        {% for name in headers %}
 
          <th>{{ name }}</th>
 
        {% endfor %}
 
      </tr>
 
    </thead>
 
    <tbody>
 
      {% for server in servers %}
 
        <tr>
 
          <td>{{ loop.index }}</td>
 
          {% if server_type in ('Exit', 'Relay') %}
 
            <td>{{ create_name(server['server_name']) }}</td>
 
            <td>{{ server['ip'] }}</td>
 
          {% else %}
 
            <td>{{ server['server_name'] }}</td>
 
          {% endif %}
 
          {% for status in (server['server_status'], server['tor_status']) %}
 
            <td>{{ colorize_status(status) }}</td>
 
          {% endfor %}
 
          <td>{{ create_country(server['country']) }}</td>
 
          <td>{{ create_fingerprint(server['fingerprint'], server['server_type']) }}</td>
 
          {% if server_type == 'Bridge' %}
 
            <td>{{ colorize_obfs(server['obfs']) }}</td>
 
            <td>{{ colorize_obfs(server['fteproxy']) }}</td>
 
            <td>{{ colorize_obfs(server['flashproxy']) }}</td>
 
            <td>{{ colorize_obfs(server['meek']) }}</td>
 
          {% endif %}
 
          <td>{{ server['last_updated'] }}</td>
 
        </tr>
 
      {% endfor %}
 
    </tbody>
 
  </table>
 
{% endmacro %}
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.1.4',
 
      version='4.2.0',
 
      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+',
 
      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',
0 comments (0 inline, 0 general)