(async function() { // 生成3位随机码 const random = Math.random().toString(36).substring(2, 5).toUpperCase(); // 获取Cookie function getCookie(name) { const value = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)'); return value ? value.pop() : ''; } // 获取基本信息 const email = document.querySelector('.username')?.textContent.trim() || decodeURIComponent(getCookie('U_USER_EMAIL')); const csrf = getCookie('U_CSRF_TOKEN'); // 获取项目ID const projectCookie = document.cookie.match(/c_project_[^=]+=([^;]+)/)?.[1]; const projectId = JSON.parse(decodeURIComponent(projectCookie || '{}')).ProjectId || 'org-acckbn'; // 获取区域 const regionCookie = getCookie('c_last_region_' + email.replace(/[@.]/g, '_')); const region = JSON.parse(decodeURIComponent(regionCookie || '{}')).region || 'us-ca'; console.log('邮箱:', email, '项目:', projectId, '区域:', region); // 通用请求函数 async function request(action, params) { const body = new URLSearchParams({ ProjectId: projectId, Region: region, Action: action, _user: email, _timestamp: Date.now(), ...params }).toString(); const response = await fetch('https://api.ucloud.cn/?Action=' + action, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', 'u-csrf-token': csrf }, credentials: 'include', body: body }); return response.json(); } // 1. 创建防火墙 const firewallName = '防火墙公众号小千哥' + random; console.log('创建防火墙:', firewallName); const createResult = await request('CreateFirewall', { 'Rule.0': 'TCP|8081|0.0.0.0/0|ACCEPT|HIGH|', 'Rule.1': 'TCP|3306|0.0.0.0/0|ACCEPT|HIGH|', 'Rule.2': 'TCP|8888|0.0.0.0/0|ACCEPT|HIGH|', 'Rule.3': 'ICMP||0.0.0.0/0|ACCEPT|HIGH|', 'Rule.4': 'TCP|22|0.0.0.0/0|ACCEPT|HIGH|', 'Rule.5': 'TCP|3389|0.0.0.0/0|ACCEPT|HIGH|', 'Rule.6': 'TCP|80|0.0.0.0/0|ACCEPT|HIGH|', 'Rule.7': 'TCP|443|0.0.0.0/0|ACCEPT|HIGH|', Name: firewallName, Tag: '' }); if (createResult.RetCode !== 0) { alert('创建失败: ' + createResult.Message); return; } const firewallId = createResult.FWId; console.log('防火墙ID:', firewallId); // 2. 获取服务器列表 const listResult = await request('DescribeULHostInstance', { Limit: 100, Offset: 0 }); if (listResult.RetCode !== 0) { alert('获取服务器失败: ' + listResult.Message); return; } const servers = listResult.ULHostInstanceSets || []; console.log('找到服务器:', servers.length, '台'); if (servers.length === 0) { alert('防火墙创建成功,但没有服务器需要绑定'); return; } // 3. 绑定服务器 let success = 0; for (const server of servers) { const result = await request('GrantSecurityGroup', { ResourceId: server.ULHostId, ResourceType: 'ulhost', GroupId: firewallId }); if (result.RetCode === 0) { console.log('✓', server.Name); success++; } else { console.log('✗', server.Name, result.Message); } await new Promise(r => setTimeout(r, 100)); } alert(`完成!\n${firewallName}\n成功: ${success}/${servers.length}`); })();