-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80e6ad6
commit c834a01
Showing
22 changed files
with
232 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,6 @@ src/**/*.ts | |
.github/ | ||
.git* | ||
|
||
# Wireguard | ||
addons/wg_go/wireguard-go | ||
|
||
# node-gyp | ||
build/**/* | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,24 @@ | ||
# 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. | ||
Efficiently manage your Wireguard interface right from nodejs, no `wg` required. | ||
|
||
> **Note** | ||
> | ||
> we have pre-copiled files for Windows, MacOS (x64/intel) and Linux (arm64, x86_64), else require `gcc` or `clang` installed to compile Node addon. | ||
With this module it is possible to: | ||
other tools are wrappers over `wg`, `wireguard-tools.js` is not like that, it is a `C/C++` addon in which you don't need to have `wg` installed, as this module has full compatibility of its own `wg`. | ||
|
||
- Add/Remove a Wireguard interface. | ||
- Add/Remove/Modify `peers` for an interface. | ||
- Add IPs to the interface. | ||
- Create `pre-shared`, `private` and `public` keys. | ||
- Write the Wireguard configuration file and convert it to JSON format. | ||
## Support to: | ||
|
||
## Example | ||
1. Userpsace [(wireguard-go)](https://git.zx2c4.com/wireguard-go/about/) | ||
2. Maneger wireguard interface (linux create if not exist's). | ||
3. Generate `pre-shared`, `private` and `public` keys. | ||
4. [wg-quick](https://man7.org/linux/man-pages/man8/wg-quick.8.html) file support. | ||
|
||
> **Note** | ||
> | ||
> we have pre-copiled files for Windows, MacOS (x64/intel) and Linux (arm64, x86_64) else arch and system require `gcc` or `clang` installed to compile Node addon. | ||
> | ||
> To manage the Wireguard interfaces in linux, root access is required. | ||
> | ||
> Another system's require [wireguard-go](https://github.com/WireGuard/wireguard-go) | ||
### Get Current peers and Statistics | ||
|
||
```ts | ||
import { parseWgDevice } from "wireguard-tools.js"; | ||
const wireguardInterfaces = parseWgDevice("wg0"); | ||
// Wg0 is the interface name. | ||
console.log("Wg0:\n%o", wireguardInterfaces); | ||
``` | ||
|
||
# Add/Update Wireguard interface | ||
|
||
```ts | ||
import { addDevice, parseWgDevice } from "wireguard-tools.js"; | ||
addDevice("wg0", { | ||
publicKey: "string", | ||
privateKey: "string", | ||
portListen: 27301, | ||
Address: [ | ||
"1.1.1.1/32", | ||
"8.8.8.8/32"/** Mark this peer to be removed, any changes remove this option */ | ||
], | ||
replacePeers: true, | ||
peers: { | ||
"publicKeyPeer": { | ||
removeMe: false, | ||
presharedKey: "string", | ||
keepInterval: 5, | ||
endpoint: "google.com:27301", | ||
allowedIPs: [ | ||
"8.8.8.8", | ||
"8.8.4.4." | ||
] | ||
} | ||
} | ||
}); | ||
const wireguardInterfaces = parseWgDevice("wg0"); | ||
// Wg0 is the interface name. | ||
console.log("Wg0:\n%o", wireguardInterfaces); | ||
``` | ||
|
||
### Parse wireguard configuration file | ||
|
||
```ts | ||
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); | ||
``` | ||
> Another system's require `wireguard-go` [(check this page)](https://github.com/WireGuard/wireguard-go) | ||
### Create Config | ||
## Examples | ||
|
||
```ts | ||
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); | ||
``` | ||
Moved to `examples` folder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { listDevices, parseWgDevice, addDevice, utils } from "wireguard-tools.js"; | ||
const deviceName = (await listDevices()).at(-1); | ||
if (!deviceName) { | ||
console.info("Create wireguard interface"); | ||
process.exit(-1); | ||
} | ||
|
||
// Get current config | ||
const currentConfig = await parseWgDevice(deviceName); | ||
|
||
// Create Peer | ||
const peerKey = await utils.keygenAsync(true); | ||
currentConfig.peers[peerKey.public] = { presharedKey: peerKey.preshared, allowedIPs: [ utils.ipManipulation.randomIp("10.22.66.1/24", Object.keys(currentConfig.peers).map(k => currentConfig.peers[k].allowedIPs).flat(2).filter(Boolean)) ] }; | ||
currentConfig.peers[peerKey.public].allowedIPs.push(utils.ipManipulation.toV6(currentConfig.peers[peerKey.public].allowedIPs.at(0))); | ||
console.log("Add %O peer", peerKey.public); | ||
|
||
// Set to interface | ||
await addDevice(deviceName, currentConfig); | ||
console.dir(currentConfig, { colors: true, depth: null }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "add_peer", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"start": "node index.mjs" | ||
}, | ||
"dependencies": { | ||
"wireguard-tools.js": "file:../.." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * as userspace from "./userspace"; | ||
export * as utils from "./utils/index"; | ||
export * from "./mergeSets"; | ||
export * as userspace from "./userspace"; | ||
export * from "./mergeSets"; |
Oops, something went wrong.