mirror of
https://github.com/Py-KMS-Organization/py-kms.git
synced 2025-06-16 01:14:38 +02:00
Implements #88, documents adjust, uniformed default values
This commit is contained in:
parent
27aa94a6e4
commit
33cedce48c
14 changed files with 144 additions and 142 deletions
|
@ -107,8 +107,8 @@ class kmsBase:
|
|||
return 4 + (((~bodyLength & 3) + 1) & 3)
|
||||
|
||||
def serverLogic(self, kmsRequest):
|
||||
if self.srv_config['sqlite'] and self.srv_config['dbSupport']:
|
||||
self.dbName = sql_initialize()
|
||||
if self.srv_config['sqlite']:
|
||||
sql_initialize(self.srv_config['sqlite'])
|
||||
|
||||
pretty_printer(num_text = 15, where = "srv")
|
||||
kmsRequest = byterize(kmsRequest)
|
||||
|
@ -210,8 +210,8 @@ could be detected as not genuine !{end}" %currentClientCount)
|
|||
'status' : infoDict["licenseStatus"],
|
||||
'product' : infoDict["skuId"]})
|
||||
|
||||
if self.srv_config['sqlite'] and self.srv_config['dbSupport']:
|
||||
sql_update(self.dbName, infoDict)
|
||||
if self.srv_config['sqlite']:
|
||||
sql_update(self.srv_config['sqlite'], infoDict)
|
||||
|
||||
return self.createKmsResponse(kmsRequest, currentClientCount)
|
||||
|
||||
|
@ -233,8 +233,8 @@ could be detected as not genuine !{end}" %currentClientCount)
|
|||
response['vLActivationInterval'] = self.srv_config["activation"]
|
||||
response['vLRenewalInterval'] = self.srv_config["renewal"]
|
||||
|
||||
if self.srv_config['sqlite'] and self.srv_config['dbSupport']:
|
||||
response = sql_update_epid(self.dbName, kmsRequest, response)
|
||||
if self.srv_config['sqlite']:
|
||||
response = sql_update_epid(self.srv_config['sqlite'], kmsRequest, response)
|
||||
|
||||
loggersrv.info("Server ePID: %s" % response["kmsEpid"].decode('utf-16le'))
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ clt_options = {
|
|||
'def' : None, 'des' : "cmid"},
|
||||
'name' : {'help' : 'Use this flag to manually specify an ASCII machine name to use. If no machine name is specified a random one \
|
||||
will be generated.', 'def' : None, 'des' : "machine"},
|
||||
'asyncmsg' : {'help' : 'Prints pretty / logging messages asynchronously. Desactivated by default.',
|
||||
'asyncmsg' : {'help' : 'Prints pretty / logging messages asynchronously. Deactivated by default.',
|
||||
'def' : False, 'des' : "asyncmsg"},
|
||||
'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"]},
|
||||
|
@ -67,7 +67,7 @@ will be generated.', 'def' : None, 'des' : "machine"},
|
|||
Type \"STDOUT\" to view log info on stdout. Type \"FILESTDOUT\" to combine previous actions. \
|
||||
Use \"STDOUTOFF\" to disable stdout messages. Use \"FILEOFF\" if you not want to create logfile.',
|
||||
'def' : os.path.join('.', '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"},
|
||||
'lsize' : {'help' : 'Use this flag to set a maximum size (in MB) to the output log file. Deactivated by default.', 'def' : 0, 'des': "logsize"},
|
||||
}
|
||||
|
||||
def client_options():
|
||||
|
|
|
@ -188,13 +188,14 @@ for server OSes and Office >=5', 'def' : None, 'des' : "clientcount"},
|
|||
'def' : 120, 'des': "activation"},
|
||||
'renewal' : {'help' : 'Use this option to specify the renewal interval (in minutes). Default is \"10080\" minutes (7 days).',
|
||||
'def' : 1440 * 7, 'des' : "renewal"},
|
||||
'sql' : {'help' : 'Use this option to store request information from unique clients in an SQLite database. Desactivated by default.',
|
||||
'sql' : {'help' : 'Use this option to store request information from unique clients in an SQLite database. Deactivated by default. \
|
||||
If enabled the default .db file is \"pykms_database.db\". You can also provide a specific location.',
|
||||
'def' : False, 'des' : "sqlite"},
|
||||
'hwid' : {'help' : 'Use this option to specify a HWID. The HWID must be an 16-character string of hex characters. \
|
||||
The default is \"364F463A8863D35F\" or type \"RANDOM\" to auto generate the HWID.', 'def' : "364F463A8863D35F", 'des' : "hwid"},
|
||||
'time0' : {'help' : 'Maximum inactivity time (in seconds) after which the connection with the client is closed. If \"None\" (default) serve forever.',
|
||||
'def' : None, 'des' : "timeoutidle"},
|
||||
'asyncmsg' : {'help' : 'Prints pretty / logging messages asynchronously. Desactivated by default.',
|
||||
'asyncmsg' : {'help' : 'Prints pretty / logging messages asynchronously. Deactivated by default.',
|
||||
'def' : False, 'des' : "asyncmsg"},
|
||||
'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"]},
|
||||
|
@ -202,7 +203,7 @@ The default is \"364F463A8863D35F\" or type \"RANDOM\" to auto generate the HWID
|
|||
Type \"STDOUT\" to view log info on stdout. Type \"FILESTDOUT\" to combine previous actions. \
|
||||
Use \"STDOUTOFF\" to disable stdout messages. Use \"FILEOFF\" if you not want to create logfile.',
|
||||
'def' : os.path.join('.', '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. Deactivated by default.', 'def' : 0, 'des': "logsize"},
|
||||
}
|
||||
|
||||
def server_options():
|
||||
|
@ -219,8 +220,8 @@ def server_options():
|
|||
default = srv_options['activation']['def'], help = srv_options['activation']['help'], type = int)
|
||||
server_parser.add_argument("-r", "--renewal-interval", action = "store", dest = srv_options['renewal']['des'],
|
||||
default = srv_options['renewal']['def'], help = srv_options['renewal']['help'], type = int)
|
||||
server_parser.add_argument("-s", "--sqlite", action = "store_true", dest = srv_options['sql']['des'],
|
||||
default = srv_options['sql']['def'], help = srv_options['sql']['help'])
|
||||
server_parser.add_argument("-s", "--sqlite", nargs = "?", dest = srv_options['sql']['des'], const = True,
|
||||
default = srv_options['sql']['def'], help = srv_options['sql']['help'], type = str)
|
||||
server_parser.add_argument("-w", "--hwid", action = "store", dest = srv_options['hwid']['des'], default = srv_options['hwid']['def'],
|
||||
help = srv_options['hwid']['help'], type = str)
|
||||
server_parser.add_argument("-t0", "--timeout-idle", action = "store", dest = srv_options['time0']['des'], default = srv_options['time0']['def'],
|
||||
|
@ -298,7 +299,6 @@ def server_options():
|
|||
except KmsParserException as e:
|
||||
pretty_printer(put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e), to_exit = True)
|
||||
|
||||
|
||||
class Etrigan_Check(Etrigan_check):
|
||||
def emit_opt_err(self, msg):
|
||||
pretty_printer(put_text = "{reverse}{red}{bold}%s{end}" %msg, to_exit = True)
|
||||
|
@ -316,7 +316,7 @@ class Etrigan(Etrigan):
|
|||
|
||||
def server_daemon():
|
||||
if 'etrigan' in srv_config.values():
|
||||
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'pykms_config.pickle')
|
||||
path = os.path.join('.', 'pykms_config.pickle')
|
||||
|
||||
if srv_config['operation'] in ['stop', 'restart', 'status'] and len(sys.argv[1:]) > 2:
|
||||
pretty_printer(put_text = "{reverse}{red}{bold}too much arguments with etrigan '%s'. Exiting...{end}" %srv_config['operation'],
|
||||
|
@ -394,15 +394,18 @@ def server_check():
|
|||
srv_config['lcid'] = check_lcid(srv_config['lcid'], loggersrv.warning)
|
||||
|
||||
# Check sqlite.
|
||||
try:
|
||||
import sqlite3
|
||||
except:
|
||||
pretty_printer(log_obj = loggersrv.warning,
|
||||
put_text = "{reverse}{yellow}{bold}Module 'sqlite3' is not installed, database support disabled.{end}")
|
||||
srv_config['dbSupport'] = False
|
||||
else:
|
||||
srv_config['dbSupport'] = True
|
||||
|
||||
if srv_config['sqlite']:
|
||||
if (isinstance(srv_config['sqlite'], str)) and (not srv_config['sqlite'].lower().endswith('.db')):
|
||||
pretty_printer(log_obj = loggersrv.error, to_exit = True,
|
||||
put_text = "{reverse}{red}{bold}Not a sqlite file (.db).{end}")
|
||||
try:
|
||||
import sqlite3
|
||||
if isinstance(srv_config['sqlite'], bool):
|
||||
srv_config['sqlite'] = os.path.join('.', 'pykms_database.db')
|
||||
except ImportError:
|
||||
pretty_printer(log_obj = loggersrv.warning,
|
||||
put_text = "{reverse}{yellow}{bold}Module 'sqlite3' is not installed, database support disabled.{end}")
|
||||
srv_config['sqlite'] = False
|
||||
|
||||
# Check other specific server options.
|
||||
list_dest = ['clientcount', 'timeoutidle']
|
||||
|
|
|
@ -5,9 +5,9 @@ import logging
|
|||
|
||||
# sqlite3 is optional.
|
||||
try:
|
||||
import sqlite3
|
||||
import sqlite3
|
||||
except ImportError:
|
||||
pass
|
||||
pass
|
||||
|
||||
from pykms_Format import pretty_printer
|
||||
|
||||
|
@ -15,87 +15,84 @@ from pykms_Format import pretty_printer
|
|||
|
||||
loggersrv = logging.getLogger('logsrv')
|
||||
|
||||
def sql_initialize():
|
||||
dbName = 'clients.db'
|
||||
if not os.path.isfile(dbName):
|
||||
# Initialize the database.
|
||||
con = None
|
||||
try:
|
||||
con = sqlite3.connect(dbName)
|
||||
cur = con.cursor()
|
||||
cur.execute("CREATE TABLE clients(clientMachineId TEXT, machineName TEXT, applicationId TEXT, skuId TEXT, \
|
||||
def sql_initialize(dbName):
|
||||
if not os.path.isfile(dbName):
|
||||
# Initialize the database.
|
||||
con = None
|
||||
try:
|
||||
con = sqlite3.connect(dbName)
|
||||
cur = con.cursor()
|
||||
cur.execute("CREATE TABLE clients(clientMachineId TEXT, machineName TEXT, applicationId TEXT, skuId TEXT, \
|
||||
licenseStatus TEXT, lastRequestTime INTEGER, kmsEpid TEXT, requestCount INTEGER)")
|
||||
|
||||
except sqlite3.Error as e:
|
||||
except sqlite3.Error as e:
|
||||
pretty_printer(log_obj = loggersrv.error, to_exit = True,
|
||||
put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e))
|
||||
finally:
|
||||
if con:
|
||||
con.commit()
|
||||
con.close()
|
||||
return dbName
|
||||
|
||||
finally:
|
||||
if con:
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
def sql_update(dbName, infoDict):
|
||||
con = None
|
||||
try:
|
||||
con = sqlite3.connect(dbName)
|
||||
cur = con.cursor()
|
||||
cur.execute("SELECT * FROM clients WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
try:
|
||||
data = cur.fetchone()
|
||||
if not data:
|
||||
# Insert row.
|
||||
cur.execute("INSERT INTO clients (clientMachineId, machineName, applicationId, \
|
||||
con = None
|
||||
try:
|
||||
con = sqlite3.connect(dbName)
|
||||
cur = con.cursor()
|
||||
cur.execute("SELECT * FROM clients WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
try:
|
||||
data = cur.fetchone()
|
||||
if not data:
|
||||
# Insert row.
|
||||
cur.execute("INSERT INTO clients (clientMachineId, machineName, applicationId, \
|
||||
skuId, licenseStatus, lastRequestTime, requestCount) VALUES (:clientMachineId, :machineName, :appId, :skuId, :licenseStatus, :requestTime, 1);", infoDict)
|
||||
else:
|
||||
# Update data.
|
||||
if data[1] != infoDict["machineName"]:
|
||||
cur.execute("UPDATE clients SET machineName=:machineName WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[2] != infoDict["appId"]:
|
||||
cur.execute("UPDATE clients SET applicationId=:appId WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[3] != infoDict["skuId"]:
|
||||
cur.execute("UPDATE clients SET skuId=:skuId WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[4] != infoDict["licenseStatus"]:
|
||||
cur.execute("UPDATE clients SET licenseStatus=:licenseStatus WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[5] != infoDict["requestTime"]:
|
||||
cur.execute("UPDATE clients SET lastRequestTime=:requestTime WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
# Increment requestCount
|
||||
cur.execute("UPDATE clients SET requestCount=requestCount+1 WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
else:
|
||||
# Update data.
|
||||
if data[1] != infoDict["machineName"]:
|
||||
cur.execute("UPDATE clients SET machineName=:machineName WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[2] != infoDict["appId"]:
|
||||
cur.execute("UPDATE clients SET applicationId=:appId WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[3] != infoDict["skuId"]:
|
||||
cur.execute("UPDATE clients SET skuId=:skuId WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[4] != infoDict["licenseStatus"]:
|
||||
cur.execute("UPDATE clients SET licenseStatus=:licenseStatus WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
if data[5] != infoDict["requestTime"]:
|
||||
cur.execute("UPDATE clients SET lastRequestTime=:requestTime WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
# Increment requestCount
|
||||
cur.execute("UPDATE clients SET requestCount=requestCount+1 WHERE clientMachineId=:clientMachineId;", infoDict)
|
||||
|
||||
except sqlite3.Error as e:
|
||||
except sqlite3.Error as e:
|
||||
pretty_printer(log_obj = loggersrv.error, to_exit = True,
|
||||
put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e))
|
||||
except sqlite3.Error as e:
|
||||
except sqlite3.Error as e:
|
||||
pretty_printer(log_obj = loggersrv.error, to_exit = True,
|
||||
put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e))
|
||||
finally:
|
||||
if con:
|
||||
con.commit()
|
||||
con.close()
|
||||
finally:
|
||||
if con:
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
def sql_update_epid(dbName, kmsRequest, response):
|
||||
cmid = str(kmsRequest['clientMachineId'].get())
|
||||
con = None
|
||||
try:
|
||||
con = sqlite3.connect(dbName)
|
||||
cur = con.cursor()
|
||||
cur.execute("SELECT * FROM clients WHERE clientMachineId=?;", [cmid])
|
||||
try:
|
||||
data = cur.fetchone()
|
||||
if data[6]:
|
||||
response["kmsEpid"] = data[6].encode('utf-16le')
|
||||
else:
|
||||
cur.execute("UPDATE clients SET kmsEpid=? WHERE clientMachineId=?;", (str(response["kmsEpid"].decode('utf-16le')),
|
||||
cmid))
|
||||
except sqlite3.Error as e:
|
||||
cmid = str(kmsRequest['clientMachineId'].get())
|
||||
con = None
|
||||
try:
|
||||
con = sqlite3.connect(dbName)
|
||||
cur = con.cursor()
|
||||
cur.execute("SELECT * FROM clients WHERE clientMachineId=?;", [cmid])
|
||||
try:
|
||||
data = cur.fetchone()
|
||||
if data[6]:
|
||||
response["kmsEpid"] = data[6].encode('utf-16le')
|
||||
else:
|
||||
cur.execute("UPDATE clients SET kmsEpid=? WHERE clientMachineId=?;", (str(response["kmsEpid"].decode('utf-16le')),
|
||||
cmid))
|
||||
except sqlite3.Error as e:
|
||||
pretty_printer(log_obj = loggersrv.error, to_exit = True,
|
||||
put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e))
|
||||
except sqlite3.Error as e:
|
||||
except sqlite3.Error as e:
|
||||
pretty_printer(log_obj = loggersrv.error, to_exit = True,
|
||||
put_text = "{reverse}{red}{bold}%s. Exiting...{end}" %str(e))
|
||||
finally:
|
||||
if con:
|
||||
con.commit()
|
||||
con.close()
|
||||
return response
|
||||
finally:
|
||||
if con:
|
||||
con.commit()
|
||||
con.close()
|
||||
return response
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue