Browse Source

Implement requested changes

pull/7153/head
Alexander Linder 3 weeks ago
parent
commit
5231f8eb75
Failed to extract signature
  1. 6
      CHANGELOG.md
  2. 28
      apps/api/src/services/fetch/fetch.service.ts
  3. 4
      apps/api/src/services/fetch/interfaces/proxy-route.interface.ts

6
CHANGELOG.md

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Added
- Added support for routing outgoing requests through a proxy per domain via the `PROXY_ROUTES` setting in the `FetchService`
## 3.17.0 - 2026-06-26 ## 3.17.0 - 2026-06-26
### Added ### Added

28
apps/api/src/services/fetch/fetch.service.ts

@ -190,25 +190,29 @@ export class FetchService implements OnModuleInit {
*/ */
private applyProxyRoute(input: RequestInfo | URL): RequestInfo | URL { private applyProxyRoute(input: RequestInfo | URL): RequestInfo | URL {
try { try {
const url = new URL( const requestUrl = new URL(
input instanceof Request ? input.url : input.toString() input instanceof Request ? input.url : input.toString()
); );
const route = this.proxyRoutes.find(({ domain }) => { const route = this.proxyRoutes.find(({ domain }) => {
return this.matchesDomain(url.hostname, domain); return this.hostnameMatchesDomain({
domain,
hostname: requestUrl.hostname
});
}); });
if (!route) { if (!route) {
return input; return input;
} }
const target = new URL(route.url); const proxyUrl = new URL(route.url);
url.protocol = target.protocol; requestUrl.protocol = proxyUrl.protocol;
url.host = target.host; requestUrl.hostname = proxyUrl.hostname;
requestUrl.port = proxyUrl.port;
return input instanceof Request return input instanceof Request
? new Request(url.toString(), input) ? new Request(requestUrl.toString(), input)
: url.toString(); : requestUrl.toString();
} catch { } catch {
return input; return input;
} }
@ -219,14 +223,20 @@ export class FetchService implements OnModuleInit {
const { hostname } = new URL(url); const { hostname } = new URL(url);
return this.webFetchRoutes.find(({ domain }) => { return this.webFetchRoutes.find(({ domain }) => {
return this.matchesDomain(hostname, domain); return this.hostnameMatchesDomain({ domain, hostname });
}); });
} catch { } catch {
return undefined; return undefined;
} }
} }
private matchesDomain(hostname: string, domain: string): boolean { private hostnameMatchesDomain({
domain,
hostname
}: {
domain: string;
hostname: string;
}): boolean {
return hostname === domain || hostname.endsWith(`.${domain}`); return hostname === domain || hostname.endsWith(`.${domain}`);
} }

4
apps/api/src/services/fetch/interfaces/proxy-route.interface.ts

@ -9,8 +9,8 @@
* *
* [ * [
* { * {
* "domain": "www.trackinsight.com", * "domain": "example.com",
* "url": "http://trackinsight-proxy:8191" * "url": "http://example-proxy:8191"
* } * }
* ] * ]
* *

Loading…
Cancel
Save