@push.rocks/smartvpn 1.4.1 → 1.6.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.
@@ -0,0 +1,123 @@
1
+ import type { IWgPeerConfig } from './smartvpn.interfaces.js';
2
+
3
+ // ============================================================================
4
+ // WireGuard .conf file generator
5
+ // ============================================================================
6
+
7
+ export interface IWgClientConfOptions {
8
+ /** Client private key (base64) */
9
+ privateKey: string;
10
+ /** Client TUN address with prefix (e.g. 10.8.0.2/24) */
11
+ address: string;
12
+ /** DNS servers */
13
+ dns?: string[];
14
+ /** TUN MTU */
15
+ mtu?: number;
16
+ /** Server peer config */
17
+ peer: {
18
+ publicKey: string;
19
+ presharedKey?: string;
20
+ endpoint: string;
21
+ allowedIps: string[];
22
+ persistentKeepalive?: number;
23
+ };
24
+ }
25
+
26
+ export interface IWgServerConfOptions {
27
+ /** Server private key (base64) */
28
+ privateKey: string;
29
+ /** Server TUN address with prefix (e.g. 10.8.0.1/24) */
30
+ address: string;
31
+ /** UDP listen port */
32
+ listenPort: number;
33
+ /** DNS servers */
34
+ dns?: string[];
35
+ /** TUN MTU */
36
+ mtu?: number;
37
+ /** Enable NAT — adds PostUp/PostDown iptables rules */
38
+ enableNat?: boolean;
39
+ /** Network interface for NAT (e.g. eth0). Auto-detected if omitted. */
40
+ natInterface?: string;
41
+ /** Configured peers */
42
+ peers: IWgPeerConfig[];
43
+ }
44
+
45
+ /**
46
+ * Generates standard WireGuard .conf files compatible with wg-quick,
47
+ * WireGuard iOS/Android apps, and other standard WireGuard clients.
48
+ */
49
+ export class WgConfigGenerator {
50
+ /**
51
+ * Generate a client .conf file content.
52
+ */
53
+ public static generateClientConfig(opts: IWgClientConfOptions): string {
54
+ const lines: string[] = [];
55
+
56
+ lines.push('[Interface]');
57
+ lines.push(`PrivateKey = ${opts.privateKey}`);
58
+ lines.push(`Address = ${opts.address}`);
59
+ if (opts.dns && opts.dns.length > 0) {
60
+ lines.push(`DNS = ${opts.dns.join(', ')}`);
61
+ }
62
+ if (opts.mtu) {
63
+ lines.push(`MTU = ${opts.mtu}`);
64
+ }
65
+
66
+ lines.push('');
67
+ lines.push('[Peer]');
68
+ lines.push(`PublicKey = ${opts.peer.publicKey}`);
69
+ if (opts.peer.presharedKey) {
70
+ lines.push(`PresharedKey = ${opts.peer.presharedKey}`);
71
+ }
72
+ lines.push(`Endpoint = ${opts.peer.endpoint}`);
73
+ lines.push(`AllowedIPs = ${opts.peer.allowedIps.join(', ')}`);
74
+ if (opts.peer.persistentKeepalive) {
75
+ lines.push(`PersistentKeepalive = ${opts.peer.persistentKeepalive}`);
76
+ }
77
+
78
+ lines.push('');
79
+ return lines.join('\n');
80
+ }
81
+
82
+ /**
83
+ * Generate a server .conf file content.
84
+ */
85
+ public static generateServerConfig(opts: IWgServerConfOptions): string {
86
+ const lines: string[] = [];
87
+
88
+ lines.push('[Interface]');
89
+ lines.push(`PrivateKey = ${opts.privateKey}`);
90
+ lines.push(`Address = ${opts.address}`);
91
+ lines.push(`ListenPort = ${opts.listenPort}`);
92
+ if (opts.dns && opts.dns.length > 0) {
93
+ lines.push(`DNS = ${opts.dns.join(', ')}`);
94
+ }
95
+ if (opts.mtu) {
96
+ lines.push(`MTU = ${opts.mtu}`);
97
+ }
98
+ if (opts.enableNat) {
99
+ const iface = opts.natInterface || 'eth0';
100
+ lines.push(`PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ${iface} -j MASQUERADE`);
101
+ lines.push(`PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ${iface} -j MASQUERADE`);
102
+ }
103
+
104
+ for (const peer of opts.peers) {
105
+ lines.push('');
106
+ lines.push('[Peer]');
107
+ lines.push(`PublicKey = ${peer.publicKey}`);
108
+ if (peer.presharedKey) {
109
+ lines.push(`PresharedKey = ${peer.presharedKey}`);
110
+ }
111
+ lines.push(`AllowedIPs = ${peer.allowedIps.join(', ')}`);
112
+ if (peer.endpoint) {
113
+ lines.push(`Endpoint = ${peer.endpoint}`);
114
+ }
115
+ if (peer.persistentKeepalive) {
116
+ lines.push(`PersistentKeepalive = ${peer.persistentKeepalive}`);
117
+ }
118
+ }
119
+
120
+ lines.push('');
121
+ return lines.join('\n');
122
+ }
123
+ }
@@ -32,10 +32,24 @@ export interface IVpnClientConfig {
32
32
  mtu?: number;
33
33
  /** Keepalive interval in seconds (default: 30) */
34
34
  keepaliveIntervalSecs?: number;
35
- /** Transport protocol: 'auto' (default, tries QUIC then WS), 'websocket', or 'quic' */
36
- transport?: 'auto' | 'websocket' | 'quic';
35
+ /** Transport protocol: 'auto' (default, tries QUIC then WS), 'websocket', 'quic', or 'wireguard' */
36
+ transport?: 'auto' | 'websocket' | 'quic' | 'wireguard';
37
37
  /** For QUIC: SHA-256 hash of server certificate (base64) for cert pinning */
38
38
  serverCertHash?: string;
39
+ /** WireGuard: client private key (base64, X25519) */
40
+ wgPrivateKey?: string;
41
+ /** WireGuard: client TUN address (e.g. 10.8.0.2) */
42
+ wgAddress?: string;
43
+ /** WireGuard: client TUN address prefix length (default: 24) */
44
+ wgAddressPrefix?: number;
45
+ /** WireGuard: preshared key (base64, optional) */
46
+ wgPresharedKey?: string;
47
+ /** WireGuard: persistent keepalive interval in seconds */
48
+ wgPersistentKeepalive?: number;
49
+ /** WireGuard: server endpoint (host:port, e.g. vpn.example.com:51820) */
50
+ wgEndpoint?: string;
51
+ /** WireGuard: allowed IPs (CIDR strings, e.g. ['0.0.0.0/0']) */
52
+ wgAllowedIps?: string[];
39
53
  }
40
54
 
41
55
  export interface IVpnClientOptions {
@@ -72,12 +86,16 @@ export interface IVpnServerConfig {
72
86
  defaultRateLimitBytesPerSec?: number;
73
87
  /** Default burst size for new clients (bytes). Omit for unlimited. */
74
88
  defaultBurstBytes?: number;
75
- /** Transport mode: 'both' (default, WS+QUIC), 'websocket', or 'quic' */
76
- transportMode?: 'websocket' | 'quic' | 'both';
89
+ /** Transport mode: 'both' (default, WS+QUIC), 'websocket', 'quic', or 'wireguard' */
90
+ transportMode?: 'websocket' | 'quic' | 'both' | 'wireguard';
77
91
  /** QUIC listen address (host:port). Defaults to listenAddr. */
78
92
  quicListenAddr?: string;
79
93
  /** QUIC idle timeout in seconds (default: 30) */
80
94
  quicIdleTimeoutSecs?: number;
95
+ /** WireGuard: UDP listen port (default: 51820) */
96
+ wgListenPort?: number;
97
+ /** WireGuard: configured peers */
98
+ wgPeers?: IWgPeerConfig[];
81
99
  }
82
100
 
83
101
  export interface IVpnServerOptions {
@@ -187,6 +205,35 @@ export interface IVpnClientTelemetry {
187
205
  burstBytes?: number;
188
206
  }
189
207
 
208
+ // ============================================================================
209
+ // WireGuard-specific types
210
+ // ============================================================================
211
+
212
+ export interface IWgPeerConfig {
213
+ /** Peer's public key (base64, X25519) */
214
+ publicKey: string;
215
+ /** Optional preshared key (base64) */
216
+ presharedKey?: string;
217
+ /** Allowed IP ranges (CIDR strings) */
218
+ allowedIps: string[];
219
+ /** Peer endpoint (host:port) — optional for server peers, required for client */
220
+ endpoint?: string;
221
+ /** Persistent keepalive interval in seconds */
222
+ persistentKeepalive?: number;
223
+ }
224
+
225
+ export interface IWgPeerInfo {
226
+ publicKey: string;
227
+ allowedIps: string[];
228
+ endpoint?: string;
229
+ persistentKeepalive?: number;
230
+ bytesSent: number;
231
+ bytesReceived: number;
232
+ packetsSent: number;
233
+ packetsReceived: number;
234
+ lastHandshakeTime?: string;
235
+ }
236
+
190
237
  // ============================================================================
191
238
  // IPC Command maps (used by smartrust RustBridge<TCommands>)
192
239
  // ============================================================================
@@ -211,6 +258,10 @@ export type TVpnServerCommands = {
211
258
  setClientRateLimit: { params: { clientId: string; rateBytesPerSec: number; burstBytes: number }; result: void };
212
259
  removeClientRateLimit: { params: { clientId: string }; result: void };
213
260
  getClientTelemetry: { params: { clientId: string }; result: IVpnClientTelemetry };
261
+ generateWgKeypair: { params: Record<string, never>; result: IVpnKeypair };
262
+ addWgPeer: { params: { peer: IWgPeerConfig }; result: void };
263
+ removeWgPeer: { params: { publicKey: string }; result: void };
264
+ listWgPeers: { params: Record<string, never>; result: { peers: IWgPeerInfo[] } };
214
265
  };
215
266
 
216
267
  // ============================================================================