(ip: string)
| 72 | |
| 73 | if (response.status === 200 && response.data) { |
| 74 | const data = response.data; |
| 75 | const country = data.country || ""; |
| 76 | const region = data.region || ""; |
| 77 | const city = data.city || ""; |
| 78 | const asn = data.asn || ""; |
| 79 | |
| 80 | let location = ""; |
| 81 | if (country) location += country; |
| 82 | if (region && region !== country) |
| 83 | location += (location ? "-" : "") + region; |
| 84 | if (city && city !== region) location += (location ? "-" : "") + city; |
| 85 | |
| 86 | if (location && asn) { |
| 87 | return `${location}-${asn}`; |
| 88 | } else if (location) { |
| 89 | return location; |
| 90 | } else if (asn) { |
| 91 | return asn; |
| 92 | } |
| 93 | |
| 94 | return "未知"; |
| 95 | } |
| 96 | } catch (error) { |
| 97 | try { |
| 98 | const response = await axios.get(`https://ipinfo.io/${ip}/json`, { |
| 99 | timeout: 3000, |
| 100 | headers: { |
| 101 | "User-Agent": "TeleBox-DNS-Plugin/1.0", |
| 102 | }, |
| 103 | }); |
| 104 | |
| 105 | if (response.status === 200 && response.data && !response.data.bogon) { |
| 106 | const data = response.data; |
| 107 | const country = data.country || ""; |
| 108 | const region = data.region || ""; |
| 109 | const city = data.city || ""; |
| 110 | const org = data.org || ""; |
| 111 | |
| 112 | let location = ""; |
| 113 | if (city) location += city; |
| 114 | if (region && region !== city) |
| 115 | location += (location ? "," : "") + region; |
| 116 | if (country) location += (location ? "," : "") + country; |
| 117 | |
| 118 | let asn = ""; |
| 119 | if (org) { |
| 120 | const asnMatch = org.match(/AS\d+/); |
| 121 | if (asnMatch) { |
| 122 | asn = asnMatch[0]; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (location && asn) { |
| 127 | return `${location}-${asn}`; |
| 128 | } else if (location) { |
| 129 | return location; |
| 130 | } else if (asn) { |
| 131 | return asn; |
no test coverage detected