import {z} from 'zod' export type ProxyConnectionType = 'none' | 'http' | 'socks5' | 'ssh'; export type Server = {host: string, port: number, id?: (string | null) | undefined, secret?: (string | null) | undefined}; export type ProxyChoice = {value: ProxyConnectionType, extra?: (Server | null) | undefined}; export type TestProxyRequest = {proxy: ProxyChoice}; export type CreateApiGeneralTestProxyArgs = {body: TestProxyRequest}; export type CreateApiGeneralTestProxyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const proxyTestResultStatus = z.enum(["success", "partial_success", "failure"]); export const proxyValidationStep = z.enum(["ssh_connection", "direct_ping", "direct_https", "proxy_ipv4", "proxy_ipv6", "proxy_website", "proxy_location"]); export const proxyTestStepResponse = z.object({step: proxyValidationStep, name: z.string(), successful: z.boolean(), comment: z.string().nullable().optional()}); export const testProxyResponse = z.object({result: proxyTestResultStatus, steps: z.array(proxyTestStepResponse)}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Tests the given proxy connection and returns every individual test step together with an overall summary. The summary is 'success' when * every step passes, 'partial_success' when the website could be reached through at least one of IPv4, IPv6 or DNS name and the IP location * lookup also succeeded, otherwise 'failure'. * * POST /general/test-proxy */ export const createApiGeneralTestProxy = async (args: CreateApiGeneralTestProxyArgs, options?: CreateApiGeneralTestProxyOptions) => { const {baseUrl = "http://localhost:5050", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/general/test-proxy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(testProxyResponse, await res.json()) };