license refactor WIP

This commit is contained in:
Jan Prochazka
2024-08-13 16:29:07 +02:00
parent 42a79b2557
commit 75465bf415
12 changed files with 296 additions and 18 deletions

View File

@@ -12,6 +12,8 @@ const currentVersion = require('../currentVersion');
const platformInfo = require('../utility/platformInfo');
const connections = require('../controllers/connections');
const { getAuthProviderFromReq } = require('../auth/authProvider');
const isElectron = require('is-electron');
const { checkLicenseApp, checkLicenseWeb } = require('../utility/checkLicense');
const lock = new AsyncLock();
@@ -45,6 +47,9 @@ module.exports = {
'Basic authentization is not allowed, when using storage. Cannot use both STORAGE_DATABASE and BASIC_AUTH';
}
const checkedLicense = isElectron() ? checkLicenseApp() : checkLicenseWeb();
const isLicenseValid = checkedLicense?.status == 'ok';
return {
runAsPortal: !!connections.portalConnections,
singleDbConnection: connections.singleDbConnection,
@@ -55,8 +60,8 @@ module.exports = {
allowShellScripting: platformInfo.allowShellScripting,
isDocker: platformInfo.isDocker,
isElectron: platformInfo.isElectron,
isLicenseValid: platformInfo.isLicenseValid,
checkedLicense: platformInfo.checkedLicense,
isLicenseValid,
checkedLicense,
configurationError,
logoutUrl: await authProvider.getLogoutUrl(),
permissions,
@@ -125,6 +130,24 @@ module.exports = {
}
},
async loadLicenseKey() {
try {
const licenseKey = await fs.readFile(path.join(datadir(), 'license.key'), { encoding: 'utf-8' });
return licenseKey;
} catch (err) {
return null;
}
},
saveLicenseKey_meta: true,
async saveLicenseKey({ licenseKey }) {
try {
await fs.writeFile(path.join(datadir(), 'license.key'), licenseKey);
} catch (err) {
return null;
}
},
updateSettings_meta: true,
async updateSettings(values, req) {
if (!hasPermission(`settings/change`, req)) return false;

View File

@@ -1,4 +1,11 @@
function checkLicense() {
function checkLicenseWeb() {
return {
status: 'ok',
type: 'community',
};
}
function checkLicenseApp() {
return {
status: 'ok',
type: 'community',
@@ -6,5 +13,6 @@ function checkLicense() {
}
module.exports = {
checkLicense,
checkLicenseWeb,
checkLicenseApp,
};

View File

@@ -3,7 +3,7 @@ const os = require('os');
const path = require('path');
const processArgs = require('./processArgs');
const isElectron = require('is-electron');
const { checkLicense } = require('./checkLicense');
const { checkLicenseWeb, checkLicenseApp } = require('./checkLicense');
const platform = process.env.OS_OVERRIDE ? process.env.OS_OVERRIDE : process.platform;
const isWindows = platform === 'win32';
@@ -13,7 +13,6 @@ const isDocker = fs.existsSync('/home/dbgate-docker/public');
const isDevMode = process.env.DEVMODE == '1';
const isNpmDist = !!global['IS_NPM_DIST'];
const isForkedApi = processArgs.isForkedApi;
const checkedLicense = checkLicense();
// function moduleAvailable(name) {
// try {
@@ -32,8 +31,6 @@ const platformInfo = {
isElectronBundle: isElectron() && !isDevMode,
isForkedApi,
isElectron: isElectron(),
checkedLicense,
isLicenseValid: checkedLicense?.status == 'ok',
isDevMode,
isNpmDist,
isSnap: process.env.ELECTRON_SNAP == 'true',