Python

config.json

{
    "license_id": ""
}

main.py

import requests
import json
import logging
import time
from datetime import datetime

logging.basicConfig(filename='error_log.txt', level=logging.ERROR)

# Load the configuration file
with open('config.json') as config_file:
    config = json.load(config_file)

license_id = config['license_id']
server_ip = "http://localhost:8080"
product = 'pasta'
checktimesec = 10  # Define the check interval in seconds

def get_public_ip():
    """Retrieve the public IP address"""
    try:
        ip_address = requests.get('https://api.ipify.org').text
        return ip_address
    except Exception as e:
        logging.error(f"Error in finding public IP: {e}")
        return None

ip = get_public_ip()

if not ip:
    print("You cannot proceed without a valid IP.")
    logging.error("You cannot proceed without a valid IP.")
    exit(1)

def verify_license():
    """Verify the license with the server"""
    url = f"{server_ip}/verify"
    payload = {
        'license_id': license_id,
        'product': product,
        'ip': ip
    }

    try:
        response = requests.post(url, json=payload)
        data = response.json()

        if data.get('status'):
            expiration = data.get('expiration', 'Unknown')

            # Check if the expiration date is in the correct format
            try:
                expiration_date = datetime.strptime(expiration, '%Y-%m-%d').date()
                current_date = datetime.now().date()

                # Check if the license has expired
                if expiration_date < current_date:
                    print(f"License Expired\nLicense: {license_id}\nExpiration: {expiration}\nProduct: {product}")
                    exit(1)  # Exit the script if the license is expired

            except ValueError:
                print(f"Invalid expiration date format: {expiration}")
                logging.error(f"Invalid expiration date format: {expiration}")
                exit(1)

            # Check if the product matches
            if data.get('product') != product:
                expected_product = data.get('product', 'Unknown')
                print(f"Product Mismatch\nLicense: {license_id}\nProduct: {product} (Expected: {expected_product})\nIP: {ip}")
                exit(1)  # Exit if the product does not match

            print(f"Script is working (valid)\nLicense: {license_id}\nIP: {ip}\nExpiration: {expiration}\nProduct: {product}")
        
        else:
            # Handle errors (IP/product mismatch, etc.)
            expiration = data.get('expiration', 'Unknown')
            error_message = data.get('message', 'Unknown error')
            print(f"License Error: {error_message}\nLicense: {license_id}\nIP: {ip}\nExpiration: {expiration}\nProduct: {product}")
            exit(1)  # Exit on error

    except requests.RequestException as e:
        print(f"Error in communication with the server: {e}")
        logging.error(f"Error in communication with the server: {e}")
        exit(1)

# Function to check the license periodically every checktimesec seconds
def run_periodic_license_check():
    while True:
        verify_license()
        time.sleep(checktimesec)

if __name__ == "__main__":
    run_periodic_license_check()

Last updated