Changeset - 10a3ee9cb0c8
[Not reviewed]
default
0 1 0
Dennis Fink - 11 years ago 2013-11-17 16:06:29
dennis.fink@c3l.lu
updated script
1 file changed with 5 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 obfs2,obfs3')
 

	
 
IP_REGEX = re.compile((r'^(OutboundBindAddress)'
 
                       r' (\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3})'))
 
                       r' (\d{1,3}\.\d{1,3}\.\d{1,3}\.\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')
 
        pids = pids.split(' ')
 
        pid_file = '.'.join([name, 'pid'])
 
        pid = open('/'.join(['/var', 'run', name, pid_file])).readline()
 
        pid = open('/'.join(['/var', 'run', 'tor', pid_file])).readline()
 

	
 
        if pid in pids:
 
            return "Online"
 
        else:
 
            return "Offline"
 
    except subprocess.CalledProcessError:
 
        return "Offline"
 

	
 

	
 
def get_tor_fingerprint(name='tor'):
 

	
 
    fingerprint_file = '/'.join(['/var', 'lib', name, 'fingerprint'])
 
@@ -58,25 +58,25 @@ def get_server_type(tor_config):
 
            return 'Bridge', 'False'
 
    elif 'ExitPolicy reject *:*' in tor_config:
 
        return 'Relay', None
 
    else:
 
        return 'Exit', None
 

	
 

	
 
def get_ip(tor_config):
 

	
 
    for i in tor_config:
 
        match = IP_REGEX.match(i)
 

	
 
        if match:
 
        if match is not None:
 
            return i.groups()[1]
 
        else:
 
            return None
 

	
 

	
 
def get_config():
 

	
 
    config = configparser.ConfigParser()
 
    config.read('/etc/ennstatus_updater.conf')
 

	
 
    return config
 

	
 
@@ -99,25 +99,26 @@ def create_server_json(tor_configfile='/
 
        dictionary['obfs'] = obfs
 

	
 
    if server_type != 'Bridge':
 
        dictionary['fingerprint'] = fingerprint
 

	
 
    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)
 
    response = requests.post(url + '/api/update', data=json.dumps(server_json),
 
                             headers=headers)
 
    response.raise_for_status()
 

	
 
    return response.text
 

	
 

	
 
def main():
 

	
 
    config = get_config()
 
    ennstatus_url = config['main']['ennstatus_url']
 

	
 
    try:
 
        if 'servers' in config['main']:
0 comments (0 inline, 0 general)