Skip to content

Commit 7e5cc21

Browse files
committed
add userData
1 parent 5b5fc8d commit 7e5cc21

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@conet.project/conet-proxy",
33

4-
"version": "0.13.2",
4+
"version": "0.14.1",
55

66
"license": "UNLICENSED",
77
"files": [
@@ -34,7 +34,8 @@
3434
"openpgp": "^5.11.2",
3535
"eth-crypto": "^2.8.0",
3636
"axios": "^1.10.0",
37-
"unzipper": "^0.12.3"
37+
"unzipper": "^0.12.3",
38+
"electron": "^37.2.1"
3839
},
3940
"devDependencies": {
4041
"@types/async": "^3.2.24",

src/localServer/localServer.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import * as openpgp from 'openpgp'
1414
import os from 'node:os'
1515
import CONET_Guardian_NodeInfo_ABI from './CONET_Guardian_NodeInfo_ABI.json'
1616
import {runUpdater} from './updateProcess'
17-
17+
import { app as electronApp } from 'electron'
18+
import fs from 'node:fs'
1819

1920
const ver = '0.1.4'
2021

@@ -215,7 +216,7 @@ type filterRule = {
215216
IP: string[]
216217
}
217218

218-
const appsPath: string = join ( __dirname )
219+
219220
export class Daemon {
220221
private logsPool: proxyLogs[] = []
221222

@@ -253,9 +254,28 @@ export class Daemon {
253254
}
254255

255256
private initialize = () => {
256-
const staticFolder = join ( appsPath, 'workers' )
257-
//const launcherFolder = join ( this.appsPath, '../launcher' )
258-
//console.dir ({ staticFolder: staticFolder, launcherFolder: launcherFolder })
257+
// --- 关键逻辑开始 ---
258+
259+
// 1. 定义默认路径(只读的应用包内部)
260+
const defaultPath = join(__dirname, 'workers')
261+
262+
// 2. 定义更新路径(可写的 userData 目录内部)
263+
const userDataPath = electronApp.getPath('userData');
264+
const updatedPath = join(userDataPath, 'workers');
265+
266+
// 3. 检查更新路径是否存在,然后决定使用哪个路径
267+
// 如果 updatedPath 存在,就用它;否则,回退到 defaultPath。
268+
const staticFolder = fs.existsSync(updatedPath) ? updatedPath : defaultPath;
269+
270+
// 确保我们选择的目录确实存在(主要针对首次启动时 defaultPath 的情况)
271+
if (!fs.existsSync(staticFolder)) {
272+
// 如果连默认目录都不存在,可能需要从别处复制或创建
273+
logger(Colors.red(`CRITICAL ERROR: Static folder not found at ${staticFolder}`));
274+
// 在这种情况下,可以考虑从一个备用位置将默认 `workers` 内容复制到 `updatedPath`
275+
// fs.cpSync(join(__dirname, 'initial_workers'), updatedPath, { recursive: true });
276+
}
277+
278+
// --- 关键逻辑结束 ---
259279

260280
const app = express()
261281
const cors = require('cors')
@@ -530,17 +550,17 @@ export class Daemon {
530550
this.loginListening.write (JSON.stringify(cmd)+'\r\n\r\n')
531551
})
532552

533-
534-
535553
app.all ('/', (req: any, res: any) => {
536554
logger (Colors.red(`Local web server got unknow request URL Error! [${ splitIpAddr (req.ip) }] => ${ req.method } url =[${ req.url }]`))
537555
return res.status(404).end (return404 ())
538556
})
539557

540558
this.localserver = app.listen ( this.PORT, () => {
541-
return console.table([
542-
{ 'CONET Local Web Server': `http://localhost:${ this.PORT }, local-path = [${ staticFolder }]` },
543-
])
559+
// 在日志中打印出当前正在使用的路径,这对于调试至关重要!
560+
return console.table([
561+
{ 'CONET Local Web Server': `http://localhost:${this.PORT}` },
562+
{ 'Serving files from': staticFolder }
563+
])
544564
})
545565
}
546566
}

src/localServer/updateProcess.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as unzipper from 'unzipper'
55
import http from 'node:http'
66
import currentVer from './workers/update.json'
77
import { inspect } from "node:util"
8+
import { app as electronApp } from 'electron'
89
// 定义 update.json 的数据结构
910
interface UpdateInfo {
1011
ver: string
@@ -135,15 +136,20 @@ export const runUpdater = async (nodes: nodes_info[] ) => {
135136
}
136137
// --- 步骤 3: 从选定节点下载并解压文件 ---
137138
const downloadUrl = `${baseApiUrl}${updateInfo.filename}`
138-
const extractPath = join(__dirname, 'workers')
139-
140-
logger(`⏳ 正在从 ${downloadUrl} 下载并解压...`)
141-
logger(`将解压到目录: ${extractPath}`)
139+
// 关键改动 2: 不再使用 __dirname,而是使用 userData 目录
140+
// 这确保了我们将文件解压到 Electron 应用的可写区域
141+
const userDataPath = electronApp.getPath('userData');
142+
const extractPath = join(userDataPath, 'workers');
143+
144+
logger(`⏳ 正在从 ${downloadUrl} 下载并解压...`);
145+
logger(`将解压到可写目录: ${extractPath}`); // 日志信息更新,更准确
146+
147+
// 确保目标目录存在
148+
// 这个逻辑在这里依然有效,它会创建 userData/workers 目录(如果尚不存在)
149+
if (!fs.existsSync(extractPath)) {
150+
fs.mkdirSync(extractPath, { recursive: true });
151+
}
142152

143-
// 确保目标目录存在
144-
if (!fs.existsSync(extractPath)) {
145-
fs.mkdirSync(extractPath, { recursive: true })
146-
}
147153
await downloadAndUnzip(downloadUrl, extractPath)
148154
logger(`🎉 成功下载并解压文件到 ${extractPath}`)
149155

0 commit comments

Comments
 (0)