File size: 1,457 Bytes
14776e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5342de8
2f20ffc
14776e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { createServer } from 'node:http';
import express from 'express';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { createBareServer } from '@tomphttp/bare-server-node';
import { uvPath } from '@titaniumnetwork-dev/ultraviolet';
import { baremuxPath } from '@mercuryworkshop/bare-mux/node';

const __dirname = dirname(fileURLToPath(import.meta.url));

const bareServer = createBareServer('/bare/');
const app = express();

app.use(express.static(join(__dirname, 'public')));
app.use('/uv/', express.static(uvPath));
app.use('/baremux/', express.static(join(__dirname, 'node_modules', '@mercuryworkshop', 'bare-mux', 'dist')));
app.use('/bareclient/', express.static(join(__dirname, 'node_modules', '@tomphttp', 'bare-client', 'dist')));

app.use((req, res) => {
    res.status(404).sendFile(join(__dirname, 'public', 'index.html'));
});

const server = createServer();

server.on('request', (req, res) => {
    if (bareServer.shouldRoute(req)) {
        bareServer.routeRequest(req, res);
    } else {
        app(req, res);
    }
});

server.on('upgrade', (req, socket, head) => {
    if (bareServer.shouldRoute(req)) {
        bareServer.routeUpgrade(req, socket, head);
    } else {
        socket.end();
    }
});

const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
    console.log(`Shadow Proxy is running at http://localhost:${PORT}`);
});