| 70 | |
| 71 | |
| 72 | class ServerNode: |
| 73 | is_local = False |
| 74 | |
| 75 | def __init__(self, origin: str, api_key: str, app_key: str, remarks: str = "", timeout: int = 20): |
| 76 | self.origin = origin |
| 77 | self.api_key = api_key |
| 78 | self.app_key: Optional[NodeAPPKey] = Node.parse_app_key(app_key) |
| 79 | self.remarks = remarks |
| 80 | self.timeout = timeout |
| 81 | |
| 82 | @property |
| 83 | def node_server_ip(self): |
| 84 | from urllib.parse import urlparse |
| 85 | host = urlparse(self.origin).hostname |
| 86 | try: |
| 87 | if isinstance(host, str) and ipaddress.ip_address(host): |
| 88 | return host |
| 89 | except: |
| 90 | pass |
| 91 | try: |
| 92 | ip_address = socket.gethostbyname(host) |
| 93 | return ip_address |
| 94 | except socket.gaierror as e: |
| 95 | print(f"Error: {e}") |
| 96 | return host |
| 97 | |
| 98 | def app_bind(self) -> Optional[str]: |
| 99 | if self.app_key is None: |
| 100 | return "app秘钥解析失败,请重新确认秘钥信息" |
| 101 | version, arch = _get_os_info() |
| 102 | public.print_log(self.app_key.app_token) |
| 103 | pdata = { |
| 104 | "bind_token": self.app_key.app_token, |
| 105 | "client_brand": "BT-Panel", |
| 106 | "client_model": "Linux" if not version else "Linux {} {}".format(version, arch) |
| 107 | } |
| 108 | header = { |
| 109 | 'Content-Type': 'application/x-www-form-urlencoded', |
| 110 | "User-Agent": "Bt-Panel/Node Manager" |
| 111 | } |
| 112 | bind_url = self.app_key.origin + "/check_bind" |
| 113 | try: |
| 114 | res = requests.post(bind_url, data=pdata, headers=header, verify=False, timeout=self.timeout) |
| 115 | if res.status_code != 200: |
| 116 | return "绑定请求失败,返回状态码:%s,响应信息为:%s" % (res.status_code, res.text) |
| 117 | res_str = res.text.strip() |
| 118 | if res_str == "0": |
| 119 | return "绑定失败,请检查您的密钥是否正确" |
| 120 | elif res_str == "1": |
| 121 | return None |
| 122 | else: |
| 123 | return "绑定失败,错误信息为:{}".format(json.loads(res_str)) |
| 124 | except Exception as e: |
| 125 | return "网络连接失败,无法请求到目标服务器" |
| 126 | |
| 127 | def app_bind_status(self) -> Optional[str]: |
| 128 | if self.app_key is None: |
| 129 | return "app秘钥解析失败,请重新确认秘钥信息" |
no outgoing calls
no test coverage detected