Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jason5ng32 committed Dec 26, 2023
1 parent 4290220 commit 9c29b86
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 19 deletions.
4 changes: 4 additions & 0 deletions api/validate-site.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = (req, res) => {
const isIpCheckEnabled = process.env.IS_IPCHECK_ING === 'ipcheck.ing';
res.status(200).json({ isIpCheckEnabled });
};
8 changes: 4 additions & 4 deletions public/contents/ipDataCards.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default [
{
id: "upai",
id: "taobao",
ip: "",
country_name: "",
region: "",
Expand All @@ -12,10 +12,10 @@ export default [
asnlink: "",
mapUrl: "res/img/defaultMap.jpg",
showMap: false,
source: "Upai",
source: "Taobao",
},
{
id: "taobao",
id: "special",
ip: "",
country_name: "",
region: "",
Expand All @@ -27,7 +27,7 @@ export default [
asnlink: "",
mapUrl: "res/img/defaultMap.jpg",
showMap: false,
source: "Taobao",
source: "Special",
},
{
id: "cloudflare_v4",
Expand Down
72 changes: 57 additions & 15 deletions public/res/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@ new Vue({
keyMap,
},
methods: {

getIPFromTaobao() {
window.ipCallback = (data) => {
var ip = data.ip;
this.ipDataCards[0].source = "TaoBao";
this.fetchIPDetails(0, ip);
delete window.ipCallback; // 清理
};
var script = document.createElement("script");
script.src = "https://www.taobao.com/help/getip.php?callback=ipCallback";
document.head.appendChild(script);
// 清理
document.head.removeChild(script);
},

getIPFromSpecial() {
fetch('/api/validate-site')
.then(response => response.json())
.then(data => {
if (data.isIpCheckEnabled) {
this.getIPFromGCR();
} else {
this.getIPFromUpai();
}
});
},

getIPFromUpai() {
const unixTime = Date.now();
const url = `https://pubstatic.b0.upaiyun.com/?_upnode&t=${unixTime}`;
Expand All @@ -82,26 +109,38 @@ new Vue({
})
.then((data) => {
const ip = data.remote_addr;
this.fetchIPDetails(0, ip);
this.ipDataCards[1].source = "Upai"
this.fetchIPDetails(1, ip);
})
.catch((error) => {
console.error("Error fetching IP from Upai:", error);
this.ipDataCards[0].ip = this.currentTexts.ipInfos.IPv4Error;
this.ipDataCards[1].ip = this.currentTexts.ipInfos.IPv4Error;
});
},

getIPFromTaobao() {
window.ipCallback = (data) => {
var ip = data.ip;
this.ipDataCards[1].source = "TaoBao";
this.fetchIPDetails(1, ip);
delete window.ipCallback; // 清理
};
var script = document.createElement("script");
script.src = "https://www.taobao.com/help/getip.php?callback=ipCallback";
document.head.appendChild(script);
// 清理
document.head.removeChild(script);
getIPFromGCR() {
const url = `https://getipfromgoogle.ipcheck.ing/`;

fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
// 从 data.ip 获取第一个逗号前的部分
const fullIp = data.ip;
const ip = fullIp.includes(',') ? fullIp.split(',')[0] : fullIp;
this.ipDataCards[1].source = "IPCheck.ing";
// 使用提取的 IP 地址
this.fetchIPDetails(1, ip);
})

.catch((error) => {
console.error("Error fetching IP from IPCheck.ing:", error);
this.ipDataCards[1].ip = this.currentTexts.ipInfos.IPv4Error;
});
},

getIPFromCloudflare_V4() {
Expand Down Expand Up @@ -247,6 +286,9 @@ new Vue({
case "Upai":
this.getIPFromUpai(card);
break;
case "IPCheck.ing":
this.getIPFromGCR(card);
break;
case "TaoBao":
this.getIPFromTaobao(card);
break;
Expand Down Expand Up @@ -278,7 +320,7 @@ new Vue({
checkAllIPs() {
// 从所有来源获取 IP 地址
setTimeout(() => {
this.getIPFromUpai();
this.getIPFromSpecial();
}, 100);
setTimeout(() => {
this.getIPFromTaobao();
Expand Down
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const express = require('express');
const path = require('path');
const mapHandler = require('./api/map');
const validateMapKeyHandler = require('./api/validate-map-key');
const validateSite = require('./api/validate-site');
const ipinfoHandler = require('./api/ipinfo');
const ipapicomHandler = require('./api/ipapicom');

Expand All @@ -19,7 +20,9 @@ app.get('/api/ipapicom', ipapicomHandler);
// 设置静态文件服务
app.use(express.static(path.join(__dirname, 'public')));

// 一些判断
app.all('/api/validate-map-key', validateMapKeyHandler);
app.all('/api/validate-site', validateSite);

// 启动服务器
app.listen(port, () => {
Expand Down

0 comments on commit 9c29b86

Please sign in to comment.