73 lines
2.0 KiB
JavaScript
73 lines
2.0 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
const test = require("node:test");
|
|
|
|
const { createUniqueProxyName, parseFrpConfig, renderParsedFrpConfig } = require("../src/lib/frp-config");
|
|
|
|
test("parses and renders frp proxy sections", () => {
|
|
const parsed = parseFrpConfig(`server_addr = "81.70.134.9"
|
|
server_port = 15443
|
|
token = "secret"
|
|
log_file = "/var/log/frpc.log"
|
|
|
|
[ssh]
|
|
type = "tcp"
|
|
local_ip = "127.0.0.1"
|
|
local_port = 22
|
|
remote_port = 17227
|
|
`);
|
|
|
|
assert.equal(parsed.sections.get("ssh").values.local_port, 22);
|
|
assert.equal(parsed.sections.get("ssh").values.remote_port, 17227);
|
|
|
|
parsed.sections.set("mysql", {
|
|
name: "mysql",
|
|
values: {
|
|
type: "tcp",
|
|
local_ip: "127.0.0.1",
|
|
local_port: 3306,
|
|
remote_port: 33061,
|
|
},
|
|
});
|
|
|
|
const rendered = renderParsedFrpConfig(parsed);
|
|
|
|
assert.match(rendered, /serverAddr = "81.70.134.9"/);
|
|
assert.match(rendered, /serverPort = 15443/);
|
|
assert.match(rendered, /auth.token = "secret"/);
|
|
assert.match(rendered, /log.to = "\/var\/log\/frpc.log"/);
|
|
assert.match(rendered, /\[\[proxies\]\]/);
|
|
assert.match(rendered, /name = "ssh"/);
|
|
assert.match(rendered, /localPort = 22/);
|
|
assert.match(rendered, /name = "mysql"/);
|
|
assert.match(rendered, /remotePort = 33061/);
|
|
assert.doesNotMatch(rendered, /log_file/);
|
|
});
|
|
|
|
test("parses modern frp proxy arrays", () => {
|
|
const parsed = parseFrpConfig(`serverAddr = "81.70.134.9"
|
|
serverPort = 15443
|
|
auth.token = "secret"
|
|
|
|
[[proxies]]
|
|
name = "ssh"
|
|
type = "tcp"
|
|
localIP = "127.0.0.1"
|
|
localPort = 22
|
|
remotePort = 17227
|
|
`);
|
|
|
|
assert.equal(parsed.sections.get("ssh").values.local_ip, "127.0.0.1");
|
|
assert.equal(parsed.sections.get("ssh").values.local_port, 22);
|
|
assert.equal(parsed.sections.get("ssh").values.remote_port, 17227);
|
|
});
|
|
|
|
test("generates unique proxy names with random suffixes", () => {
|
|
const sections = new Map();
|
|
const name = createUniqueProxyName("ssh", sections);
|
|
|
|
assert.match(name, /^ssh-[a-f0-9]{8}$/);
|
|
assert.notEqual(name, "ssh");
|
|
});
|