org-feed / script.js
Kakadvn's picture
Rename Script.js to script.js
ee0821b verified
Raw History Blame Contribute Delete
7.29 kB
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)();
})