UNPKG

wireguard-tools

Version:

The best way to interact with WireGuard from Node

76 lines (75 loc) 3.33 kB
import { WgConfigObject, WgConfigPeer } from './types/WgConfigObject'; interface GenerateKeysOptions { /** Also create a preshared key */ preSharedKey?: boolean; /** * Overwrite this object's private key if it already exists * * If this is not set to true, a new public key will be generated from the existing private key if * the private key exists. */ overwrite?: boolean; } /** A Javascript object representation of a WireGuard config file with some extras */ export declare class WgConfig implements WgConfigObject { /** Defines the VPN settings for the local node. */ wgInterface: WgConfigObject['wgInterface']; /** An array of VPN settings for remote peers */ peers: WgConfigObject['peers']; /** A place to keep the public key for this node (it's not saved in the WireGuard config) */ publicKey: WgConfigObject['publicKey']; /** A place to keep the pre-shared key for this node (it's not saved in the WireGuard config) */ preSharedKey: WgConfigObject['preSharedKey']; /** The file path where this config should be written to by default */ filePath: string; /** creates a new WgConfig */ constructor(init: Partial<WgConfigObject> & { filePath?: string; }); /** Return a string akin to a WireGuard config file from this WgConfig object */ toString(): string; /** JSON.stringify this WgConfig object */ toJson(): string; /** Parse a WireGuard config file in the form of a string into this WgConfig object */ parse(configAsString: string): void; /** Parse a WireGuard config file from it's path in the file system */ parseFile(filePath?: string): Promise<void>; /** Write this WgConfig object as a WireGuard config file to a file in the system */ writeToFile(filePath?: string): Promise<void>; /** Generate a public/private key pair for this WgConfig object */ generateKeys(opts?: GenerateKeysOptions): Promise<{ privateKey: string; publicKey: string; preSharedKey: string | undefined; }>; /** * Add a peer to the peers for this WgConfig. * * If the peer already exists (found by public key) then it will be updated by merging the * existing with the new. The allowedIps will be replaced by the new peer's allowedIps unless * { mergeAllowedIps: true } is passed in as settings */ addPeer(peer: WgConfigPeer, settings?: { mergeAllowedIps?: boolean; }): void; /** * Remove a peer if it exists in the peer array by its public key */ removePeer(publicKey: string): void; /** Creates a WfgConfigPeer object from this WgCongig object */ createPeer(settings: Omit<WgConfigPeer, 'publicKey'>): WgConfigPeer; /** Get a peer from the peer array by it's public key */ getPeer(publicKey: string): WgConfigPeer | undefined; /** brings up the wireguard interface */ up(filePath?: string): Promise<void>; /** brings down the wireguard interface */ down(filePath?: string): Promise<void>; /** restarts the wireguard interface */ restart(filePath?: string): Promise<void>; /** Saves the config to file and restarts it unless `{ noUp: true }` is passed */ save(opts?: { filePath?: string; noUp: boolean; }): Promise<void>; } export {};