tambahannya
This commit is contained in:
220
bin/src/frp.ts
220
bin/src/frp.ts
@@ -6,159 +6,169 @@ import * as path from "path";
|
|||||||
const CONFIG_FILE = path.join(os.homedir(), ".frpdev.conf");
|
const CONFIG_FILE = path.join(os.homedir(), ".frpdev.conf");
|
||||||
|
|
||||||
interface FrpConfig {
|
interface FrpConfig {
|
||||||
FRP_HOST: string;
|
FRP_HOST: string;
|
||||||
FRP_PORT: string;
|
FRP_PORT: string;
|
||||||
FRP_USER: string;
|
FRP_USER: string;
|
||||||
FRP_SECRET: string;
|
FRP_SECRET: string;
|
||||||
FRP_PROTO: string;
|
FRP_PROTO: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProxyConf {
|
interface ProxyConf {
|
||||||
type?: string;
|
type?: string;
|
||||||
remotePort?: number;
|
remotePort?: number;
|
||||||
subdomain?: string;
|
subdomain?: string;
|
||||||
customDomains?: string[];
|
customDomains?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Proxy {
|
interface Proxy {
|
||||||
name?: string;
|
name?: string;
|
||||||
status?: string;
|
status?: string;
|
||||||
conf?: ProxyConf;
|
conf?: ProxyConf;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ProxyResponse {
|
interface ProxyResponse {
|
||||||
proxies?: Proxy[];
|
proxies?: Proxy[];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function ensureConfigFile(): Promise<void> {
|
async function ensureConfigFile(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await fs.access(CONFIG_FILE);
|
await fs.access(CONFIG_FILE);
|
||||||
} catch {
|
} catch {
|
||||||
const template = `
|
const template = `
|
||||||
FRP_HOST=""
|
FRP_HOST=""
|
||||||
FRP_USER=""
|
FRP_USER=""
|
||||||
FRP_SECRET=""
|
FRP_SECRET=""
|
||||||
`;
|
`;
|
||||||
console.error(`❌ Config not found. Template created at: ${CONFIG_FILE}`);
|
console.error(`❌ Config not found. Template created at: ${CONFIG_FILE}`);
|
||||||
console.log(template);
|
console.log(template);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadConfig(): Promise<FrpConfig> {
|
async function loadConfig(): Promise<FrpConfig> {
|
||||||
await ensureConfigFile();
|
await ensureConfigFile();
|
||||||
|
|
||||||
const raw = await fs.readFile(CONFIG_FILE, "utf8");
|
const raw = await fs.readFile(CONFIG_FILE, "utf8");
|
||||||
const lines = raw
|
const lines = raw
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map((line) => line.trim())
|
.map((line) => line.trim())
|
||||||
.filter(Boolean);
|
.filter(Boolean);
|
||||||
|
|
||||||
const conf: Record<string, string> = {};
|
const conf: Record<string, string> = {};
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const [key, ...rest] = line.split("=");
|
const [key, ...rest] = line.split("=");
|
||||||
if (!key) continue;
|
if (!key) continue;
|
||||||
let value = rest.join("=").trim();
|
let value = rest.join("=").trim();
|
||||||
|
|
||||||
if (value.startsWith('"') && value.endsWith('"')) {
|
if (value.startsWith('"') && value.endsWith('"')) {
|
||||||
value = value.slice(1, -1);
|
value = value.slice(1, -1);
|
||||||
|
}
|
||||||
|
conf[key] = value;
|
||||||
}
|
}
|
||||||
conf[key] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
FRP_HOST: conf.FRP_HOST || "",
|
FRP_HOST: conf.FRP_HOST || "",
|
||||||
FRP_PORT: "443",
|
FRP_PORT: "443",
|
||||||
FRP_USER: conf.FRP_USER || "",
|
FRP_USER: conf.FRP_USER || "",
|
||||||
FRP_SECRET: conf.FRP_SECRET || "",
|
FRP_SECRET: conf.FRP_SECRET || "",
|
||||||
FRP_PROTO: "https",
|
FRP_PROTO: "https",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchFrp(config: FrpConfig, url: string): Promise<ProxyResponse> {
|
async function fetchFrp(config: FrpConfig, url: string): Promise<ProxyResponse> {
|
||||||
const fullUrl = `${config.FRP_PROTO}://${config.FRP_HOST}:${config.FRP_PORT}${url}`;
|
const fullUrl = `${config.FRP_PROTO}://${config.FRP_HOST}:${config.FRP_PORT}${url}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const resp = await fetch(fullUrl, {
|
const resp = await fetch(fullUrl, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization:
|
Authorization:
|
||||||
"Basic " +
|
"Basic " +
|
||||||
Buffer.from(`${config.FRP_USER}:${config.FRP_SECRET}`).toString("base64"),
|
Buffer.from(`${config.FRP_USER}:${config.FRP_SECRET}`).toString("base64"),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!resp.ok) return { proxies: [] };
|
if (!resp.ok) return { proxies: [] };
|
||||||
|
|
||||||
return (await resp.json()) as ProxyResponse;
|
return (await resp.json()) as ProxyResponse;
|
||||||
} catch {
|
} catch {
|
||||||
return { proxies: [] };
|
return { proxies: [] };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortProxies(proxies: Proxy[]): Proxy[] {
|
function sortProxies(proxies: Proxy[]): Proxy[] {
|
||||||
return [...proxies].sort((a, b) => {
|
return [...proxies].sort((a, b) => {
|
||||||
const order = (status?: string) =>
|
// Urutan utama: status (online/running duluan)
|
||||||
status?.toLowerCase() === "online" || status?.toLowerCase() === "running"
|
const order = (status?: string) =>
|
||||||
? 0
|
status?.toLowerCase() === "online" || status?.toLowerCase() === "running"
|
||||||
: 1;
|
? 0
|
||||||
return order(a.status) - order(b.status);
|
: 1;
|
||||||
});
|
|
||||||
|
const statusDiff = order(a.status) - order(b.status);
|
||||||
|
if (statusDiff !== 0) return statusDiff;
|
||||||
|
|
||||||
|
// Jika status sama, urutkan berdasarkan remotePort numerik (ascending)
|
||||||
|
const portA = a.conf?.remotePort ?? Number.MAX_SAFE_INTEGER;
|
||||||
|
const portB = b.conf?.remotePort ?? Number.MAX_SAFE_INTEGER;
|
||||||
|
|
||||||
|
return portA - portB;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
function formatTable(headers: string[], rows: string[][]): string {
|
||||||
.map((row) =>
|
const allRows = [headers, ...rows];
|
||||||
row.map((cell, i) => (cell || "").padEnd(colWidths[i] ?? 0)).join(" ").trimEnd(),
|
const colWidths = headers.map((_, i) =>
|
||||||
)
|
Math.max(...allRows.map((row) => (row[i] || "").length)),
|
||||||
.join("\n");
|
);
|
||||||
|
|
||||||
|
return allRows
|
||||||
|
.map((row) =>
|
||||||
|
row.map((cell, i) => (cell || "").padEnd(colWidths[i] ?? 0)).join(" ").trimEnd(),
|
||||||
|
)
|
||||||
|
.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function printTable(
|
async function printTable(
|
||||||
title: string,
|
title: string,
|
||||||
headers: string[],
|
headers: string[],
|
||||||
rows: string[][],
|
rows: string[][],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
console.log(`========== ${title} ==========`);
|
console.log(`========== ${title} ==========`);
|
||||||
|
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
console.log("No proxies found.\n");
|
console.log("No proxies found.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(formatTable(headers, rows));
|
console.log(formatTable(headers, rows));
|
||||||
console.log();
|
console.log();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function frp(): Promise<void> {
|
async function frp(): Promise<void> {
|
||||||
const config = await loadConfig();
|
const config = await loadConfig();
|
||||||
|
|
||||||
const [tcpResp, httpResp] = await Promise.all([
|
const [tcpResp, httpResp] = await Promise.all([
|
||||||
fetchFrp(config, "/api/proxy/tcp"),
|
fetchFrp(config, "/api/proxy/tcp"),
|
||||||
fetchFrp(config, "/api/proxy/http"),
|
fetchFrp(config, "/api/proxy/http"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const tcpRows: string[][] = sortProxies(tcpResp.proxies || []).map((p) => [
|
const tcpRows: string[][] = sortProxies(tcpResp.proxies || []).map((p) => [
|
||||||
p.name ?? "-",
|
p.name ?? "-",
|
||||||
p.status ?? "-",
|
p.status ?? "-",
|
||||||
p.conf?.remotePort?.toString() ?? "-",
|
p.conf?.remotePort?.toString() ?? "-",
|
||||||
]);
|
]);
|
||||||
await printTable("TCP PROXIES", ["NAME", "STATUS", "PORT"], tcpRows);
|
await printTable("TCP PROXIES", ["NAME", "STATUS", "PORT"], tcpRows);
|
||||||
|
|
||||||
const httpRows: string[][] = sortProxies(httpResp.proxies || []).map((p) => [
|
const httpRows: string[][] = sortProxies(httpResp.proxies || []).map((p) => [
|
||||||
p.name ?? "-",
|
p.name ?? "-",
|
||||||
p.status ?? "-",
|
p.status ?? "-",
|
||||||
Array.isArray(p.conf?.customDomains) ? p.conf.customDomains.join(",") : "",
|
Array.isArray(p.conf?.customDomains) ? p.conf.customDomains.join(",") : "",
|
||||||
]);
|
]);
|
||||||
await printTable(
|
await printTable(
|
||||||
"HTTP PROXIES",
|
"HTTP PROXIES",
|
||||||
["NAME", "STATUS", "CUSTOM_DOMAIN"],
|
["NAME", "STATUS", "CUSTOM_DOMAIN"],
|
||||||
httpRows,
|
httpRows,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default frp;
|
export default frp;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "g3n",
|
"name": "g3n",
|
||||||
"version": "1.0.8",
|
"version": "1.0.9",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"bin": {
|
"bin": {
|
||||||
"g3n": "./bin/g3n.ts"
|
"g3n": "./bin/g3n.ts"
|
||||||
|
|||||||
Reference in New Issue
Block a user