168 lines
3.8 KiB
TypeScript
168 lines
3.8 KiB
TypeScript
#!/usr/bin/env bun
|
|
import { promises as fs } from "fs";
|
|
import * as os from "os";
|
|
import * as path from "path";
|
|
|
|
const CONFIG_FILE = path.join(os.homedir(), ".frpdev.conf");
|
|
|
|
interface FrpConfig {
|
|
FRP_HOST: string;
|
|
FRP_PORT: string;
|
|
FRP_USER: string;
|
|
FRP_SECRET: string;
|
|
FRP_PROTO: string;
|
|
}
|
|
|
|
interface ProxyConf {
|
|
type?: string;
|
|
remotePort?: number;
|
|
subdomain?: string;
|
|
customDomains?: string[];
|
|
}
|
|
|
|
interface Proxy {
|
|
name?: string;
|
|
status?: string;
|
|
conf?: ProxyConf;
|
|
}
|
|
|
|
interface ProxyResponse {
|
|
proxies?: Proxy[];
|
|
}
|
|
|
|
async function ensureConfigFile(): Promise<void> {
|
|
try {
|
|
await fs.access(CONFIG_FILE);
|
|
} catch {
|
|
const template = `
|
|
FRP_HOST=""
|
|
FRP_USER=""
|
|
FRP_SECRET=""
|
|
`;
|
|
console.error(`❌ Config not found. Template created at: ${CONFIG_FILE}`);
|
|
console.log(template);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
async function loadConfig(): Promise<FrpConfig> {
|
|
await ensureConfigFile();
|
|
|
|
const raw = await fs.readFile(CONFIG_FILE, "utf8");
|
|
const lines = raw
|
|
.split("\n")
|
|
.map((line) => line.trim())
|
|
.filter(Boolean);
|
|
|
|
const conf: Record<string, string> = {};
|
|
for (const line of lines) {
|
|
const [key, ...rest] = line.split("=");
|
|
if (!key) continue;
|
|
let value = rest.join("=").trim();
|
|
|
|
if (value.startsWith('"') && value.endsWith('"')) {
|
|
value = value.slice(1, -1);
|
|
}
|
|
conf[key] = value;
|
|
}
|
|
|
|
return {
|
|
FRP_HOST: conf.FRP_HOST || "",
|
|
FRP_PORT: "443",
|
|
FRP_USER: conf.FRP_USER || "",
|
|
FRP_SECRET: conf.FRP_SECRET || "",
|
|
FRP_PROTO: "https",
|
|
};
|
|
}
|
|
|
|
async function fetchFrp(config: FrpConfig, url: string): Promise<ProxyResponse> {
|
|
const fullUrl = `${config.FRP_PROTO}://${config.FRP_HOST}:${config.FRP_PORT}${url}`;
|
|
|
|
try {
|
|
const resp = await fetch(fullUrl, {
|
|
headers: {
|
|
Authorization:
|
|
"Basic " +
|
|
Buffer.from(`${config.FRP_USER}:${config.FRP_SECRET}`).toString("base64"),
|
|
},
|
|
});
|
|
|
|
if (!resp.ok) return { proxies: [] };
|
|
|
|
return (await resp.json()) as ProxyResponse;
|
|
} catch {
|
|
return { proxies: [] };
|
|
}
|
|
}
|
|
|
|
function sortProxies(proxies: Proxy[]): Proxy[] {
|
|
return [...proxies].sort((a, b) => {
|
|
const order = (status?: string) =>
|
|
status?.toLowerCase() === "online" || status?.toLowerCase() === "running"
|
|
? 0
|
|
: 1;
|
|
return order(a.status) - order(b.status);
|
|
});
|
|
}
|
|
|
|
function formatTable(headers: string[], rows: string[][]): string {
|
|
const allRows = [headers, ...rows];
|
|
const colWidths = headers.map((_, i) =>
|
|
Math.max(...allRows.map((row) => (row[i] || "").length)),
|
|
);
|
|
|
|
return allRows
|
|
.map((row) =>
|
|
row.map((cell, i) => (cell || "").padEnd(colWidths[i] ?? 0)).join(" ").trimEnd(),
|
|
)
|
|
.join("\n");
|
|
}
|
|
|
|
async function printTable(
|
|
title: string,
|
|
headers: string[],
|
|
rows: string[][],
|
|
): Promise<void> {
|
|
console.log(`========== ${title} ==========`);
|
|
|
|
if (rows.length === 0) {
|
|
console.log("No proxies found.\n");
|
|
return;
|
|
}
|
|
|
|
console.log(formatTable(headers, rows));
|
|
console.log();
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const config = await loadConfig();
|
|
|
|
const [tcpResp, httpResp] = await Promise.all([
|
|
fetchFrp(config, "/api/proxy/tcp"),
|
|
fetchFrp(config, "/api/proxy/http"),
|
|
]);
|
|
|
|
const tcpRows: string[][] = sortProxies(tcpResp.proxies || []).map((p) => [
|
|
p.name ?? "-",
|
|
p.status ?? "-",
|
|
p.conf?.remotePort?.toString() ?? "-",
|
|
]);
|
|
await printTable("TCP PROXIES", ["NAME", "STATUS", "PORT"], tcpRows);
|
|
|
|
const httpRows: string[][] = sortProxies(httpResp.proxies || []).map((p) => [
|
|
p.name ?? "-",
|
|
p.status ?? "-",
|
|
Array.isArray(p.conf?.customDomains) ? p.conf.customDomains.join(",") : "",
|
|
]);
|
|
await printTable(
|
|
"HTTP PROXIES",
|
|
["NAME", "STATUS", "CUSTOM_DOMAIN"],
|
|
httpRows,
|
|
);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error("❌ Error:", err);
|
|
process.exit(1);
|
|
});
|