Files
@ aa2a22511c01
Branch filter:
Location: FVDE/ennstatus/Scripts/update_server.py
aa2a22511c01
3.3 KiB
text/x-python
updated ennstatus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 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)\ (\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(' ')
print(pids)
pid_file = '.'.join([name, 'pid'])
pid = open('/'.join(['/var', 'run', 'tor', pid_file])).readline()
print(pid)
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'])
with open(fingerprint_file) as fb:
line = fb.readline()[:-1]
hostname, fingerprint = line.split(' ')
return hostname, fingerprint
def get_server_type(tor_config):
if 'BridgeRelay 1' in tor_config:
if any(OBFS_REGEX.match(i) for i in tor_config):
return 'Bridge', 'True'
else:
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 is not None:
return match.groups()[1]
return None
def get_config():
config = configparser.ConfigParser()
config.read('/etc/ennstatus_updater.conf')
return config
def create_server_json(tor_configfile='/etc/tor/torrc', name='tor'):
tor_config = read_tor_config(tor_configfile)
server_type, obfs = get_server_type(tor_config)
hostname, fingerprint = get_tor_fingerprint(name)
tor_status = get_tor_status(name)
ip = get_ip(tor_config)
dictionary = {'server_type': server_type, 'server_name': hostname,
'tor_status': tor_status}
if ip is not None:
dictionary['ip'] = ip
if obfs is not None:
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)
return response.text
def main():
config = get_config()
ennstatus_url = config['main']['ennstatus_url']
if 'servers' in config['main']:
for server in literal_eval(config['main']['servers']):
server_json = create_server_json(config[server]['configfile'],
server)
response = update_server(server_json, ennstatus_url)
print(response, end='')
else:
server_json = create_server_json()
response = update_server(server_json, ennstatus_url)
print(response, end='')
if __name__ == '__main__':
main()
|