wireguard-config-parse 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +45 -0
  3. package/index.js +58 -0
  4. package/package.json +14 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ricco020
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # wireguard-config-parse
2
+
3
+ Parse a **WireGuard `.conf`** file into a plain object: one `[Interface]` and any
4
+ number of `[Peer]` sections. Zero dependencies, comment-safe. Ports become numbers
5
+ and comma-separated fields (`Address`, `DNS`, `AllowedIPs`) become arrays.
6
+
7
+ ```js
8
+ const parse = require('wireguard-config-parse');
9
+
10
+ parse(`[Interface]
11
+ PrivateKey = <key>
12
+ Address = 10.0.0.1/24
13
+ ListenPort = 51820
14
+
15
+ [Peer]
16
+ PublicKey = <key>
17
+ AllowedIPs = 0.0.0.0/0, ::/0
18
+ Endpoint = 203.0.113.5:51820
19
+ PersistentKeepalive = 25`);
20
+ // {
21
+ // interface: { PrivateKey:'<key>', Address:['10.0.0.1/24'], ListenPort:51820 },
22
+ // peers: [ { PublicKey:'<key>', AllowedIPs:['0.0.0.0/0','::/0'],
23
+ // Endpoint:'203.0.113.5:51820', PersistentKeepalive:25 } ]
24
+ // }
25
+ ```
26
+
27
+ ## Behaviour
28
+
29
+ - Lines starting with `#` or `;`, and inline ` #` comments, are ignored.
30
+ - `ListenPort`, `PersistentKeepalive`, `MTU`, `FwMark` are coerced to numbers.
31
+ - `Address`, `DNS`, `AllowedIPs` are split into arrays on commas.
32
+ - Unknown sections are ignored; a missing `[Interface]` yields `interface: null`.
33
+
34
+ **Honest note:** this parses structure and values only. It does not validate that
35
+ keys are cryptographically valid or that the config actually connects.
36
+
37
+ ## Notes
38
+
39
+ Maintained by the team behind [VPNSmith](https://www.vpnsmith.com), a self-hosted
40
+ VPN resource. For plain-English guides on WireGuard and running your own VPN, see
41
+ [vpnsmith.com](https://www.vpnsmith.com).
42
+
43
+ ## License
44
+
45
+ MIT
package/index.js ADDED
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * wireguard-config-parse
5
+ * Parse a WireGuard .conf file into a plain object: one [Interface] and any
6
+ * number of [Peer] sections. No dependencies, no network. Comment-safe.
7
+ * It parses structure and values; it does not validate keys cryptographically.
8
+ */
9
+
10
+ // Keys whose values are comma-separated lists in WireGuard configs.
11
+ var LIST_KEYS = { Address: true, DNS: true, AllowedIPs: true };
12
+
13
+ function coerce(key, value) {
14
+ if (key === 'ListenPort' || key === 'PersistentKeepalive' || key === 'MTU' || key === 'FwMark') {
15
+ var n = parseInt(value, 10);
16
+ return isNaN(n) ? value : n;
17
+ }
18
+ if (LIST_KEYS[key]) {
19
+ return value.split(',').map(function (s) { return s.trim(); }).filter(Boolean);
20
+ }
21
+ return value;
22
+ }
23
+
24
+ /**
25
+ * @param {string} text contents of a WireGuard .conf file
26
+ * @returns {{interface: (object|null), peers: object[]}}
27
+ */
28
+ function parse(text) {
29
+ if (typeof text !== 'string') { throw new TypeError('config must be a string'); }
30
+ var iface = null;
31
+ var peers = [];
32
+ var current = null; // reference to the section object being filled
33
+ var lines = text.split(/\r?\n/);
34
+ for (var i = 0; i < lines.length; i++) {
35
+ var line = lines[i].trim();
36
+ if (line === '' || line.charAt(0) === '#' || line.charAt(0) === ';') { continue; }
37
+ var section = line.match(/^\[(.+?)\]$/);
38
+ if (section) {
39
+ var name = section[1].toLowerCase();
40
+ if (name === 'interface') { iface = {}; current = iface; }
41
+ else if (name === 'peer') { var p = {}; peers.push(p); current = p; }
42
+ else { current = null; } // unknown section: ignore its keys
43
+ continue;
44
+ }
45
+ var eq = line.indexOf('=');
46
+ if (eq === -1 || current === null) { continue; }
47
+ var key = line.slice(0, eq).trim();
48
+ var value = line.slice(eq + 1).trim();
49
+ // strip inline comment
50
+ var hash = value.indexOf(' #');
51
+ if (hash !== -1) { value = value.slice(0, hash).trim(); }
52
+ current[key] = coerce(key, value);
53
+ }
54
+ return { interface: iface, peers: peers };
55
+ }
56
+
57
+ module.exports = parse;
58
+ module.exports.parse = parse;
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "wireguard-config-parse",
3
+ "version": "1.0.0",
4
+ "description": "Parse a WireGuard .conf file into a plain object (Interface + Peers). Zero dependencies, comment-safe, coerces ports to numbers and lists to arrays.",
5
+ "main": "index.js",
6
+ "type": "commonjs",
7
+ "scripts": { "test": "node test.js" },
8
+ "keywords": ["wireguard", "config", "parser", "vpn", "wg", "conf", "self-hosted"],
9
+ "homepage": "https://www.vpnsmith.com",
10
+ "author": "ricco020",
11
+ "license": "MIT",
12
+ "engines": { "node": ">=12" },
13
+ "files": ["index.js", "README.md", "LICENSE"]
14
+ }