def __create_service(self, name, configs): listen_ip = configs.get("listen_ip", "0.0.0.0") if not cfg_check.is_ipv6(listen_ip) and not cfg_check.is_ipv4(listen_ip): sys.stderr.write("wrong listen_ip configure from name %s\r\n" % name) return False is_ipv6 = cfg_check.is_ipv6(listen_ip) if not cfg_check.is_port(configs.get("port", None)): sys.stderr.write("wrong listen port configure from name %s\r\n" % name) return False port = int(configs["port"]) remote_addr = configs.get("remote_address", "127.0.0.1") if not cfg_check.is_ipv6(remote_addr) and not cfg_check.is_ipv4(remote_addr): sys.stderr.write("wrong remote_address configure from name %s\r\n" % name) return False remote_is_ipv6 = cfg_check.is_ipv6(remote_addr) if not cfg_check.is_port(configs.get("remote_port", None)): sys.stderr.write("wrong remote_port configure from name %s\r\n" % name) return False remote_port = int(configs["remote_port"]) if not cfg_check.is_number(configs.get("timeout", None)): sys.stderr.write("wrong timeout configure from name %s\r\n" % name) return False timeout = int(configs["timeout"]) if timeout < 1: sys.stderr.write("wrong timeout configure from name %s\r\n" % name) return False if "auth_id" not in configs: sys.stderr.write("not found auth_id configure from name %s\r\n" % name) return False remote_info = { "address": remote_addr, "port": remote_port, "is_ipv6": remote_is_ipv6, "timeout": timeout } auth_id = configs["auth_id"] if auth_id in self.__binds: sys.stderr.write("the auth_id %s exists\r\n" % auth_id) return False fd = self.create_handler(-1, wan_raw.listener, (listen_ip, port,), configs["auth_id"], remote_info, is_ipv6=is_ipv6) if fd < 0: sys.stderr.write("create listen %s failed\r\n" % name) return False self.__binds[auth_id] = fd return True
def check(self, o): keys = ( "port", "protocol", "is_ipv6", "address", ) if not isinstance(o, dict): return False for k in keys: if k not in o: return False port = o["port"] protocol = o["protocol"] is_ipv6 = o["is_ipv6"] address = o["address"] if not cfg_check.is_port(port): return False if protocol not in ( "tcp", "udp", ): return False if is_ipv6 and not utils.is_ipv6_address(address): return False if not is_ipv6 and not utils.is_ipv4_address(address): return False return True
def main(): help_doc = """ debug | start | stop debug | start --port=port debug: windows support it start only unix-like support stop only unix-like support """ if len(sys.argv) < 2: print(help_doc) return action = sys.argv[1] if action not in ("debug", "stop", "start"): print(help_doc) return if action == "stop": stop() return try: opts, args = getopt.getopt(sys.argv[2:], "", ["port="]) except getopt.GetoptError: print(help_doc) return port = None for k, v in opts: if k == "--port": port = v if None == port: print(help_doc) return if not cfg_check.is_port(port): sys.stderr.write("wrong port number\r\n") return debug = True if action == "start": debug = False pid = os.fork() if pid != 0: sys.exit(0) os.setsid() os.umask(0) pid = os.fork() if pid != 0: sys.exit(0) proc.write_pid(PID_PATH) cls = power_ctl(int(port), debug=debug) try: cls.wait() except KeyboardInterrupt: cls.release()
def main(): help_doc = """ start | stop | debug start | debug --wol_listen_port=port --wol_key=key --wol_bind_ip=ip """ if len(sys.argv) < 2: print(help_doc) return if sys.argv[1] not in ("start", "stop", "debug",): print(help_doc) return d = sys.argv[1] if d == "stop": pid = proc.get_pid(PID_PATH) if pid > 0: os.kill(pid, signal.SIGINT) return try: opts, args = getopt.getopt(sys.argv[2:], "", ["wol_listen_port=", "wol_key=", "wol_bind_ip="]) except getopt.GetoptError: print(help_doc) return except IndexError: print(help_doc) return wol_port = 5888 wol_key = None wol_bind_ip = None for k, v in opts: if k == "--wol_listen_port": if not cfg_check.is_port(v): sys.stderr.write("wrong port number\r\n") return wol_port = int(v) if k == "--wol_key": wol_key = v if k == "--wol_bind_ip": wol_bind_ip = v '''''' if not wol_key: sys.stderr.write("please set wol key\r\n") return if not wol_bind_ip: sys.stderr.write("please set wol bind ip") return if d == "debug": debug = True else: debug = False start(debug, wol_key, wol_port, wol_bind_ip)
def __check_rule(self, rule: dict): """检查每一条规则 :param rule: :return: """ keys = ( "is_ipv6", "dest_addr", "rewrite_dest_addr", "dest_port", "rewrite_dest_port", ) for k in keys: if k not in rule: return False is_ipv6 = rule["is_ipv6"] dest_addr = rule["dest_addr"] rewrite_dest = rule["rewrite_dest_addr"] dest_port = rule["dest_port"] rewrite_dest_port = rule["rewrite_dest_port"] protocol = rule["protocol"] if protocol not in ( "tcp", "udp", "udplite", "sctp", ): return False if is_ipv6 and (not utils.is_ipv6_address(dest_addr) or not utils.is_ipv6_address(rewrite_dest)): return False if not is_ipv6 and (not utils.is_ipv4_address(dest_addr) or not utils.is_ipv4_address(rewrite_dest)): return False if not cfg_check.is_port(dest_port): return False if not cfg_check.is_port(rewrite_dest_port): return False return True
def init_func(self, wol_key, wol_port=5888, wol_bind_ip="0.0.0.0", debug=False): self.__wol_fd = -1 self.__debug = debug self.__sessions = {} self.__configs = {} self.__conns = {} self.__time = time.time() self.__debug = debug if not cfg_check.is_port(wol_port): sys.stderr.write("wrong wol port number %s\r\n") return self.create_poll() self.create_wol(wol_key, wol_port, wol_bind_ip) self.create_connections()
def __create_conn(self, name, configs): if "host" not in configs: sys.stderr.write("not found host from configure %s\r\n" % name) return False if "auth_id" not in configs: sys.stderr.write("not found auth_id from configure %s\r\n" % name) return False if "URI" not in configs: sys.stderr.write("not found URI from configure %s\r\n" % name) return False if not cfg_check.is_number(configs.get("force_ipv6", "0")): sys.stderr.write("wrong force_ipv6 value from configure %s\r\n" % name) return False force_ipv6 = bool(int(configs.get("force_ipv6", "0"))) host = configs["host"] is_ipv6 = False if not cfg_check.is_ipv4(host) and not cfg_check.is_ipv6(host): is_ipv6 = force_ipv6 if cfg_check.is_ipv6(host): is_ipv6 = True if not cfg_check.is_port(configs.get("port", "443")): sys.stderr.write("wrong port value from configure %s\r\n" % name) return False port = int(configs.get("port", "443")) auth_id = configs["auth_id"] uri = configs["URI"] if auth_id in self.__conns: sys.stderr.write("auth id %s exists\r\n" % auth_id) return False fd = self.create_handler(-1, lan_fwd.client, (host, port,), uri, auth_id, is_ipv6=is_ipv6) if fd < 0: sys.stderr.write("create %s connection is failed\r\n" % name) return False self.__conns[auth_id] = fd return True
def get_check_servers(self): path = "%s/fdslight_etc/power_monitor.json" % BASE_DIR servers = [] if not os.path.isfile(path): sys.stderr.write("cannot found configure file %s\r\n" % path) return None with open(path, "r") as f: s = f.read() f.close() o = json.loads(s) for k, v in o.items(): if not cfg_check.is_port(v): sys.stderr.write("the %s is not valid port number\r\n" % k) return None servers.append(( k, int(v), )) return servers
def main(): help_doc = """ direct send packet to LAN internet send packet to internet when is is internet,the argument you must have: --key=key --host=host --port=port """ if len(sys.argv) < 2: print(help_doc) return _type = sys.argv[1] if _type not in ( "direct", "internet", ): print(help_doc) return if _type == "direct": wake_up_direct() return if len(sys.argv) != 5: print(help_doc) return try: opts, args = getopt.getopt(sys.argv[2:], "", ["host=", "port=", "key="]) except getopt.GetoptError: print(help_doc) return host = None port = None is_ipv6 = False key = "key" for k, v in opts: if k == "--host": host = v if k == "--port": port = v if k == "--key": key = v if cfg_check.is_ipv6(host): is_ipv6 = True if not cfg_check.is_port(port): sys.stderr.write("wrong port number %s\r\n" % port) return if not host or not port: print(help_doc) return port = int(port) cls = wake_up_internet(host, port, key, is_ipv6=is_ipv6) cls.wake() cls.release()
def main(): help_doc = """ debug | start | stop | shutdown debug | start | shutdown --port=port debug: windows support it start only unix-like support --bind_ip=bind_ip stop only unix-like support shutdown will close all hosts """ if len(sys.argv) < 2: print(help_doc) return action = sys.argv[1] if action not in ( "debug", "stop", "start", "shutdown", ): print(help_doc) return if action == "stop": stop() return try: opts, args = getopt.getopt(sys.argv[2:], "", ["port=", "bind_ip="]) except getopt.GetoptError: print(help_doc) return port = None bind_ip = None for k, v in opts: if k == "--port": port = v if k == "--bind_ip": bind_ip = v if None == port or not bind_ip: print(help_doc) return if not cfg_check.is_port(port): sys.stderr.write("wrong port number\r\n") return debug = True if action == "start": debug = False pid = os.fork() if pid != 0: sys.exit(0) os.setsid() os.umask(0) pid = os.fork() if pid != 0: sys.exit(0) proc.write_pid(PID_PATH) port = int(port) if action == "shutdown": print("send shutdown to all hosts") shutdown(bind_ip, port) return cls = power_monitor(port, debug=debug, bind_ip=bind_ip) try: cls.monitor() except KeyboardInterrupt: cls.release()