def api_gatelist(params): if not type(params) is dict: params = to_dict(params) # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() auth_code = params.get("auth_code") or APPCtrl().get_accesskey() if auth_code: APIHandler.Manager.TRAccesskey = auth_code else: return failure(id, "params lost auth_code") ret = APIHandler.Manager.gatelist() if ret: return success(id, ret) else: return failure(id, "error")
def api_ready(params: gateItem): if not type(params) is dict: params = params.dict() # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() auth_code = params.get("auth_code") or APPCtrl().get_accesskey() if auth_code: APIHandler.Manager.TRAccesskey = auth_code else: return failure(id, "params lost auth_code") ret, ret_content = APIHandler.Manager.vserial_ready(params.get("gate")) if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_status(): id = str(time.time()) ret, ret_content = APIHandler.Manager.vserial_status() if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_listall(): id = str(time.time()) ret = APIHandler.Manager.list_all() if ret: return success(id, ret) else: return failure(id, "Nothing")
def api_bench(params: benchItem): if not type(params) is dict: params = params.dict() # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() host = params.get("host") port = params.get("port") size = params.get("size") direct = params.get("direct") if not is_ipv4(host): return failure(id, "host must be ipv4") ret = APIHandler.Manager.speed_bench(host, port, size, direct) if ret: return success(id, ret) else: return failure(id, 'error')
def api_load(params): if not type(params) is dict: params = to_dict(params) id = params.get("id") or uuid.uuid1() npshost = None npsuser = None gate = None auth_code = None config = ConfigParser() if os.access(os.getcwd() + '\\config.ini', os.F_OK): config.read('config.ini') if config.has_option('thingscloud', 'npshost'): npshost = config.get('thingscloud', 'npshost') if config.has_option('thingscloud', 'npsuser'): npsuser = config.get('thingscloud', 'npsuser') if config.has_option('thingscloud', 'gate'): gate = config.get('thingscloud', 'gate') if config.has_option('thingscloud', 'accesskey'): if len(config.get('thingscloud', 'accesskey')) == 48: auth_code = base64.b64decode( config.get('thingscloud', 'accesskey').encode()).decode() else: return None ret = { "npshost": npshost, "npsuser": npsuser, "gate": gate, "auth_code": auth_code } if ret: return success(id, ret) else: return failure(id, "error")
def api_start(params: vnetItem): if not type(params) is dict: params = params.dict() # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() host = params.get("host") user = params.get("user") gate = params.get("gate") ret, ret_content = None, None if host and user and gate: if not urlCheck(host).get_url(): return failure(id, "host must be domain or ipv4 or url") else: vret, vret_content = APIHandler.Manager.vnet_status() if vret: str = "用户 {0} 正在使用中……,如需重新配置,请先停止再启动".format( vret_content.userinfo.get("name")) return failure(id, str) else: auth_code = params.get( "auth_code") or APPCtrl().get_accesskey() if auth_code: APIHandler.Manager.TRAccesskey = auth_code else: return failure(id, "params lost auth_code") if params.get("local_ip"): if is_ipv4(params.get("local_ip")): APIHandler.Manager.userinfo[ 'local_vnet_ip'] = params.get("local_ip") if params.get("dest_ip"): if is_ipv4(params.get("dest_ip")): APIHandler.Manager.userinfo['dest_ip'] = params.get( "dest_ip") APIHandler.Manager.nps_host = urlCheck(host).get_url() APIHandler.Manager.userinfo['name'] = user APIHandler.Manager.userinfo['tunnel_host'] = urlCheck( host).get_host() APIHandler.Manager.userinfo['gate'] = gate ret, ret_content = APIHandler.Manager.start_vnet() else: ret_content = "params lost host or user or gate" if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_ping(params: pingItem): if not type(params) is dict: params = params.dict() id = params.get("id") or uuid.uuid1() ret = params if ret: return success(id, params) else: return failure(id, "error")
def api_ping(params: pingItem): if not type(params) is dict: params = params.dict() id = params.get("id") or uuid.uuid1() ret = APIHandler.Manager.enable_heartbeat(True, 60) if ret: return success(id, ret) else: return failure(id, "error")
def api_result(params): if not type(params) is dict: params = to_dict(params) id = params.get("id") or uuid.uuid1() ret = APIHandler.Manager.get_bench() if ret: return success(id, ret) else: return failure(id, 'error')
def api_stop(params: stopItem): if not type(params) is dict: params = params.dict() # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() ret, ret_content = APIHandler.Manager.stop_vserial() if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_action(params: actionItem): if not type(params) is dict: params = params.dict() # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() auth_code = params.get("auth_code") or APPCtrl().get_accesskey() if auth_code: APIHandler.Manager.TRAccesskey = auth_code else: return failure(id, "params lost auth_code") gate = params.get("gate") action = params.get("action") if action not in ["install", "uninstall", "start", "stop", "upgrade"]: return failure(id, "action is unsupported") ret, ret_content = APIHandler.Manager.vserial_action(gate, action) if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_remove(params): if not type(params) is dict: params = params.dict() id = params.get("id") or uuid.uuid1() config = ConfigParser() if os.access(os.getcwd() + '\\config.ini', os.F_OK): config.read('config.ini') config.remove_section("thingscloud") config.write(open('config.ini', 'w')) return success(id, "ok") else: return failure(id, "config.ini does not exist")
def api_start(params: startItem): if not type(params) is dict: params = params.dict() # print(json.dumps(params, sort_keys=True, indent=4, separators=(',', ':'))) id = params.get("id") or uuid.uuid1() host = params.get("host") user = params.get("user") gate = params.get("gate") port = params.get("gate_port") ret, ret_content = None, None if not port: return failure(id, "params port_name lost") if host and user and gate: vret, vret_content = APIHandler.Manager.vserial_status() if vret: str = "用户 {0} 正在使用中……,如需重新配置,请先停止再启动".format( vret_content.userinfo.get("name")) return failure(id, str) else: auth_code = params.get("auth_code") or APPCtrl().get_accesskey() if auth_code: APIHandler.Manager.TRAccesskey = auth_code else: return failure(id, "params lost auth_code") if params.get("port"): APIHandler.Manager.frps_port = params.get("port") if params.get("token"): APIHandler.Manager.frps_token = params.get("token") APIHandler.Manager.frps_host = "http://" + host + ":2699" APIHandler.Manager.userinfo['name'] = user APIHandler.Manager.userinfo['tunnel_host'] = host APIHandler.Manager.userinfo['gate'] = gate APIHandler.Manager.userinfo['gate_port_name'] = port.lower() ret, ret_content = APIHandler.Manager.start_vserial() else: ret_content = "params lost host or user or gate" if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_npstunnel(params): if not type(params) is dict: params = to_dict(params) id = params.get("id") or uuid.uuid1() host = params.get("host") user = params.get("user") ret, ret_content = None, None if host and user: if not urlCheck(host).get_url(): return failure(id, "host must be domain or ipv4 or url") else: APIHandler.Manager.nps_host = urlCheck(host).get_url() APIHandler.Manager.userinfo['name'] = user APIHandler.Manager.userinfo['tunnel_host'] = urlCheck( host).get_host() ret, ret_content = APIHandler.Manager.nps_tunnel() else: ret_content = "params lost host or user" if ret: return success(id, ret_content) else: return failure(id, ret_content)
def api_save(params: configItem): if not type(params) is dict: params = params.dict() id = params.get("id") or uuid.uuid1() npshost = params.get("npshost") npsuser = params.get("npsuser") gate = params.get("gate") accesskey = params.get("auth_code") if len(accesskey) == 36: accesskey = base64.b64encode(accesskey.encode()).decode() config = ConfigParser() if os.access(os.getcwd() + '\\config.ini', os.F_OK): config.read('config.ini') if not config.has_section("thingscloud"): config.add_section("thingscloud") print("@@@@@@@@@@@", npshost, npsuser, gate, accesskey) if npshost: config.set("thingscloud", 'npshost', npshost) else: config.remove_option("thingscloud", 'npshost') if npsuser: config.set("thingscloud", 'npsuser', npsuser) else: config.remove_option("thingscloud", 'npsuser') if gate: config.set("thingscloud", 'gate', gate) else: config.remove_option("thingscloud", 'gate') config.set("thingscloud", 'accesskey', accesskey) config.write(open('config.ini', 'w')) else: config.read('config.ini') config.add_section("thingscloud") if npshost: config.set("thingscloud", 'npshost', npshost) if npsuser: config.set("thingscloud", 'npsuser', npsuser) if gate: config.set("thingscloud", 'gate', gate) config.set("thingscloud", 'accesskey', accesskey) config.write(open('config.ini', 'w')) return success(id, "ok") else: return failure(id, "auth_code is error")