đ JavaScript Code Examples
Node.js Exampleâ
const https = require('https');
const http = require('http');
const crypto = require('crypto');
const os = require('os');
const readline = require('readline');
const API_KEY = 'your-api-key-here';
const API_URL = 'http://your-domain.com/api/license/auth';
const PRODUCT = 'MyJSApp';
const VERSION = '1.0.0';
class TKIAuth {
static getHWID() {
// Generate HWID based on system info
const cpus = os.cpus();
const hostname = os.hostname();
const platform = os.platform();
const arch = os.arch();
const hwString = `${hostname}-${platform}-${arch}-${cpus[0].model}`;
return 'JS-' + crypto.createHash('md5').update(hwString).digest('hex').substring(0, 16);
}
static getLocalIP() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return iface.address;
}
}
}
return '127.0.0.1';
}
static async authenticate(licenseKey) {
return new Promise((resolve, reject) => {
const payload = {
data: {
product: PRODUCT,
version: VERSION,
licensekey: licenseKey,
ip: this.getLocalIP(),
hwid: this.getHWID()
}
};
const jsonData = JSON.stringify(payload);
const url = new URL(API_URL);
const httpModule = url.protocol === 'https:' ? https : http;
const options = {
hostname: url.hostname,
port: url.port || (url.protocol === 'https:' ? 443 : 80),
path: url.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': API_KEY,
'Content-Length': Buffer.byteLength(jsonData)
},
timeout: 30000
};
const req = httpModule.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(data);
if (res.statusCode === 200 && response.status_overview === 'success') {
console.log('â License authenticated!');
console.log(`User: ${response.discord_username}`);
console.log(`Expires: ${response.expire_date}`);
console.log(`Staff License: ${response.staff_license}`);
resolve(true);
} else {
console.log(`â Authentication failed: ${response.message || 'Unknown error'}`);
resolve(false);
}
} catch (error) {
console.log('â Failed to parse response');
resolve(false);
}
});
});
req.on('error', (error) => {
console.log(`â Network error: ${error.message}`);
resolve(false);
});
req.on('timeout', () => {
console.log('â Request timeout');
req.abort();
resolve(false);
});
req.write(jsonData);
req.end();
});
}
}
// Main function
async function main() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter license key: ', async (licenseKey) => {
const isValid = await TKIAuth.authenticate(licenseKey.trim());
if (isValid) {
console.log('Starting application...');
// Your application logic here
} else {
console.log('Authentication failed!');
process.exit(1);
}
rl.close();
});
}
main().catch(console.error);
Browser Exampleâ
<!DOCTYPE html>
<html>
<head>
<title>TKI Auth Example</title>
</head>
<body>
<h1>License Authentication</h1>
<input type="text" id="licenseKey" placeholder="Enter license key">
<button onclick="authenticate()">Authenticate</button>
<div id="result"></div>
<script>
const API_KEY = 'your-api-key-here';
const API_URL = 'http://your-domain.com/api/license/auth';
const PRODUCT = 'MyWebApp';
const VERSION = '1.0.0';
class TKIAuth {
static getHWID() {
// Browser fingerprinting for HWID
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.textBaseline = 'top';
ctx.font = '14px Arial';
ctx.fillText('TKI Auth fingerprint', 2, 2);
const fingerprint = canvas.toDataURL() +
navigator.userAgent +
navigator.language +
screen.width + 'x' + screen.height;
// Simple hash function
let hash = 0;
for (let i = 0; i < fingerprint.length; i++) {
const char = fingerprint.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return 'WEB-' + Math.abs(hash).toString(36).substring(0, 16);
}
static async getPublicIP() {
try {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
return data.ip;
} catch (error) {
return '127.0.0.1';
}
}
static async authenticate(licenseKey) {
try {
const ip = await this.getPublicIP();
const hwid = this.getHWID();
const payload = {
data: {
product: PRODUCT,
version: VERSION,
licensekey: licenseKey,
ip: ip,
hwid: hwid
}
};
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': API_KEY
},
body: JSON.stringify(payload)
});
const data = await response.json();
if (response.ok && data.status_overview === 'success') {
return {
success: true,
data: data
};
} else {
return {
success: false,
error: data.message || 'Authentication failed'
};
}
} catch (error) {
return {
success: false,
error: 'Network error: ' + error.message
};
}
}
}
async function authenticate() {
const licenseKey = document.getElementById('licenseKey').value.trim();
const resultDiv = document.getElementById('result');
if (!licenseKey) {
resultDiv.innerHTML = '<p style="color: red;">Please enter a license key</p>';
return;
}
resultDiv.innerHTML = '<p>Authenticating...</p>';
const result = await TKIAuth.authenticate(licenseKey);
if (result.success) {
resultDiv.innerHTML = `
`;
// Your application logic here
console.log('Application started successfully');
} else {
resultDiv.innerHTML = `
`;
}
}
</script>
</body>
</html>
Express.js Server Exampleâ
const express = require('express');
const axios = require('axios');
const crypto = require('crypto');
const session = require('express-session');
const app = express();
const API_KEY = 'your-api-key-here';
const API_URL = 'http://your-domain.com/api/license/auth';
const PRODUCT = 'MyExpressApp';
const VERSION = '1.0.0';
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
secret: 'your-session-secret',
resave: false,
saveUninitialized: false
}));
class TKIAuth {
static getHWID(req) {
const userAgent = req.get('User-Agent') || '';
const acceptLanguage = req.get('Accept-Language') || '';
const ip = req.ip || '';
const hwString = userAgent + acceptLanguage + ip;
return 'EXPRESS-' + crypto.createHash('md5').update(hwString).digest('hex').substring(0, 16);
}
static async authenticate(licenseKey, req) {
try {
const payload = {
data: {
product: PRODUCT,
version: VERSION,
licensekey: licenseKey,
ip: req.ip || '127.0.0.1',
hwid: this.getHWID(req)
}
};
const response = await axios.post(API_URL, payload, {
headers: {
'Content-Type': 'application/json',
'Authorization': API_KEY
},
timeout: 30000
});
if (response.status === 200 && response.data.status_overview === 'success') {
return {
success: true,
data: response.data
};
} else {
return {
success: false,
error: response.data.message || 'Authentication failed'
};
}
} catch (error) {
return {
success: false,
error: error.response?.data?.message || error.message
};
}
}
}
// Middleware to check license
async function requireLicense(req, res, next) {
if (req.session.authenticated) {
return next();
}
res.redirect('/login');
}
// Routes
app.get('/', requireLicense, (req, res) => {
res.send(`
<h1>Welcome to Licensed App!</h1>
<p>User: ${req.session.userInfo.discord_username}</p>
<p>Expires: ${req.session.userInfo.expire_date}</p>
<a href="/logout">Logout</a>
`);
});
app.get('/login', (req, res) => {
res.send(`
<h1>License Authentication</h1>
<form method="post" action="/authenticate">
<input type="text" name="licenseKey" placeholder="Enter license key" required>
<button type="submit">Authenticate</button>
</form>
${req.query.error ? `<p style="color: red;">${req.query.error}</p>` : ''}
`);
});
app.post('/authenticate', async (req, res) => {
const { licenseKey } = req.body;
if (!licenseKey) {
return res.redirect('/login?error=Please enter a license key');
}
const result = await TKIAuth.authenticate(licenseKey, req);
if (result.success) {
req.session.authenticated = true;
req.session.userInfo = result.data;
res.redirect('/');
} else {
res.redirect(`/login?error=${encodeURIComponent(result.error)}`);
}
});
app.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/login');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Package.json Dependenciesâ
{
"dependencies": {
"express": "^4.18.2",
"express-session": "^1.17.3",
"axios": "^1.4.0"
}
}