From c0cf0aee018b4f03204c4287204abe3bed249ebc Mon Sep 17 00:00:00 2001 From: Pierce Date: Tue, 7 May 2019 19:12:24 -0400 Subject: [PATCH] Checksum, fix random version file request, removed node-fetch --- components/handler.js | 20 +++++++++++++++++--- package-lock.json | 28 ++++++++++++++++++++++------ package.json | 4 ++-- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/components/handler.js b/components/handler.js index 944721c..f23ea1f 100644 --- a/components/handler.js +++ b/components/handler.js @@ -2,6 +2,7 @@ const fs = require('fs'); const shelljs = require('shelljs'); const path = require('path'); const request = require('request'); +const checksum = require('checksum'); const zip = require('adm-zip'); const event = require('./events'); @@ -56,9 +57,18 @@ function downloadAsync (url, directory, name) { }); } +function checkSum(hash, file, size) { + return new Promise(resolve => { + checksum.file(file, (err, sum) => resolve(hash === sum)); + }); +} + module.exports.getVersion = function (version, directory) { return new Promise(resolve => { - if(fs.existsSync(path.join(directory, `${version}.json`))) resolve(require(path.join(directory, `${version}.json`))); + if(fs.existsSync(path.join(directory, `${version}.json`))) { + resolve(require(path.join(directory, `${version}.json`))); + return; + } const manifest = "https://launchermeta.mojang.com/mc/game/version_manifest.json"; request.get(manifest, function(error, response, body) { @@ -108,7 +118,7 @@ module.exports.getAssets = function (directory, version) { const subhash = hash.substring(0,2); const assetDirectory = path.join(directory, 'assets', 'objects', subhash); - if(!fs.existsSync(path.join(assetDirectory, hash))) { + if(!fs.existsSync(path.join(assetDirectory, hash)) || !await checkSum(hash, path.join(assetDirectory, hash))) { const download = await downloadAsync(`${assetsUrl}/${subhash}/${hash}`, assetDirectory, hash); if(download.failed) failed.push(download.asset); @@ -164,6 +174,9 @@ module.exports.getNatives = function (root, version, os) { if (native) { const name = native.path.split('/').pop(); await downloadAsync(native.url, nativeDirectory, name); + if(!await checkSum(native.sha1, path.join(nativeDirectory, name))) { + await downloadAsync(native.url, nativeDirectory, name); + } try {new zip(path.join(nativeDirectory, name)).extractAllTo(nativeDirectory, true);} catch(e) { // Only doing a console.warn since a stupid error happens. You can basically ignore this. // if it says Invalid file name, just means two files were downloaded and both were deleted. @@ -248,9 +261,10 @@ module.exports.getClasses = function (options, version) { const libraryPath = _lib.downloads.artifact.path; const libraryUrl = _lib.downloads.artifact.url; + const libraryHash = _lib.downloads.artifact.sha1; const libraryDirectory = path.join(options.root, 'libraries', libraryPath); - if(!fs.existsSync(libraryDirectory)) { + if(!fs.existsSync(libraryDirectory) || !await checkSum(libraryHash, libraryDirectory)) { let directory = libraryDirectory.split(path.sep); const name = directory.pop(); directory = directory.join(path.sep); diff --git a/package-lock.json b/package-lock.json index 3cf9503..ec9095f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "minecraft-launcher-core", - "version": "2.5.6", + "version": "2.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -76,6 +76,14 @@ "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz", "integrity": "sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw=" }, + "checksum": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/checksum/-/checksum-0.1.1.tgz", + "integrity": "sha1-3GUn1MkL6FYNvR7Uzs8yl9Uo6ek=", + "requires": { + "optimist": "~0.3.5" + } + }, "co": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", @@ -299,11 +307,6 @@ "brace-expansion": "^1.1.7" } }, - "node-fetch": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.2.0.tgz", - "integrity": "sha512-OayFWziIxiHY8bCUyLX6sTpDH8Jsbp4FfYd1j1f7vZyfgkcOnAyM4oQR16f8a0s7Gl/viMGRey8eScYk4V4EZA==" - }, "oauth-sign": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", @@ -317,6 +320,14 @@ "wrappy": "1" } }, + "optimist": { + "version": "0.3.7", + "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz", + "integrity": "sha1-yQlBrVnkJzMokjB00s8ufLxuwNk=", + "requires": { + "wordwrap": "~0.0.2" + } + }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", @@ -464,6 +475,11 @@ "extsprintf": "^1.2.0" } }, + "wordwrap": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz", + "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc=" + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index 3611712..45411cc 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "minecraft-launcher-core", - "version": "2.6.2", + "version": "2.7.0", "description": "Module that downloads Minecraft assets and runs Minecraft. Also Supports Forge", "main": "index.js", "dependencies": { "adm-zip": "^0.4.13", - "node-fetch": "^2.2.0", + "checksum": "^0.1.1", "request": "^2.88.0", "shelljs": "^0.8.2", "uuid": "^3.3.2"