Skip to content

Commit

Permalink
feat: global vars from json
Browse files Browse the repository at this point in the history
  • Loading branch information
mishig25 committed Oct 15, 2018
1 parent d003ba8 commit d193067
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 29 deletions.
16 changes: 8 additions & 8 deletions bfcp/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def send_randomly():
else:
node = self._trust_table.get_node_with_requirement(msg.end_node_requirement)
if node is None:
if remaining_hops < -MAX_HOPS_WITHOUT_END_NODE:
if remaining_hops < -GLOBAL_VARS['MAX_HOPS_WITHOUT_END_NODE']:
raise NodeNotFoundError('A node suitable for becoming EN was not found')
else:
await send_randomly()
Expand Down Expand Up @@ -152,7 +152,7 @@ async def _become_end_node(self, conn_request: bfcp_pb2.ConnectionRequest, sende
conn_resp.signature_challenge_response = solved_challenge

# Prepare the session key
session_key = utils.generate_aes_key(OS_EN_KEY_SIZE)
session_key = utils.generate_aes_key(GLOBAL_VARS['OS_EN_KEY_SIZE'])

original_sender_pub_key = proto_to_pubkey(conn_request.sender_connection_key)
cipher_rsa = PKCS1_OAEP.new(original_sender_pub_key)
Expand Down Expand Up @@ -242,8 +242,8 @@ def __init__(self, conn_manager: ConnectionManager, traffic_manager: TrafficMana
self.uuid: str = str(uuid4())

# Encryption:
self._sender_connection_key: RsaKey = RSA.generate(SENDER_CONNECTION_KEY_BITS)
self._challenge_bytes: bytes = get_random_bytes(SIGNATURE_CHALLENGE_BYTES)
self._sender_connection_key: RsaKey = RSA.generate(GLOBAL_VARS['SENDER_CONNECTION_KEY_BITS'])
self._challenge_bytes: bytes = get_random_bytes(GLOBAL_VARS['SIGNATURE_CHALLENGE_BYTES'])
self._session_key: Optional[bytes] = None

def initiate_connection(self, en_requirement: bfcp_pb2.EndNodeRequirement, ts_address: Tuple[str, int]):
Expand All @@ -255,7 +255,7 @@ def initiate_connection(self, en_requirement: bfcp_pb2.EndNodeRequirement, ts_ad
conn_request = bfcp_pb2.ConnectionRequest()

conn_request.conn_params.uuid = self.uuid
conn_request.conn_params.remaining_hops = randint(MIN_CHANNEL_LENGTH, MAX_CHANNEL_LENGTH)
conn_request.conn_params.remaining_hops = randint(GLOBAL_VARS['MIN_CHANNEL_LENGTH'], GLOBAL_VARS['MAX_CHANNEL_LENGTH'])

conn_request.end_node_requirement.CopyFrom(en_requirement)
conn_request.target_server_address = ts_address[0]
Expand All @@ -280,7 +280,7 @@ async def on_end_node_found(self, conn_resp: bfcp_pb2.ConnectionResponse):
self._session_key = rsa_cipher.decrypt(conn_resp.session_key.key)

# Found an EN, now try to establish channels
for i in range(CHANNELS_PER_CONNECTION):
for i in range(GLOBAL_VARS['CHANNELS_PER_CONNECTION']):
channel_uuid = str(uuid4())
channel_request = bfcp_pb2.ChannelRequest()
channel_request.end_node.CopyFrom(conn_resp.selected_end_node)
Expand All @@ -296,7 +296,7 @@ def on_channel_established(self, channel_uuid: str, next_hop_pub_key: RsaKey):

self._channels.append((channel_uuid, next_hop_pub_key))
if (not self._establish_event_fired) and \
len(self._channels) >= MIN_CHANNELS_TO_FIRE_ESTABLISH_EVENT:
len(self._channels) >= GLOBAL_VARS['MIN_CHANNELS_TO_FIRE_ESTABLISH_EVENT']:
self._establish_event_fired = True
for callback in self._on_established:
# TODO how do we know what exceptions are raised when there's a failure?
Expand Down Expand Up @@ -412,7 +412,7 @@ def _sync_send(self, msg: bfcp_pb2.BouncyMessage, pub_key: Optional[bytes] = Non
ensure_future(self._traffic_manager.send(msg, pub_key))

def _make_channel_length(self):
return randint(MIN_CHANNEL_LENGTH, MAX_CHANNEL_LENGTH)
return randint(GLOBAL_VARS['MIN_CHANNEL_LENGTH'], GLOBAL_VARS['MAX_CHANNEL_LENGTH'])

def _close_internal(self):
if self._is_closed:
Expand Down
2 changes: 1 addition & 1 deletion bfcp/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def next_message(self) -> BouncyMessage:
await self._handshake_task

msg = BouncyMessage()
await recv_proto_msg(self._reader, msg, MAX_MESSAGE_LENGTH, self._handshake.session_key)
await recv_proto_msg(self._reader, msg, GLOBAL_VARS['MAX_MESSAGE_LENGTH'], self._handshake.session_key)
return msg

async def send_bouncy_message(self, msg: BouncyMessage):
Expand Down
2 changes: 1 addition & 1 deletion bfcp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def make_rsa_challenge(challenged_pub_key: RsaKey) -> Tuple[bytes, RsaChallenge]
solved = handshake.solve_rsa_challenge(key, challenge)
self.assertTrue(handshake.verify_rsa_challenge(decrypted, solved))
"""
decrypted = get_random_bytes(CHALLENGE_SIZE)
decrypted = get_random_bytes(GLOBAL_VARS['CHALLENGE_SIZE'])
cipher_rsa = PKCS1_OAEP.new(challenged_pub_key)
encrypted = cipher_rsa.encrypt(decrypted)

Expand Down
2 changes: 1 addition & 1 deletion bfcp/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class TestConfig(unittest.TestCase):
def test_global_var(self):
value = 5
self.assertEqual(MAX_HOPS_WITHOUT_END_NODE, value)
self.assertEqual(GLOBAL_VARS['MAX_HOPS_WITHOUT_END_NODE'], value)
24 changes: 6 additions & 18 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
# -*- coding: utf-8 -*-
from typing import List, Tuple
import json
from protos.bfcp_pb2 import BouncyMessage
from google.protobuf import text_format


""" GLOBAL VARIABLES """
# Specifies how many hops can be done after remaining_hops = 0 before the request is dropped. This
# is necessary, since some requirements might not be matched by any of the nodes, and the requests
# need to be dropped eventually.
MAX_HOPS_WITHOUT_END_NODE = 5
GLOBAL_VARS = None
with open('constants.json') as f:
data = json.load(f)
GLOBAL_VARS = data['constants']

SIGNATURE_CHALLENGE_BYTES = 128

OS_EN_KEY_SIZE = 256
SENDER_CONNECTION_KEY_BITS = 4096

CHANNELS_PER_CONNECTION = 5
MIN_CHANNEL_LENGTH = 5
MAX_CHANNEL_LENGTH = 10
MIN_CHANNELS_TO_FIRE_ESTABLISH_EVENT = MIN_CHANNEL_LENGTH * (2/3)

CHALLENGE_SIZE = 64

MAX_MESSAGE_LENGTH = 64 * 2**10 # 64 KiB
READ_CHUNK_SIZE = 4096

class HTTPProxyServerConfig:
"""
Expand Down Expand Up @@ -52,6 +39,7 @@ def write_to_file(self, file_path: str, proto_object: BouncyMessage) -> None:
"""
Writes NodeTable object into file
:param file_dir: directory of file to be written
usage: <protoio>.write_to_file(<file-path>, NodeTable())
"""
text_proto = text_format.MessageToString(proto_object)
file = open(file_path, 'w')
Expand Down

0 comments on commit d193067

Please sign in to comment.