wireshade 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.
package/lib/server.js ADDED
@@ -0,0 +1,121 @@
1
+ const { Duplex } = require('stream');
2
+ const EventEmitter = require('events');
3
+
4
+ /**
5
+ * WireShadeServer - A server that listens on a port inside the VPN tunnel.
6
+ */
7
+ class WireShadeServer extends EventEmitter {
8
+ constructor(gw, options = {}) {
9
+ super();
10
+ this.gw = gw;
11
+ this.options = options;
12
+ this.logging = options.logging !== false;
13
+ this.log = this.logging ? console.log : () => { };
14
+ this.connections = new Map();
15
+ this.port = null;
16
+ this.listening = false;
17
+ }
18
+
19
+ async listen(port, callback) {
20
+ this.port = port;
21
+
22
+ try {
23
+ await this.gw.listen(
24
+ port,
25
+ // onConnection: napi-rs passes (err, connId, remoteIp, remotePort)
26
+ (err, connId, remoteIp, remotePort) => {
27
+ if (err) {
28
+ this.log(`[Server] Connection error: ${err}`);
29
+ return;
30
+ }
31
+ this.log(`[Server] New connection ${connId} from ${remoteIp}:${remotePort}`);
32
+
33
+ const stream = this._createStream(connId);
34
+ stream.remoteAddress = remoteIp;
35
+ stream.remotePort = remotePort;
36
+ this.connections.set(connId, { stream });
37
+ this.emit('connection', stream, { remoteAddress: remoteIp, remotePort });
38
+ },
39
+ // onData: napi-rs passes (err, connId, buffer)
40
+ (err, connId, buffer) => {
41
+ if (err) return;
42
+ const conn = this.connections.get(connId);
43
+ if (conn && conn.stream && buffer) {
44
+ this.log(`[Server] Received ${buffer.length} bytes on conn ${connId}`);
45
+ conn.stream.push(buffer);
46
+ }
47
+ },
48
+ // onClose: napi-rs passes (err, connId)
49
+ (err, connId) => {
50
+ if (err) return;
51
+ this.log(`[Server] Connection ${connId} closed`);
52
+ const conn = this.connections.get(connId);
53
+ if (conn && conn.stream) {
54
+ conn.stream.push(null);
55
+ conn.stream.emit('close');
56
+ this.connections.delete(connId);
57
+ }
58
+ }
59
+ );
60
+
61
+ this.listening = true;
62
+ this.log(`[Server] Listening on VPN port ${port}`);
63
+ this.emit('listening');
64
+ if (callback) callback();
65
+ } catch (err) {
66
+ this.emit('error', err);
67
+ throw err;
68
+ }
69
+ }
70
+
71
+ _createStream(connId) {
72
+ const self = this;
73
+ const log = this.log;
74
+
75
+ const stream = new Duplex({
76
+ allowHalfOpen: true,
77
+ read(size) { },
78
+ write(chunk, encoding, callback) {
79
+ log(`[Server] Writing ${chunk.length} bytes to conn ${connId}`);
80
+ self.gw.sendTo(connId, chunk)
81
+ .then(() => callback())
82
+ .catch((err) => {
83
+ log(`[Server] Write error: ${err}`);
84
+ callback(err);
85
+ });
86
+ }
87
+ });
88
+
89
+ stream.remoteAddress = null;
90
+ stream.remotePort = null;
91
+ stream.connId = connId;
92
+
93
+ stream.end = (data, encoding, callback) => {
94
+ const finish = () => {
95
+ self.gw.closeConnection(connId).catch(() => { });
96
+ self.connections.delete(connId);
97
+ if (callback) callback();
98
+ };
99
+ if (data) {
100
+ stream.write(data, encoding, finish);
101
+ } else {
102
+ finish();
103
+ }
104
+ };
105
+
106
+ stream.destroy = () => {
107
+ self.gw.closeConnection(connId).catch(() => { });
108
+ self.connections.delete(connId);
109
+ };
110
+
111
+ return stream;
112
+ }
113
+
114
+ close(callback) {
115
+ this.listening = false;
116
+ this.emit('close');
117
+ if (callback) callback();
118
+ }
119
+ }
120
+
121
+ module.exports = { WireShadeServer };
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "wireshade",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "napi": {
6
+ "binaryName": "wireshade",
7
+ "targets": [
8
+ "x86_64-pc-windows-msvc",
9
+ "x86_64-unknown-linux-gnu",
10
+ "x86_64-apple-darwin",
11
+ "aarch64-apple-darwin",
12
+ "aarch64-unknown-linux-gnu",
13
+ "armv7-unknown-linux-gnueabihf"
14
+ ]
15
+ },
16
+ "scripts": {
17
+ "build": "napi build --platform --release",
18
+ "build:debug": "napi build --platform",
19
+ "test": "node test.js"
20
+ },
21
+ "keywords": [],
22
+ "author": "",
23
+ "license": "MIT",
24
+ "description": "",
25
+ "devDependencies": {
26
+ "@napi-rs/cli": "^3.5.1"
27
+ },
28
+ "dependencies": {
29
+ "express": "^5.2.1"
30
+ }
31
+ }
Binary file