// ==UserScript== // @name ip-checker // @namespace http://tampermonkey.net/ // @version 2.6 // @description 显示当前使用的公网IP地址,并带有折叠展开功能和刷新功能,以及IP风险查询功能 // @author https://linux.do/u/snaily // @match http://*/* // @match https://*/* // @grant GM_xmlhttpRequest // @grant GM_setValue // @grant GM_getValue // @connect api.ipify.org // @connect api64.ipify.org // @connect ip-api.com // @connect scamalytics.com // @connect ping0.cc // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/497045/ip-checker.user.js // @updateURL https://update.greasyfork.icu/scripts/497045/ip-checker.meta.js // ==/UserScript== (function () { "use strict"; // 检查是否为顶级窗口 if (window !== top) { console.log("Not in top window, exiting script."); return; } function fetchCurrentIP() { console.log("Fetching current IP..."); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = true; refreshButton.innerHTML = "正在刷新..."; } let ipv6 = null; // Fetch IPv6 GM_xmlhttpRequest({ method: "GET", url: "https://api64.ipify.org?format=json", onload: function (response) { console.log("IPv6 fetched:", response.responseText); const ipInfo = JSON.parse(response.responseText); ipv6 = isIPv6(ipInfo.ip) ? ipInfo.ip : null; console.log(ipv6); }, onerror: function (error) { console.log("Error fetching IPv6:", error); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } }, }); // Fetch IPv4 GM_xmlhttpRequest({ method: "GET", url: "https://api.ipify.org?format=json", onload: function (response) { console.log("IPv4 fetched:", response.responseText); const ipInfo = JSON.parse(response.responseText); fetchIPDetails(ipInfo.ip, ipv6); }, onerror: function (error) { console.log("Error fetching IPv4:", error); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } }, }); } function isIPv6(ip) { // IPv6正则表达式 const ipv6Pattern = new RegExp( "^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)$|^([0-9a-fA-F]{1,4}:){1,7}:$|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})$|^:((:[0-9a-fA-F]{1,4}){1,7}|:)$|^fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}$|^::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9])?[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9])?[0-9])$|^([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9])?[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9])?[0-9])$" ); return ipv6Pattern.test(ip); } function fetchIPDetails(ip, ipv6, callback) { console.log("Fetching IP details for:", ip); console.log(ipv6); GM_xmlhttpRequest({ method: "GET", url: "http://ip-api.com/json/" + ip, onload: function (response) { console.log("IP details fetched:", response.responseText); const ipDetails = JSON.parse(response.responseText); fetchIPRisk(ip, ipv6, ipDetails, callback); }, onerror: function (error) { console.log("Error fetching IP details:", error); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } if (callback) { callback(); // 查询失败后恢复按钮状态 } }, }); } function fetchIPRisk(ip, ipv6, details, callback) { console.log("Fetching IP risk for:", ip); console.log(ipv6); GM_xmlhttpRequest({ method: "GET", url: `https://scamalytics.com/ip/${ip}`, onload: function (response) { console.log("IP risk fetched:", response.responseText); const riskData = parseIPRisk(response.responseText); fetchPing0Risk(ip, ipv6, details, riskData); if (callback) { callback(); // 查询成功后恢复按钮状态 } }, onerror: function (error) { console.log("Error fetching IP risk:", error); displayIPDetails(ipv6, details, null, null); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } }, }); } function fetchPing0Risk(ip, ipv6, details, riskData) { console.log("Fetching Ping0 risk for:", ip); console.log(ipv6); GM_xmlhttpRequest({ method: "GET", url: `https://ping0.cc/ip/${ip}`, headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36", }, onload: function (response) { console.log("Initial Ping0 response:", response.responseText); const windowX = parseWindowX(response.responseText); if (windowX) { console.log("Parsed window.x value:", windowX); GM_xmlhttpRequest({ method: "GET", url: `https://ping0.cc/ip/${ip}`, headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36", Cookie: `jskey=${windowX}`, }, onload: function (response) { console.log("Final Ping0 response:", response.responseText); const ping0Data = parsePing0Risk(response.responseText); displayIPDetails(ipv6, details, riskData, ping0Data); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } }, onerror: function (error) { console.log("Error fetching final Ping0 risk:", error); displayIPDetails(ipv6, details, riskData, null); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } }, }); } else { console.log("Failed to retrieve window.x value."); displayIPDetails(ipv6, details, riskData, null); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } } }, onerror: function (error) { console.log("Error fetching initial Ping0 page:", error); displayIPDetails(ipv6, details, riskData, null); const refreshButton = document.getElementById("refreshIpInfo"); if (refreshButton) { refreshButton.disabled = false; refreshButton.innerHTML = "刷新IP信息"; } }, }); } function parseIPRisk(html) { console.log("Parsing IP risk data..."); const scoreMatch = html.match(/"score":"(.*?)"/); const riskMatch = html.match(/"risk":"(.*?)"/); if (riskMatch) { const riskData = { score: scoreMatch[1], risk: riskMatch[1], }; console.log("Parsed risk data:", riskData); return riskData; } console.log("Failed to parse risk data."); return null; } function parseWindowX(html) { console.log("Parsing window.x value..."); const match = html.match(/window\.x\s*=\s*'([^']+)'/); const windowX = match ? match[1] : null; console.log("Parsed window.x:", windowX); return windowX; } function parsePing0Risk(html) { console.log("Parsing Ping0 risk data..."); const parser = new DOMParser(); const doc = parser.parseFromString(html, "text/html"); const ipType = doc.evaluate( "/html/body/div[2]/div[2]/div[1]/div[2]/div[8]/div[2]/span", doc, null, XPathResult.STRING_TYPE, null ).stringValue; const nativeIP = doc.evaluate( "/html/body/div[2]/div[2]/div[1]/div[2]/div[10]/div[2]/span", doc, null, XPathResult.STRING_TYPE, null ).stringValue; const element = doc.querySelector( "#check > div.container > div.info > div.content > div.line.line-risk > div.content > div > div.riskitem.riskcurrent > span.value" ); const riskValue = element.textContent; const ping0Data = { riskValue: riskValue.trim(), ipType: ipType.trim(), nativeIP: nativeIP.trim(), }; console.log("Parsed Ping0 data:", ping0Data); return ping0Data; } function displayIPDetails(ipv6, details, riskData, ping0Data) { console.log("Displaying IP details..."); let ipElement = document.getElementById("ipInfo"); if (!ipElement) { ipElement = document.createElement("div"); ipElement.id = "ipInfo"; ipElement.style.position = "fixed"; ipElement.style.top = GM_getValue("ipInfoTop", "10px"); ipElement.style.right = "-1000px"; ipElement.style.backgroundColor = "#0a0a0a"; ipElement.style.padding = "15px"; ipElement.style.borderRadius = "0 0 0 10px"; ipElement.style.boxShadow = "0 0 20px rgba(0,255,255,0.3)"; ipElement.style.fontSize = "14px"; ipElement.style.color = "#00ffff"; ipElement.style.zIndex = "9999"; ipElement.style.transition = "right 0.3s ease, box-shadow 0.3s ease"; ipElement.style.fontFamily = "'Orbitron', sans-serif"; ipElement.style.border = "1px solid #00ffff"; const title = document.createElement("div"); title.style.fontWeight = "bold"; title.style.marginBottom = "10px"; title.style.fontSize = "18px"; title.style.textShadow = "0 0 5px #00ffff"; title.innerText = "IP检测信息"; const refreshButton = createButton("刷新IP信息", fetchCurrentIP); refreshButton.id = "refreshIpInfo"; const toggleButton = createButton("展开信息", toggleIpInfo); toggleButton.id = "toggleIpInfo"; toggleButton.style.display = "none"; const inputContainer = document.createElement("div"); inputContainer.style.marginTop = "10px"; const ipInput = document.createElement("input"); ipInput.id = "queryIpInput"; ipInput.type = "text"; ipInput.placeholder = "输入IP地址"; ipInput.style.marginRight = "5px"; ipInput.style.marginRight = "5px"; ipInput.style.width = "auto"; ipInput.style.color = "#00ffff"; ipInput.style.border = "1px solid #00ffff"; ipInput.style.padding = "5px"; const queryButton = createButton("查询IP", queryIpInfo); queryButton.id = "queryIpButton"; queryButton.innerHTML = "查询IP"; queryButton.style.width = "auto"; queryButton.style.backgroundColor = "#1a1a1a"; queryButton.style.color = "#00ffff"; queryButton.style.border = "1px solid #00ffff"; queryButton.style.borderRadius = "0"; queryButton.style.padding = "5px 10px"; queryButton.style.cursor = "pointer"; queryButton.style.fontSize = "12px"; queryButton.style.transition = "background-color 0.3s, box-shadow 0.3s"; queryButton.onclick = queryIpInfo; const dragHandle = document.createElement("div"); dragHandle.style.width = "100%"; dragHandle.style.height = "10px"; dragHandle.style.backgroundColor = "#00ffff"; dragHandle.style.cursor = "move"; dragHandle.style.marginBottom = "10px"; dragHandle.onmousedown = startDragging; const content = document.createElement("div"); content.id = "ipInfoContent"; title.appendChild(refreshButton); title.appendChild(toggleButton); inputContainer.appendChild(ipInput); inputContainer.appendChild(queryButton); title.appendChild(inputContainer); ipElement.appendChild(title); ipElement.appendChild(dragHandle); ipElement.appendChild(content); document.body.appendChild(ipElement); // 创建展开按钮 const expandButton = createButton("展开信息", toggleIpInfo); expandButton.id = "expandIpInfo"; expandButton.style.position = "fixed"; expandButton.style.top = GM_getValue("ipInfoTop", "10px"); expandButton.style.right = "0"; expandButton.style.display = "block"; document.body.appendChild(expandButton); expandButton.style.zIndex = "999999999"; } let contentElement = document.getElementById("ipInfoContent"); if (!contentElement) { contentElement = document.createElement("div"); contentElement.id = "ipInfoContent"; ipElement.appendChild(contentElement); } const content = `