gcc vpn防掉线

一点小小的hook

// ==UserScript==
// @name         gcc vpn防掉线
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  一点小小的hook
// @author       LinXingJun
// @include      *://*.vpn.gcc.edu.cn:*/*
// @include      *://vpn.gcc.edu.cn/*
// @license MIT
// ==/UserScript==
function gethtml() {
    const hostname = window.location.hostname;
    var url = "http://" + hostname + ":8118/sf-webproxy/api/vpn-config";
    console.log("开始请求:" + Date.now() + hostname);
    const Http = new XMLHttpRequest();
    Http.open("GET", url);
    Http.send();
    console.log("请求完毕:" + Date.now() + hostname);
    // 生成 55-65 秒之间的随机时间(单位:毫秒)
    const delay = Math.floor(Math.random() * 10000) + 55000; // 55000 ~ 65000 ms
    // 递归调用自身,实现下一次延迟执行
    setTimeout(gethtml, delay);
}
function reload_windows(){
    // 定义定时器变量
    let inactivityTimer;

    // 重置定时器函数
    function resetTimer() {
        // 清除当前定时器
        clearTimeout(inactivityTimer);

        // 设置新的定时器,5分钟后执行刷新
        inactivityTimer = setTimeout(() => {
            window.location.reload();
        }, 300000); // 300000 毫秒 = 5 分钟
    }

    // 监听用户活动事件
    const activityEvents = ['mousemove', 'keydown', 'click', 'touchstart', 'scroll'];
    activityEvents.forEach(event => {
        window.addEventListener(event, resetTimer);
    });

    // 页面加载时启动定时器
    resetTimer();
}
(function () {
    if (window.__hiddenIframeScriptLoaded) return;
    window.__hiddenIframeScriptLoaded = true;
    gethtml();
    reload_windows();


    // 保存原生方法
    const origXHR = window.XMLHttpRequest.prototype.open;
    const origFetch = window.fetch;

    // 重写open方法
    window.XMLHttpRequest.prototype.open = function (method, url) {
        // 检查是否为匹配的GET请求
        if (method === 'get' && shouldIntercept(url)) {
            console.log('拦截到登出请求:', url);
            // 阻止请求发送(可选:修改URL或参数)
            return;
        }
        // 执行原始方法
        return origXHR.apply(this, arguments);
    };

    // 重写fetch方法
    window.fetch = function (input, init) {
        let url = '';
        // 解析输入参数
        if (typeof input === 'string') {
            url = input;
        } else if (input instanceof window.Request) {
            url = input.url;
        }

        // 检查是否为匹配的GET请求
        if (shouldIntercept(url) &&
           (!init || init.method?.toUpperCase() === 'GET')) {
            console.log('拦截到fetch登出请求:', url);
            // 返回空响应(可根据需求修改)
            return Promise.resolve(new window.Response());
        }

        // 执行原始fetch
        return origFetch(input, init);

 };

    // URL匹配函数
function shouldIntercept(url) {
            try {
            // 匹配 https://127.0.0.1[:port]/ECAgent/?op=Logout*
            return /^https:\/\/127\.0\.0\.1(?:$|:|\/)[^?]*\/ECAgent\/\?op=Logout/i.test(url);
        } catch (e) {
            return false;
        }
    }
})();