-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_subnets.php
More file actions
560 lines (494 loc) · 22.1 KB
/
import_subnets.php
File metadata and controls
560 lines (494 loc) · 22.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* 子网代理批量导入工具
* 支持多个子网使用相同的端口/用户名/密码配置
*/
require_once 'config.php';
require_once 'auth.php';
require_once 'monitor.php';
require_once 'includes/functions.php';
if (file_exists(__DIR__ . '/includes/AuditLogger.php')) {
require_once __DIR__ . '/includes/AuditLogger.php';
}
// 检查登录状态
Auth::requireLogin();
$monitor = new NetworkMonitor();
function ipToLongUnsigned($ip) {
$long = ip2long($ip);
if ($long === false) {
return false;
}
return (int)sprintf('%u', $long);
}
// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result = null;
$error = null;
try {
$csrfToken = $_POST['csrf_token'] ?? '';
if (!Auth::validateCsrfToken($csrfToken)) {
throw new Exception('CSRF验证失败,请刷新页面后重试');
}
// 获取公共配置
$port = (int)($_POST['port'] ?? 0);
$type = strtolower(trim($_POST['type'] ?? ''));
$username = trim($_POST['username']) ?: null;
$password = trim($_POST['password']) ?: null;
$importMode = $_POST['import_mode'] ?? 'skip';
if ($port < 1 || $port > 65535 || !in_array($type, ['socks5', 'http'], true)) {
throw new Exception('请填写有效的端口和代理类型');
}
$proxyList = [];
$totalProxies = 0;
$maxProxies = defined('MAX_SUBNET_IMPORT_PROXIES') ? (int)MAX_SUBNET_IMPORT_PROXIES : 50000;
// 处理每个子网
for ($i = 1; $i <= 20; $i++) {
$startIp = trim($_POST["start_ip_$i"] ?? '');
$endIp = trim($_POST["end_ip_$i"] ?? '');
if (empty($startIp) || empty($endIp)) {
continue;
}
// 验证IP格式
if (!filter_var($startIp, FILTER_VALIDATE_IP) || !filter_var($endIp, FILTER_VALIDATE_IP)) {
throw new Exception("子网 $i: IP地址格式无效");
}
// 生成IP范围内的所有代理
$startLong = ipToLongUnsigned($startIp);
$endLong = ipToLongUnsigned($endIp);
if ($startLong === false || $endLong === false) {
throw new Exception("子网 $i: IP地址格式无效");
}
if ($startLong > $endLong) {
throw new Exception("子网 $i: 开始IP不能大于结束IP");
}
$rangeCount = ($endLong - $startLong) + 1;
if ($rangeCount < 1) {
throw new Exception("子网 $i: IP范围无效");
}
if (($totalProxies + $rangeCount) > $maxProxies) {
throw new Exception('生成代理数量过大,请分批导入');
}
for ($ipLong = $startLong; $ipLong <= $endLong; $ipLong++) {
$ip = long2ip((int)$ipLong);
$proxyList[] = [
'ip' => $ip,
'port' => $port,
'type' => $type,
'username' => $username,
'password' => $password
];
$totalProxies++;
}
}
if (empty($proxyList)) {
throw new Exception('请至少配置一个子网');
}
$result = $monitor->importProxies($proxyList, $importMode);
$result['total_generated'] = $totalProxies;
if ($result && class_exists('AuditLogger')) {
AuditLogger::log('subnet_import', 'proxy', null, [
'import_mode' => $importMode,
'total_generated' => $totalProxies,
'imported' => $result['imported'] ?? null,
'skipped' => $result['skipped'] ?? null,
'errors' => isset($result['errors']) ? count($result['errors']) : null
]);
}
} catch (Exception $e) {
$error = $e->getMessage();
if (class_exists('AuditLogger')) {
AuditLogger::log('subnet_import_failed', 'proxy', null, [
'error' => $error
]);
}
}
}
?>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>子网代理导入 - NetWatch</title>
<link rel="stylesheet" href="includes/style-v2.css?v=<?php echo filemtime(__DIR__ . '/includes/style-v2.css'); ?>">
<style>
/* 页面特有样式 - 表单和子网配置 */
.section {
padding: 25px;
}
.section h2 {
margin-bottom: 20px;
}
.form-row {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
margin-bottom: 20px;
}
.form-row .form-group {
margin-bottom: 0;
}
.form-row .form-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: var(--color-text);
}
.form-row .form-group input,
.form-row .form-group select,
.form-group select,
.form-group input {
width: 100%;
background: var(--color-panel-light);
color: var(--color-text);
border: 1px solid var(--color-border);
padding: 8px 12px;
border-radius: 4px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: var(--color-text);
}
.btn-add {
background: var(--color-success);
padding: 8px 16px;
font-size: 12px;
}
.btn-add:hover {
background: #0ea572;
}
.subnet-item {
border: 1px solid var(--color-border);
border-radius: 8px;
padding: 20px;
margin-bottom: 15px;
background: var(--color-panel);
}
.subnet-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.subnet-title {
font-weight: 600;
color: var(--color-text);
}
.ip-inputs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.ip-inputs .form-group {
margin-bottom: 0;
}
.ip-inputs .form-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: var(--color-text);
}
.ip-inputs .form-group input {
width: 100%;
background: var(--color-panel-light);
color: var(--color-text);
border: 1px solid var(--color-border);
padding: 8px 12px;
border-radius: 4px;
}
.help-text {
font-size: 13px;
color: var(--color-muted);
margin-top: 5px;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.stat-item {
text-align: center;
padding: 15px;
background: var(--color-panel);
border-radius: 8px;
border: 1px solid var(--color-border);
}
.stat-number {
font-size: 24px;
font-weight: bold;
color: var(--color-primary);
}
.stat-label {
font-size: 12px;
color: var(--color-muted);
margin-top: 5px;
}
.example {
background: var(--color-panel);
padding: 10px;
border-radius: 5px;
font-size: 13px;
margin-top: 10px;
border-left: 4px solid var(--color-primary);
color: var(--color-text);
}
</style>
</head>
<body>
<div class="header">
<div class="container">
<div class="header-content">
<div class="header-left">
<h1>🌐 子网代理导入</h1>
<p>导入子网代理服务器</p>
</div>
<?php if (Auth::isLoginEnabled()): ?>
<div class="header-right">
<div class="user-info">
<div class="user-row">
<div class="username">👤 <?php echo htmlspecialchars(Auth::getCurrentUser()); ?></div>
<a href="#" class="logout-btn" onclick="event.preventDefault(); showCustomConfirm('确定要退出登录吗?', () => window.location.href='index.php?action=logout'); return false;">退出</a>
</div>
</div>
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- 导航链接 -->
<div class="container">
<div class="nav-links">
<a href="index.php" class="nav-link">主页</a>
<a href="import.php" class="nav-link">代理导入</a>
</div>
</div>
<div class="container">
<?php if (isset($result)): ?>
<div class="alert alert-success">
<h3>导入完成</h3>
<div class="stats">
<div class="stat-item">
<div class="stat-number"><?php echo $result['total_generated']; ?></div>
<div class="stat-label">生成代理</div>
</div>
<div class="stat-item">
<div class="stat-number"><?php echo $result['imported']; ?></div>
<div class="stat-label">成功导入</div>
</div>
<?php if (isset($result['skipped']) && $result['skipped'] > 0): ?>
<div class="stat-item">
<div class="stat-number"><?php echo $result['skipped']; ?></div>
<div class="stat-label">跳过重复</div>
</div>
<?php endif; ?>
<div class="stat-item">
<div class="stat-number"><?php echo count($result['errors']); ?></div>
<div class="stat-label">导入失败</div>
</div>
</div>
<?php if (!empty($result['errors'])): ?>
<h4>错误详情:</h4>
<div style="max-height: 200px; overflow-y: auto; background: #f8f9fa; padding: 10px; border-radius: 5px; margin-top: 10px;">
<?php foreach ($result['errors'] as $error): ?>
<div style="padding: 5px 0; border-bottom: 1px solid #e9ecef; font-size: 13px;"><?php echo htmlspecialchars($error); ?></div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-error">
<strong>导入失败:</strong> <?php echo htmlspecialchars($error); ?>
</div>
<?php endif; ?>
<form method="post" id="subnetForm">
<input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(Auth::getCsrfToken()); ?>">
<div class="section">
<h2>公共配置</h2>
<div class="form-row">
<div class="form-group">
<label for="port">端口</label>
<input type="number" name="port" id="port" value="<?php echo $_POST['port'] ?? '1080'; ?>" required min="1" max="65535">
</div>
<div class="form-group">
<label for="type">代理类型</label>
<select name="type" id="type" required>
<option value="socks5" <?php echo ($_POST['type'] ?? '') === 'socks5' ? 'selected' : ''; ?>>SOCKS5</option>
<option value="http" <?php echo ($_POST['type'] ?? '') === 'http' ? 'selected' : ''; ?>>HTTP</option>
</select>
</div>
<div class="form-group">
<label for="username">用户名</label>
<input type="text" name="username" id="username" value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>">
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" name="password" id="password" value="<?php echo htmlspecialchars($_POST['password'] ?? ''); ?>">
</div>
</div>
<div class="form-group">
<label for="import_mode">导入模式</label>
<select name="import_mode" id="import_mode" required>
<option value="skip" <?php echo ($_POST['import_mode'] ?? 'skip') === 'skip' ? 'selected' : ''; ?>>跳过模式 - 跳过已存在的代理 (推荐)</option>
<option value="add" <?php echo ($_POST['import_mode'] ?? '') === 'add' ? 'selected' : ''; ?>>新增模式 - 添加到现有代理列表</option>
</select>
<div class="help-text">
新增模式:所有代理都会被添加,可能产生重复记录<br>
跳过模式:如果相同IP:端口已存在,则跳过不导入
</div>
</div>
<div class="help-text">
所有子网将使用相同的端口、类型、用户名和密码配置
</div>
</div>
<div class="section">
<h2>子网配置</h2>
<div id="subnets-container">
<?php
// 默认显示9个子网配置输入框,支持任意子网大小(/27、/28、/29等)
// 只需设置开始和结束IP,系统会自动生成范围内所有代理配置
for ($i = 1; $i <= 9; $i++):
$startIp = $_POST["start_ip_$i"] ?? '';
$endIp = $_POST["end_ip_$i"] ?? '';
?>
<div class="subnet-item">
<div class="subnet-header">
<div class="subnet-title">子网 <?php echo $i; ?></div>
</div>
<div class="ip-inputs">
<div class="form-group">
<label for="start_ip_<?php echo $i; ?>">开始IP</label>
<input type="text" name="start_ip_<?php echo $i; ?>" id="start_ip_<?php echo $i; ?>"
value="<?php echo htmlspecialchars($startIp); ?>"
placeholder="如: 1.2.3.2">
</div>
<div class="form-group">
<label for="end_ip_<?php echo $i; ?>">结束IP</label>
<input type="text" name="end_ip_<?php echo $i; ?>" id="end_ip_<?php echo $i; ?>"
value="<?php echo htmlspecialchars($endIp); ?>"
placeholder="如: 1.2.3.30">
</div>
</div>
</div>
<?php endfor; ?>
</div>
<button type="button" class="btn btn-add" onclick="addSubnet()">+ 添加新子网</button>
</div>
<div class="section">
<button type="submit" class="btn">开始导入</button>
<button type="button" class="btn btn-secondary" onclick="clearForm()">清空表单</button>
<button type="button" class="btn btn-secondary" onclick="previewProxies()">预览代理数量</button>
</div>
</form>
<div class="section">
<h2>使用说明</h2>
<ul style="line-height: 1.6; margin-left: 20px;">
<li><strong>公共配置:</strong> 所有子网使用相同的端口、类型、用户名和密码</li>
<li><strong>灵活的子网支持:</strong> 支持任意子网大小(/27、/28、/29等)和任意IP范围</li>
<li><strong>IP范围配置:</strong> 只需设置开始和结束IP,系统自动生成范围内所有代理</li>
<li><strong>公网/内网兼容:</strong> 支持公网IP和内网IP,不限制网络类型</li>
<li><strong>动态扩展:</strong> 默认9个子网输入框,可添加更多</li>
<li><strong>预览功能:</strong> 导入前可以预览将生成的代理数量</li>
</ul>
<div class="example">
<strong>子网示例:</strong><br>
• <strong>/27子网:</strong> 32个地址,可用IP范围如 x.x.x.2-30(29个)<br>
• <strong>/28子网:</strong> 16个地址,可用IP范围如 x.x.x.2-14(13个)<br>
• <strong>/29子网:</strong> 8个地址,可用IP范围如 x.x.x.2-6(5个)<br>
• <strong>自定义范围:</strong> 也可以不按子网划分,直接指定任意IP范围<br><br>
<strong>实际场景示例:</strong><br>
• <strong>场景1:</strong> 8个/27子网,每个29个IP = 232个代理<br>
• <strong>场景2:</strong> 16个/28子网,每个13个IP = 208个代理<br>
• <strong>场景3:</strong> 混合子网大小,根据实际部署灵活配置<br>
使用此工具,只需配置一次公共信息,然后设置各子网IP范围即可批量导入。
</div>
</div>
</div>
<script>
let subnetCount = 9;
function addSubnet() {
subnetCount++;
const container = document.getElementById('subnets-container');
const subnetHtml = `
<div class="subnet-item">
<div class="subnet-header">
<div class="subnet-title">子网 ${subnetCount}</div>
</div>
<div class="ip-inputs">
<div class="form-group">
<label for="start_ip_${subnetCount}">开始IP</label>
<input type="text" name="start_ip_${subnetCount}" id="start_ip_${subnetCount}"
placeholder="如: x.x.x.2">
</div>
<div class="form-group">
<label for="end_ip_${subnetCount}">结束IP</label>
<input type="text" name="end_ip_${subnetCount}" id="end_ip_${subnetCount}"
placeholder="如: x.x.x.30">
</div>
</div>
</div>
`;
container.insertAdjacentHTML('beforeend', subnetHtml);
}
function clearForm() {
// 重置表单基本字段
document.getElementById('port').value = '1080';
document.getElementById('type').value = 'socks5';
document.getElementById('username').value = '';
document.getElementById('password').value = '';
document.getElementById('import_mode').value = 'skip';
// 清空所有子网输入框(包括原始的9个和动态添加的)
for (let i = 1; i <= subnetCount; i++) {
const startInput = document.getElementById(`start_ip_${i}`);
const endInput = document.getElementById(`end_ip_${i}`);
if (startInput) startInput.value = '';
if (endInput) endInput.value = '';
}
// 重置子网计数器到初始状态
subnetCount = 9;
// 移除动态添加的子网输入框
const container = document.getElementById('subnets-container');
const subnetItems = container.querySelectorAll('.subnet-item');
// 保留前9个,删除其余的
for (let i = 9; i < subnetItems.length; i++) {
subnetItems[i].remove();
}
}
function previewProxies() {
let totalProxies = 0;
let validSubnets = 0;
for (let i = 1; i <= subnetCount; i++) {
const startIp = document.getElementById(`start_ip_${i}`)?.value.trim();
const endIp = document.getElementById(`end_ip_${i}`)?.value.trim();
if (startIp && endIp) {
try {
const startLong = ipToLong(startIp);
const endLong = ipToLong(endIp);
if (startLong <= endLong) {
const count = endLong - startLong + 1;
totalProxies += count;
validSubnets++;
}
} catch (e) {
// 忽略无效IP
}
}
}
alert(`预览结果:\n有效子网: ${validSubnets} 个\n将生成代理: ${totalProxies} 个`);
}
function ipToLong(ip) {
const parts = ip.split('.');
if (parts.length !== 4) throw new Error('Invalid IP');
return (parseInt(parts[0]) << 24) +
(parseInt(parts[1]) << 16) +
(parseInt(parts[2]) << 8) +
parseInt(parts[3]);
}
</script>
<!-- 新模块化JS -->
<script src="includes/js/core.js?v=<?php echo time(); ?>"></script>
<script src="includes/js/ui.js?v=<?php echo time(); ?>"></script>
<script src="includes/utils.js?v=<?php echo time(); ?>"></script>
</body>
</html>