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/.github/workflows/publish.yml +111 -0
- package/Cargo.lock +1238 -0
- package/Cargo.toml +27 -0
- package/LICENSE +21 -0
- package/README.de.md +256 -0
- package/README.es.md +109 -0
- package/README.fr.md +109 -0
- package/README.md +258 -0
- package/README.zh.md +109 -0
- package/build.rs +5 -0
- package/examples/01_quickstart.js +36 -0
- package/examples/02_http_request.js +44 -0
- package/examples/03_https_custom_dns.js +47 -0
- package/examples/04_tcp_socket.js +50 -0
- package/examples/05_internet_routing.js +33 -0
- package/examples/06_simple_server.js +40 -0
- package/examples/07_express_app.js +37 -0
- package/examples/08_local_forwarding.js +30 -0
- package/examples/09_reconnect_config.js +128 -0
- package/examples/10_remote_forwarding.js +63 -0
- package/examples/README.md +54 -0
- package/examples/_legacy_easy_api.js +53 -0
- package/examples/wireguard.conf +8 -0
- package/index.d.ts +0 -0
- package/index.js +29 -0
- package/lib/agent.js +116 -0
- package/lib/client.js +548 -0
- package/lib/config_parser.js +77 -0
- package/lib/server.js +121 -0
- package/package.json +31 -0
- package/parallel_output.txt +0 -0
- package/src/lib.rs +841 -0
- package/wireshade.darwin-arm64.node +0 -0
- package/wireshade.darwin-x64.node +0 -0
- package/wireshade.linux-arm-gnueabihf.node +0 -0
- package/wireshade.linux-arm64-gnu.node +0 -0
- package/wireshade.linux-x64-gnu.node +0 -0
- package/wireshade.win32-x64-msvc.node +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# 👻 WireShade using Node.js
|
|
2
|
+
|
|
3
|
+
**The Ultimate Userspace WireGuard® Implementation for Node.js**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/wireshade)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
**WireShade** enables your Node.js application to connect directly to a WireGuard VPN **without root privileges**, kernel modules, or modifying system network settings. It runs entirely in userspace using a custom Rust-based TCP/IP stack (`smoltcp`) integrated directly into Node.js.
|
|
9
|
+
|
|
10
|
+
<div align="center">
|
|
11
|
+
|
|
12
|
+
[🇺🇸 English](README.md) | [🇩🇪 Deutsch](README.de.md) | [🇪🇸 Español](README.es.md) | [🇫🇷 Français](README.fr.md) | [🇨🇳 中文](README.zh.md)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🚀 Why WireShade?
|
|
19
|
+
|
|
20
|
+
WireShade solves complex networking implementation challenges with a clean, native userspace solution:
|
|
21
|
+
|
|
22
|
+
* **🛡️ Stealth & Security:** Route specific Node.js traffic through a secure WireGuard VPN while keeping the rest of your system traffic normal. Perfect for **web scraping**, **bots**, or **secure communication**.
|
|
23
|
+
* **🌍 Reverse Tunneling:** Expose a local Express server, WebSocket server, or Next.js app to the private VPN network, even if you are behind a NAT or firewall.
|
|
24
|
+
* **🔌 Zero-Config Client:** No need to install WireGuard on the host machine. Just `npm install` and go.
|
|
25
|
+
* **🔄 Automatic Reconnection:** Built-in logic to handle connection drops and network changes seamlessly.
|
|
26
|
+
* **⚡ High Performance:** Powered by Rust and NAPI-RS for near-native performance.
|
|
27
|
+
|
|
28
|
+
## 🧠 How it Works (Technical Deep Dive)
|
|
29
|
+
|
|
30
|
+
WireShade bypasses the host operating system's network stack by running a **userspace TCP/IP stack** ([smoltcp](https://github.com/smoltcp-rs/smoltcp)) inside your Node.js process.
|
|
31
|
+
|
|
32
|
+
1. **Handshake:** WireShade establishes a WireGuard handshake over UDP.
|
|
33
|
+
2. **Encapsulation:** IP packets are encrypted and encapsulated within UDP packets.
|
|
34
|
+
3. **Userspace Routing:** Decrypted packets are handled by `smoltcp` in Rust, which manages TCP state, retransmission, and buffering.
|
|
35
|
+
4. **Node.js Integration:** Data moves between Rust streams and Node.js `net.Socket`/`http.Agent` instances via high-performance NAPI bindings.
|
|
36
|
+
|
|
37
|
+
This architecture means:
|
|
38
|
+
- **No Virtual Network Interface (TUN/TAP)** is created on your OS.
|
|
39
|
+
- **Root privileges are NOT required.**
|
|
40
|
+
- **No conflict** with existing VPNs or system networking.
|
|
41
|
+
- **Cross-platform** compatibility (Windows, macOS, Linux, **Raspberry Pi**, Docker containers) without kernel modules.
|
|
42
|
+
|
|
43
|
+
## 📦 Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install wireshade
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
_Note: Windows users need basic build tools (Visual Studio Build Tools) if prebuilds are not available, but prebuilt binaries are planned._
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## 🛠️ Usage Examples
|
|
54
|
+
|
|
55
|
+
All examples assume you have initialized the client:
|
|
56
|
+
```javascript
|
|
57
|
+
const { WireShade, readConfig } = require('wireshade');
|
|
58
|
+
const client = new WireShade(readConfig('./wg0.conf'));
|
|
59
|
+
await client.start();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 1. HTTP/HTTPS Requests (Client)
|
|
63
|
+
Use WireShade as a transparent agent for your requests.
|
|
64
|
+
|
|
65
|
+
**Simplified API:**
|
|
66
|
+
```javascript
|
|
67
|
+
const html = await client.get('https://internal.service/api');
|
|
68
|
+
console.log(html);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Native `http`/`https` Module:**
|
|
72
|
+
```javascript
|
|
73
|
+
const https = require('https');
|
|
74
|
+
|
|
75
|
+
https.get('https://api.internal/data', { agent: client.getHttpsAgent() }, (res) => {
|
|
76
|
+
res.pipe(process.stdout);
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Axios:**
|
|
81
|
+
```javascript
|
|
82
|
+
const axios = require('axios');
|
|
83
|
+
|
|
84
|
+
// Configure Axios to use the VPN agent
|
|
85
|
+
const response = await axios.get('https://internal.service/api', {
|
|
86
|
+
httpAgent: client.getHttpAgent(),
|
|
87
|
+
httpsAgent: client.getHttpsAgent()
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Fetch (`node-fetch`):**
|
|
92
|
+
```javascript
|
|
93
|
+
const fetch = require('node-fetch');
|
|
94
|
+
|
|
95
|
+
const response = await fetch('https://internal.service/api', {
|
|
96
|
+
agent: (parsedUrl) => {
|
|
97
|
+
return parsedUrl.protocol === 'https:'
|
|
98
|
+
? client.getHttpsAgent()
|
|
99
|
+
: client.getHttpAgent();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 2. TCP & WebSockets to VPN (Client)
|
|
105
|
+
Connect to raw TCP services or WebSockets running inside the VPN.
|
|
106
|
+
|
|
107
|
+
**Raw TCP:**
|
|
108
|
+
```javascript
|
|
109
|
+
const socket = client.connect({ host: '10.0.0.5', port: 22 });
|
|
110
|
+
socket.write('SSH-2.0-MyClient\r\n');
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**WebSockets (using `ws` library):**
|
|
114
|
+
```javascript
|
|
115
|
+
const WebSocket = require('ws');
|
|
116
|
+
|
|
117
|
+
// Use the WireShade agent for the handshake
|
|
118
|
+
const ws = new WebSocket('ws://10.0.0.5:8080/stream', {
|
|
119
|
+
agent: client.getHttpAgent()
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
ws.on('open', () => console.log('Connected to VPN WebSocket!'));
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 3. Expose Local Servers (Express, Next.js, WebSockets)
|
|
126
|
+
Make your local server accessible **only** via the VPN (Reverse Tunneling).
|
|
127
|
+
|
|
128
|
+
**Express / Next.js / Fastify:**
|
|
129
|
+
```javascript
|
|
130
|
+
const express = require('express');
|
|
131
|
+
const http = require('http');
|
|
132
|
+
const { WireShadeServer } = require('wireshade');
|
|
133
|
+
|
|
134
|
+
// 1. Setup your App
|
|
135
|
+
const app = express();
|
|
136
|
+
app.get('/', (req, res) => res.send('🎉 Hidden inside the VPN!'));
|
|
137
|
+
|
|
138
|
+
// 2. Create standard HTTP server (not listening yet)
|
|
139
|
+
const httpServer = http.createServer(app);
|
|
140
|
+
|
|
141
|
+
// 3. Listen on the VPN
|
|
142
|
+
const vpnServer = new WireShadeServer(client);
|
|
143
|
+
vpnServer.on('connection', (socket) => {
|
|
144
|
+
httpServer.emit('connection', socket); // Feed VPN socket to HTTP server
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
await vpnServer.listen(80); // Listen on Port 80 of the VPN IP
|
|
148
|
+
console.log('Server online at http://<VPN-IP>/');
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**WebSocket Server:**
|
|
152
|
+
```javascript
|
|
153
|
+
const { WebSocketServer } = require('ws');
|
|
154
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
155
|
+
|
|
156
|
+
httpServer.on('upgrade', (req, socket, head) => {
|
|
157
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
158
|
+
wss.emit('connection', ws, req);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 4. Port Forwarding
|
|
164
|
+
WireShade supports both **Local Forwarding** (access VPN service locally) and **Remote Forwarding** (expose local service to VPN).
|
|
165
|
+
|
|
166
|
+
**Local Forwarding (VPN -> Localhost):**
|
|
167
|
+
Access a PostgreSQL database running at `10.0.0.5:5432` inside the VPN via `localhost:3333`.
|
|
168
|
+
```javascript
|
|
169
|
+
await client.forwardLocal(3333, '10.0.0.5', 5432);
|
|
170
|
+
console.log('Connect to DB at localhost:3333');
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
**Remote Forwarding (Localhost -> VPN):**
|
|
174
|
+
Expose your local development server (`localhost:3000`) to the VPN on port `8080`.
|
|
175
|
+
```javascript
|
|
176
|
+
// Listen on VPN Port 8080 -> Forward to localhost:3000
|
|
177
|
+
await client.forwardRemote(8080, 'localhost', 3000);
|
|
178
|
+
console.log('VPN users can access your dev server at http://<VPN-IP>:8080');
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## ⚙️ Configuration & Features
|
|
184
|
+
|
|
185
|
+
### Auto-Reconnection
|
|
186
|
+
WireShade includes robust reconnection logic.
|
|
187
|
+
|
|
188
|
+
```javascript
|
|
189
|
+
const client = new WireShade({
|
|
190
|
+
wireguard: { ... },
|
|
191
|
+
reconnect: {
|
|
192
|
+
enabled: true,
|
|
193
|
+
maxAttempts: 10,
|
|
194
|
+
delay: 1000, // Start with 1s delay
|
|
195
|
+
backoffMultiplier: 1.5 // Exponential backoff
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
client.on('reconnecting', (attempt) => console.log(`🔄 Reconnecting... (${attempt})`));
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Custom DNS / Hosts
|
|
203
|
+
Map internal VPN hostnames to IPs without touching `/etc/hosts`.
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
const client = new WireShade({
|
|
207
|
+
wireguard: { ... },
|
|
208
|
+
hosts: {
|
|
209
|
+
'internal-api.local': '10.0.0.4',
|
|
210
|
+
'db-prod': '10.0.0.5'
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## 📚 API Reference
|
|
216
|
+
|
|
217
|
+
**`new WireShade(config)`**
|
|
218
|
+
- Creates a new VPN instance. `config` matches standard WireGuard parameters (`privateKey`, `endpoint`, etc.).
|
|
219
|
+
|
|
220
|
+
**`client.start()`**
|
|
221
|
+
- Connects to the VPN. Returns a `Promise` that resolves on connection.
|
|
222
|
+
|
|
223
|
+
**`client.get(url, [options])`**
|
|
224
|
+
- Helper to make a simple HTTP GET request through the VPN. Returns connection body.
|
|
225
|
+
|
|
226
|
+
**`client.connect(options)`**
|
|
227
|
+
- Creates a raw TCP socket (`net.Socket`) connected through the tunnel.
|
|
228
|
+
|
|
229
|
+
**`client.listen(port, [callback])`**
|
|
230
|
+
- Starts a TCP server listening on the **VPN IP** at the specified port.
|
|
231
|
+
|
|
232
|
+
**`client.forwardLocal(localPort, remoteHost, remotePort)`**
|
|
233
|
+
- Forwards a local port to a remote destination inside the VPN.
|
|
234
|
+
|
|
235
|
+
**`client.forwardRemote(vpnPort, targetHost, targetPort)`**
|
|
236
|
+
- Forwards a listener on the VPN IP to a target on your local machine.
|
|
237
|
+
|
|
238
|
+
**`client.getHttpAgent() / client.getHttpsAgent()`**
|
|
239
|
+
- Returns a Node.js `http.Agent` / `https.Agent` configured to route traffic through the tunnel.
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## 🎯 Use Cases
|
|
246
|
+
|
|
247
|
+
* **Microservices Communication:** Connect secure microservices across different clouds without exposing public ports.
|
|
248
|
+
* **Web Scraping:** Rotate IP addresses by creating multiple WireShade instances connected to different VPN endpoints.
|
|
249
|
+
* **Development Access:** Give developers access to private internal databases from their local machines securely.
|
|
250
|
+
* **IoT & Edge:** Connect edge devices behind restrictive NATs back to a central server using server mode.
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
## 📜 License
|
|
255
|
+
|
|
256
|
+
MIT License.
|
|
257
|
+
|
|
258
|
+
*WireGuard is a registered trademark of Jason A. Donenfeld.*
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# 👻 WireShade Node.js 版
|
|
2
|
+
|
|
3
|
+
**Node.js 终极用户态 WireGuard® 实现**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/wireshade)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
**WireShade** 使您的 Node.js 应用程序能够直接连接到 WireGuard VPN,而**无需 root 权限**、内核模块或修改系统网络设置。它使用直接集成到 Node.js 中的自定义 Rust TCP/IP 栈(`smoltcp`)完全在用户态运行。
|
|
9
|
+
|
|
10
|
+
<div align="center">
|
|
11
|
+
|
|
12
|
+
[🇺🇸 English](README.md) | [🇩🇪 Deutsch](README.de.md) | [🇪🇸 Español](README.es.md) | [🇫🇷 Français](README.fr.md) | [🇨🇳 中文](README.zh.md)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🚀 为什么选择 WireShade?
|
|
19
|
+
|
|
20
|
+
WireShade 以干净的原生用户态解决方案解决了复杂的网络实现挑战:
|
|
21
|
+
|
|
22
|
+
* **🛡️ 隐蔽与安全:** 通过安全的 WireGuard VPN 路由特定的 Node.js 流量,同时保持其余系统流量正常。非常适合 **Web 爬虫**、**机器人**或**安全通信**。
|
|
23
|
+
* **🌍 反向隧道:** 将本地 Express 服务器、WebSocket 服务器或 Next.js 应用程序暴露给私有 VPN 网络,即使您在 NAT 或防火墙后面。
|
|
24
|
+
* **🔌 零配置客户端:** 无需在主机上安装 WireGuard。只需 `npm install` 即可。
|
|
25
|
+
* **🔄 自动重连:** 内置逻辑,可无缝处理连接断开和网络更改。
|
|
26
|
+
* **⚡ 高性能:** 由 Rust 和 NAPI-RS 提供支持,具有近乎原生的性能。
|
|
27
|
+
|
|
28
|
+
## 📦 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install wireshade
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 🛠️ 使用示例
|
|
37
|
+
|
|
38
|
+
所有示例均假设您已初始化客户端:
|
|
39
|
+
```javascript
|
|
40
|
+
const { WireShade, readConfig } = require('wireshade');
|
|
41
|
+
const client = new WireShade(readConfig('./wg0.conf'));
|
|
42
|
+
await client.start();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 1. HTTP/HTTPS请求 (客户端)
|
|
46
|
+
使用 WireShade 作为请求的透明代理。
|
|
47
|
+
|
|
48
|
+
**原生 `http`/`https` 模块:**
|
|
49
|
+
```javascript
|
|
50
|
+
const https = require('https');
|
|
51
|
+
|
|
52
|
+
https.get('https://api.internal/data', { agent: client.getHttpsAgent() }, (res) => {
|
|
53
|
+
res.pipe(process.stdout);
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**Axios:**
|
|
58
|
+
```javascript
|
|
59
|
+
const axios = require('axios');
|
|
60
|
+
|
|
61
|
+
const response = await axios.get('https://internal.service/api', {
|
|
62
|
+
httpAgent: client.getHttpAgent(),
|
|
63
|
+
httpsAgent: client.getHttpsAgent()
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. TCP & WebSockets 到 VPN (客户端)
|
|
68
|
+
连接到 VPN 内部运行的原始 TCP 服务或 WebSocket。
|
|
69
|
+
|
|
70
|
+
**WebSockets:**
|
|
71
|
+
```javascript
|
|
72
|
+
const WebSocket = require('ws');
|
|
73
|
+
|
|
74
|
+
const ws = new WebSocket('ws://10.0.0.5:8080/stream', {
|
|
75
|
+
agent: client.getHttpAgent()
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
ws.on('open', () => console.log('已连接到 VPN WebSocket!'));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 3. 暴露本地服务器 (反向隧道)
|
|
82
|
+
使您的本地服务器**仅**通过 VPN 可访问。
|
|
83
|
+
|
|
84
|
+
**Express / Next.js:**
|
|
85
|
+
```javascript
|
|
86
|
+
const express = require('express');
|
|
87
|
+
const http = require('http');
|
|
88
|
+
const { WireShadeServer } = require('wireshade');
|
|
89
|
+
|
|
90
|
+
const app = express();
|
|
91
|
+
app.get('/', (req, res) => res.send('🎉 隐藏在 VPN 内部!'));
|
|
92
|
+
|
|
93
|
+
const httpServer = http.createServer(app);
|
|
94
|
+
const vpnServer = new WireShadeServer(client);
|
|
95
|
+
|
|
96
|
+
// 将 VPN 套接字传输到 HTTP 服务器
|
|
97
|
+
vpnServer.on('connection', (socket) => httpServer.emit('connection', socket));
|
|
98
|
+
|
|
99
|
+
await vpnServer.listen(80);
|
|
100
|
+
console.log('服务器在线地址 http://<VPN-IP>/');
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## 📜 许可证
|
|
106
|
+
|
|
107
|
+
MIT 许可证。
|
|
108
|
+
|
|
109
|
+
*WireGuard 是 Jason A. Donenfeld 的注册商标。*
|
package/build.rs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const { WireShade } = require('../index');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
async function main() {
|
|
5
|
+
console.log("--- Zero-Config WireShade Demo ---");
|
|
6
|
+
|
|
7
|
+
// 1. Initialize
|
|
8
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'));
|
|
9
|
+
|
|
10
|
+
// 2. Connect
|
|
11
|
+
console.log("Connecting...");
|
|
12
|
+
await gw.start();
|
|
13
|
+
console.log("✅ VPN Connected!");
|
|
14
|
+
|
|
15
|
+
// 3. Simple HTTP Request (like fetch)
|
|
16
|
+
try {
|
|
17
|
+
console.log("Fetching IP...");
|
|
18
|
+
const body = await gw.get('http://10.0.0.5/');
|
|
19
|
+
console.log(`🌍 External IP: ${body.trim()}`);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
console.error("Request failed:", e.message);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 4. Simple Server
|
|
25
|
+
console.log("Starting server on port 8080...");
|
|
26
|
+
await gw.listen(8080, (socket) => {
|
|
27
|
+
console.log("incoming connection!");
|
|
28
|
+
socket.end('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello from minimal WireShade!');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log("Server running. Waiting for connections...");
|
|
32
|
+
|
|
33
|
+
gw.on('error', console.error);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
main();
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
const { WireShade } = require('../index');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Example: Simple HTTP Request
|
|
6
|
+
* Demonstrates the simplified get() API.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function exampleHttp() {
|
|
10
|
+
const log = (msg) => console.log(`[WireShade HTTP] ${msg}`);
|
|
11
|
+
|
|
12
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'));
|
|
13
|
+
|
|
14
|
+
gw.on('connect', async () => {
|
|
15
|
+
const target = 'http://10.0.0.5/';
|
|
16
|
+
log(`VPN Connected. Fetching ${target}...`);
|
|
17
|
+
|
|
18
|
+
console.log("\n--- Method 1: Simplified API (gw.get) ---");
|
|
19
|
+
try {
|
|
20
|
+
console.time("Request");
|
|
21
|
+
const body = await gw.get(target);
|
|
22
|
+
console.timeEnd("Request");
|
|
23
|
+
|
|
24
|
+
log(`Response length: ${body.length} bytes`);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
console.error("Method 1 failed:", err.message);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
console.log("\n--- Method 2: Native-like API (gw.http.get) ---");
|
|
30
|
+
gw.http.get(target, (res) => {
|
|
31
|
+
log(`Status: ${res.statusCode}`);
|
|
32
|
+
res.on('data', d => process.stdout.write(`Chunk: ${d.length} bytes\n`));
|
|
33
|
+
res.on('end', () => {
|
|
34
|
+
log("Done.");
|
|
35
|
+
process.exit(0);
|
|
36
|
+
});
|
|
37
|
+
}).on('error', (err) => {
|
|
38
|
+
console.error("Method 2 failed:", err);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exampleHttp();
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const { WireShade } = require('../index');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Example: HTTPS Request with Custom DNS
|
|
6
|
+
* Demonstrates simplified configuration for hosts.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function exampleHttps() {
|
|
10
|
+
const log = (msg) => console.log(`[WireShade HTTPS] ${msg}`);
|
|
11
|
+
|
|
12
|
+
// Config + Options (Hosts)
|
|
13
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'), {
|
|
14
|
+
hosts: {
|
|
15
|
+
'internal.service.lan': '10.0.0.5'
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
gw.on('connect', async () => {
|
|
20
|
+
const target = 'https://internal.service.lan/';
|
|
21
|
+
log(`VPN Connected. Fetching ${target}...`);
|
|
22
|
+
|
|
23
|
+
// Method 1: Simplified API (gw.get)
|
|
24
|
+
// const body = await gw.get(target);
|
|
25
|
+
|
|
26
|
+
// Method 2: Native-like API (gw.https.get) using host mapping
|
|
27
|
+
log("Using gw.https.get() with Host Mapping...");
|
|
28
|
+
|
|
29
|
+
const req = gw.https.get(target, (res) => {
|
|
30
|
+
log(`Status: ${res.statusCode}`);
|
|
31
|
+
let data = '';
|
|
32
|
+
res.on('data', chunk => data += chunk);
|
|
33
|
+
res.on('end', () => {
|
|
34
|
+
log(`Response received!`);
|
|
35
|
+
console.log(data.substring(0, 100));
|
|
36
|
+
process.exit(0);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
req.on('error', (err) => {
|
|
41
|
+
console.error("HTTPS Request Failed:", err);
|
|
42
|
+
process.exit(1);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
exampleHttps();
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const { WireShade } = require('../index');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Example: Raw TCP Socket
|
|
6
|
+
* Demonstrates low-level access to the TCP connection using the simplified API.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function exampleTcp() {
|
|
10
|
+
const log = (msg) => console.log(`[WireShade TCP] ${msg}`);
|
|
11
|
+
|
|
12
|
+
// Initialize with config file
|
|
13
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'));
|
|
14
|
+
|
|
15
|
+
gw.on('connect', () => {
|
|
16
|
+
log("VPN Connected. Establishing TCP connection...");
|
|
17
|
+
|
|
18
|
+
const targetIp = '10.0.0.5';
|
|
19
|
+
const targetPort = 80;
|
|
20
|
+
|
|
21
|
+
const socket = gw.connect({
|
|
22
|
+
host: targetIp,
|
|
23
|
+
port: targetPort
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
socket.on('connect', () => {
|
|
27
|
+
log(`Socket connected to ${targetIp}:${targetPort}`);
|
|
28
|
+
|
|
29
|
+
const request = "GET / HTTP/1.1\r\nHost: 10.0.0.5\r\nConnection: close\r\n\r\n";
|
|
30
|
+
socket.write(request);
|
|
31
|
+
log("HTTP GET sent.");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
socket.on('data', (data) => {
|
|
35
|
+
log(`Received ${data.length} bytes:`);
|
|
36
|
+
console.log(data.toString().split('\n')[0] + '...'); // Print first line
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
socket.on('close', () => {
|
|
40
|
+
log("Connection closed.");
|
|
41
|
+
process.exit(0);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
socket.on('error', (err) => {
|
|
45
|
+
console.error("Socket error:", err.message);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exampleTcp();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const { WireShade } = require('../index');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Example: Internet Access
|
|
6
|
+
* Demonstrates routing traffic to the public internet through the WireGuard tunnel.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
async function exampleInternet() {
|
|
10
|
+
const log = (msg) => console.log(`[WireShade Internet] ${msg}`);
|
|
11
|
+
|
|
12
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'));
|
|
13
|
+
|
|
14
|
+
gw.on('connect', async () => {
|
|
15
|
+
log("VPN Connected. Checking public IP...");
|
|
16
|
+
|
|
17
|
+
// Using gw.https.get for native-like control
|
|
18
|
+
gw.https.get('https://ifconfig.me/ip', (res) => {
|
|
19
|
+
let ip = '';
|
|
20
|
+
res.on('data', c => ip += c);
|
|
21
|
+
res.on('end', () => {
|
|
22
|
+
log(`\nResult IP: ${ip.trim()}`);
|
|
23
|
+
log("(This should match your WireGuard Exit-IP)");
|
|
24
|
+
process.exit(0);
|
|
25
|
+
});
|
|
26
|
+
}).on('error', (err) => {
|
|
27
|
+
console.error("Failed:", err.message);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
exampleInternet();
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const { WireShade } = require('../index');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Server über WireGuard veröffentlichen (Reverse Tunnel)
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const VPN_PORT = 8080;
|
|
9
|
+
|
|
10
|
+
async function main() {
|
|
11
|
+
console.log("=== WireShade Reverse Tunnel Demo ===");
|
|
12
|
+
|
|
13
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'));
|
|
14
|
+
|
|
15
|
+
gw.on('connect', async () => {
|
|
16
|
+
console.log("Listen on http://10.0.0.2:8080/");
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
await gw.listen(VPN_PORT, (socket) => {
|
|
20
|
+
console.log(`[Server] New connection!`);
|
|
21
|
+
|
|
22
|
+
socket.end([
|
|
23
|
+
'HTTP/1.1 200 OK',
|
|
24
|
+
'Content-Type: text/html',
|
|
25
|
+
'Connection: close',
|
|
26
|
+
'',
|
|
27
|
+
'<h1>Hello from WireShade Server!</h1>'
|
|
28
|
+
].join('\r\n'));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log(`✅ Server listening on VPN port ${VPN_PORT}`);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
console.error("Server start failed:", err);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
gw.on('error', console.error);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
main();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const { WireShade } = require('../index');
|
|
3
|
+
const http = require('http');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Express über WireGuard veröffentlichen (Reverse Tunnel)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
async function main() {
|
|
11
|
+
console.log("=== WireShade + Express Demo ===");
|
|
12
|
+
|
|
13
|
+
// 1. Setup Express
|
|
14
|
+
const app = express();
|
|
15
|
+
app.get('/', (req, res) => res.send('<h1>🎉 Express via WireShade!</h1>'));
|
|
16
|
+
app.get('/api/status', (req, res) => res.json({ status: 'online', vpn: true }));
|
|
17
|
+
|
|
18
|
+
// Create standard HTTP server (but don't listen on TCP port)
|
|
19
|
+
const httpServer = http.createServer(app);
|
|
20
|
+
|
|
21
|
+
// 2. Setup GhostWire
|
|
22
|
+
const gw = new WireShade(path.join(__dirname, 'wireguard.conf'));
|
|
23
|
+
|
|
24
|
+
gw.on('connect', async () => {
|
|
25
|
+
console.log(`VPN Connected as ${gw.config.wireguard.sourceIp}`);
|
|
26
|
+
|
|
27
|
+
// 3. Bridge VPN connections to Express
|
|
28
|
+
await gw.listen(8080, (socket) => {
|
|
29
|
+
// Inject the VPN socket into the HTTP server
|
|
30
|
+
httpServer.emit('connection', socket);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log("✅ Express Server accessible at http://10.0.0.2:8080");
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
main();
|