a remote administration server written in javascript in javascript

To create a remote administration server using JavaScript, you can leverage the power of Node.js and its libraries such as Express.js and Socket.io.

Here are the high-level steps to implement it:

  1. Install Node.js: If not already installed, install Node.js from https://nodejs.org/en/download/.

  2. Create a new directory for the project and navigate inside: mkdir remote-administration && cd remote-administration

  3. Create a package.json file: Run npm init command and fill out the necessary details.

  4. Install necessary dependencies: Run npm i express socket.io child_process command.

  5. Create index.js file: Here's sample code to get you started.

index.tsx
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const { exec } = require('child_process');

// Serve static files from public directory
app.use(express.static('public'));

io.on('connection', (socket) => {
  console.log('a user connected');

  socket.on('cmd', (cmd) => {
    console.log(`command: ${cmd}`);
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        socket.emit('output', error.message);
        return;
      }
      if (stderr) {
        socket.emit('output', stderr);
        return;
      }
      socket.emit('output', stdout);
    });
  });

  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
});

// Start the server
http.listen(3000, () => {
  console.log('listening on *:3000');
});
844 chars
37 lines
  1. Create a public directory: Create a public directory, create index.html file inside it and add the following code.
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Remote Administration Server</title>
</head>

<body>
  <h1>Remote Administration Server</h1>
  <input id="input" type="text" placeholder="Type your command...">
  <button id="send">Send</button>
  <div id="output"></div>

  <script src="/socket.io/socket.io.js"></script>
  <script>
    const socket = io();

    const input = document.getElementById('input');
    const sendButton = document.getElementById('send');
    const outputDiv = document.getElementById('output');

    sendButton.addEventListener('click', () => {
      const cmd = input.value;
      input.value = '';
      socket.emit('cmd', cmd);
    });

    socket.on('output', (output) => {
      outputDiv.innerHTML += output + '<br>';
    });
  </script>
</body>
</html>
887 chars
36 lines
  1. Start the server: Run npm start command.

Now, you can access your remote administration server in a web browser by visiting http://localhost:3000/. From there, you can type a command and execute that on the server. The output of the command will be displayed on the web page.

gistlibby LogSnag