mirror of
https://github.com/Py-KMS-Organization/py-kms.git
synced 2025-05-16 16:14:50 -04:00
Pretty-print server errors, added argparse catching
This commit is contained in:
parent
bc9365f791
commit
f3974813a4
2 changed files with 46 additions and 30 deletions
|
@ -120,10 +120,16 @@ MsgMap = {0 : {'text' : "{yellow}\n\t\t\tClient generating RPC Bind Request...{
|
||||||
-2 : {'text' : "{white}\n\n\t\t\t\t\t\t\t\tClient sending{end}", 'where' : "srv"},
|
-2 : {'text' : "{white}\n\n\t\t\t\t\t\t\t\tClient sending{end}", 'where' : "srv"},
|
||||||
-3 : {'text' : "{white}\t\t\t\t\t\t\t\tClient receiving{end}", 'where' : "srv"},
|
-3 : {'text' : "{white}\t\t\t\t\t\t\t\tClient receiving{end}", 'where' : "srv"},
|
||||||
-4 : {'text' : "{white}\n\nServer sending{end}", 'where' : "clt"},
|
-4 : {'text' : "{white}\n\nServer sending{end}", 'where' : "clt"},
|
||||||
30 : {'text' : "{red}{bold}Server connection timed out. Exiting...{end}", 'where' : "srv"}
|
|
||||||
|
30 : {'text' : "{red}{bold}Server connection timed out. Exiting...{end}", 'where' : "srv"},
|
||||||
|
31 : {'text' : "{red}{bold}HWID '{0}' is invalid. Digit {1} non hexadecimal. Exiting...{end}", 'where' : "srv"},
|
||||||
|
32 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is odd length. Exiting...{end}", 'where' : "srv"},
|
||||||
|
33 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too short. Exiting...{end}", 'where' : "srv"},
|
||||||
|
34 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too long. Exiting...{end}", 'where' : "srv"},
|
||||||
|
35 : {'text' : "{red}{bold}Port number '{0}' is invalid. Enter between 1 - 65535. Exiting...{end}", 'where' : "srv"},
|
||||||
|
36 : {'text' : "{red}{bold}{0}. Exiting...{end}", 'where' : "srv"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def pick_MsgMap(messagelist):
|
def pick_MsgMap(messagelist):
|
||||||
pattern = r"(?<!\{)\{([^}]+)\}(?!\})"
|
pattern = r"(?<!\{)\{([^}]+)\}(?!\})"
|
||||||
picktxt, pickarrw = [ [] for _ in range(2) ]
|
picktxt, pickarrw = [ [] for _ in range(2) ]
|
||||||
|
@ -163,7 +169,7 @@ if pyver < (3, 3):
|
||||||
|
|
||||||
# https://ryanjoneil.github.io/posts/2014-02-14-capturing-stdout-in-a-python-child-process.html
|
# https://ryanjoneil.github.io/posts/2014-02-14-capturing-stdout-in-a-python-child-process.html
|
||||||
class ShellMessage(object):
|
class ShellMessage(object):
|
||||||
view = None
|
view = True
|
||||||
|
|
||||||
class Collect(StringIO):
|
class Collect(StringIO):
|
||||||
# Capture string sent to stdout.
|
# Capture string sent to stdout.
|
||||||
|
@ -176,8 +182,9 @@ class ShellMessage(object):
|
||||||
self.print_queue = Queue.Queue()
|
self.print_queue = Queue.Queue()
|
||||||
self.get_text = get_text
|
self.get_text = get_text
|
||||||
self.plaintext = []
|
self.plaintext = []
|
||||||
self.put_text = put_text
|
if not isinstance(put_text, list):
|
||||||
|
self.put_text = [put_text]
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if not ShellMessage.view:
|
if not ShellMessage.view:
|
||||||
return
|
return
|
||||||
|
@ -204,7 +211,7 @@ class ShellMessage(object):
|
||||||
# Save everything that would otherwise go to stdout.
|
# Save everything that would otherwise go to stdout.
|
||||||
outstream = ShellMessage.Collect()
|
outstream = ShellMessage.Collect()
|
||||||
sys.stdout = outstream
|
sys.stdout = outstream
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Print something.
|
# Print something.
|
||||||
if not isinstance(self.nshell, list):
|
if not isinstance(self.nshell, list):
|
||||||
|
|
|
@ -36,9 +36,7 @@ class KeyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
allow_reuse_address = True
|
allow_reuse_address = True
|
||||||
|
|
||||||
def handle_timeout(self):
|
def handle_timeout(self):
|
||||||
errmsg = ShellMessage.Process(30, get_text = True).run()
|
server_errors(30)
|
||||||
loggersrv.error(errmsg[0])
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
class server_thread(threading.Thread):
|
class server_thread(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -98,9 +96,17 @@ The default is \"364F463A8863D35F\" or type \"RANDOM\" to auto generate the HWID
|
||||||
'def' : os.path.dirname(os.path.abspath( __file__ )) + "/pykms_logserver.log", 'des' : "logfile"},
|
'def' : os.path.dirname(os.path.abspath( __file__ )) + "/pykms_logserver.log", 'des' : "logfile"},
|
||||||
'lsize' : {'help' : 'Use this flag to set a maximum size (in MB) to the output log file. Desactivated by default.', 'def' : 0, 'des': "logsize"},
|
'lsize' : {'help' : 'Use this flag to set a maximum size (in MB) to the output log file. Desactivated by default.', 'def' : 0, 'des': "logsize"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class KmsSrvException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class KmsSrvParser(argparse.ArgumentParser):
|
||||||
|
def error(self, message):
|
||||||
|
raise KmsSrvException(message)
|
||||||
|
|
||||||
def server_options():
|
def server_options():
|
||||||
parser = argparse.ArgumentParser(description = srv_description, epilog = 'version: ' + srv_version)
|
parser = KmsSrvParser(description = srv_description, epilog = 'version: ' + srv_version)
|
||||||
parser.add_argument("ip", nargs = "?", action = "store", default = srv_options['ip']['def'], help = srv_options['ip']['help'], type = str)
|
parser.add_argument("ip", nargs = "?", action = "store", default = srv_options['ip']['def'], help = srv_options['ip']['help'], type = str)
|
||||||
parser.add_argument("port", nargs = "?", action = "store", default = srv_options['port']['def'], help = srv_options['port']['help'], type = int)
|
parser.add_argument("port", nargs = "?", action = "store", default = srv_options['port']['def'], help = srv_options['port']['help'], type = int)
|
||||||
parser.add_argument("-e", "--epid", dest = srv_options['epid']['des'], default = srv_options['epid']['def'], help = srv_options['epid']['help'], type = str)
|
parser.add_argument("-e", "--epid", dest = srv_options['epid']['des'], default = srv_options['epid']['def'], help = srv_options['epid']['help'], type = str)
|
||||||
|
@ -123,10 +129,22 @@ def server_options():
|
||||||
help = srv_options['lfile']['help'], type = str)
|
help = srv_options['lfile']['help'], type = str)
|
||||||
parser.add_argument("-S", "--logsize", dest = srv_options['lsize']['des'], action = "store", default = srv_options['lsize']['def'],
|
parser.add_argument("-S", "--logsize", dest = srv_options['lsize']['des'], action = "store", default = srv_options['lsize']['def'],
|
||||||
help = srv_options['lsize']['help'], type = float)
|
help = srv_options['lsize']['help'], type = float)
|
||||||
|
|
||||||
srv_config.update(vars(parser.parse_args()))
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
srv_config.update(vars(parser.parse_args()))
|
||||||
|
except KmsSrvException as e:
|
||||||
|
server_errors(36, False, str(e), False)
|
||||||
|
|
||||||
|
def server_errors(error_num, get_text = True, put_text = None, log_text = True):
|
||||||
|
""" error_num --> an int or list of int.
|
||||||
|
put_text --> a string or list of strings. (applied to each "error_num")
|
||||||
|
"""
|
||||||
|
error_msgs = ShellMessage.Process(error_num, get_text = get_text, put_text = put_text).run()
|
||||||
|
if log_text:
|
||||||
|
for err in error_msgs:
|
||||||
|
loggersrv.error(err)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
def server_check():
|
def server_check():
|
||||||
# Setup hidden or not messages.
|
# Setup hidden or not messages.
|
||||||
ShellMessage.view = ( False if srv_config['logfile'] == 'STDOUT' else True )
|
ShellMessage.view = ( False if srv_config['logfile'] == 'STDOUT' else True )
|
||||||
|
@ -144,19 +162,15 @@ def server_check():
|
||||||
diff = set(hexstr).symmetric_difference(set(hexsub))
|
diff = set(hexstr).symmetric_difference(set(hexsub))
|
||||||
|
|
||||||
if len(diff) != 0:
|
if len(diff) != 0:
|
||||||
loggersrv.error("HWID \"%s\" is invalid. Non hexadecimal digit %s found." %(hexstr.upper(), diff))
|
server_errors(31, put_text = [hexstr.upper(), diff])
|
||||||
sys.exit(1)
|
|
||||||
else:
|
else:
|
||||||
lh = len(hexsub)
|
lh = len(hexsub)
|
||||||
if lh % 2 != 0:
|
if lh % 2 != 0:
|
||||||
loggersrv.error("HWID \"%s\" is invalid. Hex string is odd length." % hexsub.upper())
|
server_errors(32, put_text = hexsub.upper())
|
||||||
sys.exit(1)
|
|
||||||
elif lh < 16:
|
elif lh < 16:
|
||||||
loggersrv.error("HWID \"%s\" is invalid. Hex string is too short." % hexsub.upper())
|
server_errors(33, put_text = hexsub.upper())
|
||||||
sys.exit(1)
|
|
||||||
elif lh > 16:
|
elif lh > 16:
|
||||||
loggersrv.error("HWID \"%s\" is invalid. Hex string is too long." % hexsub.upper())
|
server_errors(34, put_text = hexsub.upper())
|
||||||
sys.exit(1)
|
|
||||||
else:
|
else:
|
||||||
srv_config['hwid'] = binascii.a2b_hex(hexsub)
|
srv_config['hwid'] = binascii.a2b_hex(hexsub)
|
||||||
|
|
||||||
|
@ -187,13 +201,8 @@ def server_check():
|
||||||
srv_config['dbSupport'] = True
|
srv_config['dbSupport'] = True
|
||||||
|
|
||||||
# Check port.
|
# Check port.
|
||||||
try:
|
if not 1 <= srv_config['port'] <= 65535:
|
||||||
if srv_config['port'] > 65535:
|
server_errors(35, put_text = srv_config['port'])
|
||||||
loggersrv.error('Please enter a valid port number between 1 - 65535')
|
|
||||||
sys.exit(1)
|
|
||||||
except Exception as e:
|
|
||||||
loggersrv.error('%s' %e)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
def server_create():
|
def server_create():
|
||||||
server = KeyServer((srv_config['ip'], srv_config['port']), kmsServerHandler)
|
server = KeyServer((srv_config['ip'], srv_config['port']), kmsServerHandler)
|
||||||
|
@ -204,7 +213,7 @@ def server_create():
|
||||||
|
|
||||||
def srv_main_without_gui():
|
def srv_main_without_gui():
|
||||||
# Parse options.
|
# Parse options.
|
||||||
server_options()
|
server_options()
|
||||||
# Run threaded server.
|
# Run threaded server.
|
||||||
serverqueue.put('start')
|
serverqueue.put('start')
|
||||||
serverthread.join()
|
serverthread.join()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue