fix: Usage logging improvements.

This commit is contained in:
Sam Tolmay 2023-11-10 11:39:58 +02:00
parent b50d6043d6
commit 5757ad1a6b
No known key found for this signature in database
4 changed files with 20 additions and 10 deletions

View File

@ -13,6 +13,7 @@
function createLogUsage({ usageDataRef }) {
let lastTimestamp = 0;
let isOffline = false;
let machine = localStorage.getItem('lowdefy_machine_id');
if (!machine) {
machine = crypto.randomUUID();
@ -20,7 +21,7 @@ function createLogUsage({ usageDataRef }) {
}
async function logUsage() {
if (lastTimestamp > Date.now() - 1000 * 10) {
if (isOffline || lastTimestamp > Date.now() - 1000 * 60 * 60) {
return;
}
lastTimestamp = Date.now();
@ -33,11 +34,17 @@ function createLogUsage({ usageDataRef }) {
body: JSON.stringify({ user: usageDataRef.current.user, machine }),
});
const { offline, data } = await res.json();
console.log({ offline, data });
if (offline) {
isOffline = true;
return;
}
// fetch ldf endpoint
await fetch('https://billing.lowdefy.net/v4/billing/usage', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
}
return logUsage;
}

View File

@ -9,6 +9,7 @@
*/
import crypto from 'crypto';
import appJson from '../../build/app.json';
import packageJson from '../../package.json';
import apiWrapper from '../../lib/server/apiWrapper.js';
import validateLicense from '../../lib/server/validateLicense.js';
@ -24,12 +25,11 @@ async function handler({ context, req, res }) {
if (license.entitlements.includes['OFFLINE']) {
return res.status(200).json({ offline: true });
}
if (license.code === 'NO_LICENSE') {
throw new Error('No license key found.'); // TODO:
}
const timestamp = Date.now();
const data = [
`git_sha: ${appJson.git_sha}`,
`host: ${req.headers.host}`,
`license_key: ${license.id}`,
`machine: ${machine}`,
`timestamp: ${timestamp}`,
@ -37,20 +37,20 @@ async function handler({ context, req, res }) {
`version: ${packageJson.version}`,
].join('\n');
const hmac = crypto.createHmac('sha256', process.env.LOWDEFY_LICENSE_KEY);
const hmac = crypto.createHmac('sha256', process.env.LOWDEFY_LICENSE_KEY ?? 'NO_LICENSE');
hmac.update(data);
const sig = hmac.digest('base64');
// post to lowdefy api
return res.status(200).json({
offline: false,
data: {
git_sha: appJson.git_sha,
host: req.headers.host,
license_key: license.id,
machine,
sig,
timestamp,
user,
sig,
version: packageJson.version,
},
});

View File

@ -25,6 +25,7 @@ async function keygenValidateLicense({ config }) {
// TODO: Return this or undefined/null?
if (!licenseKey) {
return {
id: 'NO_LICENSE',
code: 'NO_LICENSE',
entitlements,
};
@ -97,6 +98,7 @@ async function keygenValidateLicense({ config }) {
code: meta?.code,
entitlements,
expiry: data?.attributes?.expiry ? new Date(data?.attributes?.expiry) : undefined,
metadata: data?.attributes?.metadata,
timestamp: new Date(), // TODO: timestamp, validated, validated_at, validated_ts?
};
}

View File

@ -50,6 +50,7 @@ async function keygenValidateLicenseOffline({ config, licenseKey }) {
code: expiry.valueOf() < Date.now() ? 'EXPIRED' : 'VALID',
entitlements: decoded.entitlements,
expiry: expiry,
metadata: decoded.metadata,
timestamp: new Date(),
};
}