Spaces:
Build error
Build error
File size: 14,553 Bytes
0bfe2e3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
import { AddonDetail, Config } from '@aiostreams/types';
import {
addonDetails,
isValueEncrypted,
parseAndDecryptString,
serviceDetails,
Settings,
unminifyConfig,
} from '@aiostreams/utils';
export const allowedFormatters = [
'gdrive',
'minimalistic-gdrive',
'torrentio',
'torbox',
'imposter',
'custom',
];
export const allowedLanguages = [
'Multi',
'English',
'Japanese',
'Chinese',
'Russian',
'Arabic',
'Portuguese',
'Spanish',
'French',
'German',
'Italian',
'Korean',
'Hindi',
'Bengali',
'Punjabi',
'Marathi',
'Gujarati',
'Tamil',
'Telugu',
'Kannada',
'Malayalam',
'Thai',
'Vietnamese',
'Indonesian',
'Turkish',
'Hebrew',
'Persian',
'Ukrainian',
'Greek',
'Lithuanian',
'Latvian',
'Estonian',
'Polish',
'Czech',
'Slovak',
'Hungarian',
'Romanian',
'Bulgarian',
'Serbian',
'Croatian',
'Slovenian',
'Dutch',
'Danish',
'Finnish',
'Swedish',
'Norwegian',
'Malay',
'Latino',
'Unknown',
'Dual Audio',
'Dubbed',
];
export function validateConfig(
config: Config,
environment: 'client' | 'server' = 'server'
): {
valid: boolean;
errorCode: string | null;
errorMessage: string | null;
} {
config = unminifyConfig(config);
const createResponse = (
valid: boolean,
errorCode: string | null,
errorMessage: string | null
) => {
return { valid, errorCode, errorMessage };
};
if (config.addons.length < 1) {
return createResponse(
false,
'noAddons',
'At least one addon must be selected'
);
}
if (config.addons.length > Settings.MAX_ADDONS) {
return createResponse(
false,
'tooManyAddons',
`You can only select a maximum of ${Settings.MAX_ADDONS} addons`
);
}
// check for apiKey if Settings.API_KEY is set
if (environment === 'server' && Settings.API_KEY) {
const { apiKey } = config;
if (!apiKey) {
return createResponse(
false,
'missingApiKey',
'The AIOStreams API key is required'
);
}
let decryptedApiKey = apiKey;
if (isValueEncrypted(apiKey)) {
const decryptionResult = parseAndDecryptString(apiKey);
if (decryptionResult === null) {
return createResponse(
false,
'decryptionFailed',
'Failed to decrypt the AIOStreams API key'
);
} else if (decryptionResult === '') {
return createResponse(
false,
'emptyDecryption',
'Decrypted API key is empty'
);
}
decryptedApiKey = decryptionResult;
}
if (decryptedApiKey !== Settings.API_KEY) {
return createResponse(
false,
'invalidApiKey',
'Invalid AIOStreams API key. Please use the one defined in your environment variables'
);
}
}
const duplicateAddons = config.addons.filter(
(addon, index) =>
config.addons.findIndex(
(a) =>
a.id === addon.id &&
JSON.stringify(a.options) === JSON.stringify(addon.options)
) !== index
);
if (duplicateAddons.length > 0) {
return createResponse(
false,
'duplicateAddons',
'Duplicate addons found. Please remove any duplicates'
);
}
for (const addon of config.addons) {
if (Settings.DISABLE_TORRENTIO && addon.id === 'torrentio') {
return createResponse(
false,
'torrentioDisabled',
Settings.DISABLE_TORRENTIO_MESSAGE
);
}
const details = addonDetails.find(
(detail: AddonDetail) => detail.id === addon.id
);
if (!details) {
return createResponse(
false,
'invalidAddon',
`Invalid addon: ${addon.id}`
);
}
if (details.requiresService) {
const supportedServices = details.supportedServices;
const isAtLeastOneServiceEnabled = config.services.some(
(service) => supportedServices.includes(service.id) && service.enabled
);
const isOverrideUrlSet = addon.options?.overrideUrl;
if (!isAtLeastOneServiceEnabled && !isOverrideUrlSet) {
return createResponse(
false,
'missingService',
`${addon.options?.name || details.name} requires at least one of the following services to be enabled: ${supportedServices
.map(
(service) =>
serviceDetails.find((detail) => detail.id === service)?.name ||
service
)
.join(', ')}`
);
}
}
if (details.options) {
for (const option of details.options) {
if (option.required && !addon.options[option.id]) {
return createResponse(
false,
'missingRequiredOption',
`Option ${option.label} is required for addon ${addon.id}`
);
}
if (
option.id.toLowerCase().includes('url') &&
addon.options[option.id] &&
((isValueEncrypted(addon.options[option.id]) &&
environment === 'server') ||
!isValueEncrypted(addon.options[option.id]))
) {
const url = parseAndDecryptString(addon.options[option.id] ?? '');
if (url === null) {
return createResponse(
false,
'decryptionFailed',
`Failed to decrypt URL for ${option.label}`
);
} else if (url === '') {
return createResponse(
false,
'emptyDecryption',
`Decrypted URL for ${option.label} is empty`
);
}
if (
Settings.DISABLE_TORRENTIO &&
url.match(/torrentio\.strem\.fun/) !== null
) {
// if torrentio is disabled, don't allow the user to set URLs with torrentio.strem.fun
return createResponse(
false,
'torrentioDisabled',
Settings.DISABLE_TORRENTIO_MESSAGE
);
} else if (
Settings.DISABLE_TORRENTIO &&
url.match(/stremthru\.elfhosted\.com/) !== null
) {
// if torrentio is disabled, we need to inspect the stremthru URL to see if it's using torrentio
try {
const parsedUrl = new URL(url);
// get the component before manifest.json
const pathComponents = parsedUrl.pathname.split('/');
if (pathComponents.includes('manifest.json')) {
const index = pathComponents.indexOf('manifest.json');
const componentBeforeManifest = pathComponents[index - 1];
// base64 decode the component before manifest.json
const decodedComponent = atob(componentBeforeManifest);
const stremthruData = JSON.parse(decodedComponent);
if (stremthruData?.manifest_url?.match(/torrentio.strem.fun/)) {
return createResponse(
false,
'torrentioDisabled',
Settings.DISABLE_TORRENTIO_MESSAGE
);
}
}
} catch (_) {
// ignore
}
} else {
try {
new URL(url);
} catch (_) {
return createResponse(
false,
'invalidUrl',
` Invalid URL for ${option.label}`
);
}
}
}
if (option.type === 'number' && addon.options[option.id]) {
const input = addon.options[option.id];
if (input !== undefined && !parseInt(input)) {
return createResponse(
false,
'invalidNumber',
`${option.label} must be a number`
);
} else if (input !== undefined) {
const value = parseInt(input);
const { min, max } = option.constraints || {};
if (
(min !== undefined && value < min) ||
(max !== undefined && value > max)
) {
return createResponse(
false,
'invalidNumber',
`${option.label} must be between ${min} and ${max}`
);
}
}
}
}
}
}
if (!allowedFormatters.includes(config.formatter)) {
if (config.formatter.startsWith('custom') && config.formatter.length > 7) {
const jsonString = config.formatter.slice(7);
const data = JSON.parse(jsonString);
if (!data.name || !data.description) {
return createResponse(
false,
'invalidCustomFormatter',
'Invalid custom formatter: name and description are required'
);
}
} else {
return createResponse(
false,
'invalidFormatter',
`Invalid formatter: ${config.formatter}`
);
}
}
for (const service of config.services) {
if (service.enabled) {
const serviceDetail = serviceDetails.find(
(detail) => detail.id === service.id
);
if (!serviceDetail) {
return createResponse(
false,
'invalidService',
`Invalid service: ${service.id}`
);
}
for (const credential of serviceDetail.credentials) {
if (!service.credentials[credential.id]) {
return createResponse(
false,
'missingCredential',
`${credential.label} is required for ${service.name}`
);
}
}
}
}
// need at least one visual tag, resolution, quality
if (
!config.visualTags.some((tag) => Object.values(tag)[0]) ||
!config.resolutions.some((resolution) => Object.values(resolution)[0]) ||
!config.qualities.some((quality) => Object.values(quality)[0])
) {
return createResponse(
false,
'noFilters',
'At least one visual tag, resolution, and quality must be selected'
);
}
for (const [min, max] of [
[config.minMovieSize, config.maxMovieSize],
[config.minEpisodeSize, config.maxEpisodeSize],
[config.minSize, config.maxSize],
]) {
if (min && max) {
if (min >= max) {
return createResponse(
false,
'invalidSizeRange',
"Your minimum size limit can't be greater than or equal to your maximum size limit"
);
}
}
}
if (config.maxResultsPerResolution && config.maxResultsPerResolution < 1) {
return createResponse(
false,
'invalidMaxResultsPerResolution',
'Max results per resolution must be greater than 0'
);
}
if (
config.mediaFlowConfig?.mediaFlowEnabled &&
config.stremThruConfig?.stremThruEnabled
) {
return createResponse(
false,
'multipleProxyServices',
'Multiple proxy services are not allowed'
);
}
if (config.mediaFlowConfig?.mediaFlowEnabled) {
if (!config.mediaFlowConfig.proxyUrl) {
return createResponse(
false,
'missingProxyUrl',
'Proxy URL is required if MediaFlow is enabled'
);
}
if (!config.mediaFlowConfig.apiPassword) {
return createResponse(
false,
'missingApiPassword',
'API Password is required if MediaFlow is enabled'
);
}
}
if (config.stremThruConfig?.stremThruEnabled) {
if (!config.stremThruConfig.url) {
return createResponse(
false,
'missingUrl',
'URL is required if Stremthru is enabled'
);
}
if (!config.stremThruConfig.credential) {
return createResponse(
false,
'missingCredential',
'Credential is required if StremThru is enabled'
);
}
}
if (
(config.excludeFilters?.length ?? 0) > Settings.MAX_KEYWORD_FILTERS ||
(config.strictIncludeFilters?.length ?? 0) > Settings.MAX_KEYWORD_FILTERS
) {
return createResponse(
false,
'tooManyFilters',
`You can only have a maximum of ${Settings.MAX_KEYWORD_FILTERS} filters`
);
}
const filters = [
...(config.excludeFilters || []),
...(config.strictIncludeFilters || []),
];
filters.forEach((filter) => {
if (filter.length > 20) {
return createResponse(
false,
'invalidFilter',
'One of your filters is too long'
);
}
if (!filter) {
return createResponse(
false,
'invalidFilter',
'Filters must not be empty'
);
}
});
if (config.regexFilters) {
if (!config.apiKey) {
return createResponse(
false,
'missingApiKey',
'Regex filtering requires an API key to be set'
);
}
if (config.regexFilters.excludePattern) {
try {
new RegExp(config.regexFilters.excludePattern);
} catch (e) {
return createResponse(
false,
'invalidExcludeRegex',
'Invalid exclude regex pattern'
);
}
}
if (config.regexFilters.includePattern) {
try {
new RegExp(config.regexFilters.includePattern);
} catch (e) {
return createResponse(
false,
'invalidIncludeRegex',
'Invalid include regex pattern'
);
}
}
}
if (config.regexSortPatterns) {
if (!config.apiKey) {
return createResponse(
false,
'missingApiKey',
'Regex sorting requires an API key to be set'
);
}
// Split the pattern by spaces and validate each one
const patterns = config.regexSortPatterns.split(/\s+/).filter(Boolean);
// Enforce an upper bound on the number of patterns
if (patterns.length > Settings.MAX_REGEX_SORT_PATTERNS) {
return createResponse(
false,
'tooManyRegexSortPatterns',
`You can specify at most ${Settings.MAX_REGEX_SORT_PATTERNS} regex sort patterns`
);
}
for (const pattern of patterns) {
const delimiter = '<::>';
const delimiterIndex = pattern.indexOf(delimiter);
let name: string = 'Unamed';
let regexPattern = pattern;
if (delimiterIndex !== -1) {
name = pattern.slice(0, delimiterIndex).replace(/_/g, ' ');
regexPattern = pattern.slice(delimiterIndex + delimiter.length);
}
try {
new RegExp(regexPattern);
} catch (e) {
return createResponse(
false,
'invalidRegexSortPattern',
`Invalid regex sort pattern: ${name ? `"${name}" ` : ''}${regexPattern}`
);
}
}
}
return createResponse(true, null, null);
}
|