import type { ConcreteNetwork, GeoTarget } from "@fetcha/core"; import { executeHttp } from "./http"; import type { ProviderHealth, ProviderRequest, ProviderResponse, ProxyEndpoint, ProxyProvider } from "./types"; import { probeHealth } from "./probe"; /** * SOAX residential / mobile network. Gateway `proxy.soax.com:5000`. * Username grammar: `package-[-country-xx][-region-xxx][-city-xxx][-sessionid-][-sessionlength-]`. * Documentation: https://helpcenter.soax.com/ * * The adapter is complete but the platform only routes to it once credentials are present * (`SOAX_USERNAME` / `SOAX_PASSWORD`). Until then `isConfigured()` is false and the admin * dashboard shows it as "unconfigured" — we never pretend a route exists. */ export class SoaxProvider implements ProxyProvider { readonly id = "soax" as const; readonly label = "SOAX"; readonly networks: ConcreteNetwork[] = ["residential", "mobile"]; private readonly username: string | undefined; private readonly password: string | undefined; private readonly prices: Record; constructor(env: NodeJS.ProcessEnv = process.env, prices: Record = { residential: 9, mobile: 15 }) { this.username = env.SOAX_USERNAME?.trim() || undefined; this.password = env.SOAX_PASSWORD?.trim() || undefined; this.prices = prices; } isConfigured(): boolean { return Boolean(this.username && this.password); } supportsGeo(_geo: GeoTarget): boolean { return true; } pricePerGb(network: ConcreteNetwork): number { return this.prices[network] ?? this.prices.residential ?? 9; } estimateCost(network: ConcreteNetwork, bytes: number): number { return (bytes / 1_073_741_824) * this.pricePerGb(network); } /** Build the upstream gateway endpoint for a request (exposed for contract tests; never returned to customers). */ endpoint(req: Pick): ProxyEndpoint { if (!this.username || !this.password) throw new Error("SOAX is not configured"); let user = this.username; if (req.geo.country) user += `-country-${req.geo.country.toLowerCase()}`; if (req.geo.region) user += `-region-${req.geo.region}`; if (req.geo.city) user += `-city-${req.geo.city}`; if (req.sessionKey) { user += `-sessionid-${sanitize(req.sessionKey)}`; user += `-sessionlength-${Math.min(Math.max((req.sessionMinutes ?? 10) * 60, 60), 3600)}`; } return { host: "proxy.soax.com", port: 5000, username: user, password: this.password }; } proxyEndpoint(req: Pick): ProxyEndpoint | null { return this.endpoint(req); } async fetch(request: ProviderRequest): Promise { return executeHttp(this.id, this.endpoint(request), request); } async health(network: ConcreteNetwork = "residential"): Promise { if (!this.isConfigured()) return { provider: this.id, network, status: "unconfigured", latencyMs: null, checkedAt: new Date(), detail: "Missing credentials" }; return probeHealth(this, network, "https://checker.soax.com/api/ipinfo"); } } function sanitize(s: string): string { return s.replace(/[^a-zA-Z0-9]/g, "").slice(0, 32) || "s"; }