Files
seabox_fanyi_application/test_proxy_real.js
2025-09-18 15:14:14 +08:00

91 lines
2.4 KiB
JavaScript

const { app, BrowserWindow, session, net } = require('electron');
// 测试真实的代理连接
async function testRealProxy() {
console.log('开始测试真实代理连接...');
const config = {
proxyIp: '143.20.228.192',
proxyPort: '3306',
username: 'mNrz1aEg',
password: '3xV3dBYB'
};
// 创建测试会话
const testSession = session.fromPartition('proxy-test-real', { cache: false });
// 设置代理(不带认证信息)
const proxyConfig = {
mode: 'fixed_servers',
proxyRules: `http=${config.proxyIp}:${config.proxyPort}`,
proxyBypassRules: 'localhost,127.0.0.1,<local>'
};
console.log('代理配置:', proxyConfig);
await testSession.setProxy(proxyConfig);
// 创建测试窗口
const testWindow = new BrowserWindow({
width: 800,
height: 600,
show: false,
webPreferences: {
session: testSession,
nodeIntegration: false,
contextIsolation: true
}
});
// 监听login事件
testWindow.webContents.on('login', (event, request, authInfo, callback) => {
console.log('收到login事件:', {
isProxy: authInfo.isProxy,
scheme: authInfo.scheme,
host: authInfo.host,
port: authInfo.port,
realm: authInfo.realm
});
event.preventDefault();
if (authInfo.isProxy && authInfo.host === config.proxyIp && authInfo.port === Number(config.proxyPort)) {
console.log('提供代理认证凭据');
callback(config.username, config.password);
} else {
console.log('非匹配的认证请求');
callback('', '');
}
});
// 监听页面加载事件
testWindow.webContents.on('did-finish-load', () => {
console.log('✅ 页面加载完成');
});
testWindow.webContents.on('did-fail-load', (event, errorCode, errorDescription, validatedURL) => {
console.log('❌ 页面加载失败:', errorCode, errorDescription, validatedURL);
});
// 测试加载页面
console.log('尝试加载测试页面...');
try {
await testWindow.loadURL('https://httpbin.org/ip');
console.log('页面加载请求已发送');
} catch (error) {
console.log('页面加载出错:', error.message);
}
// 等待5秒后关闭
setTimeout(() => {
testWindow.close();
console.log('测试完成');
process.exit(0);
}, 5000);
}
app.whenReady().then(testRealProxy);
app.on('window-all-closed', () => {
app.quit();
});