basicswap_miserver/basicswap/ui/page_tor.py

55 lines
1.5 KiB
Python
Raw Normal View History

2022-03-26 22:08:15 +00:00
# -*- coding: utf-8 -*-
# Copyright (c) 2022 tecnovert
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
def extract_data(bytes_in):
str_in = bytes_in.decode('utf-8')
start = str_in.find('=')
if start < 0:
return None
start += 1
end = str_in.find('\r', start)
if end < 0:
return None
return str_in[start: end]
2022-10-08 12:37:22 +00:00
def get_tor_established_state(swap_client):
rv = swap_client.torControl('GETINFO status/circuit-established')
return extract_data(rv)
2022-03-26 22:08:15 +00:00
def page_tor(self, url_split, post_string):
swap_client = self.server.swap_client
2022-10-10 21:44:14 +00:00
summary = swap_client.getSummary()
2022-03-26 22:08:15 +00:00
page_data = {}
2022-10-08 12:37:22 +00:00
try:
page_data['circuit_established'] = get_tor_established_state(swap_client)
except Exception:
page_data['circuit_established'] = 'error'
try:
rv = swap_client.torControl('GETINFO traffic/read')
page_data['bytes_written'] = extract_data(rv)
except Exception:
page_data['bytes_written'] = 'error'
try:
rv = swap_client.torControl('GETINFO traffic/written')
page_data['bytes_read'] = extract_data(rv)
except Exception:
page_data['bytes_read'] = 'error'
2022-03-26 22:08:15 +00:00
messages = []
template = self.server.env.get_template('tor.html')
2022-07-31 17:33:01 +00:00
return self.render_template(template, {
'messages': messages,
'data': page_data,
2022-10-10 21:44:14 +00:00
'summary': summary,
2022-07-31 17:33:01 +00:00
})