update:
This commit is contained in:
23
src/cli.js
23
src/cli.js
@@ -3,6 +3,7 @@
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { randomBytes } = require("node:crypto");
|
||||
const { spawnSync } = require("node:child_process");
|
||||
|
||||
const DEFAULT_FRP_VERSION = "0.58.1";
|
||||
@@ -267,8 +268,9 @@ function addFrpProxy(name, flags, runner) {
|
||||
}
|
||||
|
||||
const config = parseFrpConfig(fs.readFileSync(configPath, "utf8"));
|
||||
config.sections.set(name, {
|
||||
name,
|
||||
const proxyName = createUniqueProxyName(name, config.sections);
|
||||
config.sections.set(proxyName, {
|
||||
name: proxyName,
|
||||
values: {
|
||||
type: stringFlag(flags, "type", "tcp"),
|
||||
local_ip: stringFlag(flags, "local-ip", "127.0.0.1"),
|
||||
@@ -277,10 +279,26 @@ function addFrpProxy(name, flags, runner) {
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`Added frp proxy ${proxyName}`);
|
||||
writeFile(configPath, renderParsedFrpConfig(config), runner);
|
||||
restartServiceIfRequested(flags, runner);
|
||||
}
|
||||
|
||||
function createUniqueProxyName(baseName, sections) {
|
||||
for (let attempt = 0; attempt < 10; attempt += 1) {
|
||||
const name = `${baseName}-${randomString(8)}`;
|
||||
if (!sections.has(name)) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`Could not generate a unique proxy name for ${baseName}.`);
|
||||
}
|
||||
|
||||
function randomString(length) {
|
||||
return randomBytes(Math.ceil(length / 2)).toString("hex").slice(0, length);
|
||||
}
|
||||
|
||||
function removeFrpProxy(name, flags, runner) {
|
||||
if (!name) {
|
||||
throw new Error("frp remove requires a proxy name.");
|
||||
@@ -705,6 +723,7 @@ function fail(message) {
|
||||
|
||||
module.exports = {
|
||||
main,
|
||||
createUniqueProxyName,
|
||||
parseArgs,
|
||||
parseFrpConfig,
|
||||
renderParsedFrpConfig,
|
||||
|
||||
Reference in New Issue
Block a user