84 lines
6.9 KiB
TypeScript
84 lines
6.9 KiB
TypeScript
import { lookup } from 'node:dns/promises'
|
|
import { request as httpsRequest } from 'node:https'
|
|
import { isIP } from 'node:net'
|
|
import { Readable } from 'node:stream'
|
|
import { isPublicRoutableIp } from './proxy-media-security'
|
|
import { AiModelError } from './ai-model-access'
|
|
|
|
export function normalizeAiModelBaseUrl(value: string): string {
|
|
let url: URL
|
|
try { url = new URL(value.trim()) } catch { throw new AiModelError('请输入有效的模型服务地址') }
|
|
if (url.protocol !== 'https:' || (url.port && url.port !== '443') || url.username || url.password || url.search || url.hash || url.hostname.endsWith('.') || /%|\\/.test(value)) throw new AiModelError('模型服务须使用无账号、查询参数的 HTTPS 标准端口地址')
|
|
const hostname = url.hostname.replace(/^\[|\]$/g, '').toLowerCase()
|
|
if (hostname === 'localhost' || hostname.endsWith('.localhost') || hostname.endsWith('.local') || hostname.endsWith('.internal') || !hostname.includes('.') && !isIP(hostname) || isIP(hostname) && !isPublicRoutableIp(hostname)) throw new AiModelError('模型服务不能使用本机或内网地址', 400, 'unsafe_provider_url')
|
|
if (!/^\/(?:[a-zA-Z0-9._~-]+\/?)*$/.test(url.pathname) || url.pathname.split('/').some(part => part === '.' || part === '..')) throw new AiModelError('模型服务路径无效')
|
|
return url.href.replace(/\/+$/, '').replace(/\/v1$/, '') + '/v1'
|
|
}
|
|
|
|
export type AiModelResolver = (hostname: string) => Promise<Array<{ address: string; family: number }>>
|
|
const resolveAddresses: AiModelResolver = async hostname => isIP(hostname) ? [{ address: hostname, family: isIP(hostname) }] : lookup(hostname, { all: true, verbatim: true })
|
|
|
|
export async function resolveAiModelPublicAddresses(baseUrl: string, resolver: AiModelResolver = resolveAddresses): Promise<Array<{ address: string; family: number }>> {
|
|
const host = new URL(normalizeAiModelBaseUrl(baseUrl)).hostname.replace(/^\[|\]$/g, '')
|
|
let addresses: Array<{ address: string; family: number }>
|
|
let timeout: ReturnType<typeof setTimeout> | undefined
|
|
try {
|
|
addresses = await Promise.race([resolver(host), new Promise<never>((_resolve, reject) => { timeout = setTimeout(() => reject(new Error('DNS timeout')), 10000) })])
|
|
} catch { throw new AiModelError('模型服务域名无法解析', 502, 'provider_dns_failed') } finally { if (timeout) clearTimeout(timeout) }
|
|
if (!addresses.length || addresses.some(value => !isPublicRoutableIp(value.address) || isIP(value.address) !== value.family)) throw new AiModelError('模型服务解析到了受保护地址', 400, 'unsafe_provider_url')
|
|
return addresses
|
|
}
|
|
|
|
/** Fixed DNS lookup, original hostname TLS verification, no redirects and bounded streamed response. */
|
|
export async function requestAiModel(baseUrl: string, apiKey: string, endpoint: '/models' | '/chat/completions' | '/images/edits', body?: string | FormData, idempotencyKey?: string, beforeRequest?: () => Promise<void>): Promise<Response> {
|
|
const normalized = normalizeAiModelBaseUrl(baseUrl)
|
|
return requestAiModelUrl(normalized, new URL(normalized + endpoint), { Authorization: `Bearer ${apiKey}`, ...(idempotencyKey ? { 'Idempotency-Key': idempotencyKey } : {}) }, endpoint === '/models', body, beforeRequest)
|
|
}
|
|
|
|
/** This provider publishes a separate typed directory. Its public request never carries the private API key. */
|
|
export async function requestAiModelCapabilityCatalog(baseUrl: string, beforeRequest?: () => Promise<void>): Promise<Response> {
|
|
const normalized = normalizeAiModelBaseUrl(baseUrl)
|
|
if (normalized !== 'https://api.vip98k.com/v1') throw new AiModelError('该上游没有已适配的独立能力目录', 400, 'capability_catalog_unavailable')
|
|
return requestAiModelUrl(normalized, new URL('https://api.vip98k.com/api/v2/catalog/models'), {}, true, undefined, beforeRequest)
|
|
}
|
|
|
|
async function requestAiModelUrl(normalized: string, target: URL, headers: Record<string, string>, catalog: boolean, body?: string | FormData, beforeRequest?: () => Promise<void>): Promise<Response> {
|
|
const addresses = await resolveAiModelPublicAddresses(normalized)
|
|
let requestBody: Readable | null = null
|
|
if (typeof body === 'string') { headers['Content-Type'] = 'application/json'; headers['Content-Length'] = String(Buffer.byteLength(body)); requestBody = Readable.from([body]) }
|
|
else if (body) {
|
|
const formRequest = new Request(target, { method: 'POST', body })
|
|
headers['Content-Type'] = formRequest.headers.get('content-type')!
|
|
requestBody = Readable.fromWeb(formRequest.body as any)
|
|
}
|
|
if (beforeRequest) await beforeRequest()
|
|
return new Promise<Response>((resolve, reject) => {
|
|
let complete = false
|
|
const finish = (error: unknown, response?: Response) => { if (complete) return; complete = true; clearTimeout(timer); error ? reject(error) : resolve(response!) }
|
|
const req = httpsRequest(target, {
|
|
method: body ? 'POST' : 'GET', headers, agent: false,
|
|
lookup: ((_hostname: string, options: any, callback: any) => {
|
|
const selected = addresses.filter(address => !options?.family || address.family === options.family)
|
|
if (!selected.length) return callback(new Error('Validated address unavailable'))
|
|
if (options?.all) callback(null, selected)
|
|
else callback(null, selected[0].address, selected[0].family)
|
|
}) as any,
|
|
}, res => {
|
|
if ((res.statusCode || 0) >= 300 && (res.statusCode || 0) < 400) { res.destroy(); finish(new AiModelError('模型服务重定向已拒绝,请配置最终服务地址', 502, 'provider_redirect_rejected')); return }
|
|
const limit = catalog ? (target.pathname === '/api/v2/catalog/models' ? 8 : 2) * 1024 * 1024 : 32 * 1024 * 1024
|
|
const chunks: Buffer[] = []; let size = 0
|
|
if (Number(res.headers['content-length'] || 0) > limit) { res.destroy(); finish(new Error('Provider response exceeds limit')); return }
|
|
res.on('data', (chunk: Buffer) => { size += chunk.length; if (size > limit) { res.destroy(); finish(new Error('Provider response exceeds limit')) } else chunks.push(chunk) })
|
|
res.on('error', () => finish(new Error('Provider response interrupted')))
|
|
res.on('end', () => {
|
|
const responseHeaders = new Headers()
|
|
for (const name of ['content-type', 'x-request-id']) { const value = res.headers[name]; if (typeof value === 'string') responseHeaders.set(name, value) }
|
|
try { finish(null, new Response([204, 205, 304].includes(res.statusCode || 0) ? null : new Uint8Array(Buffer.concat(chunks)), { status: res.statusCode || 502, headers: responseHeaders })) } catch { finish(new Error('Provider response invalid')) }
|
|
})
|
|
})
|
|
const timer = setTimeout(() => { req.destroy(); finish(new Error('Provider request timed out')) }, catalog ? 20_000 : 180_000)
|
|
req.on('error', () => finish(new Error('Provider request failed')))
|
|
if (requestBody) { requestBody.on('error', () => { req.destroy(); finish(new Error('Provider request body failed')) }); requestBody.pipe(req) } else req.end()
|
|
})
|
|
}
|