mirror of
https://github.com/Viren070/AIOStreams.git
synced 2025-12-01 23:14:04 +01:00
feat: add user agent header for addon requests and allow wrappers to specify additional headers
This commit is contained in:
@@ -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,6 +2,7 @@
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src"
|
||||
"rootDir": "src",
|
||||
"resolveJsonModule": true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user