// todo, actual file logic // - ready query param to get flashrom path, check for its existance // - handle last part to redirect to lc2-download-complete // - handle failures request_is_async = true; function doLocalFlashROM() { fs.readFile(flashrom_file_path, null, function (err, data) { try { var data_128 = new Buffer.alloc(128); data.copy(data_128, 0, 0, 128); var flashrom_message = new Buffer.from(data_128.toString('hex').substring(36 * 2, 68 * 2), 'hex').toString('ascii').replace(/[^0-9a-z\ \.\-]/gi, ""); processLC2DownloadPage(request_headers.query.path, flashrom_message, (request_headers.query.numparts || null)); } catch (e) { var errpage = doErrorPage(404, "The service could not find the requested ROM.") headers = errpage[0]; data = errpage[1]; sendToClient(socket, headers, data); } }); } if (!request_headers.query.path) { var errpage = doErrorPage(400); headers = errpage[0]; data = errpage[1]; } else { var request_path = unescape(request_headers.query.path); var flashrom_file_path = null; Object.keys(service_vaults).forEach(function (g) { if (flashrom_file_path != null) return; flashrom_file_path = service_vaults[g] + "/" + service_name + "/" + request_path; if (!fs.existsSync(flashrom_file_path)) flashrom_file_path = null; }); if (minisrv_config.services[service_name].use_zefie_server && !flashrom_file_path) { // read first 256 bytes of flashrom file from archive.midnightchannel.net // to get `flashrom_message` and `numparts` if missing var options = { host: "archive.midnightchannel.net", path: "/zefie/files/wtv-flashrom/" + request_path, method: 'GET', timeout: 5000, headers: { 'Range': 'bytes=0-256' } } var chunk; const req = https.request(options, function (res) { var data = ''; res.setEncoding('hex'); res.on('data', function (d) { data += d; }); res.on('error', function (e) { console.log(" * Upstream FlashROM Error:", e); var errpage = doErrorPage(400) headers = errpage[0]; data = errpage[1]; sendToClient(socket, headers, data); }); res.on('end', function () { if (res.statusCode == 206) { var flashrom_message = new Buffer.from(data.substring(36 * 2, 68 * 2), 'hex').toString('ascii').replace(/[^0-9a-z\ \.\-]/gi, ""); processLC2DownloadPage(request_headers.query.path, flashrom_message, (request_headers.query.numparts || null)); return; } else if (res.statusCode == 404) { var errpage = doErrorPage(404, "The service could not find the requested ROM on zefie's server.") headers = errpage[0]; data = errpage[1]; } else { var errpage = doErrorPage(400) headers = errpage[0]; data = errpage[1]; } sendToClient(socket, headers, data); }); }); req.end(); } else { // use local flashrom files doLocalFlashROM(flashrom_file_path); } } async function processLC2DownloadPage(path, flashrom_message, numparts = null) { if (numparts != null) var flashrom_numparts = parseInt(numparts); if (!flashrom_numparts) flashrom_numparts = flashrom_message.substring(flashrom_message.length - 4).replace(/\D/g, ''); var ind = new Array(); ind[0] = (path.indexOf("part") + 4); ind[1] = (path.indexOf(".", ind[0]) + 1); var flashrom_part_num = path.substr(ind[0], (path.length - ind[1])); var flashrom_lastpart = (flashrom_numparts == (parseInt(flashrom_part_num) + 1)) ? true : false; var flashrom_rompath = 'wtv-flashrom:/get-by-path?path=' + path + '&raw=true'; var flashrom_isboot = (/\.brom$/).test(path); if (flashrom_lastpart) { flashrom_next_rompath = "wtv-flashrom:/lc2-download-complete?"; } else { var flashrom_next_part_num = (parseInt(flashrom_part_num) + 1); if (flashrom_next_part_num < 10) flashrom_next_part_num = "00" + flashrom_next_part_num; // 1s else if (flashrom_next_part_num >= 10 && flashrom_next_part_num < 100) flashrom_next_part_num = "0" + flashrom_next_part_num; // 10s var flashrom_next_rompath = flashrom_rompath.replace("part"+flashrom_part_num, "part"+flashrom_next_part_num).replace('get-by-path', 'get-lc2-page').replace("&raw=true", "&numparts=" + parseInt(flashrom_numparts)); } if (!flashrom_part_num || !flashrom_lastpart || !flashrom_rompath || !flashrom_next_rompath || !flashrom_isboot) { headers = `200 OK Content-type: text/html` data = `
|
|
|
Updating now | |||||||||
|
| |||||||||||