Created
February 4, 2025 00:06
-
-
Save maxktz/b4e2f39d27ec84c87b0b2c8cbc2357bb to your computer and use it in GitHub Desktop.
Map Proxy String to GramJS ProxyInterface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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