@auto-forge-org/stunly 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 +18 -0
  3. package/index.js +119 -0
  4. package/package.json +18 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 auto-forge-org
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,18 @@
1
+ # @auto-forge-org/stunly
2
+
3
+ stunly is a lightweight STUN Binding server (RFC 5389) implemented in pure JavaScript. It provides a small, dependency-free server suitable for testing and lightweight deployments.
4
+
5
+ Usage:
6
+
7
+ ```js
8
+ const { StunServer } = require('@auto-forge-org/stunly');
9
+
10
+ const s = new StunServer({ port: 3478, software: 'stunly/1.0' });
11
+ s.start(() => console.log('stunly listening on 3478'));
12
+
13
+ // stop with s.stop()
14
+ ```
15
+
16
+ The server responds to STUN Binding requests with XOR-MAPPED-ADDRESS and SOFTWARE attributes plus FINGERPRINT.
17
+
18
+ License: MIT
package/index.js ADDED
@@ -0,0 +1,119 @@
1
+ const dgram = require('dgram');
2
+
3
+ const MAGIC_COOKIE = 0x2112A442;
4
+
5
+ function crc32(buf) {
6
+ const table = crc32.table || (crc32.table = (function() {
7
+ const t = new Uint32Array(256);
8
+ for (let i = 0; i < 256; i++) {
9
+ let c = i;
10
+ for (let k = 0; k < 8; k++) c = (c & 1) ? 0xEDB88320 ^ (c >>> 1) : (c >>> 1);
11
+ t[i] = c >>> 0;
12
+ }
13
+ return t;
14
+ })());
15
+ let crc = 0xFFFFFFFF;
16
+ for (let i = 0; i < buf.length; i++) crc = (crc >>> 8) ^ table[(crc ^ buf[i]) & 0xFF];
17
+ return (crc ^ 0xFFFFFFFF) >>> 0;
18
+ }
19
+
20
+ function buildXorMappedAttribute(addr, port, txid) {
21
+ const family = addr.includes(':') ? 0x02 : 0x01; // IPv6 not handled fully
22
+ const attrType = 0x0020; // XOR-MAPPED-ADDRESS
23
+ const portX = port ^ (MAGIC_COOKIE >>> 16);
24
+ const ip = addr.split('.').map(Number);
25
+ const ipBuf = Buffer.from(ip.map((b, i) => b ^ ((MAGIC_COOKIE >>> ((3 - i) * 8)) & 0xFF)));
26
+ const attrBuf = Buffer.alloc(4 + ipBuf.length);
27
+ attrBuf.writeUInt8(0, 0); // reserved
28
+ attrBuf.writeUInt8(family, 1);
29
+ attrBuf.writeUInt16BE(portX, 2);
30
+ ipBuf.copy(attrBuf, 4);
31
+
32
+ const header = Buffer.alloc(4);
33
+ header.writeUInt16BE(attrType, 0);
34
+ header.writeUInt16BE(attrBuf.length, 2);
35
+ return Buffer.concat([header, attrBuf]);
36
+ }
37
+
38
+ function buildSoftwareAttribute(text) {
39
+ const type = 0x8022;
40
+ const buf = Buffer.from(String(text || 'stunly/1.0'), 'utf8');
41
+ const pad = (4 - (buf.length % 4)) % 4;
42
+ const header = Buffer.alloc(4);
43
+ header.writeUInt16BE(type, 0);
44
+ header.writeUInt16BE(buf.length, 2);
45
+ if (pad) return Buffer.concat([header, buf, Buffer.alloc(pad)]);
46
+ return Buffer.concat([header, buf]);
47
+ }
48
+
49
+ function buildFingerprintAttr(msgWithoutFingerprint) {
50
+ const type = 0x8028;
51
+ const crc = crc32(msgWithoutFingerprint) ^ 0x5354554e;
52
+ const header = Buffer.alloc(4);
53
+ header.writeUInt16BE(type, 0);
54
+ header.writeUInt16BE(4, 2);
55
+ const val = Buffer.alloc(4);
56
+ val.writeUInt32BE(crc >>> 0, 0);
57
+ return Buffer.concat([header, val]);
58
+ }
59
+
60
+ function buildSuccessResponse(reqBuf, rinfo, software) {
61
+ if (!reqBuf || reqBuf.length < 20) return null;
62
+ const msgType = reqBuf.readUInt16BE(0);
63
+ const msgLen = reqBuf.readUInt16BE(2);
64
+ const cookie = reqBuf.readUInt32BE(4);
65
+ if (cookie !== MAGIC_COOKIE) return null;
66
+ const tx = reqBuf.slice(8, 20);
67
+ // Check Binding Request (0x0001)
68
+ if (msgType !== 0x0001) return null;
69
+
70
+ const xorAttr = buildXorMappedAttribute(rinfo.address, rinfo.port, tx);
71
+ const softwareAttr = buildSoftwareAttribute(software);
72
+ let attrs = Buffer.concat([xorAttr, softwareAttr]);
73
+
74
+ // Build header
75
+ const msgTypeRes = 0x0101; // Binding Success Response
76
+ const header = Buffer.alloc(20);
77
+ header.writeUInt16BE(msgTypeRes, 0);
78
+ header.writeUInt16BE(attrs.length + 8 /*fingerprint*/, 2); // will include fingerprint attr size
79
+ header.writeUInt32BE(MAGIC_COOKIE, 4);
80
+ tx.copy(header, 8);
81
+
82
+ const msgWithoutFingerprint = Buffer.concat([header, attrs]);
83
+ const fingerprint = buildFingerprintAttr(msgWithoutFingerprint);
84
+ const final = Buffer.concat([msgWithoutFingerprint, fingerprint]);
85
+ // Update length to actual attrs (without fingerprint attr length already included above), ensure correct
86
+ final.writeUInt16BE(attrs.length + 8, 2);
87
+ return final;
88
+ }
89
+
90
+ class StunServer {
91
+ constructor(opts = {}) {
92
+ this.port = opts.port || 3478;
93
+ this.host = opts.host || '0.0.0.0';
94
+ this.software = opts.software || 'stunly/1.0';
95
+ this.socket = dgram.createSocket('udp4');
96
+ this._onMessage = this._onMessage.bind(this);
97
+ }
98
+
99
+ _onMessage(msg, rinfo) {
100
+ try {
101
+ const res = buildSuccessResponse(msg, rinfo, this.software);
102
+ if (res) this.socket.send(res, rinfo.port, rinfo.address);
103
+ } catch (e) {
104
+ // silent
105
+ }
106
+ }
107
+
108
+ start(cb) {
109
+ this.socket.on('message', this._onMessage);
110
+ this.socket.bind(this.port, this.host, cb);
111
+ }
112
+
113
+ stop(cb) {
114
+ this.socket.removeListener('message', this._onMessage);
115
+ this.socket.close(cb);
116
+ }
117
+ }
118
+
119
+ module.exports = { StunServer, buildSuccessResponse };
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@auto-forge-org/stunly",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight STUN Binding server (RFC 5389) implemented in pure JavaScript.",
5
+ "main": "index.js",
6
+ "keywords": ["stun","webrtc","udp","stun-server"],
7
+ "author": "auto-forge-org",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/auto-forge-org/stunly.git"
12
+ },
13
+ "files": ["index.js","README.md","LICENSE"],
14
+ "engines": { "node": ">=14" },
15
+ "scripts": {
16
+ "test": "node test.js"
17
+ }
18
+ }