-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
110 lines (92 loc) · 3.13 KB
/
start.js
File metadata and controls
110 lines (92 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
// Get mode from command line argument
const mode = process.argv[2];
if (!mode || !['local', 'tunnel'].includes(mode)) {
console.log('🚀 FileShare Launcher\n');
console.log('Usage: node start.js <mode>');
console.log('');
console.log('Modes:');
console.log(' local - Start local server only (default port 3000)');
console.log(' tunnel - Start with a public HTTPS tunnel (no signup required)');
console.log('');
console.log('Examples:');
console.log(' node start.js local');
console.log(' node start.js tunnel');
process.exit(1);
}
const PORT = 3000;
console.log(`🚀 Starting FileShare in ${mode.toUpperCase()} mode...\n`);
// Check if uploads directory exists
const uploadDir = path.join(__dirname, 'uploads');
if (!fs.existsSync(uploadDir)) {
console.log('📁 Creating uploads directory...');
fs.mkdirSync(uploadDir);
}
// Start the FileShare server
console.log(`🔧 Starting FileShare server on port ${PORT}...`);
const server = spawn('node', ['server.js'], {
stdio: ['inherit', 'pipe', 'pipe']
});
server.stdout.on('data', (data) => {
console.log(data.toString());
});
server.stderr.on('data', (data) => {
console.error(`Server error: ${data}`);
});
server.on('close', (code) => {
console.log(`\n🔴 Server exited with code ${code}`);
process.exit(code);
});
if (mode === 'local') {
console.log(`✅ FileShare started in LOCAL mode!`);
console.log(`🌍 Access your FileShare at: http://localhost:${PORT}`);
console.log('💡 Press Ctrl+C to stop the server\n');
process.on('SIGINT', () => {
console.log('\n🛑 Shutting down...');
server.kill();
process.exit(0);
});
} else if (mode === 'tunnel') {
console.log('✅ Starting tunnel...\n');
startTunnel();
}
async function startTunnel() {
console.log('🌐 Creating public tunnel...');
let localtunnel;
try {
localtunnel = require('localtunnel');
} catch {
console.error('❌ localtunnel not found. Run: npm install');
server.kill();
process.exit(1);
}
let tunnel;
try {
tunnel = await localtunnel({ port: PORT });
} catch (err) {
console.error('❌ Failed to create tunnel:', err.message);
server.kill();
process.exit(1);
}
console.log('\n🎉 SUCCESS! Your FileShare is now accessible online at:');
console.log(`🌍 ${tunnel.url}`);
console.log('\n📋 Share this URL with anyone to access your FileShare!');
console.log('💡 Note: first-time visitors will see a one-time confirmation page.');
console.log('💡 Press Ctrl+C to stop both server and tunnel\n');
tunnel.on('error', (err) => {
console.error('❌ Tunnel error:', err.message);
});
tunnel.on('close', () => {
console.log('\n🔴 Tunnel closed. Stopping server...');
server.kill();
process.exit(0);
});
process.on('SIGINT', () => {
console.log('\n🛑 Shutting down...');
tunnel.close();
server.kill();
process.exit(0);
});
}