Skip to content

Commit

Permalink
✨ 1.0.3版本
Browse files Browse the repository at this point in the history
  • Loading branch information
luckjiawei committed Jul 17, 2024
1 parent a3f0043 commit 7ab26bc
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 76 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</div>

## 里程碑
- 2024-07-17: 发布v1.0.3版本 修复已知bug 增加开机自启 增加删除frp版本
- 2024-01-29: 发布v1.0.2版本 增加Linux客户端和代理模式
- 2023-12-01: 发布v1.0.1版本
- 2023-11-28: 发布v1.0版本
Expand Down
192 changes: 140 additions & 52 deletions electron/api/frpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {app, ipcMain, Notification} from "electron";
import {Config, getConfig} from "../storage/config";
import {listProxy} from "../storage/proxy";
import {listProxy, Proxy} from "../storage/proxy";
import {getVersionById} from "../storage/version";
import treeKill from "tree-kill";

Expand All @@ -25,43 +25,42 @@ const getFrpcVersionWorkerPath = (
) => {
getVersionById(versionId, (err2, version) => {
if (!err2) {
callback(version["frpcVersionPath"]);
if (version) {
callback(version["frpcVersionPath"]);
}
}
});
};

/**
* 生成配置文件
* 生成toml配置文件
* @param config
* @param proxys
*/
export const generateConfig = (
config: Config,
callback: (configPath: string) => void
) => {
listProxy((err3, proxys) => {
if (!err3) {
const proxyToml = proxys.map(m => {
let toml = `
const genTomlConfig = (config: Config, proxys: Proxy[]) => {
const proxyToml = proxys.map(m => {
let toml = `
[[proxies]]
name = "${m.name}"
type = "${m.type}"
localIP = "${m.localIp}"
localPort = ${m.localPort}
`;
switch (m.type) {
case "tcp":
toml += `remotePort = ${m.remotePort}`;
break;
case "http":
case "https":
toml += `customDomains=[${m.customDomains.map(m => `"${m}"`)}]`;
break;
default:
break;
}
switch (m.type) {
case "tcp":
toml += `remotePort = ${m.remotePort}`;
break;
case "http":
case "https":
toml += `customDomains=[${m.customDomains.map(m => `"${m}"`)}]`;
break;
default:
break;
}

return toml;
});
let toml = `
return toml;
});
const toml = `
serverAddr = "${config.serverAddr}"
serverPort = ${config.serverPort}
auth.method = "${config.authMethod}"
Expand All @@ -82,17 +81,92 @@ ${config.proxyConfigEnable ? `
transport.proxyURL = "${config.proxyConfigProxyUrl}"
` : ""}
${proxyToml.join("")}
`;
return toml;
}

// const configPath = path.join("frp.toml");
const filename = "frp.toml";

/**
* 生成ini配置
* @param config
* @param proxys
*/
const genIniConfig = (config: Config, proxys: Proxy[]) => {
const proxyIni = proxys.map(m => {
let ini = `
[${m.name}]
type = "${m.type}"
local_ip = "${m.localIp}"
local_port = ${m.localPort}
`;
switch (m.type) {
case "tcp":
ini += `remote_port = ${m.remotePort}`;
break;
case "http":
case "https":
ini += `custom_domains=[${m.customDomains.map(m => `"${m}"`)}]`;
break;
default:
break;
}

return ini;
});
const ini = `
[common]
server_addr = ${config.serverAddr}
server_port = ${config.serverPort}
authentication_method = "${config.authMethod}"
auth_token = "${config.authToken}"
log_file = "frpc.log"
log_level = ${config.logLevel}
log_max_days = ${config.logMaxDays}
admin_addr = 127.0.0.1
admin_port = 57400
tls_enable = ${config.tlsConfigEnable}
${config.tlsConfigEnable ? `
tls_cert_file = ${config.tlsConfigCertFile}
tls_key_file = ${config.tlsConfigKeyFile}
tls_trusted_ca_file = ${config.tlsConfigTrustedCaFile}
tls_server_name = ${config.tlsConfigServerName}
` : ""}
${config.proxyConfigEnable ? `
http_proxy = "${config.proxyConfigProxyUrl}"
` : ""}
${proxyIni.join("")}
`
return ini;
}

/**
* 生成配置文件
*/
export const generateConfig = (
config: Config,
callback: (configPath: string) => void
) => {
listProxy((err3, proxys) => {
if (!err3) {
console.log(config, 'config')
const {currentVersion} = config;
let filename = null;
let configContent = "";
if (currentVersion < 124395282) {
// 版本小于v0.52.0
filename = "frp.ini";
configContent = genIniConfig(config, proxys)
} else {
filename = "frp.toml";
configContent = genTomlConfig(config, proxys)
}
const configPath = path.join(app.getPath("userData"), filename)
console.debug("生成配置成功", configPath)
fs.writeFile(
configPath, // 配置文件目录
toml, // 配置文件内容
configContent, // 配置文件内容
{flag: "w"},
err => {
if (!err) {
Expand All @@ -111,6 +185,7 @@ ${proxyToml.join("")}
* @param configPath
*/
const startFrpcProcess = (commandPath: string, configPath: string) => {
console.log(commandPath, 'commandP')
const command = `${commandPath} -c ${configPath}`;
console.info("启动", command)
frpcProcess = spawn(command, {
Expand All @@ -124,7 +199,8 @@ const startFrpcProcess = (commandPath: string, configPath: string) => {
});
frpcProcess.stdout.on("error", data => {
console.log("启动错误", data)
stopFrpcProcess()
stopFrpcProcess(() => {
})
});
frpcStatusListener = setInterval(() => {
const status = frpcProcessStatus()
Expand Down Expand Up @@ -196,6 +272,35 @@ export const frpcProcessStatus = () => {
}
}

/**
* 启动frpc流程
* @param config
*/
export const startFrpWorkerProcess = (config: Config) => {
getFrpcVersionWorkerPath(
config.currentVersion,
(frpcVersionPath: string) => {
console.log(1, '1')
if (frpcVersionPath) {
generateConfig(config, configPath => {
const platform = process.platform;
if (platform === 'win32') {
startFrpcProcess(
path.join(frpcVersionPath, "frpc.exe"),
configPath
);
} else {
startFrpcProcess(
path.join(frpcVersionPath, "frpc"),
configPath
);
}
});
}
}
);
}


export const initFrpcApi = () => {
ipcMain.handle("frpc.running", async (event, args) => {
Expand All @@ -206,38 +311,21 @@ export const initFrpcApi = () => {
getConfig((err1, config) => {
if (!err1) {
if (config) {
getFrpcVersionWorkerPath(
config.currentVersion,
(frpcVersionPath: string) => {
generateConfig(config, configPath => {
const platform = process.platform;
if (platform === 'win32') {
startFrpcProcess(
path.join(frpcVersionPath, "frpc.exe"),
configPath
);
} else {
startFrpcProcess(
path.join(frpcVersionPath, "frpc"),
configPath
);
}
});
}
);
startFrpWorkerProcess(config)
} else {
event.reply(
"Home.frpc.start.error.hook",
"请先前往设置页面,修改配置后再启动"
"Home.frpc.start.error.hook", "请先前往设置页面,修改配置后再启动"
);
}
}
});

});

ipcMain.on("frpc.stop", () => {
if (frpcProcess && !frpcProcess.killed) {
stopFrpcProcess()
stopFrpcProcess(() => {
})
}
});
};
22 changes: 20 additions & 2 deletions electron/api/github.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {app, BrowserWindow, ipcMain, net, shell} from "electron";
import {insertVersion} from "../storage/version";
import {deleteVersionById, insertVersion} from "../storage/version";

const fs = require("fs");
const path = require("path");
Expand Down Expand Up @@ -86,7 +86,7 @@ export const initGitHubApi = () => {
ipcMain.on("github.getFrpVersions", async event => {
const request = net.request({
method: "get",
url: "https://api.github.com/repos/fatedier/frp/releases"
url: "https://api.github.com/repos/fatedier/frp/releases?page=1&per_page=1000"
});
request.on("response", response => {
let responseData: Buffer = Buffer.alloc(0);
Expand All @@ -104,6 +104,7 @@ export const initGitHubApi = () => {
const asset = getAdaptiveAsset(m.id);
if (asset) {
const absPath = `${downloadPath}/${asset.name}`;
m.absPath = absPath;
m.download_completed = fs.existsSync(absPath);
}
return m;
Expand Down Expand Up @@ -163,6 +164,23 @@ export const initGitHubApi = () => {
});
});

/**
* 删除下载
*/
ipcMain.on("github.deleteVersion", async (event, args) => {
const {absPath, id} = args;
console.log('删除下载', args)
if (fs.existsSync(absPath)) {
deleteVersionById(id, () => {
fs.unlinkSync(absPath)
})
}
event.reply("Download.deleteVersion.hook", {
err: null,
data: "删除成功"
});
})

/**
* 打开GitHub
*/
Expand Down
13 changes: 12 additions & 1 deletion electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import node_path, {join} from "node:path";
import {initGitHubApi} from "../api/github";
import {initConfigApi} from "../api/config";
import {initProxyApi} from "../api/proxy";
import {initFrpcApi, stopFrpcProcess} from "../api/frpc";
import {initFrpcApi, startFrpWorkerProcess, stopFrpcProcess} from "../api/frpc";
import {initLoggerApi} from "../api/logger";
import {initFileApi} from "../api/file";
import {getConfig} from "../storage/config";
// The built directory structure
//
// ├─┬ dist-electron
Expand Down Expand Up @@ -135,6 +136,16 @@ export const createTray = () => {
tray.on('double-click', () => {
win.show();
})

getConfig((err, config) => {
if (!err) {
if (config) {
if (config.systemStartupConnect) {
startFrpWorkerProcess(config)
}
}
}
})
}

app.whenReady().then(() => {
Expand Down
Loading

0 comments on commit 7ab26bc

Please sign in to comment.