feat: add user agent header for addon requests and allow wrappers to specify additional headers

This commit is contained in:
Viren070
2025-04-28 14:53:01 +01:00
parent 8048ae8f7c
commit 657abaef4c
3 changed files with 27 additions and 9 deletions
+3
View File
@@ -1,5 +1,6 @@
import dotenv from 'dotenv';
import path from 'path';
import p from '../package.json';
try {
dotenv.config({ path: path.resolve(__dirname, '../../../.env') });
@@ -26,6 +27,8 @@ export class Settings {
public static readonly SHOW_DIE = process.env.SHOW_DIE
? process.env.SHOW_DIE === 'true'
: false;
public static readonly ADDON_REQUEST_USER_AGENT =
process.env.ADDON_REQUEST_USER_AGENT ?? `AIOStreams/${p.version}`;
public static readonly LOG_SENSITIVE_INFO = process.env.LOG_SENSITIVE_INFO
? process.env.LOG_SENSITIVE_INFO === 'true'
: false;
+2 -1
View File
@@ -2,6 +2,7 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"rootDir": "src",
"resolveJsonModule": true
}
}
+22 -8
View File
@@ -20,6 +20,16 @@ import { emojiToLanguage, codeToLanguage } from '@aiostreams/formatters';
const logger = createLogger('wrappers');
const IP_HEADERS = [
'X-Client-IP',
'X-Forwarded-For',
'X-Real-IP',
'CF-Connecting-IP',
'True-Client-IP',
'X-Forwarded',
'Forwarded-For',
];
export class BaseWrapper {
private readonly streamPath: string = 'stream/{type}/{id}.json';
private indexerTimeout: number;
@@ -27,18 +37,24 @@ export class BaseWrapper {
private addonUrl: string;
private addonId: string;
private userConfig: Config;
private headers: Headers;
constructor(
addonName: string,
addonUrl: string,
addonId: string,
userConfig: Config,
indexerTimeout?: number
indexerTimeout?: number,
requestHeaders?: HeadersInit
) {
this.addonName = addonName;
this.addonUrl = this.standardizeManifestUrl(addonUrl);
this.addonId = addonId;
(this.indexerTimeout = indexerTimeout || Settings.DEFAULT_TIMEOUT),
(this.userConfig = userConfig);
this.headers = new Headers({
'User-Agent': Settings.ADDON_REQUEST_USER_AGENT,
...(requestHeaders || {}),
});
}
protected standardizeManifestUrl(url: string): string {
@@ -136,12 +152,11 @@ export class BaseWrapper {
}
protected makeRequest(url: string): Promise<any> {
const headers = new Headers();
const userIp = this.userConfig.requestingIp;
if (userIp) {
headers.set('X-Client-IP', userIp);
headers.set('X-Forwarded-For', userIp);
headers.set('X-Real-IP', userIp);
for (const header of IP_HEADERS) {
this.headers.set(header, userIp);
}
}
let sanitisedUrl = this.getLoggableUrl(url);
@@ -152,17 +167,16 @@ export class BaseWrapper {
userIp ? maskSensitiveInfo(userIp) : 'not set'
}`
);
let response = useProxy
? uFetch(url, {
dispatcher: new ProxyAgent(Settings.ADDON_PROXY),
method: 'GET',
headers: headers,
headers: this.headers,
signal: AbortSignal.timeout(this.indexerTimeout),
})
: fetch(url, {
method: 'GET',
headers: headers,
headers: this.headers,
signal: AbortSignal.timeout(this.indexerTimeout),
});