Skip to content

Commit

Permalink
big update
Browse files Browse the repository at this point in the history
changed the whole structure,
src contains all the interfaces and core that contains modules that shouldn't be executed,
removed every config.json read,
every parameter is either and argument or a constant if the user shouldn't modify it,
added a lot of options, when running a single channel,
added a subparser for Server.py when running the channel locally,
updated README.md in src,
fixed an issue in channel related to noise and eve,
removed some ugly path insertions

please note:
some changes are made automatically while refactoring
FTP is not compatible with the new structure, no plans to adapt it for now,
  • Loading branch information
GGmorello committed Nov 15, 2021
1 parent ca8fee6 commit 8f06e94
Show file tree
Hide file tree
Showing 24 changed files with 377 additions and 408 deletions.
2 changes: 1 addition & 1 deletion QKDSimkit/FTP/FTP_Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def startServer(self):
sys.exit()

try:
s = json.load(open('../config.json', ))['Server']
s = json.load(open('../data/config.json', ))['Server']
handler = MyHandler # select the created custom FTP handler
handler.authorizer = authorizer # assign the authorizer to the handler
handler.banner = "Server Ready.." # server banner is returned when the client calls a getWelcomeMessage() call
Expand Down
29 changes: 0 additions & 29 deletions QKDSimkit/QKDSimChannels/QKD_Channel.py

This file was deleted.

Empty file.
65 changes: 0 additions & 65 deletions QKDSimkit/QKDSimClients/models.py

This file was deleted.

5 changes: 0 additions & 5 deletions QKDSimkit/QKDSimClients/qexceptions.py

This file was deleted.

Empty file.
File renamed without changes.
34 changes: 34 additions & 0 deletions QKDSimkit/src/Channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""
Created on Wed May 12 21:31:42 2021
@author: Alberto Di Meglio
"""

import os
import argparse
import core


def run_channel(host: str = '', port: str = '5000', noise: float = 0.0, eve: bool = False):
# clean up
_ = os.system('clear')

# instantiate a receiver channel
theChannel = core.channel.public_channel(host, port, noise, eve)

# initiate the channel and listen for connections
theChannel.initiate_channel()


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Channel for Quantumacy')
parser.add_argument('--host', default='', type=str,
help='Bind socket to this host (default: %(default)s)')
parser.add_argument('--port', default='5000', type=str,
help='Bind socket to this port (default: %(default)s)')
parser.add_argument('-n', '--noise', default=0.0, type=float,
help='Set a noise value for channel, type a float number in [0,1] (default: %(default)s)')
parser.add_argument('-e', '--eve', action='store_true', help='Add an eavesdropper to the channel')
args = parser.parse_args()
run_channel(args.host, args.port, args.noise, args.eve)
104 changes: 50 additions & 54 deletions QKDSimkit/Server_Client_wrapper/Client.py → QKDSimkit/src/Client.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
import http.client
import urllib.parse
import sys
import argparse
import os

this_file_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, this_file_dir + "/../..")

from QKDSimkit.QKDSimClients.utils import hash_token, decrypt
from QKDSimkit.QKDSimClients.QKD_Bob import import_key


def get_key(alice_address, channel_address, token, number, size):
hashed = hash_token(token)
params = urllib.parse.urlencode({'hashed': hashed})
conn = http.client.HTTPConnection(f"{alice_address}")
conn.request("GET", f"/hello?{params}")
r = conn.getresponse()
if r.status != 200:
return r.status
data = r.read().decode()
proof = decrypt(token, data)
conn.close()
hash_proof = hash_token(proof)
params = urllib.parse.urlencode({'number': number, 'size': size, 'hashed': hashed, 'hash_proof': hash_proof})
conn.close()
conn1 = http.client.HTTPConnection(f"{alice_address}")
conn1.request("GET", f"/proof?{params}")
r = conn1.getresponse()
if r.status == 200:
key_list = []
for n in range(number):
key_list.append(import_key(channel_address=channel_address, ID=hashed, size=size).decode())
return key_list
else:
return r.status


def manage_args():
parser = argparse.ArgumentParser(description='Client for Quantumacy')
parser.add_argument('alice_address', type=str, help='Address of server/Alice [host:port]')
parser.add_argument('channel_address', type=str, help='Address of channel [host:port]')
parser.add_argument('-t', '--token', default='7KHuKtJ1ZsV21DknPbcsOZIXfmH1_MnKdOIGymsQ5aA=', type=str,
help='Auth token, for development purposes a default token is provided')
parser.add_argument('-n', '--number', default=1, type=int, help="Number of keys (default: %(default)s)")
parser.add_argument('-s', '--size', default=256, type=int, help="Size of keys (default: %(default)s)")
return parser.parse_args()


if __name__ == '__main__':
args = manage_args()
l = get_key(args.alice_address, args.channel_address, args.token, args.number, args.size)
print(l)
import http.client
import urllib.parse
import argparse
import core


def get_key(alice_address, channel_address, token, number, size):
hashed = core.utils.hash_token(token)
params = urllib.parse.urlencode({'hashed': hashed})
conn = http.client.HTTPConnection(f"{alice_address}")
conn.request("GET", f"/hello?{params}")
r = conn.getresponse()
if r.status != 200:
return r.status
data = r.read().decode()
proof = core.utils.decrypt(token, data)
conn.close()
hash_proof = core.utils.hash_token(proof)
params = urllib.parse.urlencode({'number': number, 'size': size, 'hashed': hashed, 'hash_proof': hash_proof})
conn.close()
conn1 = http.client.HTTPConnection(f"{alice_address}")
conn1.request("GET", f"/proof?{params}")
r = conn1.getresponse()
if r.status == 200:
key_list = []
for n in range(number):
res = core.bob.import_key(channel_address=channel_address, ID=hashed, size=size)
if res == -1:
print("Can't exchange a safe key")
if isinstance(res, bytearray):
key_list.append(res.decode())
return key_list
else:
return r.status


def manage_args():
parser = argparse.ArgumentParser(description='Client for Quantumacy')
parser.add_argument('alice_address', type=str, help='Address of server/Alice [host:port]')
parser.add_argument('channel_address', type=str, help='Address of channel [host:port]')
parser.add_argument('-t', '--token', default='7KHuKtJ1ZsV21DknPbcsOZIXfmH1_MnKdOIGymsQ5aA=', type=str,
help='Auth token, for development purposes a default token is provided')
parser.add_argument('-n', '--number', default=1, type=int, help="Number of keys (default: %(default)s)")
parser.add_argument('-s', '--size', default=256, type=int, help="Size of keys (default: %(default)s)")
return parser.parse_args()


if __name__ == '__main__':
args = manage_args()
l = get_key(args.alice_address, args.channel_address, args.token, args.number, args.size)
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@ pip install -r ../requirements.txt

### Executing program

* Run with the most basic configuration: channel will run on the same machine as server
* Run with the most basic configuration: channel will run on the same machine as the server
```
python Server.py -l
python Server.py l
```
* Run just server and use an external channel

* Run with personalized channel settings (noise, eavesdropper)
```
python Server.py l -n 0.5 -e True
```

* Run server and use an external channel
```
python Server.py -ca [host:port] --host [host] --port [port]
python Server.py --host [host] --port [port] ca [host:port]
```

### Help

For more options please check
```
python Server.py -h
python Server.py l -h
python Server.py ca -h
```
##Client

### Dependencies
Expand Down Expand Up @@ -54,9 +68,9 @@ python Client.py [server_host:port] [channel_host:port]
python Client.py [server_host:port] [channel_host:port] -n [num_keys] -s [size]
```

## Help
### Help

Any advise for common problems or issues.
For more options please check
```
python Client.py -h
```
Expand Down
Loading

0 comments on commit 8f06e94

Please sign in to comment.