Optimize hwid and port checking

This commit is contained in:
Matteo ℱan 2019-10-06 22:37:29 +02:00
parent c1fa1445d4
commit 8d105710e6

View File

@ -128,17 +128,26 @@ def server_check():
srv_config['hwid'] = randomhwid[:16] srv_config['hwid'] = randomhwid[:16]
# Sanitize HWID. # Sanitize HWID.
try: hexstr = srv_config['hwid'].strip('0x')
srv_config['hwid'] = binascii.a2b_hex(re.sub(r'[^0-9a-fA-F]', '', srv_config['hwid'].strip('0x'))) hexsub = re.sub(r'[^0-9a-fA-F]', '', hexstr)
if len(binascii.b2a_hex(srv_config['hwid'])) < 16: diff = set(hexstr).symmetric_difference(set(hexsub))
loggersrv.error("HWID \"%s\" is invalid. Hex string is too short." % deco(binascii.b2a_hex(srv_config['hwid']), 'utf-8').upper())
return if len(diff) != 0:
elif len(binascii.b2a_hex(srv_config['hwid'])) > 16: loggersrv.error("HWID \"%s\" is invalid. Non hexadecimal digit %s found." %(hexstr.upper(), diff))
loggersrv.error("HWID \"%s\" is invalid. Hex string is too long." % deco(binascii.b2a_hex(srv_config['hwid']), 'utf-8').upper()) sys.exit(1)
return else:
except TypeError: lh = len(hexsub)
loggersrv.error("HWID \"%s\" is invalid. Odd-length hex string." % deco(binascii.b2a_hex(srv_config['hwid']), 'utf-8').upper()) if lh % 2 != 0:
return loggersrv.error("HWID \"%s\" is invalid. Hex string is odd length." % hexsub.upper())
sys.exit(1)
elif lh < 16:
loggersrv.error("HWID \"%s\" is invalid. Hex string is too short." % hexsub.upper())
sys.exit(1)
elif lh > 16:
loggersrv.error("HWID \"%s\" is invalid. Hex string is too long." % hexsub.upper())
sys.exit(1)
else:
srv_config['hwid'] = binascii.a2b_hex(hexsub)
# Check LCID. # Check LCID.
# http://stackoverflow.com/questions/3425294/how-to-detect-the-os-default-language-in-python # http://stackoverflow.com/questions/3425294/how-to-detect-the-os-default-language-in-python
@ -168,13 +177,12 @@ def server_check():
# Check port. # Check port.
try: try:
p = srv_config['port'] if srv_config['port'] > 65535:
if p > 65535:
loggersrv.error('Please enter a valid port number between 1 - 65535') loggersrv.error('Please enter a valid port number between 1 - 65535')
return sys.exit(1)
except Exception as e: except Exception as e:
loggersrv.error('%s' %e) loggersrv.error('%s' %e)
return sys.exit(1)
def server_create(): def server_create():
socketserver.TCPServer.allow_reuse_address = True socketserver.TCPServer.allow_reuse_address = True