mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 12:47:39 +00:00
refactor(net): share hostname normalization
This commit is contained in:
7
src/infra/net/hostname.ts
Normal file
7
src/infra/net/hostname.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export function normalizeHostname(hostname: string): string {
|
||||||
|
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
|
||||||
|
if (normalized.startsWith("[") && normalized.endsWith("]")) {
|
||||||
|
return normalized.slice(1, -1);
|
||||||
|
}
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { lookup as dnsLookupCb, type LookupAddress } from "node:dns";
|
import { lookup as dnsLookupCb, type LookupAddress } from "node:dns";
|
||||||
import { lookup as dnsLookup } from "node:dns/promises";
|
import { lookup as dnsLookup } from "node:dns/promises";
|
||||||
import { Agent, type Dispatcher } from "undici";
|
import { Agent, type Dispatcher } from "undici";
|
||||||
|
import { normalizeHostname } from "./hostname.js";
|
||||||
|
|
||||||
type LookupCallback = (
|
type LookupCallback = (
|
||||||
err: NodeJS.ErrnoException | null,
|
err: NodeJS.ErrnoException | null,
|
||||||
@@ -25,14 +26,6 @@ export type SsrFPolicy = {
|
|||||||
|
|
||||||
const BLOCKED_HOSTNAMES = new Set(["localhost", "metadata.google.internal"]);
|
const BLOCKED_HOSTNAMES = new Set(["localhost", "metadata.google.internal"]);
|
||||||
|
|
||||||
function normalizeHostname(hostname: string): string {
|
|
||||||
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
|
|
||||||
if (normalized.startsWith("[") && normalized.endsWith("]")) {
|
|
||||||
return normalized.slice(1, -1);
|
|
||||||
}
|
|
||||||
return normalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeHostnameSet(values?: string[]): Set<string> {
|
function normalizeHostnameSet(values?: string[]): Set<string> {
|
||||||
if (!values || values.length === 0) {
|
if (!values || values.length === 0) {
|
||||||
return new Set<string>();
|
return new Set<string>();
|
||||||
|
|||||||
@@ -1,17 +1,10 @@
|
|||||||
import type { WebClient as SlackWebClient } from "@slack/web-api";
|
import type { WebClient as SlackWebClient } from "@slack/web-api";
|
||||||
import type { FetchLike } from "../../media/fetch.js";
|
import type { FetchLike } from "../../media/fetch.js";
|
||||||
import type { SlackFile } from "../types.js";
|
import type { SlackFile } from "../types.js";
|
||||||
|
import { normalizeHostname } from "../../infra/net/hostname.js";
|
||||||
import { fetchRemoteMedia } from "../../media/fetch.js";
|
import { fetchRemoteMedia } from "../../media/fetch.js";
|
||||||
import { saveMediaBuffer } from "../../media/store.js";
|
import { saveMediaBuffer } from "../../media/store.js";
|
||||||
|
|
||||||
function normalizeHostname(hostname: string): string {
|
|
||||||
const normalized = hostname.trim().toLowerCase().replace(/\.$/, "");
|
|
||||||
if (normalized.startsWith("[") && normalized.endsWith("]")) {
|
|
||||||
return normalized.slice(1, -1);
|
|
||||||
}
|
|
||||||
return normalized;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isSlackHostname(hostname: string): boolean {
|
function isSlackHostname(hostname: string): boolean {
|
||||||
const normalized = normalizeHostname(hostname);
|
const normalized = normalizeHostname(hostname);
|
||||||
if (!normalized) {
|
if (!normalized) {
|
||||||
|
|||||||
Reference in New Issue
Block a user