Compare commits
7 Commits
863a42e982
...
master
Author | SHA1 | Date | |
---|---|---|---|
f65db31c12 | |||
e7c4945ae9 | |||
94fcda4c2c | |||
c40e59ceb9 | |||
57af60ee07 | |||
48fd34117d
|
|||
d66cd15ec9
|
@ -11,6 +11,8 @@ from netwrapper import NetWrapper
|
|||||||
ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
|
ABSOLUTE_PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
|
||||||
DOWNLOAD_LOCATION = ABSOLUTE_PATH + os.path.sep + 'download' + os.path.sep
|
DOWNLOAD_LOCATION = ABSOLUTE_PATH + os.path.sep + 'download' + os.path.sep
|
||||||
CONFIG_LOCATION = ABSOLUTE_PATH + os.path.sep + 'config' + os.path.sep + 'config.txt'
|
CONFIG_LOCATION = ABSOLUTE_PATH + os.path.sep + 'config' + os.path.sep + 'config.txt'
|
||||||
|
PASSPHRASE = ''
|
||||||
|
SERVER_ADDRESS = ''
|
||||||
LOGGED_IN = False
|
LOGGED_IN = False
|
||||||
|
|
||||||
|
|
||||||
@ -54,6 +56,7 @@ def printCommand():
|
|||||||
' Get current directory -> GWD \n' +
|
' Get current directory -> GWD \n' +
|
||||||
' Change current directory -> CWD <path> \n' +
|
' Change current directory -> CWD <path> \n' +
|
||||||
' List content of current directory -> LST \n' +
|
' List content of current directory -> LST \n' +
|
||||||
|
' Remove file from current directory -> RMF <filename> \n' +
|
||||||
' Upload file to current directory -> UPL <filename> \n' +
|
' Upload file to current directory -> UPL <filename> \n' +
|
||||||
' Download file from current directory -> DNL <filename> \n' +
|
' Download file from current directory -> DNL <filename> \n' +
|
||||||
' Login -> LIN <username> <password> \n' +
|
' Login -> LIN <username> <password> \n' +
|
||||||
@ -67,7 +70,7 @@ def printCommandsWihtoutLogin():
|
|||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(sys.argv[1:], 'hp:')
|
opts, args = getopt.getopt(sys.argv[1:], 'hp:s:')
|
||||||
except getopt.GetoptError:
|
except getopt.GetoptError:
|
||||||
print('Error: Unknown option detected.')
|
print('Error: Unknown option detected.')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@ -76,10 +79,13 @@ for opt, arg in opts:
|
|||||||
if opt in '-p':
|
if opt in '-p':
|
||||||
PASSPHRASE = arg
|
PASSPHRASE = arg
|
||||||
|
|
||||||
|
if opt in '-s':
|
||||||
|
SERVER_ADDRESS = arg
|
||||||
|
|
||||||
if PASSPHRASE == '':
|
if PASSPHRASE == '':
|
||||||
print('Key required to start client!')
|
print('Key required to start client!')
|
||||||
print('Usage:')
|
print('Usage:')
|
||||||
print(' client.py -p <passphrase>')
|
print(' client.py -p <passphrase> -s <server_address>')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not os.path.isfile(CONFIG_LOCATION) or os.stat(CONFIG_LOCATION).st_size == 0:
|
if not os.path.isfile(CONFIG_LOCATION) or os.stat(CONFIG_LOCATION).st_size == 0:
|
||||||
@ -91,6 +97,8 @@ CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE)
|
|||||||
CLIENT_ADDRESS = loadAddress()
|
CLIENT_ADDRESS = loadAddress()
|
||||||
LOGGED_IN = False
|
LOGGED_IN = False
|
||||||
|
|
||||||
|
network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, SERVER_PUBLIC_KEY, serverAddr=SERVER_ADDRESS)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
command = input("Type a command:")
|
command = input("Type a command:")
|
||||||
separatedCommand = command.split(" ")
|
separatedCommand = command.split(" ")
|
||||||
@ -102,7 +110,7 @@ while True:
|
|||||||
|
|
||||||
if not LOGGED_IN:
|
if not LOGGED_IN:
|
||||||
if separatedCommand[0] == 'LIN' and len(separatedCommand) == 3:
|
if separatedCommand[0] == 'LIN' and len(separatedCommand) == 3:
|
||||||
network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, separatedCommand[1], SERVER_PUBLIC_KEY)
|
network.username = separatedCommand[1]
|
||||||
try:
|
try:
|
||||||
network.connectToServer(separatedCommand[2])
|
network.connectToServer(separatedCommand[2])
|
||||||
response = network.recieveMessage().decode('UTF-8')
|
response = network.recieveMessage().decode('UTF-8')
|
||||||
@ -115,7 +123,6 @@ while True:
|
|||||||
print("Error: "+str(e))
|
print("Error: "+str(e))
|
||||||
LOGGED_IN = False
|
LOGGED_IN = False
|
||||||
continue
|
continue
|
||||||
continue
|
|
||||||
else:
|
else:
|
||||||
print('You are already logged in!')
|
print('You are already logged in!')
|
||||||
|
|
||||||
@ -130,6 +137,7 @@ while True:
|
|||||||
if separatedCommand[0] == 'LOUT' and len(separatedCommand) == 1:
|
if separatedCommand[0] == 'LOUT' and len(separatedCommand) == 1:
|
||||||
network.sendMessage(command.encode('UTF-8'))
|
network.sendMessage(command.encode('UTF-8'))
|
||||||
print(network.recieveMessage().decode('UTF-8'))
|
print(network.recieveMessage().decode('UTF-8'))
|
||||||
|
LOGGED_IN = False
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if separatedCommand[0] == 'MKD' and len(separatedCommand) == 2:
|
if separatedCommand[0] == 'MKD' and len(separatedCommand) == 2:
|
||||||
@ -157,13 +165,18 @@ while True:
|
|||||||
print(network.recieveMessage().decode('UTF-8'))
|
print(network.recieveMessage().decode('UTF-8'))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
if separatedCommand[0] == 'RMF' and len(separatedCommand) == 2:
|
||||||
|
network.sendMessage(command.encode('UTF-8'))
|
||||||
|
print(network.recieveMessage().decode('UTF-8'))
|
||||||
|
continue
|
||||||
|
|
||||||
if separatedCommand[0] == 'UPL' and len(separatedCommand) == 2:
|
if separatedCommand[0] == 'UPL' and len(separatedCommand) == 2:
|
||||||
if os.path.isfile(separatedCommand[1]):
|
if os.path.isfile(separatedCommand[1]):
|
||||||
cmd = 'UPL ' + separatedCommand[1].split(os.path.sep)[-1]
|
cmd = 'UPL ' + separatedCommand[1].split(os.path.sep)[-1]
|
||||||
network.sendMessage(cmd.encode('UTF-8'))
|
network.sendMessage(cmd.encode('UTF-8'))
|
||||||
|
|
||||||
with open(separatedCommand[1], "rb") as file:
|
with open(separatedCommand[1], "rb") as file:
|
||||||
network.sendMessage(file.readlines())
|
network.sendMessage(file.read())
|
||||||
|
|
||||||
network.sendMessage('EOF'.encode('UTF-8'))
|
network.sendMessage('EOF'.encode('UTF-8'))
|
||||||
response = network.recieveMessage().decode('UTF-8')
|
response = network.recieveMessage().decode('UTF-8')
|
||||||
@ -177,12 +190,15 @@ while True:
|
|||||||
dnlFilename = separatedCommand[1].split(os.path.sep)[-1]
|
dnlFilename = separatedCommand[1].split(os.path.sep)[-1]
|
||||||
cmd = 'DNL ' + dnlFilename
|
cmd = 'DNL ' + dnlFilename
|
||||||
network.sendMessage(cmd.encode('UTF-8'))
|
network.sendMessage(cmd.encode('UTF-8'))
|
||||||
file = network.recieveMessage().decode('UTF-8')
|
filecontent = network.recieveMessage()
|
||||||
response = network.recieveMessage().decode('UTF-8')
|
response = network.recieveMessage().decode('UTF-8')
|
||||||
|
|
||||||
if response == 'OK':
|
print(DOWNLOAD_LOCATION + dnlFilename)
|
||||||
|
|
||||||
|
if response == 'EOF':
|
||||||
with open(DOWNLOAD_LOCATION + dnlFilename, "wb+") as file:
|
with open(DOWNLOAD_LOCATION + dnlFilename, "wb+") as file:
|
||||||
file.writelines(file)
|
file.write(filecontent)
|
||||||
|
print('OK')
|
||||||
else:
|
else:
|
||||||
print(response)
|
print(response)
|
||||||
|
|
||||||
|
File diff suppressed because one or more lines are too long
@ -13,7 +13,7 @@ from netsim import network_interface
|
|||||||
|
|
||||||
|
|
||||||
class NetWrapper:
|
class NetWrapper:
|
||||||
def __init__(self, privateKey: RsaKey, clientAddress: str, username: str, serverPubKey: RsaKey,
|
def __init__(self, privateKey: RsaKey, clientAddress: str, serverPubKey: RsaKey, username: str = "",
|
||||||
serverAddr: str = 'A'):
|
serverAddr: str = 'A'):
|
||||||
self.network = network_interface('./../../netsim/network/', clientAddress)
|
self.network = network_interface('./../../netsim/network/', clientAddress)
|
||||||
self.serverAddr = serverAddr
|
self.serverAddr = serverAddr
|
||||||
@ -105,6 +105,10 @@ class NetWrapper:
|
|||||||
print("Authentication error")
|
print("Authentication error")
|
||||||
|
|
||||||
def connectToServer(self, password: str) -> None:
|
def connectToServer(self, password: str) -> None:
|
||||||
|
if self.username == "":
|
||||||
|
raise Exception('Username is not initialized')
|
||||||
|
if password == "":
|
||||||
|
raise Exception('Passowrd may not be empty')
|
||||||
identStatus = self.identifyServer()
|
identStatus = self.identifyServer()
|
||||||
if not identStatus:
|
if not identStatus:
|
||||||
raise Exception('Server identification faliure')
|
raise Exception('Server identification faliure')
|
||||||
|
Reference in New Issue
Block a user