operator-time
quadrismegistus 4 years ago
parent 9030cb760c
commit 8e6b5ceffe

@ -81,14 +81,19 @@ class TheSwitchboard(FlaskView, Logger):
@route('/please/<encr_b64_str>')
def please(self,encr_b64_str=None):
if not encr_b64_str: return OPERATOR_INTERCEPT_MESSAGE
if not isBase64(encr_b64_str): return OPERATOR_INTERCEPT_MESSAGE
# first try to get from string to bytes
self.log('incoming <--',encr_b64_str)
try:
encr_b64_b = encr_b64_str.encode('utf-8')
self.log('encr_b64_b',encr_b64_b)
encr_b = b64decode(encr_b64_b)
self.log('encr_b',encr_b)
return f'successfully understood input: {encr_b}'
except (UnicodeDecodeError,binascii.Error) as e:
return OPERATOR_INTERCEPT_MESSAGE

@ -34,8 +34,19 @@ class Logger(object):
caller = calframe[1][3]
log(f'\n[{mytype}.{caller}()]',*x)
import binascii
def isBase64(sb):
try:
if isinstance(sb, str):
# If there's any unicode here, an exception will be thrown and the function will return false
sb_bytes = bytes(sb, 'ascii')
elif isinstance(sb, bytes):
sb_bytes = sb
else:
raise ValueError("Argument must be string or bytes")
return base64.b64encode(base64.b64decode(sb_bytes)) == sb_bytes
except binascii.Error:
return False

Loading…
Cancel
Save