|
1 | 1 | const axios = require("axios"); |
2 | 2 | const jwt = require("jsonwebtoken"); |
3 | 3 | const configStore = require("./config-store"); |
| 4 | +const fs = require("fs"); |
| 5 | +const path = require("path"); |
4 | 6 |
|
5 | 7 | class AuthService { |
6 | 8 | constructor() { |
7 | 9 | this.runnerConfig = null; |
8 | 10 | this.runnerToken = null; |
9 | 11 | this.deviceId = null; |
10 | 12 | this.lastUpdated = new Date(); |
| 13 | + // 修改存储路径到 data 文件夹 |
| 14 | + this.dataDir = path.join(__dirname, "../data"); |
| 15 | + this.authFile = path.join(this.dataDir, "auth.json"); |
| 16 | + this.ensureDataDirectory(); |
| 17 | + this.loadAuthData(); |
| 18 | + } |
| 19 | + |
| 20 | + // 确保数据目录和文件存在 |
| 21 | + ensureDataDirectory() { |
| 22 | + try { |
| 23 | + if (!fs.existsSync(this.dataDir)) { |
| 24 | + fs.mkdirSync(this.dataDir, { recursive: true }); |
| 25 | + console.log("[AuthService] ✅ 成功创建数据目录"); |
| 26 | + } |
| 27 | + |
| 28 | + if (!fs.existsSync(this.authFile)) { |
| 29 | + fs.writeFileSync(this.authFile, JSON.stringify({}, null, 2)); |
| 30 | + console.log("[AuthService] ✅ 成功创建认证数据文件"); |
| 31 | + } |
| 32 | + } catch (error) { |
| 33 | + console.error("[AuthService] ❌ 创建数据目录或文件失败:", error.message); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + loadAuthData() { |
| 38 | + try { |
| 39 | + if (fs.existsSync(this.authFile)) { |
| 40 | + const data = JSON.parse(fs.readFileSync(this.authFile, 'utf8')); |
| 41 | + this.runnerConfig = data.runnerConfig; |
| 42 | + this.runnerToken = data.runnerToken; |
| 43 | + this.deviceId = data.deviceId; |
| 44 | + this.lastUpdated = new Date(data.lastUpdated); |
| 45 | + |
| 46 | + // Update JWT secret from loaded config if available |
| 47 | + if (this.runnerConfig && this.runnerConfig.jwtSecret) { |
| 48 | + configStore.set("jwt.secret", this.runnerConfig.jwtSecret); |
| 49 | + } |
| 50 | + |
| 51 | + console.log("[AuthService] ✅ 成功从文件加载认证数据"); |
| 52 | + } |
| 53 | + } catch (error) { |
| 54 | + console.error("[AuthService] ❌ 加载认证数据失败:", error.message); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + saveAuthData() { |
| 59 | + try { |
| 60 | + const data = { |
| 61 | + runnerConfig: this.runnerConfig, |
| 62 | + runnerToken: this.runnerToken, |
| 63 | + deviceId: this.deviceId, |
| 64 | + lastUpdated: this.lastUpdated.toISOString() |
| 65 | + }; |
| 66 | + fs.writeFileSync(this.authFile, JSON.stringify(data, null, 2)); |
| 67 | + console.log("[AuthService] ✅ 成功保存认证数据到文件"); |
| 68 | + } catch (error) { |
| 69 | + console.error("[AuthService] ❌ 保存认证数据失败:", error.message); |
| 70 | + } |
11 | 71 | } |
12 | 72 |
|
13 | 73 | async getRunnerConfig() { |
@@ -41,6 +101,9 @@ class AuthService { |
41 | 101 | configStore.set("jwt.secret", response.data.jwtSecret); |
42 | 102 | } |
43 | 103 |
|
| 104 | + // Save the new auth data to file |
| 105 | + this.saveAuthData(); |
| 106 | + |
44 | 107 | console.log("[AuthService] ✅ Runner配置获取成功"); |
45 | 108 | return this.runnerConfig; |
46 | 109 | } catch (error) { |
@@ -68,6 +131,7 @@ class AuthService { |
68 | 131 |
|
69 | 132 | async updateLastUpdated() { |
70 | 133 | this.lastUpdated = new Date(); |
| 134 | + this.saveAuthData(); // Save when last updated changes |
71 | 135 | } |
72 | 136 |
|
73 | 137 | verifyToken(token) { |
|
0 commit comments