style: update ui to accept proxy separately

This commit is contained in:
zhom
2025-06-09 23:19:34 +04:00
parent ac293f6204
commit c4b1745a0f
7 changed files with 191 additions and 55 deletions
+41 -13
View File
@@ -11,10 +11,15 @@ import { runProxyWorker } from "./proxy-worker";
program
.command("proxy")
.argument("<action>", "start, stop, or list proxies")
.option("-h, --host <host>", "upstream proxy host")
.option("-P, --proxy-port <port>", "upstream proxy port", Number.parseInt)
.option(
"-u, --upstream <url>",
"upstream proxy URL (protocol://[username:password@]host:port)"
"-t, --type <type>",
"upstream proxy type (http, https, socks4, socks5)",
"http"
)
.option("-u, --username <username>", "upstream proxy username")
.option("-w, --password <password>", "upstream proxy password")
.option(
"-p, --port <number>",
"local port to use (random if not specified)",
@@ -27,23 +32,35 @@ program
async (
action: string,
options: {
upstream?: string;
host?: string;
proxyPort?: number;
type?: string;
username?: string;
password?: string;
port?: number;
ignoreCertificate?: boolean;
id?: string;
}
) => {
if (action === "start") {
if (!options.upstream) {
console.error("Error: Upstream proxy URL is required");
if (!options.host || !options.proxyPort) {
console.error("Error: Upstream proxy host and port are required");
console.log(
"Example: proxy start -u http://username:password@proxy.example.com:8080"
"Example: proxy start -h proxy.example.com -P 8080 -t http -u username -w password"
);
return;
}
try {
const config = await startProxyProcess(options.upstream, {
// Construct the upstream URL with credentials if provided
let upstreamProxyUrl: string;
if (options.username && options.password) {
upstreamProxyUrl = `${options.type}://${options.username}:${options.password}@${options.host}:${options.proxyPort}`;
} else {
upstreamProxyUrl = `${options.type}://${options.host}:${options.proxyPort}`;
}
const config = await startProxyProcess(upstreamProxyUrl, {
port: options.port,
ignoreProxyCertificate: options.ignoreCertificate,
});
@@ -56,14 +73,25 @@ program
const stopped = await stopProxyProcess(options.id);
console.log(`{
"success": ${stopped}}`);
} else if (options.upstream) {
// Find proxies with this upstream URL
const configs = listProxyConfigs().filter(
(config) => config.upstreamUrl === options.upstream
);
} else if (options.host && options.proxyPort && options.type) {
// Find proxies with matching upstream details
const configs = listProxyConfigs().filter((config) => {
try {
const url = new URL(config.upstreamUrl);
return (
url.hostname === options.host &&
Number.parseInt(url.port) === options.proxyPort &&
url.protocol.replace(":", "") === options.type
);
} catch {
return false;
}
});
if (configs.length === 0) {
console.error(`No proxies found for ${options.upstream}`);
console.error(
`No proxies found for ${options.host}:${options.proxyPort}`
);
return;
}
+49 -19
View File
@@ -1,7 +1,7 @@
import {
startProxyProcess,
stopProxyProcess,
stopAllProxyProcesses
import {
startProxyProcess,
stopProxyProcess,
stopAllProxyProcesses,
} from "./proxy-runner";
import { listProxyConfigs } from "./proxy-storage";
@@ -9,41 +9,71 @@ import { listProxyConfigs } from "./proxy-storage";
interface ProxyOptions {
port?: number;
ignoreProxyCertificate?: boolean;
username?: string;
password?: string;
}
/**
* Start a local proxy server that forwards to an upstream proxy
* @param upstreamProxyUrl The upstream proxy URL (protocol://[username:password@]host:port)
* @param options Optional configuration
* @param upstreamProxyHost The upstream proxy host
* @param upstreamProxyPort The upstream proxy port
* @param upstreamProxyType The upstream proxy type (http, https, socks4, socks5)
* @param options Optional configuration including credentials
* @returns Promise resolving to the local proxy URL
*/
export async function startProxy(
upstreamProxyUrl: string,
upstreamProxyHost: string,
upstreamProxyPort: number,
upstreamProxyType: string,
options: ProxyOptions = {}
): Promise<string> {
// Construct the upstream proxy URL with credentials if provided
let upstreamProxyUrl: string;
if (options.username && options.password) {
upstreamProxyUrl = `${upstreamProxyType}://${options.username}:${options.password}@${upstreamProxyHost}:${upstreamProxyPort}`;
} else {
upstreamProxyUrl = `${upstreamProxyType}://${upstreamProxyHost}:${upstreamProxyPort}`;
}
const config = await startProxyProcess(upstreamProxyUrl, {
port: options.port,
ignoreProxyCertificate: options.ignoreProxyCertificate,
});
return config.localUrl || `http://localhost:${config.localPort}`;
}
/**
* Stop a specific proxy by its upstream URL
* @param upstreamProxyUrl The upstream proxy URL to stop
* Stop a specific proxy by its upstream host, port, and type
* @param upstreamProxyHost The upstream proxy host
* @param upstreamProxyPort The upstream proxy port
* @param upstreamProxyType The upstream proxy type
* @returns Promise resolving to true if proxy was found and stopped, false otherwise
*/
export async function stopProxy(upstreamProxyUrl: string): Promise<boolean> {
// Find all proxies with this upstream URL
const configs = listProxyConfigs().filter(
config => config.upstreamUrl === upstreamProxyUrl
);
export async function stopProxy(
upstreamProxyHost: string,
upstreamProxyPort: number,
upstreamProxyType: string
): Promise<boolean> {
// Find all proxies with matching upstream details (ignoring credentials in URL)
const configs = listProxyConfigs().filter((config) => {
// Parse the upstream URL to extract host, port, and type
try {
const url = new URL(config.upstreamUrl);
return (
url.hostname === upstreamProxyHost &&
Number.parseInt(url.port) === upstreamProxyPort &&
url.protocol.replace(":", "") === upstreamProxyType
);
} catch {
return false;
}
});
if (configs.length === 0) {
return false;
}
// Stop all matching proxies
let success = true;
for (const config of configs) {
@@ -52,7 +82,7 @@ export async function stopProxy(upstreamProxyUrl: string): Promise<boolean> {
success = false;
}
}
return success;
}
@@ -61,7 +91,7 @@ export async function stopProxy(upstreamProxyUrl: string): Promise<boolean> {
* @returns Array of upstream proxy URLs
*/
export function getActiveProxies(): string[] {
return listProxyConfigs().map(config => config.upstreamUrl);
return listProxyConfigs().map((config) => config.upstreamUrl);
}
/**