Skip to content

Commit

Permalink
I can't write english this late...
Browse files Browse the repository at this point in the history
  • Loading branch information
mwisniewski0 committed Oct 16, 2018
1 parent 6e548ec commit 9eb604c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
1 change: 1 addition & 0 deletions bfcp/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def __init__(self, tm: TrafficManager, conn_uuid: str, prev_hops: List[Tuple[str

async def initiate_connection(self, addr: Tuple[str, int]):
# Initiate the connection with the target server
print('END SERVER: Connecting to: ', addr)
self._reader_writer_future = ensure_future(open_connection(*addr))
reader, _ = await self._reader_writer_future
while True:
Expand Down
2 changes: 1 addition & 1 deletion bfcp/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"CHANNELS_PER_CONNECTION": 1,
"MIN_CHANNEL_LENGTH": 2,
"MAX_CHANNEL_LENGTH": 2,
"MIN_CHANNELS_TO_FIRE_ESTABLISH_EVENT": 3.333,
"MIN_CHANNELS_TO_FIRE_ESTABLISH_EVENT": 1,
"CHALLENGE_SIZE": 64,
"MAX_MESSAGE_LENGTH": 65536,
"READ_CHUNK_SIZE": 4096,
Expand Down
54 changes: 33 additions & 21 deletions bfcp/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from bfcp.connection import SocketConnection
from bfcp.node import BFCNode
from bfcp.protocol import pubkey_to_proto
from config import HTTPProxyServerConfig
from event_server import EventServer
from http_proxy import HTTPProxyServer
from protos.bfcp_pb2 import NodeTable, Node, EndNodeRequirement


Expand Down Expand Up @@ -107,27 +110,36 @@ async def start_scenario():
)

def user_thread():
# Sample connection
print('User_tread id', threading.get_ident())
reqs = EndNodeRequirement()
reqs.country = 840
conn = node1.connection_manager.new_connection(reqs, ('127.0.0.1', target_server_port))
sock = SocketConnection(conn)
print('WTF1')
sock.sendall(b'01234')
print('WTF2')
sock.sendall(b'56789')
print('WTF3')
self.assertEqual(sock.recv_all(7), b'0123456')
sock.sendall(b'01234')
print('WTF4')
sock.sendall(b'56789')
print('WTF5')
self.assertEqual(sock.recv_all(4), b'7890')
self.assertEqual(sock.recv_all(9), b'123456789')
sock.close()
print('WTF6')
node1.traffic_manager.get_loop().stop()
http_proxy_default_config = HTTPProxyServerConfig([("127.0.0.1", 8080)])
ev_server = EventServer()
http_proxy = HTTPProxyServer(http_proxy_default_config, node1, ev_server)
print("(Proxy) Epoll event looping...")
http_proxy.start()
ev_server.start()

# def user_thread():
# # Sample connection
# asyncio.set_event_loop(asyncio.new_event_loop())
# print('User_tread id', threading.get_ident())
# reqs = EndNodeRequirement()
# reqs.country = 840
# conn = node1.connection_manager.new_connection(reqs, ('127.0.0.1', target_server_port))
# sock = SocketConnection(conn)
# print('WTF1')
# sock.sendall(b'01234')
# print('WTF2')
# sock.sendall(b'56789')
# print('WTF3')
# self.assertEqual(sock.recv_all(7), b'0123456')
# sock.sendall(b'01234')
# print('WTF4')
# sock.sendall(b'56789')
# print('WTF5')
# self.assertEqual(sock.recv_all(4), b'7890')
# self.assertEqual(sock.recv_all(9), b'123456789')
# sock.close()
# print('WTF6')
# node1.traffic_manager.get_loop().stop()

thread = threading.Thread(target=user_thread)
thread.start()
Expand Down
5 changes: 3 additions & 2 deletions http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from http_parser import HTTPHeaderParser, HTTPParseStatus, HTTPBodyParser, HTTPRequestHeader
from bfcp.node import BFCNode
from bfcp.connection import OriginalSenderConnection
from protos.bfcp_pb2 import EndNodeRequirement
from utils import Ref

from logger import getLogger
Expand Down Expand Up @@ -67,7 +68,7 @@ def queue_send(self, s: bytes=b"")->int:
self._client_send_buf += s
try:
while self._client_send_buf and self._client_writable:
byte_sent = self._client_socket.on_payload(self._client_send_buf)
byte_sent = self._client_socket.send(self._client_send_buf)
total_byte_sent += byte_sent
self._client_send_buf = self._client_send_buf[byte_sent:]
except socket.error as e:
Expand Down Expand Up @@ -217,7 +218,7 @@ def send(self, s: bytes):

def start(self):
_log.info("Relaying to %s.", self._location)
self._bfc_conn = self._bfc.new_connection(None, self._location)
self._bfc_conn = self._bfc.new_connection(EndNodeRequirement(), self._location)
_log.info("%s: got BFC connection.", self._location)
self._bfc_conn.register_on_new_data(self.recv_callback)

Expand Down
34 changes: 14 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-

import threading
from threading import Thread

from Crypto.PublicKey import RSA
Expand All @@ -16,31 +16,25 @@
from logger import getLogger
_log = getLogger(__name__)


def main():
http_proxy_default_config = HTTPProxyServerConfig([("127.0.0.1", 8080)])

bfc = None

def run_bfc():
nonlocal bfc
# TODO: `node.txt` and `node_table.txt`
asyncio.set_event_loop(asyncio.new_event_loop())
node = ProtoIO.read_from_file('node.txt', Node())
node_table = ProtoIO.read_from_file('node_table.txt', NodeTable())
bfc = BFCNode(node, ("0.0.0.0", node.last_port), RSA.generate(2048), node_table)

asyncio.set_event_loop(asyncio.new_event_loop())
node = ProtoIO.read_from_file('node.txt', Node())
node_table = ProtoIO.read_from_file('node_table.txt', NodeTable())
bfc = BFCNode(node, ("0.0.0.0", 9000), RSA.generate(2048), node_table)
bfc.run()
def run_proxy():
ev_server = EventServer()
http_proxy = HTTPProxyServer(http_proxy_default_config, bfc, ev_server)
_log.info("(Proxy) Epoll event looping...")
http_proxy.start()
ev_server.start()

Thread(target=run_bfc).start()
Thread(target=run_proxy).start()
_log.info("Started BFC")

ev_server = EventServer()

http_proxy = HTTPProxyServer(http_proxy_default_config, bfc, ev_server)
http_proxy.start()

_log.info("(Proxy) Epoll event looping...")
ev_server.start()
bfc.run()


if __name__ == "__main__":
Expand Down

0 comments on commit 9eb604c

Please sign in to comment.