Changeset - fd0f9336d6d8
[Not reviewed]
default
0 2 0
Dennis Fink - 11 years ago 2014-07-15 23:16:16
dennis.fink@c3l.lu
Breaking API change. obfs and fteproxy need to be booleans not string
2 files changed with 9 insertions and 16 deletions:
0 comments (0 inline, 0 general)
Scripts/update_server.py
Show inline comments
 
@@ -54,36 +54,30 @@ def get_tor_fingerprint(name='tor'):
 
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):
 

	
 
    if any(OBFS_REGEX.match(i) for i in tor_config):
 
        return 'True'
 
    else:
 
        return 'False'
 
    return any(OBFS_REGEX.match(i) for i in tor_config)
 

	
 

	
 
def get_fte_proxy(tor_config):
 

	
 
    if any(FTEPROXY_REGEX.match(i) for i in tor_config):
 
        return 'True'
 
    else:
 
        return 'False'
 
    return any(FTEPROXY_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
 

	
ennstatus/api/functions.py
Show inline comments
 

	
 
import re
 
import json
 

	
 
from datetime import datetime
 
from ast import literal_eval
 

	
 
import pygeoip
 

	
 
FINGERPRINT_REGEX = re.compile(r'^[A-Z0-9]{40}$', re.I)
 

	
 
IP_REGEX = (r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
 
            r'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
 
IP_REGEX = re.compile(IP_REGEX)
 

	
 
PRIVATE_IP_REGEX = (r'(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)'
 
                    r'|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|'
 
                    r'255\.255\.255\.255')
 
PRIVATE_IP_REGEX = re.compile(PRIVATE_IP_REGEX)
 

	
 
DATE_FORMAT = '%d-%m-%Y %H:%M:%S'
 

	
 
gi4 = pygeoip.GeoIP('/usr/share/GeoIP/GeoIP.dat', pygeoip.MEMORY_CACHE)
 

	
 

	
 
def check_bridge(key, server):
 

	
 
    if key not in server:
 
        raise ValueError('%s key not present!\n' % key)
 
    else:
 
        if server[key] not in ('True', 'False'):
 
            error_message = ('%s has not the right content!'
 
                             ' is: %s must be one of: True or False\n') \
 
                % (key, server[key])
 
        if not isinstance(server[key], bool):
 
            error_message = ('%s has not the right type!'
 
                             ' Needs to be a boolean!\n') \
 
                % key
 
            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') \
 
@@ -66,32 +65,32 @@ def check_json_format(server):
 
            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':
 
        server['obfs'] = literal_eval(server['obfs'])
 
        server['fteproxy'] = literal_eval(server['fteproxy'])
 

	
 
        if 'ip' in server:
 
            del server['ip']
 
    else:
 
        if 'obfs' in server:
 
            del server['obfs']
 

	
 
        if 'fteproxy' in server:
 
            del server['fteproxy']
 

	
 
        server['ip'] = ip
 

	
 
    try:
 
        filename = ''.join(['data/', server['server_name'].lower(), '.json'])
 

	
 
        with open(filename, 'w', encoding='utf-8') as fb:
 
            json.dump(server, fb)
 

	
 
        return server
 
    except Exception as e:
 
        return e
0 comments (0 inline, 0 general)