def run(self) -> None: while not self.is_stop: with MutexLock(self.op_lock) as _: if DEBUG: print("Checking timeout tunnels") for tid in [x for x, tunnel in self.tunnels.items() if tunnel.invalid]: tunnel = self.tunnels.pop(tid).stop() print("Tunnel {} closed caused by timeout: {}".format( tid, json.dumps(tunnel.properties_dict()) )) time.sleep(self.interval)
def _add_permanent_proxy(): try: with open("permanent.json", "r") as fp: with MutexLock(tunnels_op_lock) as _: for item in json.load(fp): if "expire" not in item: item["expire"] = -1 tid = _new_tunnel_id() tunnels[tid] = _create_from_dict(item) tunnels[tid].start() except Exception as ex: log.error("Error while initialize permanent tunnels.", exc_info=ex)
def run(self) -> None: while not self.is_stop: with MutexLock(self.op_lock) as _: log.debug("Checking the tunnels status.") for tid in [ x for x, tunnel in self.tunnels.items() if not tunnel.valid ]: tunnel = self.tunnels.pop(tid).stop() log.info("Tunnel {} closed caused by expired: {}".format( tid, json.dumps(tunnel.json))) time.sleep(self.interval)
def tunnel_heartbeat(request: HttpRequest): """ :param request: :return: """ tid = int(request.POST["id"]) if tid in tunnels: with MutexLock(tunnels_op_lock) as _: tunnels[tid].check() if DEBUG: print("Tunnel {} re-check heartbeat by API.".format(tid)) return {"status": "success", "id": tid} else: raise Exception("ID {} not found".format(tid))
def remove_tunnel(request: HttpRequest): """ :param request: :return: """ tid = int(request.POST["id"]) if tid in tunnels: with MutexLock(tunnels_op_lock) as _: tunnel = tunnels.pop(tid) tunnel.stop() log.info("Tunnel {} removed by API.".format(tid)) return {"status": "success", "id": tid} else: raise Exception("ID {} not found".format(tid))
def create_tunnel(request: HttpRequest): """ Create a tunnel :param request: :return: """ with MutexLock(tunnels_op_lock) as _: tid = _new_tunnel_id() # noinspection PyTypeChecker tunnels[tid] = _create_from_dict(request.POST) tunnels[tid].start() log.info("Tunnel {} created by API.".format(tid)) return {"status": "success", "id": tid, "tunnel": tunnels[tid].json}
def query_tunnel(request: HttpRequest): """ :param request: :return: """ tid = int(request.GET["id"]) if tid in tunnels: with MutexLock(tunnels_op_lock) as _: return { "status": "success", "id": tid, "tunnel": tunnels[tid].json } else: raise Exception("ID {} not found".format(tid))
def _add_permanent_proxy(): try: with open("permanent.json", "r") as fp: with MutexLock(tunnels_op_lock) as _: for item in json.load(fp): tid = _new_tunnel_id() tunnels[tid] = Tunnel(innet_port=int(item["innet"]), expose_port=int(item["expose"]), bridge_port=int(item["bridge"]), comment=str(item["comment"]), expired=-1) tunnels[tid].start() except Exception as _: print("Error while initialize permanent tunnels.") if DEBUG: print(traceback.format_exc())
def create_tunnel(request: HttpRequest): """ Create a tunnel :param request: :return: """ innet_port = request.POST["innet"] if "expose" in request.POST: expose_port = request.POST["expose"] else: expose_port = None if "bridge" in request.POST: bridge_port = request.POST["bridge"] else: bridge_port = None if "comment" in request.POST: comment = request.POST["comment"] else: comment = "" if "expire" in request.POST: expire_time = float(request.POST["expire"]) else: expire_time = 60.0 with MutexLock(tunnels_op_lock) as _: tid = _new_tunnel_id() tunnels[tid] = Tunnel(innet_port=innet_port, bridge_port=bridge_port, expose_port=expose_port, comment=comment, expired=expire_time).start() print("Tunnel {} created by API.".format(tid)) return { "status": "success", "id": tid, "tunnel": tunnels[tid].properties_dict() }