dataunlocker
Advanced tools
Comparing version 1.0.6 to 2.0.0
import { getEnv, getRelativeFileToCwd, isFileExists } from '../../lib/utils/index.js'; | ||
import { createHash } from 'crypto'; | ||
import { mkdir, readFile, writeFile } from 'fs/promises'; | ||
@@ -14,10 +13,10 @@ import { dirname, resolve } from 'path'; | ||
} | ||
const js = await readFile(file); | ||
const id = args.id || getEnv('DATAUNLOCKER_ID'); | ||
const env = getEnv('DATAUNLOCKER_ENV')?.toLowerCase() || ''; | ||
if (!file.endsWith('.js')) { | ||
return warnAndExit(`Only .js files are supported. Provided file not supported: ${file}`); | ||
} | ||
const js = (await readFile(file)).toString(); | ||
const id = args.id || getEnv('DATAUNLOCKER_ID'); | ||
const env = getEnv('DATAUNLOCKER_ENV')?.toLowerCase() || ''; | ||
if (!id || !/^[0-9a-f]{24}$/.test(id)) { | ||
return warnAndExit(`Please specify a valid DataUnlocker ID as DATAUNLOCKER_ID env var or --id CLI param`); | ||
return warnAndExit(`Please specify a valid DataUnlocker ID as DATAUNLOCKER_ID env var or --id CLI param. Copy it from the DataUnlocker dashboard.`); | ||
} | ||
@@ -34,7 +33,29 @@ if (args.backup && typeof args.backup !== 'string') { | ||
? '' | ||
: `${file}.${createHash('sha256').update(js).digest('hex').slice(0, 7)}.backup`; | ||
console.log(`Patching ${getRelativeFileToCwd(file)}, please wait...`); | ||
: `${file}.backup`; | ||
const isBackupFileExists = fileBackup | ||
? await isFileExists(fileBackup) | ||
: false; | ||
let jsToPatch = js; | ||
console.log(`Backup file: ${isBackupFileExists ? `exists (${getRelativeFileToCwd(fileBackup)})` : fileBackup ? 'does not exist' : 'disabled'}`); | ||
if (fileBackup && !isBackupFileExists) { | ||
console.info(` ↳ Backing up ${getRelativeFileToCwd(file)} -> ${getRelativeFileToCwd(fileBackup)}...`); | ||
await mkdir(dirname(fileBackup), { recursive: true }); | ||
await writeFile(fileBackup, jsToPatch); | ||
console.info(` ✔ Backed up to ${getRelativeFileToCwd(fileBackup)}`); | ||
} | ||
console.log(`Patching, please wait...`); | ||
if (isBackupFileExists) { | ||
jsToPatch = (await readFile(fileBackup)).toString(); | ||
console.log(` ↳ Using backup file contents (${getRelativeFileToCwd(fileBackup)})`); | ||
} | ||
else { | ||
console.log(` ↳ Using original file contents (${getRelativeFileToCwd(file)})`); | ||
} | ||
if (args.endpoint) { | ||
console.log(`↳ Using endpoint ${args.endpoint}`); | ||
console.log(` ↳ Using endpoint ${args.endpoint}`); | ||
} | ||
else { | ||
console.log(` ↳ Using the latest healthy endpoint (automatic)`); | ||
} | ||
console.log(` ↳ ${getRelativeFileToCwd(file)} will be overwritten`); | ||
const url = `https://api${env ? `.${env}` : ''}.dataunlocker.com/domains/${id}/defender/patch-js${args.endpoint ? `?endpoint=${encodeURIComponent(args.endpoint)}` : ''}`; | ||
@@ -45,3 +66,3 @@ let result; | ||
method: 'POST', | ||
body: js, | ||
body: jsToPatch, | ||
headers: { | ||
@@ -68,14 +89,5 @@ 'Content-Type': 'application/x-www-form-urlencoded', | ||
} | ||
if (fileBackup) { | ||
console.info(`Backing up ${getRelativeFileToCwd(file)} -> ${getRelativeFileToCwd(fileBackup)}...`); | ||
if (await isFileExists(fileBackup)) { | ||
console.info(`Overwriting existing backup file ${getRelativeFileToCwd(fileBackup)}...`); | ||
} | ||
await mkdir(dirname(fileBackup), { recursive: true }); | ||
await writeFile(fileBackup, js); | ||
console.info(`✔ File backed up, ${getRelativeFileToCwd(file)} -> ${getRelativeFileToCwd(fileBackup)}`); | ||
} | ||
console.log(`Writing ${getRelativeFileToCwd(file)}...`); | ||
console.log(` ↳ Writing ${getRelativeFileToCwd(file)}...`); | ||
await writeFile(file, text); | ||
console.log(`✔ Done!`); | ||
console.log(` ✔ Done!`); | ||
} | ||
@@ -82,0 +94,0 @@ const warnAndExit = (message) => { |
{ | ||
"name": "dataunlocker", | ||
"version": "1.0.6", | ||
"version": "2.0.0", | ||
"description": "DataUnlocker's command line interface utilities", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -11,8 +11,14 @@ # DataUnlocker's CLI | ||
### Patch a core JavaScript file of your web application | ||
### `$ npx -y dataunlocker patch` | ||
[DataUnlocker Defender](https://docs.dataunlocker.com/setup/defender) tightly integrates with your web app's JavaScript code, specifically, your build artifacts or JavaScript libraries that your web app relies on. | ||
Patches a core JavaScript file of your web application. | ||
Locate such a file (`file.js`) and patch it using the following command, ideally in your deployment pipeline. | ||
[DataUnlocker Defender](https://docs.dataunlocker.com/setup/defender) tightly integrates with your web app's JavaScript code, specifically, your build artifacts or JavaScript libraries that your web app relies on. By design, each `dataunlocker patch` run should be embedded into your build pipeline to generate the newly obfuscated JavaScript file on every build run. | ||
Follow these steps to install DataUnlocker Defender in your web app: | ||
1. Get your DataUnlocker ID from [DataUnlocker Admin](https://admin-2.dataunlocker.com). | ||
2. Locate a core file (for example, `file.js`) which is loaded on all pages of your web application. | ||
3. Patch it using the following command, ideally in your deployment pipeline. | ||
```sh | ||
@@ -34,7 +40,28 @@ npx -y dataunlocker patch file.js | ||
As a result, `file.js` will be replaced with its obfuscated version with DataUnlocker Defender baked in. A backup | ||
of the original `file.js` will be placed next to it, named `file.js.0000000.backup` by default. | ||
As a result, `file.js` will be replaced with its obfuscated version with DataUnlocker Defender baked in. A backup of the original `file.js` will be placed next to it, named `file.js.backup` by default. | ||
Note: | ||
- You can skip creating a backup file with `--no-backup` option. | ||
- You can set a name for a backup file with `--backup filename.js` option. | ||
- You can specify which endpoint to use for the patched code with `--endpoint example.com/abcdef` option (specify the endpoint URI without the protocol). | ||
- If the backup file exists, its content will be used for patching. | ||
## Example | ||
`$ npx -y dataunlocker patch file.js --id 000000000000000000000000` | ||
``` | ||
💜 DataUnlocker CLI v2.0.0 | ||
🔧 ID=000000000000000000000000 | ||
Backup file: does not exist | ||
↳ Backing up local/test.js -> local/test.js.backup... | ||
✔ Backed up to local/test.js.backup | ||
Patching, please wait... | ||
↳ Using original file contents (local/test.js) | ||
↳ Using the latest healthy endpoint (automatic) | ||
↳ local/test.js will be overwritten | ||
↳ Writing local/test.js... | ||
✔ Done! | ||
``` |
11017
193
66