- inserted more comm.header() and comm.encode_message()
- added loop to be able to process multiple commands within one session, send "exit" to exit - added code 004: Exit - added include/auth.py with init() and check_password() - added settings variable PASSWORD (stores a SHA512-encrypted password) - added authenticate() to class comm
This commit is contained in:
parent
a4f497115a
commit
25d3344104
6 changed files with 77 additions and 29 deletions
|
@ -25,8 +25,8 @@
|
||||||
# aSRC (Aliased Server Remote Control)
|
# aSRC (Aliased Server Remote Control)
|
||||||
# - SERVER -
|
# - SERVER -
|
||||||
#
|
#
|
||||||
# program version: 0.0.0.20130426
|
# program version: 0.0.0.20130617
|
||||||
# protocol version: 0.2.20130423
|
# protocol version: 0.2.20130617
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@ -53,12 +53,16 @@ class ThreadedRequestHandler(socketserver.StreamRequestHandler):
|
||||||
# send motd
|
# send motd
|
||||||
self.request.sendall(bytes((comm.motd(MOTD) + "\n"), ENCODING))
|
self.request.sendall(bytes((comm.motd(MOTD) + "\n"), ENCODING))
|
||||||
|
|
||||||
# Receive data
|
repeat = True
|
||||||
self.data = str(self.rfile.readline().strip(), ENCODING)
|
|
||||||
|
|
||||||
# content handler
|
while repeat:
|
||||||
self.request.sendall(bytes(
|
# Receive data
|
||||||
comm.command(str(self.client_address), self.data), ENCODING))
|
self.data = str(self.rfile.readline().strip(), ENCODING)
|
||||||
|
|
||||||
|
if self.data == "exit": repeat = False
|
||||||
|
# content handler
|
||||||
|
self.request.sendall(bytes(
|
||||||
|
comm.command(str(self.client_address), self.data), ENCODING))
|
||||||
|
|
||||||
|
|
||||||
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
@ -67,10 +71,10 @@ class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# import global settings
|
# import global settings
|
||||||
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING
|
global VERBOSITY, HOST, PORT, TIMEOUT, ENCODING, PASSWORD
|
||||||
|
|
||||||
|
|
||||||
comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases)
|
comm.init(ServerVersion, ProtocolVersion, VERBOSITY, aliases, PASSWORD)
|
||||||
|
|
||||||
# parse arguments
|
# parse arguments
|
||||||
parser = argparser
|
parser = argparser
|
||||||
|
@ -170,6 +174,9 @@ if __name__ == '__main__':
|
||||||
# Encoding to be used when communicating with a client
|
# Encoding to be used when communicating with a client
|
||||||
ENCODING = 'utf-8'
|
ENCODING = 'utf-8'
|
||||||
|
|
||||||
|
# SHA512-encoded Password for authentication with the server
|
||||||
|
PASSWORD = 'a52fb4e552326fd8216f52a96f3d037309ef25acb22e5ead60bf258d2ea6652ab9fd5ab1117eb4bafe7476224d081ad7737132c4c096e9e8287a3c3f9d7d14f6'
|
||||||
|
|
||||||
# Dictionary of aliases. Use python syntax. You can use
|
# Dictionary of aliases. Use python syntax. You can use
|
||||||
# the variable send for text to send to the client.
|
# the variable send for text to send to the client.
|
||||||
#
|
#
|
||||||
|
|
Binary file not shown.
|
@ -1,3 +1,4 @@
|
||||||
from .argparser import argparser
|
from .argparser import argparser
|
||||||
|
from .auth import auth
|
||||||
from .comm import comm
|
from .comm import comm
|
||||||
from .statuscodes import statuscodes
|
from .statuscodes import statuscodes
|
||||||
|
|
23
include/auth.py
Normal file
23
include/auth.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# includes/auth.py
|
||||||
|
#
|
||||||
|
# module version: 0.0.20130617
|
||||||
|
# for protocol version 0.2.20130617
|
||||||
|
#
|
||||||
|
|
||||||
|
class auth:
|
||||||
|
passwd = ""
|
||||||
|
|
||||||
|
def init(password):
|
||||||
|
#from Crypto.Hash import Hash
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
global passwd
|
||||||
|
passwd = password
|
||||||
|
|
||||||
|
|
||||||
|
def check_passwd(incpass):
|
||||||
|
"""
|
||||||
|
checks a given password
|
||||||
|
"""
|
||||||
|
if hashlib.sha512(bytearray(incpass)).hexdigest() == passwd: return True
|
||||||
|
else: return False
|
|
@ -1,11 +1,12 @@
|
||||||
# includes/comm.py
|
# includes/comm.py
|
||||||
#
|
#
|
||||||
# module version: 0.0.20130426
|
# module version: 0.0.20130617
|
||||||
# for protocol versions 0.2.20130423 and later
|
# for protocol version 0.2.20130617
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
from .statuscodes import statuscodes
|
from .statuscodes import statuscodes
|
||||||
|
from .auth import auth
|
||||||
|
|
||||||
|
|
||||||
class comm:
|
class comm:
|
||||||
|
@ -16,16 +17,22 @@ class comm:
|
||||||
protocol_version = ""
|
protocol_version = ""
|
||||||
verbosity = 0
|
verbosity = 0
|
||||||
|
|
||||||
|
|
||||||
# initializes global settings
|
# initializes global settings
|
||||||
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases):
|
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases, Password):
|
||||||
"""
|
"""
|
||||||
makes settings aviable in this class
|
makes settings aviable in this class and initialize auth
|
||||||
"""
|
"""
|
||||||
global aliases, server_version, protocol_version, verbosity
|
global aliases, server_version, protocol_version, verbosity
|
||||||
aliases = Aliases
|
aliases = Aliases
|
||||||
server_version = ServerVersion
|
server_version = ServerVersion
|
||||||
protocol_version = ProtocolVersion
|
protocol_version = ProtocolVersion
|
||||||
verbosity = Verbosity
|
verbosity = Verbosity
|
||||||
|
|
||||||
|
auth.init(Password)
|
||||||
|
|
||||||
|
def authenticate():
|
||||||
|
pass
|
||||||
|
|
||||||
# builds an header
|
# builds an header
|
||||||
def header(CodeList, AdditionalHeaderLines = ""):
|
def header(CodeList, AdditionalHeaderLines = ""):
|
||||||
|
@ -39,10 +46,10 @@ class comm:
|
||||||
for i in range(0, len(CodeList)):
|
for i in range(0, len(CodeList)):
|
||||||
ret += CodeList[int(i)] + " " +\
|
ret += CodeList[int(i)] + " " +\
|
||||||
statuscodes.description['s' + CodeList[int(i)]] + "\n"
|
statuscodes.description['s' + CodeList[int(i)]] + "\n"
|
||||||
ret += "ServerVersion: " + server_version + "\n" +\
|
ret += AdditionalHeaderLines + "\n\n"
|
||||||
AdditionalHeaderLines + "\n\n"
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
# formats a massage
|
# formats a massage
|
||||||
def encode_message(text):
|
def encode_message(text):
|
||||||
"""
|
"""
|
||||||
|
@ -50,6 +57,7 @@ class comm:
|
||||||
"""
|
"""
|
||||||
return text.replace('{', '\{').replace('}', '\}')
|
return text.replace('{', '\{').replace('}', '\}')
|
||||||
|
|
||||||
|
|
||||||
# "deformats" a message
|
# "deformats" a message
|
||||||
def decode_message(text):
|
def decode_message(text):
|
||||||
"""
|
"""
|
||||||
|
@ -57,12 +65,14 @@ class comm:
|
||||||
"""
|
"""
|
||||||
return text.replace('\{', '{').replace('\}', '}')
|
return text.replace('\{', '{').replace('\}', '}')
|
||||||
|
|
||||||
|
|
||||||
# returns the motd
|
# returns the motd
|
||||||
def motd(motd):
|
def motd(motd):
|
||||||
"""
|
"""
|
||||||
builds and returns a motd package
|
builds and returns a motd package
|
||||||
"""
|
"""
|
||||||
return comm.header(['202', '003']) + comm.encode_message(motd)
|
return comm.header(['202', '003']) + comm.encode_message(motd) + "\n{END}"
|
||||||
|
|
||||||
|
|
||||||
# handles the content
|
# handles the content
|
||||||
def command(client_address, data):
|
def command(client_address, data):
|
||||||
|
@ -80,12 +90,18 @@ class comm:
|
||||||
if verbosity >= 2: print("Got valid service command from"
|
if verbosity >= 2: print("Got valid service command from"
|
||||||
+ str(client_address) + ": ", data)
|
+ str(client_address) + ": ", data)
|
||||||
|
|
||||||
ret = ret +\
|
hdr = comm.header(['202','002'])
|
||||||
"202 Valid Service Command\n"\
|
ret = "ServerVersion:" + server_version + "\n"\
|
||||||
"002 Version\n"\
|
|
||||||
"ServerVersion:" + server_version + "\n"\
|
|
||||||
"ProtocolVersion:" + protocol_version + "\n"
|
"ProtocolVersion:" + protocol_version + "\n"
|
||||||
|
|
||||||
|
# if it's 'exit', then send .. nothing!
|
||||||
|
elif data == 'exit':
|
||||||
|
if verbosity >= 2: print("Got valid service command from"
|
||||||
|
+ str(client_address) + ": ", data)
|
||||||
|
|
||||||
|
hdr = comm.header(['202','004'])
|
||||||
|
ret = "closing connection..."
|
||||||
|
|
||||||
# if it's 'help', give a little help
|
# if it's 'help', give a little help
|
||||||
elif data == 'help':
|
elif data == 'help':
|
||||||
|
|
||||||
|
@ -93,10 +109,10 @@ class comm:
|
||||||
+ str(client_address) + ": ", data)
|
+ str(client_address) + ": ", data)
|
||||||
|
|
||||||
# send status code
|
# send status code
|
||||||
ret = ret + "202 Valid Service Command\n\n"
|
hdr = comm.header(['202'])
|
||||||
|
|
||||||
# send the list of aliases
|
# send the list of aliases
|
||||||
ret = ret + "Aviable aliases:\n"
|
ret = "Aviable aliases:\n"
|
||||||
for i in aliases.keys():
|
for i in aliases.keys():
|
||||||
ret = ret + str(i) + "\n"
|
ret = ret + str(i) + "\n"
|
||||||
|
|
||||||
|
@ -104,9 +120,9 @@ class comm:
|
||||||
elif data in aliases:
|
elif data in aliases:
|
||||||
|
|
||||||
# send status code
|
# send status code
|
||||||
ret = ret + "201 Valid Command\n\n"
|
hdr = comm.header(['201'])
|
||||||
|
|
||||||
# ohmagawd! a debug message!!1!
|
# ohmagawd! a debug message!!1! (sry...)
|
||||||
if verbosity >= 2: print("Got valid command from"
|
if verbosity >= 2: print("Got valid command from"
|
||||||
+ str(client_address) + ": ", data)
|
+ str(client_address) + ": ", data)
|
||||||
|
|
||||||
|
@ -125,11 +141,11 @@ class comm:
|
||||||
else:
|
else:
|
||||||
|
|
||||||
# send status code
|
# send status code
|
||||||
ret = ret + "203 Invalid Command\n"
|
hdr = comm.header(['203'])
|
||||||
|
|
||||||
if verbosity >= 2: print("Got invalid command from",
|
if verbosity >= 2: print("Got invalid command from",
|
||||||
str(client_address), ": ", data)
|
str(client_address), ": ", data)
|
||||||
|
|
||||||
ret = ret + "{END}\n"
|
ret = comm.encode_message(ret) + "{END}\n\n"
|
||||||
|
|
||||||
return ret
|
return hdr + ret
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# include/statuscodes.py
|
# include/statuscodes.py
|
||||||
#
|
#
|
||||||
# module version: 1.0.20130426
|
# module version: 1.0.20130617
|
||||||
# for protocol versions 0.2.20130423 and later
|
# for protocol version 0.2.20130617
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ class statuscodes:
|
||||||
s001 = "OK",
|
s001 = "OK",
|
||||||
s002 = "Version",
|
s002 = "Version",
|
||||||
s003 = "MOTD",
|
s003 = "MOTD",
|
||||||
|
s004 = "Exit",
|
||||||
# 100 authentication and maintenance
|
# 100 authentication and maintenance
|
||||||
s101 = "Challenge",
|
s101 = "Challenge",
|
||||||
s102 = "Success",
|
s102 = "Success",
|
||||||
|
|
Loading…
Reference in a new issue