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(`
${link}
`); }); app.get('/stats', (req, res) => { const html = ` Server Stats

Server Statistics

Connections

${stats.activeConnections}

Active

${stats.totalConnections}

Total

Bandwidth

${(stats.totalBandwidth / (1024 * 1024)).toFixed(2)} MB

Total

Server Info

Port: ${port}

UUID: ${uuid}

Recent Logs

${stats.logs.map(log => ` `).join('')}
Timestamp Message
${log.timestamp} ${log.message}
`; res.send(html); }); server.listen(port, () => { logcb('Server is listening on port:', port)(); })