Aegis code protection
npm install @feasibleone/aegisAegis is a code protection tool that helps you protect node.js applications
from reverse engineering and tampering. It is designed to be easy to use and
integrate into your existing projects.
- Code Encryption: Aegis encrypts your code to make it difficult to
reverse engineer.
- Code Obfuscation: Aegis obfuscates your code by compiling it to
bytecode.
- License Management: Aegis allows you to manage licenses for your
applications.
Aegis currently supports the following node.js versions:
- 16.20.2, also likely other 16.x versions
To use Aegis, you need to install the package from npm:
``bash`
npm @feasibleone/aegis
Aegis works by requiring the package at the files you want to protect.
- Protecting files
Aegis will generate protected versions of the files upon process exit when the
AEGIS_BUILD environment variable is set. The encryption parameters for protectingAEGIS_CIPHER
the files can be set via the following environment variables:
- - The encryption algorithm to use.aes-256-cbc
The default is .AEGIS_KEY
- - The encryption key to use. If not set, a built-in key is used.AEGIS_IV
- - The initialization vector to use. If not set, a built-in IV is used.
- Naming of output files
By default Aegis does not overwrite the original files, instead it creates
new files by adding .cjs extension to the original file name. This is
done to prevent accidental loss of source code files at developer machines.
Usually Aegis will run in a CI job, where it can be configured to overwrite
the original source code file by setting the AEGIS_OVERWRITE environment
variable.
> [!WARNING]
> Aegis will overwrite any existing files without a warning.
> Even if you have not set AEGIS_OVERWRITE, be careful if you have.js.cjs
> files that match names of .js files.
To obfuscate your code, require aegis at the top of each file you want to
obfuscate, as shown below:
`javascript`
require('@feasibleone/aegis')(module);
Obfuscating code has some side effects, such as loosing line numbers from
call stack for the files that were obfuscated. This is because the code is
compiled to bytecode and the original source code is not available.
If you only want to encrypt your code, without obfuscating it, you can
pass an additional parameter, as shown below:
`javascript`
require('@feasibleone/aegis')(module, 'js');
Aegis returns an object, which can be used to check licenses. This
object has a property named active which is a function. This functionexpires
returns an object, that gives information about the licensed features.
This object always has a property named which holds theAEGIS_EXPIRED
timestamp of the license expiration date. If the license has expired
this function will throw an error with code andaegis.expired
type . In addition to the expires property, the
returned object can have other properties, depending on the license.
Example usage:
`javascript
const license = require('@feasibleone/aegis')(module);
// log the license information for the feature 'test'
console.log(license.active('test'));
`
In addition to checking the license information, you can also write
logic, which depends on the license data:
`javascript
const license = require('@feasibleone/aegis')(module);
console.log(You have a license for ${license.active('test').users} users)`
Aegis allows you to generate licenses for your applications.
The license contains the license terms and the cryptographic
parameters needed to load the protected files.
Be sure to set the same values (or defaults) for AEGIS_CIPHER,AEGIS_KEY and AEGIS_IV as you used to protect the files.
To generate a license, you can use the aegis command line tool. The tool is
installed with the package and can be used as shown below:
`bash`
AEGIS='{
"terms":{
"expires":2208988800000
},
"features":{
"test":{
"aegis":true
}
}
}' aegis
Generating is also possible via the API:
`javascript`
console.log(require('@feasibleone/aegis')({
terms: {
expires: 2208988800000
},
features: {
test: {
users: 10
}
}
}));
The license is a string, which can be saved to a file or a database.
Loading the license should happen early in the application, before
loading any of the protected files. The license is loaded as shown below:
`javascript`
require('@feasibleone/aegis')('license string');
`javascript``
require('@feasibleone/aegis').info('license string');