mirror of
https://github.com/SystemRage/py-kms.git
synced 2024-11-22 08:15:38 +01:00
Added -F FILESTDOUT option server-side and client-side
generalized pretty print errors function modified shell message printer and logger creator shifted lcid checker created logfile checker colored logging on stdout
This commit is contained in:
parent
f3974813a4
commit
dddc02530b
@ -22,7 +22,7 @@ from pykms_RequestV5 import kmsRequestV5
|
||||
from pykms_RequestV6 import kmsRequestV6
|
||||
from pykms_RpcBase import rpcBase
|
||||
from pykms_DB2Dict import kmsDB2Dict
|
||||
from pykms_Misc import logger_create
|
||||
from pykms_Misc import logger_create, check_logfile
|
||||
from pykms_Format import justify, byterize, enco, deco, ShellMessage
|
||||
|
||||
clt_description = 'KMS Client Emulator written in Python'
|
||||
@ -46,7 +46,8 @@ clt_options = {
|
||||
will be generated.', 'def' : None, 'des' : "machineName"},
|
||||
'llevel' : {'help' : 'Use this option to set a log level. The default is \"ERROR\".', 'def' : "ERROR", 'des' : "loglevel",
|
||||
'choi' : ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "MINI"]},
|
||||
'lfile' : {'help' : 'Use this option to set an output log file. The default is \"pykms_logclient.log\" or type \"STDOUT\" to view log info on stdout.',
|
||||
'lfile' : {'help' : 'Use this option to set an output log file. The default is \"pykms_logclient.log\". Type \"STDOUT\" to view \
|
||||
log info on stdout. Type \"FILESTDOUT\" to combine previous actions.',
|
||||
'def' : os.path.dirname(os.path.abspath( __file__ )) + "/pykms_logclient.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"},
|
||||
}
|
||||
@ -61,17 +62,19 @@ def client_options():
|
||||
parser.add_argument("-n", "--name", dest = clt_options['name']['des'] , default = clt_options['name']['def'], help = clt_options['name']['help'], type = str)
|
||||
parser.add_argument("-V", "--loglevel", dest = clt_options['llevel']['des'], action = "store", choices = clt_options['llevel']['choi'],
|
||||
default = clt_options['llevel']['def'], help = clt_options['llevel']['help'], type = str)
|
||||
parser.add_argument("-F", "--logfile", dest = clt_options['lfile']['des'], action = "store", default = clt_options['lfile']['def'],
|
||||
parser.add_argument("-F", "--logfile", nargs = "+", dest = clt_options['lfile']['des'], default = clt_options['lfile']['def'],
|
||||
help = clt_options['lfile']['help'], type = str)
|
||||
parser.add_argument("-S", "--logsize", dest = clt_options['lsize']['des'], action = "store", default = clt_options['lsize']['def'],
|
||||
help = clt_options['lsize']['help'], type = float)
|
||||
|
||||
clt_config.update(vars(parser.parse_args()))
|
||||
# Check logfile.
|
||||
clt_config['logfile'] = check_logfile(clt_config['logfile'], clt_options['lfile']['def'], loggerclt)
|
||||
|
||||
|
||||
def client_check():
|
||||
# Setup hidden or not messages.
|
||||
ShellMessage.view = ( False if clt_config['logfile'] == 'STDOUT' else True )
|
||||
ShellMessage.view = ( False if any(i in ['STDOUT', 'FILESTDOUT'] for i in clt_config['logfile']) else True )
|
||||
# Create log.
|
||||
logger_create(loggerclt, clt_config, mode = 'a')
|
||||
|
||||
|
@ -71,7 +71,8 @@ def justify(astring, indent = 35, break_every = 100):
|
||||
return justy
|
||||
|
||||
##----------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
ColorMap = {'red' : '\x1b[91m',
|
||||
ColorMap = {'gray' : '\x1b[90m',
|
||||
'red' : '\x1b[91m',
|
||||
'green' : '\x1b[92m',
|
||||
'yellow' : '\x1b[93m',
|
||||
'blue' : '\x1b[94m',
|
||||
@ -121,13 +122,13 @@ MsgMap = {0 : {'text' : "{yellow}\n\t\t\tClient generating RPC Bind Request...{
|
||||
-3 : {'text' : "{white}\t\t\t\t\t\t\t\tClient receiving{end}", 'where' : "srv"},
|
||||
-4 : {'text' : "{white}\n\nServer sending{end}", 'where' : "clt"},
|
||||
|
||||
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"},
|
||||
40 : {'text' : "{red}{bold}Server connection timed out. Exiting...{end}", 'where' : "srv"},
|
||||
41 : {'text' : "{red}{bold}HWID '{0}' is invalid. Digit {1} non hexadecimal. Exiting...{end}", 'where' : "srv"},
|
||||
42 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is odd length. Exiting...{end}", 'where' : "srv"},
|
||||
43 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too short. Exiting...{end}", 'where' : "srv"},
|
||||
44 : {'text' : "{red}{bold}HWID '{0}' is invalid. Hex string is too long. Exiting...{end}", 'where' : "srv"},
|
||||
45 : {'text' : "{red}{bold}Port number '{0}' is invalid. Enter between 1 - 65535. Exiting...{end}", 'where' : "srv"},
|
||||
46 : {'text' : "{red}{bold}{0}. Exiting...{end}", 'where' : "srv"},
|
||||
}
|
||||
|
||||
def pick_MsgMap(messagelist):
|
||||
@ -181,13 +182,32 @@ class ShellMessage(object):
|
||||
self.nshell = nshell
|
||||
self.print_queue = Queue.Queue()
|
||||
self.get_text = get_text
|
||||
self.put_text = put_text
|
||||
self.plaintext = []
|
||||
|
||||
if not isinstance(nshell, list):
|
||||
self.nshell = [nshell]
|
||||
if not isinstance(put_text, list):
|
||||
self.put_text = [put_text]
|
||||
|
||||
|
||||
def formatter(self, num):
|
||||
if self.put_text is None:
|
||||
self.msg = MsgMap[num]['text'].format(**ColorExtraMap)
|
||||
else:
|
||||
self.msg = MsgMap[num]['text'].format(*self.put_text, **ColorExtraMap)
|
||||
|
||||
if self.get_text:
|
||||
self.plaintext.append(unshell_message(self.msg, m = 0)[0]["tag00"]['text'])
|
||||
|
||||
def run(self):
|
||||
if not ShellMessage.view:
|
||||
return
|
||||
if self.get_text:
|
||||
for num in self.nshell:
|
||||
self.formatter(num)
|
||||
return self.plaintext
|
||||
else:
|
||||
return
|
||||
|
||||
# Start thread process.
|
||||
print_thread = threading.Thread(target = self.spawn(), args=(self.print_queue,))
|
||||
print_thread.setDaemon(True)
|
||||
@ -214,16 +234,9 @@ class ShellMessage(object):
|
||||
|
||||
try:
|
||||
# Print something.
|
||||
if not isinstance(self.nshell, list):
|
||||
self.nshell = [self.nshell]
|
||||
for n in self.nshell:
|
||||
if self.put_text is None:
|
||||
msg = MsgMap[n]['text'].format(**ColorExtraMap)
|
||||
else:
|
||||
msg = MsgMap[n]['text'].format(*self.put_text, **ColorExtraMap)
|
||||
print(msg, flush = True)
|
||||
if self.get_text:
|
||||
self.plaintext.append(unshell_message(msg, m = 0)[0]["tag00"]['text'])
|
||||
for num in self.nshell:
|
||||
self.formatter(num)
|
||||
print(self.msg, flush = True)
|
||||
finally:
|
||||
# Restore stdout and send content.
|
||||
sys.stdout = sys.__stdout__
|
||||
|
1024
py-kms/pykms_Misc.py
1024
py-kms/pykms_Misc.py
File diff suppressed because it is too large
Load Diff
@ -23,7 +23,7 @@ except ImportError:
|
||||
import pykms_RpcBind, pykms_RpcRequest
|
||||
from pykms_RpcBase import rpcBase
|
||||
from pykms_Dcerpc import MSRPCHeader
|
||||
from pykms_Misc import ValidLcid, logger_create
|
||||
from pykms_Misc import logger_create, check_logfile, check_lcid, pretty_errors
|
||||
from pykms_Format import enco, deco, ShellMessage
|
||||
|
||||
srv_description = 'KMS Server Emulator written in Python'
|
||||
@ -36,8 +36,8 @@ class KeyServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||
allow_reuse_address = True
|
||||
|
||||
def handle_timeout(self):
|
||||
server_errors(30)
|
||||
|
||||
pretty_errors(40, loggersrv)
|
||||
|
||||
class server_thread(threading.Thread):
|
||||
def __init__(self):
|
||||
threading.Thread.__init__(self)
|
||||
@ -92,7 +92,8 @@ The default is \"364F463A8863D35F\" or type \"RANDOM\" to auto generate the HWID
|
||||
'time' : {'help' : 'Max time (in seconds) for server to generate an answer. If \"None\" (default) serve forever.', 'def' : None, 'des' : "timeout"},
|
||||
'llevel' : {'help' : 'Use this option to set a log level. The default is \"ERROR\".', 'def' : "ERROR", 'des' : "loglevel",
|
||||
'choi' : ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "MINI"]},
|
||||
'lfile' : {'help' : 'Use this option to set or not an output log file. The default is \"pykms_logserver.log\" or type \"STDOUT\" to view log info on stdout.',
|
||||
'lfile' : {'help' : 'Use this option to set an output log file. The default is \"pykms_logserver.log\". Type \"STDOUT\" to view \
|
||||
log info on stdout. Type \"FILESTDOUT\" to combine previous actions.',
|
||||
'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"},
|
||||
}
|
||||
@ -125,32 +126,24 @@ def server_options():
|
||||
help = srv_options['time']['help'], type = int)
|
||||
parser.add_argument("-V", "--loglevel", dest = srv_options['llevel']['des'], action = "store", choices = srv_options['llevel']['choi'],
|
||||
default = srv_options['llevel']['def'], help = srv_options['llevel']['help'], type = str)
|
||||
parser.add_argument("-F", "--logfile", dest = srv_options['lfile']['des'], action = "store", default = srv_options['lfile']['def'],
|
||||
parser.add_argument("-F", "--logfile", nargs = "+", dest = srv_options['lfile']['des'], default = srv_options['lfile']['def'],
|
||||
help = srv_options['lfile']['help'], type = str)
|
||||
parser.add_argument("-S", "--logsize", dest = srv_options['lsize']['des'], action = "store", default = srv_options['lsize']['def'],
|
||||
help = srv_options['lsize']['help'], type = float)
|
||||
|
||||
try:
|
||||
srv_config.update(vars(parser.parse_args()))
|
||||
# Check logfile.
|
||||
srv_config['logfile'] = check_logfile(srv_config['logfile'], srv_options['lfile']['def'], loggersrv)
|
||||
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)
|
||||
pretty_errors(46, loggersrv, get_text = False, put_text = str(e), log_text = False)
|
||||
|
||||
def server_check():
|
||||
# Setup hidden or not messages.
|
||||
ShellMessage.view = ( False if srv_config['logfile'] == 'STDOUT' else True )
|
||||
ShellMessage.view = ( False if any(i in ['STDOUT', 'FILESTDOUT'] for i in srv_config['logfile']) else True )
|
||||
# Create log.
|
||||
logger_create(loggersrv, srv_config, mode = 'a')
|
||||
|
||||
|
||||
# Random HWID.
|
||||
if srv_config['hwid'] == "RANDOM":
|
||||
randomhwid = uuid.uuid4().hex
|
||||
@ -162,34 +155,20 @@ def server_check():
|
||||
diff = set(hexstr).symmetric_difference(set(hexsub))
|
||||
|
||||
if len(diff) != 0:
|
||||
server_errors(31, put_text = [hexstr.upper(), diff])
|
||||
pretty_errors(41, loggersrv, put_text = [hexstr.upper(), diff])
|
||||
else:
|
||||
lh = len(hexsub)
|
||||
if lh % 2 != 0:
|
||||
server_errors(32, put_text = hexsub.upper())
|
||||
pretty_errors(42, loggersrv, put_text = hexsub.upper())
|
||||
elif lh < 16:
|
||||
server_errors(33, put_text = hexsub.upper())
|
||||
pretty_errors(43, loggersrv, put_text = hexsub.upper())
|
||||
elif lh > 16:
|
||||
server_errors(34, put_text = hexsub.upper())
|
||||
pretty_errors(44, loggersrv, put_text = hexsub.upper())
|
||||
else:
|
||||
srv_config['hwid'] = binascii.a2b_hex(hexsub)
|
||||
|
||||
# Check LCID.
|
||||
# http://stackoverflow.com/questions/3425294/how-to-detect-the-os-default-language-in-python
|
||||
if not srv_config['lcid'] or (srv_config['lcid'] not in ValidLcid):
|
||||
if hasattr(sys, 'implementation') and sys.implementation.name == 'cpython':
|
||||
srv_config['lcid'] = 1033
|
||||
elif os.name == 'nt':
|
||||
import ctypes
|
||||
|
||||
srv_config['lcid'] = ctypes.windll.kernel32.GetUserDefaultUILanguage()
|
||||
else:
|
||||
import locale
|
||||
|
||||
try:
|
||||
srv_config['lcid'] = next(k for k, v in locale.windows_locale.items() if v == locale.getdefaultlocale()[0])
|
||||
except StopIteration:
|
||||
srv_config['lcid'] = 1033
|
||||
srv_config['lcid'] = check_lcid(srv_config['lcid'], loggersrv)
|
||||
|
||||
# Check sqlite.
|
||||
try:
|
||||
@ -202,7 +181,7 @@ def server_check():
|
||||
|
||||
# Check port.
|
||||
if not 1 <= srv_config['port'] <= 65535:
|
||||
server_errors(35, put_text = srv_config['port'])
|
||||
pretty_errors(45, loggersrv, put_text = srv_config['port'])
|
||||
|
||||
def server_create():
|
||||
server = KeyServer((srv_config['ip'], srv_config['port']), kmsServerHandler)
|
||||
|
Loading…
Reference in New Issue
Block a user