@chenpu17/tcpmapping 2.0.0

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.
@@ -0,0 +1,203 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const bcrypt = require('bcryptjs');
4
+ const readline = require('readline');
5
+
6
+ // 创建readline接口
7
+ const rl = readline.createInterface({
8
+ input: process.stdin,
9
+ output: process.stdout
10
+ });
11
+
12
+ // 密码复杂度验证
13
+ function validatePassword(password) {
14
+ const minLength = 8;
15
+ const hasUppercase = /[A-Z]/.test(password);
16
+ const hasLowercase = /[a-z]/.test(password);
17
+ const hasNumbers = /\d/.test(password);
18
+ const hasSpecialChars = /[!@#$%^&*(),.?":{}|<>]/.test(password);
19
+
20
+ const errors = [];
21
+
22
+ if (password.length < minLength) {
23
+ errors.push(`密码长度至少需要${minLength}个字符`);
24
+ }
25
+
26
+ if (!hasUppercase) {
27
+ errors.push('密码必须包含至少一个大写字母');
28
+ }
29
+
30
+ if (!hasLowercase) {
31
+ errors.push('密码必须包含至少一个小写字母');
32
+ }
33
+
34
+ if (!hasNumbers) {
35
+ errors.push('密码必须包含至少一个数字');
36
+ }
37
+
38
+ return {
39
+ isValid: errors.length === 0,
40
+ errors: errors
41
+ };
42
+ }
43
+
44
+ // 获取用户输入(隐藏密码)
45
+ function getPassword(prompt) {
46
+ return new Promise((resolve) => {
47
+ // 对于密码输入,我们使用标准输入但隐藏输出
48
+ process.stdout.write(prompt);
49
+ process.stdin.setRawMode(true);
50
+ process.stdin.setEncoding('utf8');
51
+
52
+ let password = '';
53
+
54
+ const onData = (char) => {
55
+ if (char === '\n' || char === '\r' || char === '\u0004') {
56
+ // 结束输入
57
+ process.stdin.setRawMode(false);
58
+ process.stdin.removeListener('data', onData);
59
+ process.stdout.write('\n');
60
+ resolve(password);
61
+ } else if (char === '\u0003') {
62
+ // Ctrl+C
63
+ process.exit();
64
+ } else if (char === '\u007f') {
65
+ // 退格键
66
+ if (password.length > 0) {
67
+ password = password.slice(0, -1);
68
+ process.stdout.write('\b \b');
69
+ }
70
+ } else {
71
+ // 普通字符
72
+ password += char;
73
+ process.stdout.write('*');
74
+ }
75
+ };
76
+
77
+ process.stdin.on('data', onData);
78
+ });
79
+ }
80
+
81
+ // 读取现有的认证配置
82
+ function loadAuthConfig() {
83
+ try {
84
+ const configPath = path.join(__dirname, 'auth-config.json');
85
+ const configData = fs.readFileSync(configPath, 'utf8');
86
+ return JSON.parse(configData);
87
+ } catch (error) {
88
+ // 如果文件不存在,返回默认配置
89
+ return {
90
+ users: [],
91
+ passwordPolicy: {
92
+ minLength: 8,
93
+ requireUppercase: true,
94
+ requireLowercase: true,
95
+ requireNumbers: true,
96
+ requireSpecialChars: false,
97
+ passwordExpireDays: 90
98
+ },
99
+ sessionConfig: {
100
+ sessionTimeout: 24,
101
+ maxFailedAttempts: 5,
102
+ lockoutDuration: 30
103
+ }
104
+ };
105
+ }
106
+ }
107
+
108
+ // 保存认证配置
109
+ function saveAuthConfig(config) {
110
+ const configPath = path.join(__dirname, 'auth-config.json');
111
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
112
+ }
113
+
114
+ // 主要设置流程
115
+ async function setupPassword() {
116
+ console.log('=== TCP转发服务器 - 密码设置工具 ===\n');
117
+
118
+ try {
119
+ // 读取现有配置
120
+ const authConfig = loadAuthConfig();
121
+
122
+ // 获取用户名
123
+ const username = await new Promise((resolve) => {
124
+ rl.question('请输入用户名 (默认: admin): ', (input) => {
125
+ resolve(input.trim() || 'admin');
126
+ });
127
+ });
128
+
129
+ let password = '';
130
+ let isValidPassword = false;
131
+
132
+ // 获取并验证密码
133
+ while (!isValidPassword) {
134
+ password = await getPassword('请输入新密码: ');
135
+
136
+ if (password.length === 0) {
137
+ console.log('密码不能为空,请重新输入');
138
+ continue;
139
+ }
140
+
141
+ const validation = validatePassword(password);
142
+
143
+ if (!validation.isValid) {
144
+ console.log('\n密码不符合要求:');
145
+ validation.errors.forEach(error => console.log(` - ${error}`));
146
+ console.log('');
147
+ continue;
148
+ }
149
+
150
+ // 确认密码
151
+ const confirmPassword = await getPassword('请再次输入密码确认: ');
152
+
153
+ if (password !== confirmPassword) {
154
+ console.log('\n两次输入的密码不一致,请重新输入\n');
155
+ continue;
156
+ }
157
+
158
+ isValidPassword = true;
159
+ }
160
+
161
+ // 生成密码哈希
162
+ const passwordHash = await bcrypt.hash(password, 12);
163
+
164
+ // 查找现有用户或创建新用户
165
+ let userIndex = authConfig.users.findIndex(u => u.username === username);
166
+
167
+ if (userIndex === -1) {
168
+ // 创建新用户
169
+ authConfig.users.push({
170
+ username: username,
171
+ passwordHash: passwordHash,
172
+ role: 'admin',
173
+ lastPasswordChange: new Date().toISOString(),
174
+ enabled: true
175
+ });
176
+ console.log(`\n✓ 已创建新用户: ${username}`);
177
+ } else {
178
+ // 更新现有用户
179
+ authConfig.users[userIndex].passwordHash = passwordHash;
180
+ authConfig.users[userIndex].lastPasswordChange = new Date().toISOString();
181
+ console.log(`\n✓ 已更新用户密码: ${username}`);
182
+ }
183
+
184
+ // 保存配置
185
+ saveAuthConfig(authConfig);
186
+
187
+ console.log('✓ 密码设置完成');
188
+ console.log('\n密码策略要求:');
189
+ console.log(' - 最少8个字符');
190
+ console.log(' - 包含大写字母');
191
+ console.log(' - 包含小写字母');
192
+ console.log(' - 包含数字');
193
+ console.log(` - 密码有效期: ${authConfig.passwordPolicy.passwordExpireDays}天`);
194
+
195
+ } catch (error) {
196
+ console.error('设置密码时发生错误:', error.message);
197
+ } finally {
198
+ rl.close();
199
+ }
200
+ }
201
+
202
+ // 运行设置
203
+ setupPassword();