tanyu_admin 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/parser.js ADDED
@@ -0,0 +1,146 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ /**
5
+ * 从HTML中提取虚拟主机配置数据
6
+ */
7
+ function parseVirtualHostConfig(htmlContent) {
8
+ const configs = [];
9
+
10
+ // 正则匹配所有的配置项
11
+ // 匹配 ViewName0, ViewName1, ... ViewNameN
12
+ const viewNameRegex = /Transfer_meaning\('ViewName(\d+)','([^']+)'\)/g;
13
+ let match;
14
+ const indices = [];
15
+
16
+ while ((match = viewNameRegex.exec(htmlContent)) !== null) {
17
+ indices.push(match[1]);
18
+ }
19
+
20
+ // 对每个索引提取完整配置
21
+ indices.forEach(index => {
22
+ const config = extractConfigByIndex(htmlContent, index);
23
+ if (config) {
24
+ configs.push(config);
25
+ }
26
+ });
27
+
28
+ return configs;
29
+ }
30
+
31
+ /**
32
+ * 根据索引提取单个配置
33
+ */
34
+ function extractConfigByIndex(htmlContent, index) {
35
+ const fields = [
36
+ 'ViewName', 'WANCViewName', 'WANCName', 'Enable', 'Protocol',
37
+ 'Name', 'MinExtPort', 'MaxExtPort', 'InternalHost',
38
+ 'MinIntPort', 'MaxIntPort', 'Description', 'LeaseDuration',
39
+ 'PortMappCreator', 'MinRemoteHost', 'MaxRemoteHost',
40
+ 'InternalMacHost', 'MacEnable'
41
+ ];
42
+
43
+ const config = { index: parseInt(index) };
44
+
45
+ fields.forEach(field => {
46
+ const regex = new RegExp(`Transfer_meaning\\('${field}${index}','([^']*)'\\)`, 'i');
47
+ const match = htmlContent.match(regex);
48
+ if (match) {
49
+ let value = match[1];
50
+ // 解码特殊字符
51
+ value = value.replace(/\\x2e/g, '.')
52
+ .replace(/\\x3a/g, ':')
53
+ .replace(/\\x5f/g, '_');
54
+ config[field] = value;
55
+ }
56
+ });
57
+
58
+ // 转换协议代码为可读文本
59
+ if (config.Protocol !== undefined) {
60
+ const protocolMap = {
61
+ '0': 'TCP',
62
+ '1': 'UDP',
63
+ '2': 'TCP/UDP'
64
+ };
65
+ config.ProtocolText = protocolMap[config.Protocol] || config.Protocol;
66
+ }
67
+
68
+ // 转换启用状态
69
+ if (config.Enable !== undefined) {
70
+ config.EnableText = config.Enable === '1' ? '启用' : '禁用';
71
+ }
72
+
73
+ return config;
74
+ }
75
+
76
+ /**
77
+ * 从文件加载并解析配置
78
+ */
79
+ function loadConfigFromFile(filePath = './page.html') {
80
+ try {
81
+ const htmlContent = fs.readFileSync(filePath, 'utf8');
82
+ return parseVirtualHostConfig(htmlContent);
83
+ } catch (error) {
84
+ console.error('读取文件失败:', error.message);
85
+ return [];
86
+ }
87
+ }
88
+
89
+ /**
90
+ * 保存配置到JSON文件
91
+ */
92
+ function saveConfigToJson(configs, outputPath = './virtual_hosts.json') {
93
+ try {
94
+ fs.writeFileSync(outputPath, JSON.stringify(configs, null, 2), 'utf8');
95
+ // console.log(`配置已保存到: ${outputPath}`);
96
+ return true;
97
+ } catch (error) {
98
+ console.error('保存配置失败:', error.message);
99
+ return false;
100
+ }
101
+ }
102
+
103
+ /**
104
+ * 从JSON文件加载配置
105
+ */
106
+ function loadConfigFromJson(filePath = './virtual_hosts.json') {
107
+ try {
108
+ if (fs.existsSync(filePath)) {
109
+ const content = fs.readFileSync(filePath, 'utf8');
110
+ return JSON.parse(content);
111
+ }
112
+ return [];
113
+ } catch (error) {
114
+ console.error('读取JSON配置失败:', error.message);
115
+ return [];
116
+ }
117
+ }
118
+
119
+ // 测试代码
120
+ if (require.main === module) {
121
+ console.log('=== 解析虚拟主机配置 ===\n');
122
+
123
+ const configs = loadConfigFromFile('./page.html');
124
+ console.log(`找到 ${configs.length} 个虚拟主机配置:\n`);
125
+
126
+ configs.forEach((config, idx) => {
127
+ console.log(`[${idx + 1}] ${config.Name}`);
128
+ console.log(` 状态: ${config.EnableText}`);
129
+ console.log(` 协议: ${config.ProtocolText}`);
130
+ console.log(` 外部端口: ${config.MinExtPort}-${config.MaxExtPort}`);
131
+ console.log(` 内部主机: ${config.InternalHost}:${config.MinIntPort}-${config.MaxIntPort}`);
132
+ console.log(` WAN连接: ${config.WANCName}`);
133
+ console.log('');
134
+ });
135
+
136
+ // 保存到JSON
137
+ saveConfigToJson(configs);
138
+ }
139
+
140
+ module.exports = {
141
+ parseVirtualHostConfig,
142
+ extractConfigByIndex,
143
+ loadConfigFromFile,
144
+ saveConfigToJson,
145
+ loadConfigFromJson
146
+ };