def nps_authcode(self): if not self.NPSApi: self.NPSApi = npsApiv1(self.nps_host) naes = npsCryp() retkey = self.NPSApi.nps_api_get("/auth/getauthkey") if retkey: self.__auth_key = naes.decrypt(retkey.get("crypt_auth_key")) return self.__auth_key
def nps_authcode(self): if not self.NPSApi: self.NPSApi = npsApiv1(self.nps_host) naes = npsCryp() retkey = self.NPSApi.nps_api_get("/auth/getauthkey") if retkey: self.__auth_key = naes.decrypt(retkey.get( "crypt_auth_key")) # now_time = str(int(time.time())) # auth_key_md5 = hashlib.md5((auth_key + now_time).encode(encoding="UTF-8")).hexdigest() return self.__auth_key
def nps_tunnel_status(self): if not self.NPSApi: self.NPSApi = npsApiv1(self.nps_host) if not self.__auth_key: self.nps_authcode() if self.__auth_key: if self.userinfo['tid']: now_time = str(int(time.time())) auth_key_md5 = hashlib.md5((self.__auth_key + now_time).encode(encoding="UTF-8")).hexdigest() tunnel = self.NPSApi.nps_api_post("/index/getonetunnel/", {"auth_key": auth_key_md5, "timestamp": now_time, "id": self.userinfo['tid']}) if tunnel: if tunnel.get("data"): self.userinfo['client_online'] = tunnel.get("data").get("Client").get("IsConnect")
def start_vserial(self): if not self._vserial_is_running: if not self.TRCloudapi: self.TRCloudapi = CloudApiv1(self.TRAccesskey) self.enable_heartbeat(True, 60) if not self.NPSApi: self.NPSApi = npsApiv1(self.nps_host) if not self.__auth_key: self.nps_authcode() if not self.userinfo['tid']: self.nps_tunnel() if self.userinfo['tid']: now_time = str(int(time.time())) auth_key_md5 = hashlib.md5( (self.__auth_key + now_time).encode(encoding="UTF-8")).hexdigest() if not self.userinfo['client_status']: # print("用户 {0} 当前禁用".format(self.userinfo["name"])) ret = self.NPSApi.nps_api_post( "/client/changestatus/", { "auth_key": auth_key_md5, "timestamp": now_time, "id": self.userinfo['cid'], "status": 1 }) if not self.userinfo['tunnel_status']: # print("隧道 {0} 当前禁用".format(self.userinfo["tunnel_alias"])) ret = self.NPSApi.nps_api_post( "/index/start/", { "auth_key": auth_key_md5, "timestamp": now_time, "id": self.userinfo['tid'] }) # 检测网关是否在线 gate_status_ret = self.TRCloudapi.get_gate_status( self.userinfo['gate']) if gate_status_ret: if gate_status_ret['message'] == "ONLINE": self._gate_online = True self.userinfo['gate_status'] = "ONLINE" else: self._gate_online = False self.userinfo['gate_status'] = "OFFLINE" if self._gate_online: model = sn_model_map.get( self.userinfo.get("gate")[0:6]) or sn_model_map.get( self.userinfo.get("gate")[0:7]) or "C202" gate_port = model_port_map.get(model).get( self.userinfo.get("gate_port_name")) or "/dev/ttyS1" gate_vserial_command = { "port": gate_port, "nps": { "server_addr": self.userinfo['tunnel_host'] + ":7088", "vkey": self.userinfo['vkey'] }, "user_id": self.userinfo['name'] } gate_datas = { "id": self.userinfo['gate'] + '/send_command/start/' + str(time.time()), "device": self.userinfo['gate'], "data": { "device": self.userinfo['gate'] + ".freeioe_Vserial_npc", "cmd": "start", "param": gate_vserial_command } } ret, ret_content = self.TRCloudapi.post_command_to_cloud( gate_datas) # print(json.dumps(ret, sort_keys=False, indent=4, separators=(',', ':'))) if ret: if ret_content["gate_mes"]["result"]: local_ports = self.list_all() local_newPort = None for x in range(0, len(local_ports) + 1): local_newPort = "COM" + str(x + 1) if local_newPort not in local_ports: break self.userinfo["local_port_name"] = local_newPort self._vserial_is_running = True self._start_time = time.time() self.userinfo["info"] = { "user": self.userinfo.get("name"), "gate": self.userinfo.get("gate"), "gate_port": self.userinfo.get("gate_port_name"), "serial_driver": "vspax" } handler = TcpClientHander( self.userinfo.get("local_port_name"), self.userinfo.get("tunnel_host"), int(self.userinfo.get("tunnel_port")), self.userinfo.get("info")) self.add(handler) return self._vserial_is_running, self.userinfo else: self.clean_cfg() return False, "下发指令到网关不正常,请检查后重试" else: self.clean_cfg() return False, "网关Npc服务启动不正常,请检查后重试" else: self.clean_cfg() return False, "网关不在线,或你无权访问此网关,请检查后重试" else: self.clean_cfg() return False, "NPS连接错误或无此用户 {0} ".format( self.userinfo.get("name")) else: return False, "用户 {0} 正在使用中……,如需重新配置,请先停止再启动".format( self.userinfo.get("name"))
def nps_tunnel(self): if not self.NPSApi: self.NPSApi = npsApiv1(self.nps_host) if not self.__auth_key: self.nps_authcode() if self.__auth_key: used_ports = [nps_allowed_ports[0]] now_time = str(int(time.time())) auth_key_md5 = hashlib.md5( (self.__auth_key + now_time).encode(encoding="UTF-8")).hexdigest() clients = self.NPSApi.nps_api_post( "/client/list", { "auth_key": auth_key_md5, "timestamp": now_time, "start": 0, "limit": 100 }).get("rows") if clients: for c in clients: if c.get("Remark") == self.userinfo['name']: # print("@@@@@@@@@", c.get("Remark"), self.userinfo['name']) self.userinfo['cid'] = c.get("Id") self.userinfo['vkey'] = c.get("VerifyKey") break if self.userinfo['cid']: tunnels = self.NPSApi.nps_api_post( "/index/gettunnel/", { "auth_key": auth_key_md5, "timestamp": now_time, "client_id": None, "type": "tcp", "start": 0, "limit": 100 }).get("rows") if tunnels: for t in tunnels: if not t.get("Port") in used_ports: used_ports.append(t.get("Port")) if t.get("Target").get( "TargetStr" ) == self.userinfo['tunnel_Target'] and t.get( "Client").get( "Remark") == self.userinfo['name']: # print(json.dumps(t, sort_keys=False, indent=4, separators=(',', ':'))) self.userinfo['tid'] = t.get("Id") self.userinfo['client_status'] = t.get( "Client").get("Status") self.userinfo['client_online'] = t.get( "Client").get("IsConnect") self.userinfo['tunnel_status'] = t.get( "Status") self.userinfo['tunnel_online'] = t.get( "RunStatus") self.userinfo['tunnel_alias'] = t.get("Remark") self.userinfo['tunnel_port'] = t.get("Port") break if not self.userinfo['tid']: used_ports.sort() newPort = 0 for x in range(0, 5): newPort = used_ports[0] + len(used_ports) + x if not newPort in used_ports: break newTunnel = { "auth_key": auth_key_md5, "timestamp": now_time, "client_id": self.userinfo['cid'], "type": "tcp", "remark": self.userinfo['name'] + "_vserial_npc_proxy", "port": newPort, "target": "127.0.0.1:4678" } self._log.info("{0} 增加新隧道 {1}".format( self.userinfo.get("name"), newPort)) ret = self.NPSApi.nps_api_post("/index/add/", newTunnel) tunnels = self.NPSApi.nps_api_post( "/index/gettunnel/", { "auth_key": auth_key_md5, "timestamp": now_time, "client_id": self.userinfo['cid'], "type": "tcp", "start": 0, "limit": 100 }).get("rows") if tunnels: for t in tunnels: if not t.get("Port") in used_ports: used_ports.append(t.get("Port")) if t.get("Target").get( "TargetStr" ) == self.userinfo['tunnel_Target'] and t.get( "Client").get( "Remark") == self.userinfo['name']: # print(json.dumps(t, sort_keys=False, indent=4, separators=(',', ':'))) self.userinfo['tid'] = t.get("Id") self.userinfo['client_status'] = t.get( "Client").get("Status") self.userinfo['client_online'] = t.get( "Client").get("IsConnect") self.userinfo['tunnel_status'] = t.get( "Status") self.userinfo['tunnel_online'] = t.get( "RunStatus") self.userinfo['tunnel_alias'] = t.get( "Remark") self.userinfo['tunnel_port'] = t.get( "Port") break else: self._log.warning("NPS用户 {0} 不存在".format( self.userinfo.get("name"))) return True, self.userinfo else: self._log.warning("访问 NPS 异常") return False, "访问 NPS 异常"
def start_vnet(self): if not self._vnet_is_running: self.check_tinc_service() if not self.TRCloudapi: self.TRCloudapi = CloudApiv1(self.TRAccesskey) self.enable_heartbeat(True, 60) if not self.NPSApi: self.NPSApi = npsApiv1(self.nps_host) if not self.__auth_key: self.nps_authcode() if not self.userinfo['tid']: # print("get tid") self.nps_tunnel() if self.userinfo['tid']: now_time = str(int(time.time())) auth_key_md5 = hashlib.md5((self.__auth_key + now_time).encode(encoding="UTF-8")).hexdigest() if not self.userinfo['client_status']: # print("用户 {0} 当前禁用".format(self.userinfo["name"])) ret = self.NPSApi.nps_api_post("/client/changestatus/", {"auth_key": auth_key_md5, "timestamp": now_time, "id": self.userinfo['cid'], "status": 1}) if not self.userinfo['tunnel_status']: # print("隧道 {0} 当前禁用".format(self.userinfo["tunnel_alias"])) ret = self.NPSApi.nps_api_post("/index/start/", {"auth_key": auth_key_md5, "timestamp": now_time, "id": self.userinfo['tid']}) # 检测网关是否在线 gate_status_ret = self.TRCloudapi.get_gate_status(self.userinfo['gate']) if gate_status_ret: if gate_status_ret['message'] == "ONLINE": self._gate_online = True self.userinfo['gate_status'] = "ONLINE" else: self._gate_online = False self.userinfo['gate_status'] = "OFFLINE" if self._gate_online: self.gate_vpn_data() local_vnet_ip = self.userinfo["local_vnet_ip"] local_vnet_netmask = self.userinfo["gate_lan_netmask"] if local_vnet_ip and is_ipv4(local_vnet_ip): self.nps_changevk() time.sleep(0.5) self.wmi_in_thread(self.prepend_tap, "vnet", [local_vnet_ip], ["255.255.255.0"]) self.services_start() gate_vnet_config = {"net": "bridge", "Address": self.userinfo['tunnel_host'], "Port": str(self.userinfo['tunnel_port']), "proxy_name": "vnet_npc_proxy", "user_id": self.userinfo['name']} gate_datas = {"id": self.userinfo['gate'] + '/send_command/start/' + str(time.time()), "device": self.userinfo['gate'], "data": {"device": self.userinfo['gate'] + ".freeioe_Vnet_npc", "cmd": "start", "param": gate_vnet_config}} ret, ret_content = self.TRCloudapi.post_command_to_cloud(gate_datas) # print(json.dumps(ret, sort_keys=False, indent=4, separators=(',', ':'))) if ret: if ret_content["gate_mes"]["result"]: self._vnet_is_running = True self._start_time = time.time() return self._vnet_is_running, self.userinfo else: self.services_stop() self.clean_cfg() return False, "下发指令到网关不正常,请检查后重试" else: self.services_stop() self.clean_cfg() return False, "网关VPN服务启动不正常,请检查后重试" else: self.services_stop() self.clean_cfg() return False, "无法获取正确的本地Vnet IP,网关可能未安装应用,或未开启数据上传,请检查后重试" else: self.services_stop() self.clean_cfg() return False, "网关不在线,或你无权访问此网关,请检查后重试" else: self.services_stop() self.clean_cfg() return False, "NPS连接错误或无此用户 {0} ".format(self.userinfo.get("name")) else: return False, "用户 {0} 正在使用中……,如需重新配置,请先停止再启动".format(self.userinfo.get("name"))