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()
 
@@ -63,16 +55,33 @@ def check_json_format(server):
 

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

	
 
@@ -89,18 +98,26 @@ def update_server(server, ip):
 
    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)
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)
 
@@ -27,15 +29,22 @@ def update():
 
    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))
0 comments (0 inline, 0 general)