Implemented basic server diffusal test
This commit is contained in:
parent
c7ba974264
commit
a61cd273a8
@ -1,73 +0,0 @@
|
|||||||
#!/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()
|
|
||||||
|
|
@ -69,7 +69,7 @@ def gossip_server():
|
|||||||
onionrevents.event(
|
onionrevents.event(
|
||||||
'announce_rec',
|
'announce_rec',
|
||||||
data={'address': address,
|
data={'address': address,
|
||||||
'callback': connect_peer},
|
'callback': connect_peer},
|
||||||
threaded=True)
|
threaded=True)
|
||||||
writer.write(int(1).to_bytes(1, 'big'))
|
writer.write(int(1).to_bytes(1, 'big'))
|
||||||
await asyncio.wait_for(_read_announce(), 10)
|
await asyncio.wait_for(_read_announce(), 10)
|
||||||
|
@ -49,7 +49,7 @@ async def diffuse_blocks(reader: 'StreamReader', writer: 'StreamWriter'):
|
|||||||
raise ValueError("Invalid time offset")
|
raise ValueError("Invalid time offset")
|
||||||
time_offset = int(time_offset)
|
time_offset = int(time_offset)
|
||||||
|
|
||||||
if time_offset - time() < -5:
|
if time_offset - time() > -5:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Peer's specified time offset skewed too far into the future")
|
"Peer's specified time offset skewed too far into the future")
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ class TestBlockDB(unittest.TestCase):
|
|||||||
def test_store_vdf_block(self):
|
def test_store_vdf_block(self):
|
||||||
_delete_db()
|
_delete_db()
|
||||||
bl: Block = onionrblocks.create_anonvdf_block(os.urandom(10), b'bin', 2500)
|
bl: Block = onionrblocks.create_anonvdf_block(os.urandom(10), b'bin', 2500)
|
||||||
blockdb.store_vdf_block(bl)
|
blockdb.add_block_to_db(bl)
|
||||||
|
|
||||||
with dbm.open(blockdb.block_db_path, 'r') as b_db:
|
with dbm.open(blockdb.block_db_path, 'r') as b_db:
|
||||||
b_db[bl.id]
|
b_db[bl.id]
|
||||||
|
51
tests/test_server_diffuse.py
Normal file
51
tests/test_server_diffuse.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
import os, uuid
|
||||||
|
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
|
||||||
|
print("Test directory:", TEST_DIR)
|
||||||
|
os.environ["ONIONR_HOME"] = TEST_DIR
|
||||||
|
|
||||||
|
from threading import Thread
|
||||||
|
import asyncio
|
||||||
|
import unittest
|
||||||
|
import sys
|
||||||
|
sys.path.append(".")
|
||||||
|
sys.path.append("src/")
|
||||||
|
|
||||||
|
from ordered_set import OrderedSet
|
||||||
|
import onionrblocks
|
||||||
|
|
||||||
|
import blockdb
|
||||||
|
from gossip.server import gossip_server
|
||||||
|
from filepaths import gossip_server_socket_file
|
||||||
|
|
||||||
|
|
||||||
|
BLOCK_MAX_SIZE = 1024 * 2000
|
||||||
|
BLOCK_MAX_SIZE_LEN = len(str(BLOCK_MAX_SIZE))
|
||||||
|
BLOCK_ID_SIZE = 128
|
||||||
|
BLOCK_STREAM_OFFSET_DIGITS = 8
|
||||||
|
|
||||||
|
|
||||||
|
class OnionrServerDiffuseTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_one_block(self):
|
||||||
|
|
||||||
|
Thread(target=gossip_server, daemon=True).start()
|
||||||
|
|
||||||
|
bl = onionrblocks.blockcreator.create_anonvdf_block(
|
||||||
|
b"my test block", b"txt", 2800)
|
||||||
|
blockdb.add_block_to_db(bl)
|
||||||
|
|
||||||
|
async def diffuse_client():
|
||||||
|
reader, writer = await asyncio.open_unix_connection(
|
||||||
|
gossip_server_socket_file)
|
||||||
|
writer.write(int(4).to_bytes(1, 'big'))
|
||||||
|
await writer.drain()
|
||||||
|
|
||||||
|
writer.write('0'.zfill(BLOCK_STREAM_OFFSET_DIGITS).encode('utf-8'))
|
||||||
|
await writer.drain()
|
||||||
|
|
||||||
|
self.assertEqual(bl.id, await reader.readexactly(BLOCK_ID_SIZE))
|
||||||
|
|
||||||
|
|
||||||
|
asyncio.run(diffuse_client())
|
||||||
|
|
||||||
|
unittest.main()
|
Loading…
Reference in New Issue
Block a user