| … | … | @@ -8,11 +8,9 @@ |
| 8 | 8 | |
| 9 | 9 | import { GatewayIdentity } from './lib/gateway-identity.js' |
| 10 | 10 | import { UTUNManager } from './lib/utun-manager.js' |
| 11 | | import { ServiceRegistry } from './lib/service-registry.js' |
| 12 | 11 | import { HyperswarmRouter } from './lib/hyperswarm-router.js' |
| 13 | 12 | import fs from 'bare-fs/promises' |
| 14 | 13 | import minimist from 'minimist' |
| 15 | | import crypto from 'hypercore-crypto' |
| 16 | 14 | |
| 17 | 15 | const VERSION = '0.1.0' |
| 18 | 16 | |
| … | … | @@ -84,59 +82,11 @@ async function startGateway(config) { |
| 84 | 82 | // Initialize GatewayIdentity |
| 85 | 83 | console.log('📝 Initializing GatewayIdentity...') |
| 86 | 84 | const gatewayIdentity = new GatewayIdentity({ |
| 87 | | hyperidentPath: config.hyperident && config.hyperident.identityPath, |
| 88 | 85 | subnet: config.gateway.subnet, |
| 89 | | gatewayIP: config.gateway.virtualIP, |
| 90 | | staticMappings: config.staticMappings || {} |
| 86 | gatewayIP: config.gateway.virtualIP |
| 91 | 87 | }) |
| 92 | 88 | console.log(` Subnet: ${config.gateway.subnet}`) |
| 93 | | console.log(` Gateway IP: ${config.gateway.virtualIP}`) |
| 94 | | console.log(` Static mappings: ${Object.keys(config.staticMappings || {}).length}\n`) |
| 95 | | |
| 96 | | // Initialize ServiceRegistry |
| 97 | | const serviceRegistry = new ServiceRegistry(gatewayIdentity) |
| 98 | | |
| 99 | | // Register services from config |
| 100 | | console.log('📝 Registering services...') |
| 101 | | const services = config.services || [] |
| 102 | | |
| 103 | | for (const serviceConfig of services) { |
| 104 | | if (!serviceConfig.enabled) { |
| 105 | | console.log(` ⏭️ Skipping ${serviceConfig.name} (disabled)`) |
| 106 | | continue |
| 107 | | } |
| 108 | | |
| 109 | | try { |
| 110 | | // For services with static IPs, manually register |
| 111 | | const staticIP = config.staticMappings && config.staticMappings[serviceConfig.name] |
| 112 | | if (staticIP) { |
| 113 | | // Generate a deterministic topic from the service name |
| 114 | | const topic = crypto.hash(Buffer.from(`hyperident-gateway:${serviceConfig.name}`)).subarray(0, 32) |
| 115 | | |
| 116 | | gatewayIdentity.services.set(staticIP, { |
| 117 | | name: serviceConfig.name, |
| 118 | | profile: serviceConfig.profile, |
| 119 | | ip: staticIP, |
| 120 | | topic, |
| 121 | | port: serviceConfig.port, |
| 122 | | transport: serviceConfig.transport || 'tcp', |
| 123 | | connections: [] |
| 124 | | }) |
| 125 | | console.log(` ✅ ${serviceConfig.name} -> ${staticIP}:${serviceConfig.port}`) |
| 126 | | } else { |
| 127 | | // For services with hyperident profiles, use registerService |
| 128 | | await serviceRegistry.register(serviceConfig) |
| 129 | | const service = gatewayIdentity.lookup(serviceConfig.ip) |
| 130 | | console.log(` ✅ ${serviceConfig.name} -> ${service.ip}:${service.port}`) |
| 131 | | } |
| 132 | | } catch (error) { |
| 133 | | console.error(` ❌ Failed to register ${serviceConfig.name}: ${error.message}`) |
| 134 | | console.error(` Create profile with: hyperident create-profile ${serviceConfig.profile}`) |
| 135 | | } |
| 136 | | } |
| 137 | | |
| 138 | | const stats = serviceRegistry.getStats() |
| 139 | | console.log(`\n Total: ${stats.total} services registered\n`) |
| 89 | console.log(` Gateway IP: ${config.gateway.virtualIP}\n`) |
| 140 | 90 | |
| 141 | 91 | // Initialize HyperswarmRouter (before UTUNManager so we can reference it) |
| 142 | 92 | const hyperswarmRouter = new HyperswarmRouter({ |
| … | … | @@ -219,20 +169,19 @@ async function startGateway(config) { |
| 219 | 169 | console.log('============================================================\n') |
| 220 | 170 | console.log(`Virtual IP: ${config.gateway.virtualIP}`) |
| 221 | 171 | console.log(`Subnet: ${config.gateway.subnet}`) |
| 222 | | console.log(`Services: ${stats.total}`) |
| 172 | console.log(`Network Topic: ${config.gateway.networkTopic || 'hyperident-network'}`) |
| 223 | 173 | console.log() |
| 224 | 174 | console.log('Press Ctrl+C to stop\n') |
| 225 | 175 | |
| 226 | 176 | // Log stats periodically |
| 227 | 177 | setInterval(() => { |
| 228 | 178 | const tunStats = utunManager.getStats() |
| 229 | | const svcStats = serviceRegistry.getStats() |
| 230 | 179 | const routerStats = hyperswarmRouter.getStats() |
| 231 | 180 | |
| 232 | 181 | console.log(`[${new Date().toISOString()}] Stats:`) |
| 233 | 182 | console.log(` TUN: ${tunStats.packetsReceived} rx, ${tunStats.packetsSent} tx, ${tunStats.packetsDropped} dropped`) |
| 234 | 183 | console.log(` P2P: ${routerStats.packetsFromP2P} rx, ${routerStats.packetsToP2P} tx, ${routerStats.totalConnections} connections`) |
| 235 | | console.log(` Services: ${svcStats.online}/${svcStats.total} online`) |
| 184 | console.log(` Routing: ${routerStats.routingTableSize} known IPs`) |
| 236 | 185 | }, 60000) // Every minute |
| 237 | 186 | } |
| 238 | 187 | |