Last active
May 16, 2024 12:28
-
-
Save aynik/65a1ad432a6e6f29a2e290336e916c16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require("fs"); | |
const path = require("path"); | |
const { WebUSB } = require("usb"); | |
const { Worker } = require("worker_threads"); | |
const { | |
MDTrack, | |
MDSession, | |
Wireformat, | |
DevicesIds, | |
openNewDevice, | |
prepareDownload, | |
} = require("netmd-js"); | |
const { | |
makeGetAsyncPacketIteratorOnWorkerThread, | |
} = require("netmd-js/dist/node-encrypt-worker"); | |
const { ExploitStateManager, SPUpload } = require("netmd-exploits"); | |
async function main(file) { | |
function throwError(message) { | |
throw new Error(message); | |
} | |
async function uploadAtrac(file) { | |
const usb = new WebUSB({ | |
deviceTimeout: 100000, | |
allowedDevices: DevicesIds, | |
}); | |
const device = await openNewDevice(usb).then( | |
(it) => it ?? throwError("NetMD device not found") | |
); | |
const factory = await device.factory(); | |
const exploits = await ExploitStateManager.create(device, factory); | |
const spUpload = await exploits.require(SPUpload, 2); | |
await prepareDownload(device); | |
const session = new MDSession(device); | |
await session.init(); | |
const worker = new Worker( | |
path.join( | |
__dirname, | |
"node_modules", | |
"netmd-js", | |
"dist", | |
"node-encrypt-worker.js" | |
) | |
); | |
const dataBlob = await fs.openAsBlob(file); | |
const dataBuffer = await dataBlob.arrayBuffer(); | |
const data = dataBuffer.slice(2048); | |
const title = path.basename(file); | |
const track = spUpload.prepareTrack( | |
new MDTrack( | |
title, | |
Wireformat.l105kbps, | |
data.slice(2048), | |
0x400, | |
"", | |
makeGetAsyncPacketIteratorOnWorkerThread(worker) | |
) | |
); | |
try { | |
await session.downloadTrack(track); | |
console.log("done"); | |
} catch (err) { | |
console.log(err); | |
} finally { | |
worker.terminate(); | |
await session.close(); | |
await device.release(); | |
process.exit(0); | |
} | |
} | |
uploadAtrac(file); | |
} | |
if (process.argv.length < 3) { | |
console.error("Usage: netmd-session <file>"); | |
process.exit(1); | |
} else { | |
const file = process.argv[2]; | |
main(file); | |
} | |
process.on("SIGINT", () => { | |
process.exit(0); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "netmd", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"netmd-exploits": "^0.5.5", | |
"netmd-js": "^4.1.9", | |
"usb": "^2.12.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment