2022-08-06 22:42:25 +00:00
2022-08-04 16:48:03 +00:00
2022-08-06 21:18:17 +00:00
2022-08-06 22:42:25 +00:00
2022-08-06 22:42:25 +00:00
2022-08-06 21:18:17 +00:00
2022-08-06 21:18:17 +00:00
2022-07-09 21:49:37 +00:00
2022-08-04 20:28:59 +00:00
2022-08-06 22:42:25 +00:00

Wireguard-tools.js

A quick way to use Wireguard with Node.js without having to run the Wireguard tools. We've included some wg command patterns to avoid confusion and to maintain a base between the tools.

In addition to having some basic utilities already integrated:

  • Keygen: Generate private and public keys and also a pre-shared key.
  • Config file: You can generate files for Wireguard directly from here and you can transform a configuration file into a JSON.

To manage Wireguard interfaces:

  • Create a new Wireguard network interface and configure it with the provided information.
  • Delete Interface.
  • Get the peers along with the interface and peer information.

Note

This module needs make and gcc/g++ or clang. this module needs some tools to compile libnl and embbeded-wireguard.

Example

Parse wireguard configuration file:

import { readFileSync } from "node:fs";
import { utils } from "wireguard-tools.js";
const configFile = readFileSync("/etc/wireguard/wg0.conf", "utf8");
const configJson = utils.config.parseConfig(configFile);
console.log("Config file JSON:\n%o", configJson.data);

Create Config:

import { utils } from "wireguard-tools.js";
const wireguardConfig = utils.config.writeConfig({
  interface: {
    private: "CEOntDE9saQaHLhD/WzZuYky3+elOfnBUCXoSveD3kc=",
    public: "xaZtpi3VCkBMhSTKM6jl/YjPJ370iYpBlLYwSyZ3W08=",
    address: [
      {
        ip: "10.0.0.1",
        subnet: 24
      }
    ]
  },
  peer: {
    "tF4YxTqLIJdNQcqvz1jtIF993zSk79hP+zdBxQlaowA=": {
      Keepalive: 25,
      Endpoint: {
        host: "wireguard.example.com",
        port: 51820
      },
    }
  }
});
console.log("Config file:\n%s", wireguardConfig);

or

Note

To save to wireguard's default directory (/etc/wireguard), it is necessary to have root access or a user with write permissions on the directory.

import { utils } from "wireguard-tools.js";
utils.config.writeConfig({
  interface: {
    private: "CEOntDE9saQaHLhD/WzZuYky3+elOfnBUCXoSveD3kc=",
    public: "xaZtpi3VCkBMhSTKM6jl/YjPJ370iYpBlLYwSyZ3W08=",
    address: [
      {
        ip: "10.0.0.5",
        subnet: 24
      }
    ]
  },
  peer: {
    "tF4YxTqLIJdNQcqvz1jtIF993zSk79hP+zdBxQlaowA=": {
      Keepalive: 25,
      Endpoint: {
        host: "wireguard.example.com",
        port: 51820
      },
    }
  }
}, "wg0");

Get Current peers and Statistics:

import { show } from "wireguard-tools.js";
const wireguardInterfaces = show("wg0");
// Wg0 is the interface name.
console.log("Wg0:\n%o", wireguardInterfaces);
Languages
C++ 45.4%
C 35.3%
TypeScript 14.8%
CMake 1.9%
Go 1.4%
Other 1.2%