0.16.1 Registry Fix MakeMKV
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const logger = require('./logger').child('MAKEMKV_KEY');
|
||||
|
||||
const MAKEMKV_BETA_KEY_API_URL = 'https://cable.ayra.ch/makemkv/api.php?raw';
|
||||
const MAKEMKV_BETA_KEY_API_USER_AGENT = process.env.MAKEMKV_BETA_KEY_API_USER_AGENT
|
||||
|| 'Ripster/1.0 (MakeMKV beta key sync)';
|
||||
const MAKEMKV_BETA_KEY_API_FROM = process.env.MAKEMKV_BETA_KEY_API_FROM
|
||||
|| 'ripster@localhost';
|
||||
|
||||
function normalizeRegistrationKey(rawValue) {
|
||||
return String(rawValue || '').trim();
|
||||
}
|
||||
|
||||
function getMakeMKVConfigDir(homeDir = os.homedir()) {
|
||||
return path.join(String(homeDir || '').trim() || os.homedir(), '.MakeMKV');
|
||||
}
|
||||
|
||||
function getMakeMKVSettingsFilePath(homeDir = os.homedir()) {
|
||||
return path.join(getMakeMKVConfigDir(homeDir), 'settings.conf');
|
||||
}
|
||||
|
||||
function escapeKeyForSettingsFile(value) {
|
||||
return String(value || '')
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/"/g, '\\"');
|
||||
}
|
||||
|
||||
function buildUpdatedSettingsContent(currentContent, registrationKey) {
|
||||
const existingLines = String(currentContent || '')
|
||||
.split(/\r?\n/)
|
||||
.filter((line) => !/^\s*app_Key\s*=/.test(line));
|
||||
const normalizedKey = normalizeRegistrationKey(registrationKey);
|
||||
|
||||
while (existingLines.length > 0 && existingLines[existingLines.length - 1] === '') {
|
||||
existingLines.pop();
|
||||
}
|
||||
|
||||
if (normalizedKey) {
|
||||
existingLines.push(`app_Key = "${escapeKeyForSettingsFile(normalizedKey)}"`);
|
||||
}
|
||||
|
||||
return existingLines.length > 0 ? `${existingLines.join('\n')}\n` : '';
|
||||
}
|
||||
|
||||
async function syncRegistrationKeyToConfig(rawValue, options = {}) {
|
||||
const normalizedKey = normalizeRegistrationKey(rawValue);
|
||||
const homeDir = String(options?.homeDir || '').trim() || os.homedir();
|
||||
const configDir = getMakeMKVConfigDir(homeDir);
|
||||
const settingsFilePath = getMakeMKVSettingsFilePath(homeDir);
|
||||
const fileExists = fs.existsSync(settingsFilePath);
|
||||
const currentContent = fileExists
|
||||
? await fs.promises.readFile(settingsFilePath, 'utf8')
|
||||
: '';
|
||||
const nextContent = buildUpdatedSettingsContent(currentContent, normalizedKey);
|
||||
|
||||
if (!normalizedKey && !fileExists) {
|
||||
return {
|
||||
changed: false,
|
||||
path: settingsFilePath,
|
||||
hasKey: false
|
||||
};
|
||||
}
|
||||
|
||||
await fs.promises.mkdir(configDir, { recursive: true });
|
||||
|
||||
if (nextContent) {
|
||||
await fs.promises.writeFile(settingsFilePath, nextContent, 'utf8');
|
||||
await fs.promises.chmod(settingsFilePath, 0o600).catch(() => {});
|
||||
logger.info('settings-conf:key-synced', {
|
||||
path: settingsFilePath,
|
||||
hasKey: true
|
||||
});
|
||||
} else {
|
||||
await fs.promises.writeFile(settingsFilePath, '', 'utf8');
|
||||
await fs.promises.chmod(settingsFilePath, 0o600).catch(() => {});
|
||||
logger.info('settings-conf:key-cleared', {
|
||||
path: settingsFilePath,
|
||||
hasKey: false
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
changed: currentContent !== nextContent,
|
||||
path: settingsFilePath,
|
||||
hasKey: Boolean(normalizedKey)
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchCurrentBetaKey() {
|
||||
const response = await fetch(MAKEMKV_BETA_KEY_API_URL, {
|
||||
headers: {
|
||||
'User-Agent': MAKEMKV_BETA_KEY_API_USER_AGENT,
|
||||
'From': MAKEMKV_BETA_KEY_API_FROM,
|
||||
'Accept': 'text/plain, text/html;q=0.9, */*;q=0.8',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Pragma': 'no-cache'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = new Error(`Betakey konnte nicht geladen werden (HTTP ${response.status}).`);
|
||||
error.statusCode = 502;
|
||||
throw error;
|
||||
}
|
||||
|
||||
const rawBody = await response.text();
|
||||
const betaKey = String(rawBody || '')
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.find(Boolean) || '';
|
||||
|
||||
if (!/^[A-Za-z0-9][A-Za-z0-9-]{15,}$/.test(betaKey)) {
|
||||
const error = new Error('Betakey-Antwort ist ungültig.');
|
||||
error.statusCode = 502;
|
||||
throw error;
|
||||
}
|
||||
|
||||
return {
|
||||
key: betaKey,
|
||||
sourceUrl: MAKEMKV_BETA_KEY_API_URL
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MAKEMKV_BETA_KEY_API_URL,
|
||||
fetchCurrentBetaKey,
|
||||
getMakeMKVConfigDir,
|
||||
getMakeMKVSettingsFilePath,
|
||||
normalizeRegistrationKey,
|
||||
syncRegistrationKeyToConfig
|
||||
};
|
||||
Reference in New Issue
Block a user