Ubuntu / WebSSH /Tools.py
BinaryONe
Changes
d89b239
import os
import ssl
import sys
import logging
import os.path
import importlib.util
# Automatically detect the path to the webssh package
spec = importlib.util.find_spec('webssh')
base_dir = os.path.dirname(spec.origin) if spec else None
#font_dirs = ['static', 'css', 'fonts']
max_body_size = 1 * 1024 * 1024
#base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#font_dirs = ['webssh', 'static', 'css', 'fonts']
def print_version(flag):
if flag:
print(__version__)
sys.exit(0)
def is_valid_encoding(encoding):
try:
u'test'.encode(encoding)
except LookupError:
return False
except ValueError:
return False
return True
def check_encoding_setting(encoding):
if encoding and not is_valid_encoding(encoding):
raise ValueError('Unknown character encoding {!r}.'.format(encoding))
def get_server_settings(options):
settings = dict(
xheaders=options.xheaders,
max_body_size=max_body_size,
trusted_downstream=get_trusted_downstream(options.tdstream)
)
return settings
def get_trusted_downstream(tdstream):
result = set()
for ip in tdstream.split(','):
ip = ip.strip()
if ip:
to_ip_address(ip)
result.add(ip)
return result
def get_ssl_context(options):
if not options.certfile and not options.keyfile:
return None
elif not options.certfile:
raise ValueError('certfile is not provided')
elif not options.keyfile:
raise ValueError('keyfile is not provided')
elif not os.path.isfile(options.certfile):
raise ValueError('File {!r} does not exist'.format(options.certfile))
elif not os.path.isfile(options.keyfile):
raise ValueError('File {!r} does not exist'.format(options.keyfile))
else:
ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_ctx.load_cert_chain(options.certfile, options.keyfile)
return ssl_ctx