2022-07-29 10:37:20 +00:00
|
|
|
export function isIp(ip: string) {
|
|
|
|
return /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(
|
2023-07-17 15:39:16 +00:00
|
|
|
ip,
|
2022-07-29 10:37:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isDomain(domain: string) {
|
|
|
|
return /(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]/.test(
|
2023-07-17 15:39:16 +00:00
|
|
|
domain,
|
2022-07-29 10:37:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function normalizeDomain(domain: string): string {
|
|
|
|
return domain.replace(/^\.+|\.+$/g, "").replace(/^\/+|\/+$/g, "");
|
|
|
|
}
|
|
|
|
|
2022-08-14 12:19:45 +00:00
|
|
|
export async function getRelayProxies() {
|
2023-04-03 17:29:20 +00:00
|
|
|
//let relays: string[] = await relayDht.getRelayServers();
|
|
|
|
let proxies = [
|
|
|
|
{ type: "http", host: "localhost", port: 25252 },
|
2023-07-31 16:52:50 +00:00
|
|
|
{ type: "http", host: "example.com", port: 80 },
|
2023-04-03 17:29:20 +00:00
|
|
|
];
|
|
|
|
/*
|
2022-08-14 12:19:45 +00:00
|
|
|
for (const relay of relays) {
|
|
|
|
proxies.push({ type: "http", host: new URL(relay).hostname, port: 25252 });
|
2023-04-03 17:29:20 +00:00
|
|
|
}*/
|
2022-08-14 12:19:45 +00:00
|
|
|
|
|
|
|
return proxies;
|
|
|
|
}
|
|
|
|
|
2022-07-29 10:37:20 +00:00
|
|
|
export const requestProxies = [
|
|
|
|
{ type: "http", host: "localhost", port: 25252 },
|
|
|
|
{ type: "http", host: "web3portal.com", port: 80 },
|
|
|
|
];
|
2022-07-31 04:56:54 +00:00
|
|
|
|
|
|
|
export function getTld(hostname: string): string {
|
|
|
|
return hostname.includes(".")
|
|
|
|
? hostname.split(".")[hostname.split(".").length - 1]
|
|
|
|
: hostname;
|
|
|
|
}
|
2022-09-08 11:28:31 +00:00
|
|
|
export async function* iterateStream(
|
2023-07-17 15:39:16 +00:00
|
|
|
stream: ReadableStream<any>,
|
2022-09-08 11:28:31 +00:00
|
|
|
): AsyncGenerator<Uint8Array> {
|
|
|
|
let chunk;
|
|
|
|
const reader = stream.getReader();
|
|
|
|
do {
|
|
|
|
chunk = await reader.read();
|
|
|
|
if (chunk.value) {
|
|
|
|
yield chunk.value;
|
|
|
|
}
|
|
|
|
} while (!chunk.done);
|
|
|
|
|
|
|
|
reader.releaseLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function streamToArray(
|
2023-07-17 15:39:16 +00:00
|
|
|
stream: ReadableStream<Uint8Array>,
|
2022-09-08 11:28:31 +00:00
|
|
|
): Promise<Uint8Array> {
|
|
|
|
let buffer = new Uint8Array();
|
|
|
|
|
|
|
|
for await (const chunk of iterateStream(stream)) {
|
|
|
|
buffer = Uint8Array.from([...buffer, ...chunk]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|