Initial Adaptive Music v0.5.0 release
This commit is contained in:
commit
14f0d8c26b
114 files changed
+6907
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { lstat, open } from 'node:fs/promises';
|
||||
import { constants } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { MusicError } from '../src/core/controller.mjs';
|
||||
// Fixed private file only. No environment secret, URL parameter or browser input.
|
||||
export async function loadMAToken(directory) {
|
||||
try {
|
||||
const dir = await lstat(directory);
|
||||
if (!dir.isDirectory() || dir.isSymbolicLink() || (dir.mode & 0o777) !== 0o700 || dir.uid !== process.getuid()) throw Error();
|
||||
const file = await open(join(directory, 'ma-token'), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
||||
try {
|
||||
const info = await file.stat();
|
||||
if (!info.isFile() || (info.mode & 0o777) !== 0o600 || info.uid !== process.getuid() || info.size > 16384) throw Error();
|
||||
const token = (await file.readFile('utf8')).trim();
|
||||
if (!token || /[\r\n]/.test(token)) throw Error();
|
||||
return token;
|
||||
} finally { await file.close(); }
|
||||
} catch { throw new MusicError('denied'); }
|
||||
}
|
||||
|
||||
// Separate from the MA token. Back up this private key to preserve scene identifiers.
|
||||
export async function loadCatalogKey(directory) {
|
||||
const { randomBytes } = await import('node:crypto');
|
||||
const path = join(directory, 'catalog-key');
|
||||
// Reuse the directory/token owner-mode validation before creating private state.
|
||||
await loadMAToken(directory);
|
||||
try {
|
||||
const created = await open(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
||||
try { await created.writeFile(randomBytes(32)); await created.sync(); }
|
||||
finally { await created.close(); }
|
||||
} catch (error) { if (error.code !== 'EEXIST') throw new MusicError('denied'); }
|
||||
const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
||||
try {
|
||||
const info = await file.stat();
|
||||
if (!info.isFile() || info.uid !== process.getuid() || (info.mode & 0o777) !== 0o600 || info.size !== 32) throw new MusicError('denied');
|
||||
return await file.readFile();
|
||||
} finally { await file.close(); }
|
||||
}
|
||||
Reference in new issue
Block a user