Greasy Fork

Greasy Fork is available in English.

ip-checker

显示当前使用的公网IP地址,并带有折叠展开功能和刷新功能,以及IP风险查询功能

当前为 2024-06-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         ip-checker
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  显示当前使用的公网IP地址,并带有折叠展开功能和刷新功能,以及IP风险查询功能
// @author       https://linux.do/u/snaily
// @match        http://*/*
// @match        https://*/*
// @grant        GM_xmlhttpRequest
// @connect      api.ipify.org
// @connect      ip-api.com
// @connect      scamalytics.com
// @connect      ping0.cc
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // 使用api.ipify.org服务查询当前的公网IP
    function fetchCurrentIP() {
        console.log('Fetching current IP...');
        const refreshButton = document.getElementById('refreshIpInfo');
        if (refreshButton) {
            refreshButton.disabled = true;
            refreshButton.innerHTML = '正在刷新...';
        }

        GM_xmlhttpRequest({
            method: "GET",
            url: "https://api.ipify.org?format=json",
            onload: function(response) {
                console.log('IP fetched:', response.responseText);
                const ipInfo = JSON.parse(response.responseText);
                fetchIPDetails(ipInfo.ip);
            },
            onerror: function(error) {
                console.log('Error fetching IP:', error);
                if (refreshButton) {
                    refreshButton.disabled = false;
                    refreshButton.innerHTML = '点击刷新IP信息';
                }
            }
        });
    }

    // 使用ip-api.com服务查询IP详细信息
    function fetchIPDetails(ip) {
        console.log('Fetching IP details for:', ip);
        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, ipDetails);
            },
            onerror: function(error) {
                console.log('Error fetching IP details:', error);
                const refreshButton = document.getElementById('refreshIpInfo');
                if (refreshButton) {
                    refreshButton.disabled = false;
                    refreshButton.innerHTML = '点击刷新IP信息';
                }
            }
        });
    }

    // 使用scamalytics.com服务查询IP风险信息
    function fetchIPRisk(ip, details) {
        console.log('Fetching IP risk for:', ip);
        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, details, riskData);
            },
            onerror: function(error) {
                console.log('Error fetching IP risk:', error);
                displayIPDetails(details, null, null);
                const refreshButton = document.getElementById('refreshIpInfo');
                if (refreshButton) {
                    refreshButton.disabled = false;
                    refreshButton.innerHTML = '点击刷新IP信息';
                }
            }
        });
    }

    // 从HTML页面中解析出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;
    }

    // 使用ping0.cc服务查询IP风控信息
    function fetchPing0Risk(ip, details, riskData) {
        console.log('Fetching Ping0 risk for:', ip);
        // 第一次请求以获取 window.x 值
        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);
                    // 第二次请求带上 cookie: jskey={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(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(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(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(details, riskData, null);
                const refreshButton = document.getElementById('refreshIpInfo');
                if (refreshButton) {
                    refreshButton.disabled = false;
                    refreshButton.innerHTML = '点击刷新IP信息';
                }
            }
        });
    }

    // 从HTML页面中解析出 window.x 值
    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;
    }

    // 从HTML页面中解析出ping0.cc的风险信息
    function parsePing0Risk(html) {
        console.log('Parsing Ping0 risk data...');
        const parser = new DOMParser();
        const doc = parser.parseFromString(html, 'text/html');

        const riskValue = doc.evaluate('/html/body/div[2]/div[2]/div[1]/div[2]/div[9]/div[2]/span', doc, null, XPathResult.STRING_TYPE, null).stringValue;
        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[11]/div[2]/span', doc, null, XPathResult.STRING_TYPE, null).stringValue;

        const ping0Data = {
            riskValue: riskValue.trim(),
            ipType: ipType.trim(),
            nativeIP: nativeIP.trim()
        };
        console.log('Parsed Ping0 data:', ping0Data);
        return ping0Data;
    }

    // 添加一个函数来显示IP地址及详细信息,并带有折叠展开功能
    function displayIPDetails(details, riskData, ping0Data) {
        console.log('Displaying IP details...');
        var ipElement = document.getElementById('ipInfo');
        if (!ipElement) {
            ipElement = document.createElement('div');
            ipElement.id = 'ipInfo';
            ipElement.style.position = 'fixed';
            ipElement.style.bottom = '10px';
            ipElement.style.right = '0';
            ipElement.style.backgroundColor = '#fff';
            ipElement.style.padding = '10px';
            ipElement.style.borderRadius = '5px 0 0 5px'; // 仅左侧圆角
            ipElement.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
            ipElement.style.textAlign = 'left';
            ipElement.style.zIndex = '9999';
            ipElement.style.color = '#004085'; // 设置深蓝色字体颜色
            ipElement.style.transition = 'right 0.5s'; // 平滑过渡效果
            ipElement.style.width = '250px'; // 指定宽度
            ipElement.style.right = '-250px'; // 默认隐藏在屏幕边缘

            ipElement.innerHTML = `
                <div id="toggleIpInfo" style="position:absolute;left:-20px;top:0;bottom:0;width:20px;background-color:#cfe6ff;cursor:pointer;z-index:10000;">
                    <span id="toggleIcon" style="position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);font-size:18px;color:white;">◀</span>
                </div>
                <button id="refreshIpInfo" style="cursor:pointer;background-color:#28a745;color:#fff;border:none;padding:5px 10px;border-radius:5px;width:100%;margin-bottom:10px;">点击刷新IP信息</button>
                <div id="ipDetails" style="display:none;">
                    当前公网IP: ${details.query}<br>
                    组织: ${details.org}<br>
                    城市: ${details.city}<br>
                    地区: ${details.regionName}<br>
                    国家: ${details.country}<br>
                    坐标: ${details.lon},${details.lat}<br>
                    ISP: ${details.isp}<br>
                    AS: ${details.as}<br>
                    风险分数: ${riskData ? riskData.score : '查询失败'}(${riskData ? riskData.risk : ''})<br>
                    风控值: ${ping0Data ? ping0Data.riskValue : '查询失败'}<br>
                    IP类型: ${ping0Data ? ping0Data.ipType : '查询失败'}<br>
                    原生IP: ${ping0Data ? ping0Data.nativeIP : '查询失败'}
                </div>
            `;

            document.body.appendChild(ipElement);

            // 绑定点击事件
            document.getElementById('toggleIpInfo').addEventListener('click', function() {
                var ipDetails = document.getElementById('ipDetails');
                var toggleIcon = document.getElementById('toggleIcon');
                ipElement.style.right = (ipElement.style.right == '0px') ? '-250px' : '0px'; // 切换贴边隐藏
                if (ipDetails.style.display === 'none') {
                    ipDetails.style.display = 'block';
                    toggleIcon.innerHTML = '▶';
                } else {
                    ipDetails.style.display = 'none';
                    toggleIcon.innerHTML = '◀';
                }
            });

            document.getElementById('refreshIpInfo').addEventListener('click', fetchCurrentIP);
        } else {
            var ipDetails = document.getElementById('ipDetails');
            ipDetails.innerHTML = `
                当前公网IP: ${details.query}<br>
                    组织: ${details.org}<br>
                    城市: ${details.city}<br>
                    地区: ${details.regionName}<br>
                    国家: ${details.country}<br>
                    坐标: ${details.lon},${details.lat}<br>
                    ISP: ${details.isp}<br>
                    AS: ${details.as}<br>
                    风险分数: ${riskData ? riskData.score : '查询失败'}(${riskData ? riskData.risk : ''})<br>
                    风控值: ${ping0Data ? ping0Data.riskValue : '查询失败'}<br>
                    IP类型: ${ping0Data ? ping0Data.ipType : '查询失败'}<br>
                    原生IP: ${ping0Data ? ping0Data.nativeIP : '查询失败'}
            `;

        }
    }

    fetchCurrentIP();
})();