| 14 | } |
| 15 | |
| 16 | const cleanQuery = query.trim(); |
| 17 | const apiUrl = `http://ip-api.com/json/${encodeURIComponent( |
| 18 | cleanQuery |
| 19 | )}?lang=zh-CN&fields=status,message,country,regionName,city,isp,org,as,query,timezone,proxy,hosting`; |
| 20 | |
| 21 | try { |
| 22 | const response = await axios.get(apiUrl, { |
| 23 | timeout: 15000, |
| 24 | headers: { |
| 25 | "User-Agent": "TeleBox-IP-Plugin/1.0", |
| 26 | }, |
| 27 | }); |
| 28 | |
| 29 | if (response.status === 200) { |
| 30 | const data = response.data; |
| 31 | if (data.status === "fail") { |
| 32 | return { |
| 33 | status: "fail", |
| 34 | message: data.message || "查询失败,请检查IP地址或域名是否正确", |
| 35 | }; |
| 36 | } |
| 37 | return data; |
| 38 | } |
| 39 | |
| 40 | return { |
| 41 | status: "fail", |
| 42 | message: `API请求失败,HTTP状态码: ${response.status}`, |
| 43 | }; |
| 44 | } catch (error: any) { |
| 45 | console.error("IP API request failed:", error); |
| 46 | |
| 47 | let errorMessage = "网络请求失败"; |
| 48 | const errorStr = String(error.message || error); |
| 49 | |
| 50 | if (errorStr.includes("timeout") || errorStr.includes("TIMEOUT")) { |
| 51 | errorMessage = "请求超时,请稍后重试"; |
| 52 | } else if ( |
| 53 | errorStr.includes("ENOTFOUND") || |
| 54 | errorStr.includes("getaddrinfo") |
| 55 | ) { |
| 56 | errorMessage = "DNS解析失败,请检查网络连接"; |
| 57 | } else if (errorStr.includes("ECONNREFUSED")) { |
| 58 | errorMessage = "连接被拒绝,请稍后重试"; |
| 59 | } |
| 60 | |
| 61 | return { |
| 62 | status: "fail", |
| 63 | message: errorMessage, |
| 64 | }; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const ip = async (msg: Api.Message) => { |
| 69 | try { |
| 70 | const args = msg.message.slice(1).split(" ").slice(1); |
| 71 | let query = args.join(" "); |
| 72 | |
| 73 | if (!query) { |