Changeset - 9dd5a0479455
[Not reviewed]
default
0 2 0
Dennis Fink - 10 years ago 2014-12-10 23:07:05
dennis.fink@c3l.lu
Started ipv 6 support
2 files changed with 40 insertions and 14 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/functions.py
Show inline comments
 

	
 
import re
 
import json
 
import ipaddress
 

	
 
from datetime import datetime
 

	
 
import pygeoip
 

	
 
from flask import current_app
 

	
 
from ennstatus.status.functions import _send_mail
 

	
 

	
 
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)
 

	
 

	
 
mail_cache = dict()
 

	
 

	
 
def check_bridge(key, server):
 

	
 
    if key not in server:
 
        raise ValueError('%s key not present!\n' % key)
 
@@ -57,56 +49,81 @@ def check_json_format(server):
 
        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', 'flashproxy', 'meek'):
 
            check_bridge(key, server)
 

	
 
    if 'ip' in server:
 
        if IP_REGEX.match(server['ip']) is None:
 
        try:
 
            address = ipaddress.IPv4Address(server['ip'])
 

	
 
            if any([address.is_private, address.is_multicast,
 
                    address.is_unspecified, address.is_reserved,
 
                    address.is_loopback, address.link_local]):
 
                raise ValueError('ip is not accepted!\n')
 

	
 
        except ipaddress.AddressValueError:
 
            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')
 

	
 
    if 'ip6' in server:
 
        try:
 
            address = ipaddress.IPv6Address(server['ip6'])
 
            if any([address.is_private, address.is_multicast,
 
                    address.is_unspecified, address.is_reserved,
 
                    address.is_loopback, address.link_local]):
 
                raise ValueError('ip is not accepted!\n')
 

	
 
        except ipaddress.AddressValueError:
 
            raise ValueError('ip is not the right format!\n')
 

	
 
    return True
 

	
 

	
 
def _send_offline_mail(server_name, last_updated):
 

	
 
    current_app.logger.info('Sending tor status offline mail!')
 
    subject = '[Ennstatus] %s Tor status went offline'
 
    _send_mail(server_name, last_updated, subject)
 
    mail_cache[server_name] = datetime.utcnow()
 

	
 

	
 
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)
 
    server_name = server['server_name']
 

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

	
 
        if 'ip6' in server:
 
            del server['ip6']
 
    else:
 
        for key in ('obfs', 'fteproxy', 'flashproxy', 'meek'):
 
            if key in server:
 
                del server[key]
 
        if 'ip' not in server:
 
            server['ip'] = ip
 

	
 
        if isinstance(ip, ipaddress.IPv4Address):
 
            if 'ip' not in server:
 
                server['ip'] = ip
 
        elif isinstance(ip, ipaddress.IPv6Address):
 
            if 'ip6' not in server:
 
                server['ip6'] = ip
 

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

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

	
 
    if server['tor_status'] == 'Offline':
 

	
 
        if server_name in mail_cache:
ennstatus/api/views.py
Show inline comments
 
import ipaddress
 

	
 
from flask import (Blueprint, request, current_app, jsonify, render_template,
 
                   abort)
 

	
 
from ennstatus.api.functions import check_json_format, update_server
 
from ennstatus.status.functions import (single_server, all_servers,
 
                                        all_servers_by_type)
 

	
 
api_page = Blueprint('api', __name__)
 

	
 

	
 
@api_page.route('/update', methods=('POST',))
 
def update():
 
@@ -21,27 +23,34 @@ def update():
 
    if json is None:
 
        current_app.logger.info('No JSON data supplied!')
 
        return 'No JSON data supplied!\n', 400, {'Content-Type': 'text/plain'}
 

	
 
    try:
 
        check_json_format(json)
 
    except ValueError as e:
 
        current_app.logger.warning(' '.join([str(e), str(json)]))
 
        return str(e), 409, {'Content-Type': 'text/plain'}
 

	
 
    if 'ip' in json:
 
        ip = json['ip']
 
    elif 'ip6' in json:
 
        ip = json['ip6']
 
    else:
 
        ip = request.remote_addr
 

	
 
    try:
 
        ip = ipaddress.ip_address(ip)
 
    except ipaddress.AddressValueError:
 
        return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'}
 

	
 
    if request.remote_addr not in accepted_ips:
 
        current_app.logger.warn('Unallowed IP %s tried to update data!'
 
                                % ip)
 
        return 'IP not allowed!\n', 403, {'Content-Type': 'text/plain'}
 

	
 
    current_app.logger.info(str(json))
 
    server = update_server(server=json, ip=ip)
 

	
 
    if server:
 
        current_app.logger.info('Return result')
 
        current_app.logger.info(str(server))
 
        return (jsonify(server), 201,
0 comments (0 inline, 0 general)