File size: 7,290 Bytes
13968f4 | 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | const net = require('net');
const WebSocket = require('ws');
const express = require('express');
const http = require('http');
const auth = require("basic-auth");
const username = process.env.WEB_USERNAME || "admin";
const password = process.env.WEB_PASSWORD || "password";
const logcb = (...args) => console.log.bind(console, new Date().toISOString(), ...args);
const errcb = (...args) => console.error.bind(console, new Date().toISOString(), ...args);
const uuid = (process.env.UUID || '37a0bd7c-8b9f-4693-8916-bd1e2da0a817').replace(/-/g, '');
const port = process.env.PORT || 7860;
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server }, logcb('WebSocket server is listening on port:', port));
// Stats tracking
let stats = {
totalConnections: 0,
activeConnections: 0,
totalBandwidth: 0,
logs: []
};
function updateStats(action, data = {}) {
switch (action) {
case 'newConnection':
stats.totalConnections++;
stats.activeConnections++;
break;
case 'closeConnection':
stats.activeConnections--;
break;
case 'addBandwidth':
stats.totalBandwidth += data.bandwidth || 0;
break;
case 'addLog':
stats.logs.unshift({ timestamp: new Date().toISOString(), message: data.message });
if (stats.logs.length > 100) stats.logs.pop();
break;
}
}
wss.on('connection', (ws, req) => {
const clientIP = req.socket.remoteAddress;
const clientPort = req.socket.remotePort;
updateStats('newConnection');
updateStats('addLog', { message: `New connection from ${clientIP}:${clientPort}` });
ws.once('message', (msg) => {
let i = msg.readUInt8(17) + 19;
const targetPort = msg.readUInt16BE(i);
i += 2;
const ATYP = msg.slice(i, i += 1).readUInt8();
const host = ATYP === 1 ? msg.slice(i, i += 4).join('.') :
(ATYP === 2 ? new TextDecoder().decode(msg.slice(i + 1, i += 1 + msg.slice(i, i + 1).readUInt8())) :
(ATYP === 3 ? msg.slice(i, i += 16).reduce((s, b, i, a) => (i % 2 ? s.concat(a.slice(i - 1, i + 1)) : s), []).map(b => b.readUInt16BE(0).toString(16)).join(':') : ''));
updateStats('addLog', { message: `Resolved target - Host: ${host}, Port: ${targetPort}` });
ws.send(Buffer.from([msg[0], 0]));
const duplex = WebSocket.createWebSocketStream(ws);
const socket = net.connect({ host, port: targetPort }, function () {
updateStats('addLog', { message: `Connected to target - Host: ${host}, Port: ${targetPort}` });
this.write(msg.slice(i));
duplex.on('error', (err) => {
updateStats('addLog', { message: `Duplex Stream Error: ${err.message}` });
errcb('Duplex Stream Error:')(err);
})
.pipe(this)
.on('error', (err) => {
updateStats('addLog', { message: `Target Socket Error: ${err.message}` });
errcb('Target Socket Error:')(err);
})
.pipe(duplex);
// Track bandwidth
this.on('data', (chunk) => updateStats('addBandwidth', { bandwidth: chunk.length }));
});
socket.on('error', (err) => {
updateStats('addLog', { message: `Connection Error: ${err.message}` });
errcb('Connection Error:', { host, port: targetPort })(err);
});
}).on('error', (err) => {
updateStats('addLog', { message: `WebSocket Error: ${err.message}` });
errcb('WebSocket Error:')(err);
});
ws.on('close', () => {
updateStats('closeConnection');
updateStats('addLog', { message: `Connection closed with ${clientIP}:${clientPort}` });
});
});
app.use((req, res, next) => {
const user = auth(req);
if (user && user.name === username && user.pass === password) {
return next();
}
res.set("WWW-Authenticate", 'Basic realm="Node"');
return res.status(401).send();
});
app.get('/', (req, res) => {
const protocol = req.protocol;
let host = req.get('host');
let port = protocol === 'https' ? 443 : 80;
let path = req.path;
if (host.includes(':')) {
[host, port] = host.split(':');
}
const link = protocol === 'https' ?
`pler://${uuid}@${host}:${port}?path=${path}&security=tls&encryption=none&host=${host}&type=ws&sni=${host}#node-pler` :
`pler://${uuid}@${host}:${port}?type=ws&encryption=none&flow=&host=${host}&path=${path}#node-pler`;
res.send(`<html><body><pre>${link}</pre></body></html>`);
});
app.get('/stats', (req, res) => {
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Server Stats</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100">
<div class="container mx-auto px-4 py-8">
<h1 class="text-4xl font-bold mb-8 text-center text-gray-800">Server Statistics</h1>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<div class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-700">Connections</h2>
<p class="text-3xl font-bold text-blue-600">${stats.activeConnections}</p>
<p class="text-gray-600">Active</p>
<p class="text-3xl font-bold text-green-600 mt-4">${stats.totalConnections}</p>
<p class="text-gray-600">Total</p>
</div>
<div class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-700">Bandwidth</h2>
<p class="text-3xl font-bold text-purple-600">${(stats.totalBandwidth / (1024 * 1024)).toFixed(2)} MB</p>
<p class="text-gray-600">Total</p>
</div>
<div class="bg-white rounded-lg shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-700">Server Info</h2>
<p class="text-gray-600">Port: <span class="font-semibold">${port}</span></p>
<p class="text-gray-600">UUID: <span class="font-semibold">${uuid}</span></p>
</div>
</div>
<div class="mt-8 bg-white rounded-lg shadow-md p-6">
<h2 class="text-xl font-semibold mb-4 text-gray-700">Recent Logs</h2>
<div class="overflow-x-auto">
<table class="min-w-full">
<thead>
<tr>
<th class="px-4 py-2 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">Timestamp</th>
<th class="px-4 py-2 bg-gray-100 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">Message</th>
</tr>
</thead>
<tbody>
${stats.logs.map(log => `
<tr>
<td class="px-4 py-2 whitespace-nowrap text-sm text-gray-500">${log.timestamp}</td>
<td class="px-4 py-2 text-sm text-gray-800">${log.message}</td>
</tr>
`).join('')}
</tbody>
</table>
</div>
</div>
</div>
<script>
setTimeout(() => location.reload(), 5000);
</script>
</body>
</html>
`;
res.send(html);
});
server.listen(port, () => {
logcb('Server is listening on port:', port)();
}) |