added stats reporter

This commit is contained in:
Kevin Froman 2020-03-31 04:00:48 -05:00
parent 0da27a78c7
commit 8d372cccfb
7 changed files with 112 additions and 1 deletions

View File

@ -24,6 +24,8 @@ conf['log']['file']['remove_on_exit'] = True
conf['transports']['lan'] = True conf['transports']['lan'] = True
conf['transports']['tor'] = True conf['transports']['tor'] = True
conf['transports']['sneakernet'] = True conf['transports']['sneakernet'] = True
conf['statistics']['i_dont_want_privacy'] = False
conf['statistics']['server'] = ''
json.dump(conf, open('static-data/default_config.json', 'w'), sort_keys=True, indent=4) json.dump(conf, open('static-data/default_config.json', 'w'), sort_keys=True, indent=4)

View File

@ -27,6 +27,9 @@ conf['onboarding']['done'] = True
conf['general']['minimum_block_pow'] = block_pow conf['general']['minimum_block_pow'] = block_pow
conf['general']['minimum_send_pow'] = block_pow conf['general']['minimum_send_pow'] = block_pow
conf['log']['file']['remove_on_exit'] = False conf['log']['file']['remove_on_exit'] = False
if input('Stat reporting? y/n') == 'y':
conf['statistics']['i_dont_want_privacy'] = True
conf['statistics']['server'] = input('Statistics server')
json.dump(conf, open('static-data/default_config.json', 'w'), sort_keys=True, indent=4) json.dump(conf, open('static-data/default_config.json', 'w'), sort_keys=True, indent=4)

73
scripts/stats-server.py Executable file
View File

@ -0,0 +1,73 @@
#!/usr/bin/env python3
"""Onionr - Private P2P Communication.
LAN transport server thread
"""
import sys
import os
os.chdir('../')
sys.path.append("src/")
from gevent.pywsgi import WSGIServer
from flask import Flask
from flask import Response
from flask import request
import stem
from stem.control import Controller
from netcontroller import getopenport
import json
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
#passw = secrets.token_hex(32)
port_num = int(input('tor control port'))
web_port = getopenport.get_open_port()
app = Flask(__name__)
STATS_FILE = 'stats.json'
@app.route('/sendstats/<node>', methods = ['POST'])
def get_stats(node):
try:
with open(STATS_FILE, 'r') as f:
try:
data = json.loads(f.read())
except json.decoder.JSONDecodeError:
raise FileNotFoundError
except FileNotFoundError:
data = {}
data[node] = request.get_data().decode('utf-8')
with open(STATS_FILE, 'w') as f:
data = json.dumps(data)
f.write(data)
return Response('ok')
with Controller.from_port(port = port_num) as controller:
controller.authenticate(input('pass for tor')) # provide the password here if you set one
hs = controller.create_ephemeral_hidden_service(
{80: web_port},
key_type = 'NEW',
key_content = 'ED25519-V3',
await_publication=True,
detached=True)
hs = hs.service_id
print(f'stats server {hs}')
server = WSGIServer(('127.0.0.1', web_port), app, log=None)
server.serve_forever()

View File

@ -41,5 +41,5 @@ class Client:
better_sleep(self.pull_delay) better_sleep(self.poll_delay)

View File

@ -38,6 +38,7 @@ from utils.boxprint import bordered
from lan import LANManager from lan import LANManager
from lan.server import LANServer from lan.server import LANServer
from sneakernet import sneakernet_import_thread from sneakernet import sneakernet_import_thread
from onionrstatistics.devreporting import statistics_reporter
""" """
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -172,6 +173,8 @@ def daemon():
LANManager(shared_state).start() LANManager(shared_state).start()
if config.get('transports.sneakernet', True): if config.get('transports.sneakernet', True):
Thread(target=sneakernet_import_thread, daemon=True).start() Thread(target=sneakernet_import_thread, daemon=True).start()
Thread(target=statistics_reporter, args=[shared_state], daemon=True).start()
communicator.startCommunicator(shared_state) communicator.startCommunicator(shared_state)
clean_ephemeral_services() clean_ephemeral_services()

View File

@ -0,0 +1,26 @@
import config
from utils.bettersleep import better_sleep
from utils.gettransports import get as get_transports
from onionrutils import basicrequests
from onionrutils import epoch
import json
def statistics_reporter(shared_state):
server = config.get('statistics.server', '')
if not config.get('statistics.i_dont_want_privacy', False) or \
not server: return
def compile_data():
return {
'time': epoch.get_epoch(),
'adders': get_transports(),
'peers': shared_state.get_by_string('OnionrCommunicatorDaemon').onlinePeers
}
while True:
better_sleep(5)
data = compile_data()
basicrequests.do_post_request(
f'http://{server}/sendstats/' + get_transports()[0],
data=json.dumps(data))

View File

@ -48,6 +48,10 @@
], ],
"enabled": [] "enabled": []
}, },
"statistics": {
"i_dont_want_privacy": false,
"server": ""
},
"timers": { "timers": {
"getBlocks": 10, "getBlocks": 10,
"lookupBlocks": 25 "lookupBlocks": 25