@myrialabs/clopen 0.3.19 → 0.3.20
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/backend/database/migrations/039_create_file_audit_log.ts +45 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/file-audit-log-queries.test.ts +377 -0
- package/backend/database/queries/file-audit-log-queries.ts +184 -0
- package/backend/database/queries/index.ts +2 -1
- package/backend/db-client/drivers/mysql.ts +28 -10
- package/backend/db-client/drivers/postgres.ts +34 -18
- package/backend/engine/install-recipes.ts +1 -109
- package/backend/http/files-upload.ts +84 -3
- package/backend/tunnel/tunnel-config.ts +76 -209
- package/backend/utils/client-ip.test.ts +55 -0
- package/backend/utils/client-ip.ts +42 -0
- package/backend/utils/ws.ts +6 -3
- package/backend/ws/auth/login.test.ts +271 -0
- package/backend/ws/auth/login.ts +36 -3
- package/backend/ws/auth/users.ts +3 -2
- package/backend/ws/files/path-access.test.ts +60 -1
- package/backend/ws/files/path-access.ts +21 -0
- package/backend/ws/system-tools/install.ts +0 -1
- package/backend/ws/system-tools/status.ts +3 -4
- package/backend/ws/tunnel/config.ts +17 -25
- package/backend/ws/tunnel/operations.ts +63 -68
- package/dist/assets/{main-BOUXyIaW.js → main-72Z_L-nD.js} +199 -199
- package/dist/index.html +1 -1
- package/package.json +2 -1
- package/shared/utils/ws-client.ts +188 -38
- package/backend/tunnel/cloudflared/binary.ts +0 -171
- package/backend/tunnel/cloudflared/index.ts +0 -24
- package/backend/tunnel/cloudflared/tunnel.ts +0 -444
- package/backend/tunnel/global-tunnel-manager.ts +0 -687
- package/backend/tunnel/project-tunnel-manager.ts +0 -264
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { debug } from '$shared/utils/logger';
|
|
2
|
-
import { resolveCloudflaredBinary, CloudflaredTunnel, CloudflaredMissingError } from './cloudflared';
|
|
3
|
-
|
|
4
|
-
interface TunnelInstance {
|
|
5
|
-
tunnel: any;
|
|
6
|
-
publicUrl: string;
|
|
7
|
-
localPort: number;
|
|
8
|
-
startedAt: Date;
|
|
9
|
-
autoStopTimer?: NodeJS.Timeout;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
interface TunnelConnection {
|
|
13
|
-
id: string;
|
|
14
|
-
ip: string;
|
|
15
|
-
location: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class ProjectTunnelManager {
|
|
19
|
-
// Key format: "projectId:port" to support multiple tunnels per project
|
|
20
|
-
private activeTunnels = new Map<string, TunnelInstance>();
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Generate unique key for tunnel
|
|
24
|
-
*/
|
|
25
|
-
private getTunnelKey(projectId: string, port: number): string {
|
|
26
|
-
return `${projectId}:${port}`;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Verify cloudflared is available. Installation is user-initiated via
|
|
31
|
-
* Settings → System Tools; this manager never auto-downloads.
|
|
32
|
-
*/
|
|
33
|
-
private ensureBinaryAvailable(
|
|
34
|
-
onProgress?: (stage: string, data?: any) => void
|
|
35
|
-
): void {
|
|
36
|
-
const resolved = resolveCloudflaredBinary();
|
|
37
|
-
if (!resolved) {
|
|
38
|
-
debug.warn('tunnel', 'Cloudflared binary not available; install via Settings → System Tools');
|
|
39
|
-
throw new CloudflaredMissingError();
|
|
40
|
-
}
|
|
41
|
-
debug.log('tunnel', '[PROGRESS:BINARY_READY] Cloudflared binary available at:', resolved);
|
|
42
|
-
onProgress?.('binary-ready', { cached: true });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Start cloudflared tunnel for a project port
|
|
47
|
-
*/
|
|
48
|
-
async startTunnel(
|
|
49
|
-
projectId: string,
|
|
50
|
-
port: number,
|
|
51
|
-
autoStopMinutes: number = 60,
|
|
52
|
-
onProgress?: (stage: string, data?: any) => void
|
|
53
|
-
): Promise<{ publicUrl: string; status: 'active'; timings: any }> {
|
|
54
|
-
debug.log('tunnel', '[PROGRESS:CHECKING_BINARY] Checking if binary is available...');
|
|
55
|
-
onProgress?.('checking-binary');
|
|
56
|
-
|
|
57
|
-
this.ensureBinaryAvailable(onProgress);
|
|
58
|
-
const timings: any = {};
|
|
59
|
-
|
|
60
|
-
// Check if tunnel already exists for this port
|
|
61
|
-
const tunnelKey = this.getTunnelKey(projectId, port);
|
|
62
|
-
if (this.activeTunnels.has(tunnelKey)) {
|
|
63
|
-
throw new Error(`Tunnel already active for port ${port}`);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
debug.log('tunnel', `[PROGRESS:STARTING_TUNNEL] Starting tunnel for project ${projectId} on port ${port}`);
|
|
67
|
-
onProgress?.('starting-tunnel', { port });
|
|
68
|
-
|
|
69
|
-
const tunnelStartTime = Date.now();
|
|
70
|
-
|
|
71
|
-
// Create quick tunnel pointing to localhost:port
|
|
72
|
-
// Use Tunnel.quick() method for quick tunnels
|
|
73
|
-
const tunnel = CloudflaredTunnel.quick(`http://localhost:${port}`);
|
|
74
|
-
|
|
75
|
-
// Log ALL events for debugging
|
|
76
|
-
tunnel.on('url', (url: string) => debug.log('tunnel', `[EVENT] url: ${url}`));
|
|
77
|
-
tunnel.on('connected', (info: any) => debug.log('tunnel', `[EVENT] connected:`, info));
|
|
78
|
-
tunnel.on('error', (error: Error) => debug.error('tunnel', `[EVENT] error:`, error));
|
|
79
|
-
tunnel.on('exit', (code: number | null) => debug.log('tunnel', `[EVENT] exit with code ${code}`));
|
|
80
|
-
tunnel.on('stdout', (data: string) => debug.log('tunnel', `[STDOUT] ${data.trim()}`));
|
|
81
|
-
tunnel.on('stderr', (data: string) => debug.log('tunnel', `[STDERR] ${data.trim()}`));
|
|
82
|
-
|
|
83
|
-
// Wait for URL to be generated
|
|
84
|
-
debug.log('tunnel', '[PROGRESS:GENERATING_URL] Waiting for tunnel URL to be generated...');
|
|
85
|
-
onProgress?.('generating-url');
|
|
86
|
-
const urlGenerationStart = Date.now();
|
|
87
|
-
|
|
88
|
-
const publicUrl = await new Promise<string>((resolve, reject) => {
|
|
89
|
-
const timeout = setTimeout(() => {
|
|
90
|
-
tunnel.stop();
|
|
91
|
-
reject(
|
|
92
|
-
new Error(
|
|
93
|
-
'Tunnel URL generation timeout. Please check if port ' +
|
|
94
|
-
port +
|
|
95
|
-
' is accessible and has a running service.'
|
|
96
|
-
)
|
|
97
|
-
);
|
|
98
|
-
}, 90000); // 90s timeout (first time may take longer)
|
|
99
|
-
|
|
100
|
-
tunnel.on('url', (url: string) => {
|
|
101
|
-
// Validate that this is an actual tunnel URL, not the API endpoint
|
|
102
|
-
// Real tunnel URLs: https://random-words-1234.trycloudflare.com
|
|
103
|
-
// API endpoint (invalid): https://api.trycloudflare.com
|
|
104
|
-
if (url.includes('api.trycloudflare.com')) {
|
|
105
|
-
debug.log('tunnel', `Ignoring API endpoint URL: ${url}`);
|
|
106
|
-
return; // Ignore API endpoint, wait for real tunnel URL
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
clearTimeout(timeout);
|
|
110
|
-
const urlGenerationTime = Date.now() - urlGenerationStart;
|
|
111
|
-
timings.urlGeneration = urlGenerationTime;
|
|
112
|
-
|
|
113
|
-
debug.log('tunnel', `[PROGRESS:CONNECTED] ✅ Tunnel URL generated in ${urlGenerationTime}ms: ${url}`);
|
|
114
|
-
debug.log('tunnel', 'Tunnel is now ready to use!');
|
|
115
|
-
onProgress?.('connected', { url, time: urlGenerationTime });
|
|
116
|
-
resolve(url);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
tunnel.once('error', (error: Error) => {
|
|
120
|
-
clearTimeout(timeout);
|
|
121
|
-
tunnel.stop();
|
|
122
|
-
debug.error('tunnel', 'Tunnel error:', error);
|
|
123
|
-
reject(error);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
tunnel.once('exit', (code: number | null) => {
|
|
127
|
-
if (code !== 0 && code !== null) {
|
|
128
|
-
clearTimeout(timeout);
|
|
129
|
-
debug.error('tunnel', `Tunnel exited with error code ${code}`);
|
|
130
|
-
reject(
|
|
131
|
-
new Error(
|
|
132
|
-
`Tunnel failed to start (exit code ${code}). This might be a network/DNS issue. Please check your internet connection.`
|
|
133
|
-
)
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// Log connection info when it arrives (non-blocking)
|
|
140
|
-
// Note: We don't wait for 'connected' event because tunnel is already functional after URL is generated
|
|
141
|
-
tunnel.once('connected', (info: TunnelConnection) => {
|
|
142
|
-
debug.log('tunnel', `✅ Tunnel connected:`, info);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
// Setup auto-stop timer
|
|
146
|
-
const autoStopTimer = setTimeout(() => {
|
|
147
|
-
debug.log('tunnel', `Auto-stopping tunnel for project ${projectId} on port ${port}`);
|
|
148
|
-
this.stopTunnel(projectId, port);
|
|
149
|
-
}, autoStopMinutes * 60 * 1000);
|
|
150
|
-
|
|
151
|
-
// Handle tunnel exit
|
|
152
|
-
tunnel.on('exit', (code: number | null) => {
|
|
153
|
-
debug.log('tunnel', `Tunnel exited with code ${code}`);
|
|
154
|
-
this.activeTunnels.delete(tunnelKey);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
// Store tunnel instance
|
|
158
|
-
this.activeTunnels.set(tunnelKey, {
|
|
159
|
-
tunnel,
|
|
160
|
-
publicUrl,
|
|
161
|
-
localPort: port,
|
|
162
|
-
startedAt: new Date(),
|
|
163
|
-
autoStopTimer
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
const totalTime = Date.now() - tunnelStartTime;
|
|
167
|
-
timings.total = totalTime;
|
|
168
|
-
|
|
169
|
-
debug.log('tunnel', `[PROGRESS:COMPLETE] Tunnel setup completed in ${totalTime}ms`);
|
|
170
|
-
|
|
171
|
-
return {
|
|
172
|
-
publicUrl,
|
|
173
|
-
status: 'active',
|
|
174
|
-
timings
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Stop tunnel for a project port
|
|
180
|
-
*/
|
|
181
|
-
stopTunnel(projectId: string, port: number): void {
|
|
182
|
-
const tunnelKey = this.getTunnelKey(projectId, port);
|
|
183
|
-
const instance = this.activeTunnels.get(tunnelKey);
|
|
184
|
-
|
|
185
|
-
// If tunnel is not found, it's already stopped - return success silently
|
|
186
|
-
if (!instance) {
|
|
187
|
-
debug.log('tunnel', `Tunnel for port ${port} is already stopped`);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
debug.log('tunnel', `Stopping tunnel for project ${projectId} on port ${port}`);
|
|
192
|
-
|
|
193
|
-
// Clear auto-stop timer
|
|
194
|
-
if (instance.autoStopTimer) {
|
|
195
|
-
clearTimeout(instance.autoStopTimer);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Stop the tunnel
|
|
199
|
-
instance.tunnel.stop();
|
|
200
|
-
|
|
201
|
-
this.activeTunnels.delete(tunnelKey);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Get tunnel info for a project port
|
|
206
|
-
*/
|
|
207
|
-
getTunnelInfo(projectId: string, port: number): TunnelInstance | null {
|
|
208
|
-
const tunnelKey = this.getTunnelKey(projectId, port);
|
|
209
|
-
return this.activeTunnels.get(tunnelKey) || null;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Check if tunnel is active for a project port
|
|
214
|
-
*/
|
|
215
|
-
isActive(projectId: string, port: number): boolean {
|
|
216
|
-
const tunnelKey = this.getTunnelKey(projectId, port);
|
|
217
|
-
return this.activeTunnels.has(tunnelKey);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Get all active tunnels for a project
|
|
222
|
-
*/
|
|
223
|
-
getProjectTunnels(projectId: string): Array<TunnelInstance & { port: number }> {
|
|
224
|
-
const tunnels: Array<TunnelInstance & { port: number }> = [];
|
|
225
|
-
for (const [key, instance] of this.activeTunnels.entries()) {
|
|
226
|
-
if (key.startsWith(`${projectId}:`)) {
|
|
227
|
-
tunnels.push({ ...instance, port: instance.localPort });
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
return tunnels;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Get all active tunnels
|
|
235
|
-
*/
|
|
236
|
-
getAllActiveTunnels(): Map<string, TunnelInstance> {
|
|
237
|
-
return this.activeTunnels;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Cleanup all tunnels (on server shutdown)
|
|
242
|
-
*/
|
|
243
|
-
cleanup(): void {
|
|
244
|
-
debug.log('tunnel', 'Cleaning up all active tunnels');
|
|
245
|
-
for (const [key, instance] of this.activeTunnels) {
|
|
246
|
-
const [projectId, portStr] = key.split(':');
|
|
247
|
-
const port = parseInt(portStr, 10);
|
|
248
|
-
this.stopTunnel(projectId, port);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// Singleton instance
|
|
254
|
-
export const projectTunnelManager = new ProjectTunnelManager();
|
|
255
|
-
|
|
256
|
-
// Cleanup on process exit
|
|
257
|
-
process.on('exit', () => {
|
|
258
|
-
projectTunnelManager.cleanup();
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
process.on('SIGINT', () => {
|
|
262
|
-
projectTunnelManager.cleanup();
|
|
263
|
-
process.exit(0);
|
|
264
|
-
});
|