// ==UserScript== // @name China ISP P2P Speed Test (3-ISP Compare) // @namespace http://yoursite.com // @version 1.1 // @description 对比中国电信(CN2)、移动(CMI)、联通(9929)的P2P延迟与带宽 // @match *://*/* // @grant none // ==/UserScript== (function () { 'use strict'; // 仅在测速网站运行 if (!location.hostname.includes('speedtest.net') && !location.hostname.includes('fast.com')) { return; } console.log('三大运营商P2P测速脚本已启动...'); // 三大运营商节点(STUN服务器模拟) const ispNodes = { 'China Telecom CN2': 'stun:stun-cn2.example.com:3478', 'China Mobile CMI': 'stun:stun-cmi.example.com:3478', 'China Unicom 9929': 'stun:stun-cu9929.example.com:3478' }; // 模拟测速函数 async function testP2PSpeed(ispName, stunServer) { return new Promise(async (resolve) => { try { const pc = new RTCPeerConnection({ iceServers: [{ urls: stunServer }] }); const channel = pc.createDataChannel('speedTest'); let startTime, receivedBytes = 0; channel.onopen = () => { console.log(`[${ispName}] P2P连接已建立,开始测速...`); startTime = performance.now(); const data = new Uint8Array(65535); for (let i = 0; i < 100; i++) { channel.send(data); } }; channel.onmessage = (event) => { receivedBytes += event.data.length; }; pc.oniceconnectionstatechange = () => { if (pc.iceConnectionState === 'disconnected' || pc.iceConnectionState === 'failed') { const duration = (performance.now() - startTime) / 1000; const speedMbps = (receivedBytes * 8 / duration / 1e6).toFixed(2); console.log(`[${ispName}] 测速完成:${speedMbps} Mbps`); resolve({ isp: ispName, speed: speedMbps }); pc.close(); } }; const offer = await pc.createOffer(); await pc.setLocalDescription(offer); } catch (err) { console.error(`[${ispName}] 测速出错:`, err); resolve({ isp: ispName, speed: 'Error' }); } }); } // 顺序测试三大运营商 async function runTests() { const results = []; for (const [isp, stun] of Object.entries(ispNodes)) { const result = await testP2PSpeed(isp, stun); results.push(result); } // 显示结果 let msg = '三大运营商P2P测速结果:\n'; results.forEach(r => { msg += `${r.isp}: ${r.speed} Mbps\n`; }); alert(msg); } // 延迟执行,确保页面加载完成 setTimeout(runTests, 3000); })();