def run(self,config_yaml): is_config_loaded = self.load_config_from_yaml(config_yaml) if not is_config_loaded: base.fatal_fail("Unable to load config file, can't parse the YAML!") self.logger.warning("! Please check the proxiedPortList and proxiedUrl in the config.yaml !") server = websockets.serve(self.proxy_dispatcher, self.host, self.port, ping_interval=20, ping_timeout=20, close_timeout=10) self.logger.info("Initializing PROXY SERVER") asyncio.get_event_loop().run_until_complete(server) asyncio.get_event_loop().run_forever()
def load_server_config_from_yaml(self, config_yaml): server_config = config_yaml['configuration']['serverConfiguration'] self.host = server_config['listenHost'] self.port = int(server_config['port']) self.serverType = server_config['type'] self.requests_per_connection = int( server_config['requestsPerConnection']) if not self.has_valid_server_type(): self.logger.error("Server type value [" + self.serverType + "] in config is invalid. Can't start server") base.fatal_fail(None) if self.is_forced_url_server( ) or self.is_forced_url_no_password_server(): self.proxied_url = server_config['proxiedUrl'] self.proxied_port_list = server_config['proxiedPortList'] if self.proxied_url is None or self.proxied_url == "": error_message = "Proxied url in config missing--required when running in FORCED_URL mode." self.logger.error(error_message) base.fatal_fail(None) if self.proxied_port_list is None: error_message = "Proxied url in config missing--required when running in FORCED_URL mode." self.logger.error(error_message) base.fatal_fail(None) for i in range(len(self.proxied_port_list)): is_port_used[self.proxied_port_list[i]] = False
from websocket_proxy.util import base try: import websockets except ImportError: base.fatal_fail( "'websockets' library required (pip install websockets). Exiting.") from websocket_proxy.util.jsonutils import get_json_status_response import asyncio import json #dict:端口是否被占用,{"port":bool} is_port_used = {} class WebSocketProxpy: logger = None host = "" port = 0 serverType = "OPEN_URL" proxied_url = "" password = "" send_suffix = "" send_prefix = "" proxied_port_list = [] requests_per_connection = 10000 def __init__(self, logger): self.logger = logger
# from websocket_proxpy.proxy import WebSocketProxpy from proxy_await_async_version import WebSocketProxpy from websocket_proxy.util import loggers from websocket_proxy.util import base try: import yaml except ImportError: base.fatal_fail("'yaml' library required (pip install yaml). Exiting.") CONFIG_FILE_NAME = "config.yaml" config = yaml.load(open(CONFIG_FILE_NAME)) WebSocketProxpy(loggers.ConsoleDebugLogger()).run(config)