// ==================================================================== // 602TechSec List Management API - JavaScript Client Example // ==================================================================== // This is a complete example showing how to integrate the List Management // API into your JavaScript application (Browser or Node.js). // // USAGE: // 1. Copy this file to your project // 2. Replace "your-api-key-here" with your actual API key // 3. Use in browser or Node.js // ==================================================================== class ListManagementClient { constructor(apiKey) { this.apiKey = apiKey; this.baseUrl = 'https://sec.602.tech'; } // ==================== IP Management ==================== /** * Add an IP address to whitelist or blacklist * @param {string} ipAddress - The IP address to add * @param {string} listType - "whitelist" or "blacklist" * @returns {Promise} API response */ async addIpAddress(ipAddress, listType = 'whitelist') { return await this._post('/api/listmanagement/ip/add', { apiKey: this.apiKey, ipAddress: ipAddress, listType: listType }); } /** * Remove an IP address from whitelist or blacklist * @param {string} ipAddress - The IP address to remove * @param {string} listType - "whitelist" or "blacklist" * @returns {Promise} API response */ async removeIpAddress(ipAddress, listType = 'whitelist') { return await this._post('/api/listmanagement/ip/remove', { apiKey: this.apiKey, ipAddress: ipAddress, listType: listType }); } // ==================== Host Management ==================== /** * Add a host to whitelist or blacklist * @param {string} host - The host/domain to add * @param {string} listType - "whitelist" or "blacklist" * @returns {Promise} API response */ async addHost(host, listType = 'blacklist') { return await this._post('/api/listmanagement/host/add', { apiKey: this.apiKey, host: host, listType: listType }); } /** * Remove a host from whitelist or blacklist * @param {string} host - The host/domain to remove * @param {string} listType - "whitelist" or "blacklist" * @returns {Promise} API response */ async removeHost(host, listType = 'blacklist') { return await this._post('/api/listmanagement/host/remove', { apiKey: this.apiKey, host: host, listType: listType }); } // ==================== Bulk Operations ==================== /** * Bulk add multiple entries * @param {Array} entries - Array of entries to add * @param {string} entryType - "ip" or "host" * @param {string} listType - "whitelist" or "blacklist" * @returns {Promise} API response with success/failure counts */ async bulkAdd(entries, entryType = 'ip', listType = 'whitelist') { return await this._post('/api/listmanagement/bulk/add', { apiKey: this.apiKey, entries: entries, entryType: entryType, listType: listType }); } // ==================== Query Operations ==================== /** * Get all lists for the API key * @returns {Promise} Lists response with all whitelists/blacklists */ async getAllLists() { try { const response = await fetch( `${this.baseUrl}/api/listmanagement/lists?apiKey=${this.apiKey}` ); return await response.json(); } catch (error) { return { success: false, message: error.message }; } } /** * Check if an entry exists in whitelist or blacklist * @param {string} entry - The entry to check * @param {string} entryType - "ip" or "host" * @returns {Promise} Check response */ async checkEntry(entry, entryType = 'ip') { return await this._post('/api/listmanagement/check', { apiKey: this.apiKey, entry: entry, entryType: entryType }); } // ==================== Convenience Methods ==================== /** * Add an IP to whitelist (convenience method) */ async whitelistIp(ipAddress) { return await this.addIpAddress(ipAddress, 'whitelist'); } /** * Add an IP to blacklist (convenience method) */ async blacklistIp(ipAddress) { return await this.addIpAddress(ipAddress, 'blacklist'); } /** * Add a host to whitelist (convenience method) */ async whitelistHost(host) { return await this.addHost(host, 'whitelist'); } /** * Add a host to blacklist (convenience method) */ async blacklistHost(host) { return await this.addHost(host, 'blacklist'); } /** * Check if IP is whitelisted */ async isIpWhitelisted(ipAddress) { const result = await this.checkEntry(ipAddress, 'ip'); return result.inWhitelist === true; } /** * Check if IP is blacklisted */ async isIpBlacklisted(ipAddress) { const result = await this.checkEntry(ipAddress, 'ip'); return result.inBlacklist === true; } /** * Check if host is whitelisted */ async isHostWhitelisted(host) { const result = await this.checkEntry(host, 'host'); return result.inWhitelist === true; } /** * Check if host is blacklisted */ async isHostBlacklisted(host) { const result = await this.checkEntry(host, 'host'); return result.inBlacklist === true; } // ==================== Private Helper Methods ==================== async _post(endpoint, data) { try { const response = await fetch(`${this.baseUrl}${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await response.json(); } catch (error) { return { success: false, message: error.message, timestamp: new Date().toISOString() }; } } } // ==================================================================== // USAGE EXAMPLES // ==================================================================== // Example 1: Basic usage async function basicExample() { const client = new ListManagementClient('your-api-key-here'); // Add IP to whitelist const result = await client.whitelistIp('192.168.1.100'); console.log('Whitelist IP:', result); // Add host to blacklist const result2 = await client.blacklistHost('malicious.com'); console.log('Blacklist host:', result2); } // Example 2: Bulk operations async function bulkExample() { const client = new ListManagementClient('your-api-key-here'); // Bulk add IPs to whitelist const ips = ['192.168.1.100', '192.168.1.101', '10.0.0.1']; const result = await client.bulkAdd(ips, 'ip', 'whitelist'); console.log(`Added ${result.successCount} IPs, ${result.failureCount} failed`); } // Example 3: Check and conditionally add async function checkAndAddExample() { const client = new ListManagementClient('your-api-key-here'); const ipToCheck = '192.168.1.100'; // Check if IP is already whitelisted const isWhitelisted = await client.isIpWhitelisted(ipToCheck); if (!isWhitelisted) { console.log('IP not whitelisted, adding...'); const result = await client.whitelistIp(ipToCheck); console.log('Result:', result); } else { console.log('IP already whitelisted'); } } // Example 4: Get and display all lists async function displayListsExample() { const client = new ListManagementClient('your-api-key-here'); const lists = await client.getAllLists(); console.log('=== Current Lists ==='); console.log(`IP Whitelist (${lists.ipWhitelist?.length || 0}):`); lists.ipWhitelist?.forEach(ip => console.log(` - ${ip}`)); console.log(`\nIP Blacklist (${lists.ipBlacklist?.length || 0}):`); lists.ipBlacklist?.forEach(ip => console.log(` - ${ip}`)); console.log(`\nHost Whitelist (${lists.hostWhitelist?.length || 0}):`); lists.hostWhitelist?.forEach(host => console.log(` - ${host}`)); console.log(`\nHost Blacklist (${lists.hostBlacklist?.length || 0}):`); lists.hostBlacklist?.forEach(host => console.log(` - ${host}`)); } // Example 5: Error handling async function errorHandlingExample() { const client = new ListManagementClient('your-api-key-here'); try { const result = await client.whitelistIp('192.168.1.100'); if (result.success) { console.log('? Success:', result.message); } else { console.error('? Failed:', result.message); } } catch (error) { console.error('? Network error:', error.message); } } // ==================================================================== // For Node.js: Export the client // ==================================================================== if (typeof module !== 'undefined' && module.exports) { module.exports = ListManagementClient; } // ==================================================================== // For Browser: Make available globally // ==================================================================== if (typeof window !== 'undefined') { window.ListManagementClient = ListManagementClient; }