Skip to content

Instantly share code, notes, and snippets.

@maxktz
Created February 4, 2025 00:06
Show Gist options
  • Save maxktz/b4e2f39d27ec84c87b0b2c8cbc2357bb to your computer and use it in GitHub Desktop.
Save maxktz/b4e2f39d27ec84c87b0b2c8cbc2357bb to your computer and use it in GitHub Desktop.
Map Proxy String to GramJS ProxyInterface
import type { ProxyInterface } from "telegram/network/connection/TCPMTProxy";
export function mapTelegramProxyString(
proxy?: string
): ProxyInterface | undefined {
const { protocol, hostname, port, username, password } = proxy
? new URL(proxy!)
: {};
if (proxy) {
// validate
if (!protocol || !hostname || !port)
throw new Error("Invalid PROXY format");
if (protocol !== "socks4:" && protocol !== "socks5:")
throw new Error(
"Invalid PROXY format, only socks4 and socks5 protocols are supported"
);
// map
return {
socksType: protocol === "socks5:" ? 5 : 4,
ip: hostname,
port: Number(port),
password: password || undefined,
username: username || undefined,
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment