fdb2 1.0.38 → 1.0.39

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.
Files changed (3) hide show
  1. package/README.md +6 -0
  2. package/bin/fdb2.js +78 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -107,6 +107,12 @@ The remote database password never leaves the remote server: it is **not** sent
107
107
  ```bash
108
108
  # on the remote machine
109
109
  DB_TOOL_REMOTE_TOKEN=your-secret fdb2 start -p 9800
110
+
111
+ # 也可以把令牌持久化保存,之后 `fdb2 start` 会自动读取,无需每次设置环境变量:
112
+ fdb2 token set your-secret
113
+ fdb2 start -p 9800
114
+ # 查看已保存的令牌:
115
+ fdb2 token get
110
116
  ```
111
117
  Once set, the remote server rejects every HTTP request that does not carry the matching `x-db-tool-token` header, and answers `远程访问令牌无效` (*invalid remote access token*). Put the same value into the proxy connection's **Access Token** field.
112
118
 
package/bin/fdb2.js CHANGED
@@ -50,6 +50,31 @@ function deleteServerInfo() {
50
50
  }
51
51
  }
52
52
 
53
+ // 获取本地配置文件路径(与 PID/INFO 同目录)
54
+ function getConfigPath() {
55
+ const dataDir = process.env.DB_TOOL_DATA_DIR || path.join(os.homedir(), '.fdb2');
56
+ if (!fs.existsSync(dataDir)) {
57
+ fs.mkdirSync(dataDir, { recursive: true });
58
+ }
59
+ return path.join(dataDir, 'fdb2.config.json');
60
+ }
61
+
62
+ // 读取本地配置(容错:损坏时回退空对象)
63
+ function readConfig() {
64
+ try {
65
+ const p = getConfigPath();
66
+ if (!fs.existsSync(p)) return {};
67
+ return JSON.parse(fs.readFileSync(p, 'utf8')) || {};
68
+ } catch {
69
+ return {};
70
+ }
71
+ }
72
+
73
+ // 写入本地配置
74
+ function writeConfig(cfg) {
75
+ fs.writeFileSync(getConfigPath(), JSON.stringify(cfg, null, 2));
76
+ }
77
+
53
78
  // 解析命令行参数
54
79
  const args = process.argv.slice(2);
55
80
  const command = args[0] || 'help';
@@ -66,10 +91,18 @@ switch (command) {
66
91
  case 'restart':
67
92
  restartProject();
68
93
  break;
94
+ case 'token':
95
+ handleTokenCommand(commandArgs);
96
+ break;
69
97
  case '-v':
70
98
  case '--version':
71
99
  showVersion();
72
100
  break;
101
+ case '-h':
102
+ case '--help':
103
+ case 'help':
104
+ showHelp();
105
+ break;
73
106
  default: {
74
107
  // 数据库命令(conn/db/table/rows/...)交给编译后的 CLI
75
108
  const cliMain = path.join(projectRoot, 'dist', 'server', 'cli', 'main.js');
@@ -198,6 +231,16 @@ async function startProject() {
198
231
  // 创建日志文件流
199
232
  const logFileFd = createLogFileStream(logFilePath);
200
233
 
234
+ // 注入远程访问令牌:若未通过环境变量显式指定,则从本地配置读取并写入子进程环境
235
+ if (!process.env.DB_TOOL_REMOTE_TOKEN) {
236
+ const cfg = readConfig();
237
+ if (cfg.remoteToken) {
238
+ process.env.DB_TOOL_REMOTE_TOKEN = cfg.remoteToken;
239
+ console.log('已从本地配置载入远程访问令牌。');
240
+ }
241
+ }
242
+
243
+
201
244
  // 使用 node 命令启动服务器(异步,后台运行)
202
245
  const child = spawn(cmd, args, {
203
246
  cwd: projectRoot,
@@ -349,8 +392,40 @@ function showHelp() {
349
392
  console.log('FDB2 Database Tool');
350
393
  console.log('');
351
394
  console.log('Usage:');
352
- console.log(' fdb2 start Start the project');
353
- console.log(' fdb2 stop Stop the project');
354
- console.log(' fdb2 restart Restart the project');
395
+ console.log(' fdb2 start Start the project');
396
+ console.log(' fdb2 stop Stop the project');
397
+ console.log(' fdb2 restart Restart the project');
398
+ console.log(' fdb2 token set <token> Save remote access token to local config');
399
+ console.log(' fdb2 token get Show the saved remote access token');
355
400
  console.log('');
401
+ console.log('数据库操作(连接/库表/数据/SQL 等)作为子命令使用,例如 `fdb2 conn`、`fdb2 db`。');
402
+ console.log('远程连接相关说明见 README 的「远程代理服务」章节。');
403
+ }
404
+
405
+ // 处理 token 子命令:set / get
406
+ function handleTokenCommand(args) {
407
+ const sub = args[0];
408
+ if (sub === 'set') {
409
+ const value = args[1];
410
+ if (!value) {
411
+ console.error('用法: fdb2 token set <token>');
412
+ process.exitCode = 1;
413
+ return;
414
+ }
415
+ const cfg = readConfig();
416
+ cfg.remoteToken = value;
417
+ writeConfig(cfg);
418
+ console.log('远程访问令牌已保存到本地配置文件。');
419
+ console.log('下次执行 `fdb2 start` 时会自动应用到服务端。');
420
+ } else if (sub === 'get' || sub === 'view') {
421
+ const cfg = readConfig();
422
+ if (cfg.remoteToken) {
423
+ console.log(cfg.remoteToken);
424
+ } else {
425
+ console.log('(尚未设置远程访问令牌)');
426
+ }
427
+ } else {
428
+ console.error('用法: fdb2 token set <token> | fdb2 token get');
429
+ process.exitCode = 1;
430
+ }
356
431
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fdb2",
3
- "version": "1.0.38",
3
+ "version": "1.0.39",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "main": "electron/main.js",