-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (39 loc) · 1.46 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
from getopt import gnu_getopt
from sys import argv
from Connection import ConnectionSingleton
import threading
import logging
import rsa
# Default variables
username = 'Anonymous'
PORT = 8080
bufferSize = 1024
# Generation and initialization of RSA Keys
(PUBLIC_KEY, PRIVATE_KEY) = rsa.newkeys(bufferSize, poolsize=1)
peerPublicKey = rsa.PublicKey(0, 0)
# Default logging level
logging.basicConfig(level=logging.INFO)
# Sets and creates the arguments and flags for initializing the program
# Example usage: python3 main.py --username=Username --port=8080 --debug
opts, argv = gnu_getopt(argv[1:], 'u:p:d:', ["username=", "port=", 'debug']) # Fix me
for k, v in opts:
if k == '-u' or k == '--username':
username = str(v)
elif k == '-p' or k == '--port':
PORT = int(v)
elif k == '-d' or k == '--debug':
logging.getLogger().setLevel(level=logging.DEBUG)
# Debug Mode Logs
logging.debug(f'Public key: {PUBLIC_KEY}')
logging.debug(f'Private key: {PRIVATE_KEY}')
logging.debug(f'Username: {username}')
logging.debug(f'Port: {PORT}')
connection = ConnectionSingleton.instance(username, "0.0.0.0", PORT, peerPublicKey, PUBLIC_KEY, PRIVATE_KEY, bufferSize)
# Initializes and starts threads running a certain function (either send or receive)
inbound = threading.Thread(name='Inbound', target=connection.receive)
outbound = threading.Thread(name="Outbound", target=connection.send)
inbound.start()
outbound.start()
inbound.join()
outbound.join()