Javascript

config.json

{
  "license_id": "your_license_id_here"
}

main.js

const axios = require('axios');
const fs = require('fs');
const path = require('path');
const { setTimeout } = require('timers/promises');

// Load the configuration file
const configPath = path.join(__dirname, 'config.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));

const license_id = config.license_id;
const server_ip = "http://localhost:8080";
const product = 'pasta';
const checktimesec = 10;  // Define the check interval in seconds

// Function to get the public IP address
async function getPublicIP() {
    try {
        const response = await axios.get('https://api.ipify.org?format=json');
        return response.data.ip;
    } catch (error) {
        console.error(`Error in finding public IP: ${error}`);
        return null;
    }
}

async function verifyLicense() {
    const ip = await getPublicIP();
    if (!ip) {
        console.error("You cannot proceed without a valid IP.");
        process.exit(1);  // Exit if no valid IP is found
    }

    const payload = {
        license_id: license_id,
        product: product,
        ip: ip
    };

    try {
        const response = await axios.post(`${server_ip}/verify`, payload);
        const data = response.data;

        if (data.status) {
            const expiration = data.expiration || 'Unknown';

            // Check if the expiration date is in the correct format
            try {
                const expirationDate = new Date(expiration);
                const currentDate = new Date();

                // Check if the license has expired
                if (expirationDate < currentDate) {
                    console.log(`License Expired\nLicense: ${license_id}\nExpiration: ${expiration}\nProduct: ${product}`);
                    process.exit(1);  // Exit the script if the license is expired
                }
            } catch (error) {
                console.error(`Invalid expiration date format: ${expiration}`);
                process.exit(1);
            }

            // Check if the product matches
            if (data.product !== product) {
                const expectedProduct = data.product || 'Unknown';
                console.log(`Product Mismatch\nLicense: ${license_id}\nProduct: ${product} (Expected: ${expectedProduct})\nIP: ${ip}`);
                process.exit(1);  // Exit if the product does not match
            }

            console.log(`Script is working (valid)\nLicense: ${license_id}\nIP: ${ip}\nExpiration: ${expiration}\nProduct: ${product}`);
        } else {
            // Handle errors (IP/product mismatch, etc.)
            const expiration = data.expiration || 'Unknown';
            const errorMessage = data.message || 'Unknown error';
            console.log(`License Error: ${errorMessage}\nLicense: ${license_id}\nIP: ${ip}\nExpiration: ${expiration}\nProduct: ${product}`);
            process.exit(1);  // Exit on error
        }
    } catch (error) {
        console.error(`Error in communication with the server: ${error}`);
        process.exit(1);  // Exit if communication with server fails
    }
}

// Function to check the license periodically every checktimesec seconds
async function runPeriodicLicenseCheck() {
    while (true) {
        await verifyLicense();
        await setTimeout(checktimesec * 1000);  // Wait for checktimesec milliseconds 
    }
}

// Start the periodic license check
runPeriodicLicenseCheck();

Last updated