Skip to content

Commit

Permalink
Auto-align pretty messages
Browse files Browse the repository at this point in the history
messages syncronization client-server
simplified pretty print function arguments
  • Loading branch information
SystemRage committed Nov 1, 2019
1 parent 19e5abb commit 7c14eb1
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 140 deletions.
28 changes: 16 additions & 12 deletions py-kms/pykms_Base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import binascii
import logging
import os
import sys
import time
import uuid
import socket
Expand Down Expand Up @@ -113,7 +111,7 @@ def serverLogic(self, kmsRequest):
if self.srv_config['sqlite'] and self.srv_config['dbSupport']:
self.dbName = sql_initialize()

pretty_printer(None, num_text = 15)
pretty_printer(num_text = 15)
kmsRequest = byterize(kmsRequest)
loggersrv.debug("KMS Request Bytes: \n%s\n" % justify(deco(binascii.b2a_hex(enco(str(kmsRequest), 'latin-1')), 'latin-1')))
loggersrv.debug("KMS Request: \n%s\n" % justify(kmsRequest.dump(print_to_stdout = False)))
Expand All @@ -131,12 +129,12 @@ def serverLogic(self, kmsRequest):
tz = get_localzone()
local_dt = tz.localize(requestDatetime)
except UnknownTimeZoneError:
pretty_printer(loggersrv.warning, get_text = True, log_text = True,
put_text = "{yellow}{bold}Unknown time zone ! Request time not localized.{end}")
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}Unknown time zone ! Request time not localized.{end}")
local_dt = requestDatetime
except ImportError:
pretty_printer(loggersrv.warning, get_text = True, log_text = True,
put_text = "{yellow}{bold}Module 'tzlocal' not available ! Request time not localized.{end}")
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}Module 'tzlocal' not available ! Request time not localized.{end}")
local_dt = requestDatetime

# Activation threshold.
Expand All @@ -147,15 +145,19 @@ def serverLogic(self, kmsRequest):
if 0 < self.srv_config["CurrentClientCount"] < MinClients:
# fixed to 6 (product server) or 26 (product desktop)
currentClientCount = MinClients + 1
loggersrv.warning("Not enough clients ! Fixed with %s, but activated client could be detected as not genuine !" %currentClientCount)
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}Not enough clients ! Fixed with %s, but activated client \
could be detected as not genuine !{end}" %currentClientCount)
elif MinClients <= self.srv_config["CurrentClientCount"] < RequiredClients:
currentClientCount = self.srv_config["CurrentClientCount"]
loggersrv.warning("With count = %s, activated client could be detected as not genuine !" %currentClientCount)
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}With count = %s, activated client could be detected as not genuine !{end}" %currentClientCount)
elif self.srv_config["CurrentClientCount"] >= RequiredClients:
# fixed to 10 (product server) or 50 (product desktop)
currentClientCount = RequiredClients
if self.srv_config["CurrentClientCount"] > RequiredClients:
loggersrv.warning("Too many clients ! Fixed with %s" %currentClientCount)
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}Too many clients ! Fixed with %s{end}" %currentClientCount)
else:
# fixed to 10 (product server) or 50 (product desktop)
currentClientCount = RequiredClients
Expand All @@ -176,14 +178,16 @@ def serverLogic(self, kmsRequest):
break
except:
skuName = skuId
loggersrv.warning("Can't find a name for this product !!")
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}Can't find a name for this product !{end}")

try:
if uuid.UUID(appitem['Id']) == applicationId:
appName = appitem['DisplayName']
except:
appName = applicationId
loggersrv.warning("Can't find a name for this application group !!")
pretty_printer(log_obj = loggersrv.warning,
put_text = "{reverse}{yellow}{bold}Can't find a name for this application group !{end}")

infoDict = {
"machineName" : kmsRequest.getMachineName(),
Expand Down
77 changes: 46 additions & 31 deletions py-kms/pykms_Client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env python3

import re
import argparse
import binascii
import datetime
import random
Expand All @@ -11,7 +10,6 @@
import uuid
import logging
import os
import errno

import pykms_RpcBind, pykms_RpcRequest
from pykms_Filetimes import dt_to_filetime
Expand All @@ -23,6 +21,7 @@
from pykms_RpcBase import rpcBase
from pykms_DB2Dict import kmsDB2Dict
from pykms_Misc import logger_create, check_logfile, pretty_printer
from pykms_Misc import KmsParser, KmsException
from pykms_Format import justify, byterize, enco, deco, ShellMessage

clt_description = 'KMS Client Emulator written in Python'
Expand Down Expand Up @@ -53,7 +52,7 @@
}

def client_options():
parser = argparse.ArgumentParser(description = clt_description, epilog = 'version: ' + clt_version)
parser = KmsParser(description = clt_description, epilog = 'version: ' + clt_version)
parser.add_argument("ip", nargs = "?", action = "store", default = clt_options['ip']['def'], help = clt_options['ip']['help'], type = str)
parser.add_argument("port", nargs = "?", action = "store", default = clt_options['port']['def'], help = clt_options['port']['help'], type = int)
parser.add_argument("-m", "--mode", dest = clt_options['mode']['des'], default = clt_options['mode']['def'], choices = clt_options['mode']['choi'],
Expand All @@ -62,15 +61,17 @@ 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", nargs = "+", dest = clt_options['lfile']['des'], default = clt_options['lfile']['def'],
parser.add_argument("-F", "--logfile", nargs = "+", action = "store", 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)


try:
clt_config.update(vars(parser.parse_args()))
# Check logfile.
clt_config['logfile'] = check_logfile(clt_config['logfile'], clt_options['lfile']['def'])
except KmsException as e:
pretty_printer(put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e), to_exit = True)

def client_check():
# Setup hidden or not messages.
Expand All @@ -83,14 +84,13 @@ def client_check():
try:
uuid.UUID(clt_config['cmid'])
except ValueError:
loggerclt.error("Bad CMID. Exiting...")
sys.exit()

pretty_printer(log_obj = loggerclt.error, to_exit = True,
put_text = "{reverse}{red}{bold}Bad CMID. Exiting...{end}")
# Check machineName.
if clt_config['machineName'] is not None:
if len(clt_config['machineName']) < 2 or len(clt_config['machineName']) > 63:
loggerclt.error("machineName must be between 2 and 63 characters in length.")
sys.exit()
pretty_printer(log_obj = loggerclt.error, to_exit = True,
put_text = "{reverse}{red}{bold}machineName must be between 2 and 63 characters in length. Exiting...{end}")

clt_config['call_id'] = 1

Expand Down Expand Up @@ -125,31 +125,46 @@ def client_create():
loggerclt.info("Connection successful !")
binder = pykms_RpcBind.handler(None, clt_config)
RPC_Bind = enco(str(binder.generateRequest()), 'latin-1')
loggerclt.info("Sending RPC bind request...")
pretty_printer(None, num_text = [-1, 1])
s.send(RPC_Bind)

try:
loggerclt.info("Sending RPC bind request...")
s.send(RPC_Bind)
pretty_printer(num_text = [-1, 1])
except socket.error as e:
pretty_printer(log_obj = loggerclt.error, to_exit = True,
put_text = "{reverse}{red}{bold}While sending: %s{end}" %str(e))
try:
bindResponse = s.recv(1024)
if bindResponse == '' or not bindResponse:
pretty_printer(loggerclt.warning, get_text = True, log_text = True, to_exit = True,
put_text = "{yellow}{bold}No data received.{end}")
pretty_printer(None, num_text = [-4, 7])
pretty_printer(log_obj = loggerclt.warning, to_exit = True,
put_text = "{reverse}{yellow}{bold}No data received.{end}")
pretty_printer(num_text = [-4, 7])
except socket.error as e:
pretty_printer(loggerclt.error, get_text = True, log_text = True, to_exit = True,
put_text = "{red}{bold}While receiving: %s{end}" %str(e))
pretty_printer(log_obj = loggerclt.error, to_exit = True,
put_text = "{reverse}{red}{bold}While receiving: %s{end}" %str(e))

packetType = MSRPCHeader(bindResponse)['type']
if packetType == rpcBase.packetType['bindAck']:
loggerclt.info("RPC bind acknowledged.")
pretty_printer(None, num_text = 8)
pretty_printer(num_text = 8)
kmsRequest = createKmsRequest()
requester = pykms_RpcRequest.handler(kmsRequest, clt_config)
s.send(enco(str(requester.generateRequest()), 'latin-1'))
pretty_printer(None, num_text = [-1, 12])
response = s.recv(1024)

try:
loggerclt.info("Sending RPC activation request...")
s.send(enco(str(requester.generateRequest()), 'latin-1'))
pretty_printer(num_text = [-1, 12])
except socket.error as e:
pretty_printer(log_obj = loggerclt.error, to_exit = True,
put_text = "{reverse}{red}{bold}While sending: %s{end}" %str(e))
try:
response = s.recv(1024)
pretty_printer(num_text = [-4, 20])
except socket.error as e:
pretty_printer(log_obj = loggerclt.error, to_exit = True,
put_text = "{reverse}{red}{bold}While receiving: %s{end}" %str(e))

loggerclt.debug("Response: \n%s\n" % justify(deco(binascii.b2a_hex(response), 'latin-1')))
pretty_printer(None, num_text = [-4, 20])
parsed = MSRPCRespHeader(response)
kmsData = readKmsResponse(parsed['pduData'], kmsRequest, clt_config)
kmsResp = kmsData['response']
Expand All @@ -169,14 +184,14 @@ def client_create():
'status' : "Activated",
'product' : clt_config["mode"]})

pretty_printer(None, num_text = 21)
pretty_printer(num_text = 21)

elif packetType == rpcBase.packetType['bindNak']:
loggerclt.info(justify(MSRPCBindNak(bindResponse).dump(print_to_stdout = False)))
sys.exit(0)
else:
pretty_printer(loggerclt.warning, get_text = True, log_text = True, to_exit = True,
put_text = "{magenta}{bold}Something went wrong.{end}")
pretty_printer(log_obj = loggerclt.warning, to_exit = True,
put_text = "{reverse}{magenta}{bold}Something went wrong.{end}")

def clt_main(with_gui = False):
if not with_gui:
Expand Down Expand Up @@ -209,7 +224,7 @@ def createKmsRequestBase():
requestDict['mnPad'] = '\0'.encode('utf-16le') * (63 - len(requestDict['machineName'].decode('utf-16le')))

# Debug Stuff
pretty_printer(None, num_text = 9)
pretty_printer(num_text = 9)
requestDict = byterize(requestDict)
loggerclt.debug("Request Base Dictionary: \n%s\n" % justify(requestDict.dump(print_to_stdout = False)))

Expand Down
Loading

0 comments on commit 7c14eb1

Please sign in to comment.