Changeset - 209648cf3c74
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2014-09-08 19:58:07
dennis.fink@c3l.lu
Overwrite ip in server json if not yet specified
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/functions.py
Show inline comments
 
@@ -34,60 +34,60 @@ def check_bridge(key, server):
 

	
 

	
 
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', '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:
 
        for key in ('obfs', 'fteproxy', 'flashproxy', 'meek'):
 
            if key in server:
 
                del server[key]
 

	
 
        if 'ip' not in server:
 
        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)