Move some config variables to handler.py

This commit is contained in:
Sheng
2018-10-18 15:07:45 +08:00
parent 383f61d4cc
commit 5d6f92e529
5 changed files with 38 additions and 30 deletions
+6 -13
View File
@@ -10,8 +10,6 @@ import paramiko
import tornado.web
from tornado.ioloop import IOLoop
from tornado.options import options
from webssh import settings
from webssh.utils import (
is_valid_ip_address, is_valid_port, is_valid_hostname,
to_bytes, to_str, to_int, to_ip_address, UnicodeType
@@ -33,6 +31,10 @@ DELAY = 3
KEY_MAX_SIZE = 16384
DEFAULT_PORT = 22
swallow_http_errors = True
is_open_to_public = None
forbid_public_http = None
class InvalidValueError(Exception):
pass
@@ -40,20 +42,11 @@ class InvalidValueError(Exception):
class MixinHandler(object):
is_open_to_public = None
forbid_public_http = None
custom_headers = {
'Server': 'TornadoServer'
}
def initialize(self):
if self.is_open_to_public is None:
MixinHandler.is_open_to_public = settings.is_open_to_public
if self.forbid_public_http is None:
MixinHandler.forbid_public_http = options.fbidhttp
if self.is_forbidden():
result = '{} 403 Forbidden\r\n\r\n'.format(self.request.version)
self.request.connection.stream.write(to_bytes(result))
@@ -76,7 +69,7 @@ class MixinHandler(object):
)
return True
if self.is_open_to_public and self.forbid_public_http:
if is_open_to_public and forbid_public_http:
if context._orig_protocol == 'http':
ipaddr = to_ip_address(ip)
if not ipaddr.is_private:
@@ -138,7 +131,7 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
super(IndexHandler, self).initialize()
def write_error(self, status_code, **kwargs):
if self.request.method != 'POST' or not settings.swallow_http_errors:
if self.request.method != 'POST' or not swallow_http_errors:
super(IndexHandler, self).write_error(status_code, **kwargs)
else:
exc_info = kwargs.get('exc_info')
+5 -5
View File
@@ -4,6 +4,7 @@ import ssl
import sys
from tornado.options import define
from webssh import handler
from webssh.policy import (
load_host_keys, get_policy_class, check_policy_setting
)
@@ -39,9 +40,7 @@ define('version', type=bool, help='Show version information',
base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
max_body_size = 1 * 1024 * 1024
swallow_http_errors = True
xheaders = True
is_open_to_public = False
def get_app_settings(options):
@@ -120,9 +119,10 @@ def get_trusted_downstream(options):
def detect_is_open_to_public(options):
global is_open_to_public
handler.forbid_public_http = options.fbidhttp
if on_public_network_interfaces(get_ips_by_name(options.address)):
is_open_to_public = True
handler.is_open_to_public = True
logging.info('Forbid public http: {}'.format(options.fbidhttp))
else:
is_open_to_public = False
handler.is_open_to_public = False