UNPKG

@synet/net

Version:

Network abstraction layer for Synet. visit https://syntehtism.ai for more information.

150 lines (149 loc) 4.49 kB
"use strict"; /** * Main service for managing WireGuard operations * Implements simplified error handling */ Object.defineProperty(exports, "__esModule", { value: true }); exports.WireguardService = void 0; class WireguardService { /** * Creates a new instance of WireGuardService * @param adapter The WireGuard adapter implementation to use * @param logger Optional logger instance */ constructor(adapter, logger) { this.adapter = adapter; this.logger = logger; } /** * Get existing WireGuard keys */ async getKeys() { this.logInfo("Getting WireGuard keys"); return await this.adapter.getKeys(); } /** * Generate new WireGuard keys */ async generateKeys() { this.logInfo("Generating new WireGuard keys"); return await this.adapter.generateKeys(); } /** * Set up WireGuard interface */ async setInterface(config) { this.logInfo(`Setting up WireGuard interface with address ${config.address}`); return await this.adapter.setInterface(config); } /** * Add a peer to the WireGuard interface */ async addPeer(config) { this.logInfo(`Adding peer with public key ${config.publicKey.substring(0, 10)}...`); return await this.adapter.addPeer(config); } async removeInterface() { this.logInfo("Removing WireGuard interface"); return await this.adapter.removeInterface(); } async reloadInterface() { this.logInfo("Reloading WireGuard interface"); return await this.adapter.reload(); } /** * Remove a peer from the WireGuard interface */ async destroy() { this.logInfo("Destroying WireGuard interface and removing all peers..."); await this.adapter.removeInterface(); await this.adapter.removeAllPeers(); return await this.adapter.bringDown(); } /** * Remove a peer from the WireGuard interface */ async removePeer(publicKey) { this.logInfo(`Removing peer with public key ${publicKey.substring(0, 10)}...`); return await this.adapter.removePeer(publicKey); } /** * Remove a peer from the WireGuard interface */ async removeAllPeers() { this.logInfo("Removing all peers from config..."); return await this.adapter.removeAllPeers(); } /** * Bring up the WireGuard interface */ async bringUp() { this.logInfo("Bringing up interface via WireGuard"); return await this.adapter.bringUp(); } /** * Bring down the WireGuard interface */ async bringDown() { this.logInfo("Bringing down WireGuard interface"); return await this.adapter.bringDown(); } /** * Convenience method to configure and connect in one go */ async connect(config) { this.logInfo(`Connecting to peer at ${config.peer.endpoint}`); // Set up interface const interfaceResult = await this.setInterface({ privateKey: config.privateKey, address: config.address, }); if (interfaceResult.isFailure) { return interfaceResult; } // Add peer const peerResult = await this.addPeer(config.peer); if (peerResult.isFailure) { return peerResult; } // Bring up interface return await this.bringUp(); } /** * Convenience method to disconnect */ async disconnect(peerPublicKey) { this.logInfo("Disconnecting from network"); return await this.removePeer(peerPublicKey); } async status() { this.logInfo("Getting WireGuard status"); return await this.adapter.status(); } /** * Get peer status * @returns Result<void> */ async peerStatus(publicKey) { this.logInfo("Getting WireGuard peers status"); return await this.adapter.peerStatus(publicKey); } async listPeers() { this.logInfo("Listing WireGuard peers"); return await this.adapter.listPeers(); } /** * Remove all peers from the WireGuard interface */ // Helper methods for logging logInfo(message) { this.logger?.info(message); } logWarn(message) { this.logger?.warn(message); } logError(message, error) { this.logger?.error(message, error); } } exports.WireguardService = WireguardService;