quake3 server connector | index | dns lookup | Search

This code is a Node.js script that establishes an SSH connection to a remote server, downloads a URL using wget, and modifies a file on the server by appending a specific format to the end of the file. The script also includes error handling and exports the remoteGet function for use in other modules.

Run example

npm run import -- "ssh remote wget"

ssh remote wget

var fs = require('fs')
var path = require('path')
var NodeSSH = require('node-ssh')
var ssh = new NodeSSH()
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
var privateKeyPath
if(fs.existsSync('./id_rsa')) {
    privateKeyPath = path.resolve('./id_rsa')
} else {
    privateKeyPath = path.join(PROFILE_PATH, '.ssh/id_rsa')
}

var DEFAULT_SSH = process.env.DEFAULT_SSH || 'okayfun.com'
var DEFAULT_SSH_USER = process.env.DEFAULT_SSH_USER || 'root'

/*
ssh.connect({
  host: DEFAULT_SSH,
  username: DEFAULT_SSH_USER,
  privateKey: privateKeyPath
})
*/

async function remoteGet(url, output, cwd) {
    var options = {
        cwd: cwd,
        onStdout(chunk) {
          console.log('stdoutChunk', chunk.toString('utf8'))
        },
        onStderr(chunk) {
          console.log('stderrChunk', chunk.toString('utf8'))
        },
    }
    try {
        await ssh.exec('/usr/bin/wget', ['-O', output, url], options)
        await ssh.exec(`
fileLength=$(wc -l ../index.json | cut -d' ' -f1);
sed "$((fileLength-1))s/$/,/;
${fileLength}i  \\\t\"\":\"\"" ../index.json`, [], options)
        
    } catch (e) {
        console.log(e)
    }
}

module.exports = remoteGet

What the code could have been:

const fs = require('fs');
const path = require('path');
const NodeSSH = require('node-ssh');
const { console } = require('console');

// Import environment variables and user profiles
const { HOME, HOMEPATH, USERPROFILE } = process.env;
const PROFILE_PATH = HOME || HOMEPATH || USERPROFILE;

// Constants for SSH connections
const DEFAULT_SSH = process.env.DEFAULT_SSH || 'okayfun.com';
const DEFAULT_SSH_USER = process.env.DEFAULT_SSH_USER || 'root';

// Check if local SSH key exists, otherwise use system-wide path
const privateKeyPath = fs.existsSync('./id_rsa')? path.resolve('./id_rsa') : path.join(PROFILE_PATH, '.ssh/id_rsa');

// Initialize SSH client
const ssh = new NodeSSH();

// TODO: Remove hardcoded SSH connections and use environment variables instead
async function remoteGet(url, output, cwd) {
  // Options for SSH execution
  const options = {
    cwd: cwd,
    onStdout(chunk) {
      console.log('stdoutChunk', chunk.toString('utf8'));
    },
    onStderr(chunk) {
      console.log('stderrChunk', chunk.toString('utf8'));
    },
  };

  try {
    // Execute SSH command to get remote file using wget
    await ssh.exec('/usr/bin/wget', ['-O', output, url], options);

    // Append a comma to the last line of the index.json file
    const fileLength = await ssh.exec('wc -l../index.json | cut -d" " -f1', [], options);
    const sedCommand = `sed "\$(( ${fileLength} - 1 ))s/$/,/;\$(${fileLength})i  \\\\t\":\"\"""../index.json`;
    await ssh.exec(sedCommand, [], options);
  } catch (error) {
    console.error(error);
  }
}

module.exports = remoteGet;

Code Breakdown

Importing Modules

var fs = require('fs')
var path = require('path')
var NodeSSH = require('node-ssh')
var ssh = new NodeSSH()

Environment Variable Setup

var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE
var privateKeyPath
if(fs.existsSync('./id_rsa')) {
    privateKeyPath = path.resolve('./id_rsa')
} else {
    privateKeyPath = path.join(PROFILE_PATH, '.ssh/id_rsa')
}

Default SSH Configuration

var DEFAULT_SSH = process.env.DEFAULT_SSH || 'okayfun.com'
var DEFAULT_SSH_USER = process.env.DEFAULT_SSH_USER || 'root'

SSH Connection (Commented Out)

// ssh.connect({
//   host: DEFAULT_SSH,
//   username: DEFAULT_SSH_USER,
//   privateKey: privateKeyPath
// })

Remote Get Function

async function remoteGet(url, output, cwd) {
    //...
}

Function Body

try {
    await ssh.exec('/usr/bin/wget', ['-O', output, url], options)
    await ssh.exec(`
    fileLength=$(wc -l../index.json | cut -d''-f1);
    sed "$((fileLength-1))s/$/,/;
    ${fileLength}i  \\\t\"\":\"\""../index.json`, [], options)
} catch (e) {
    console.log(e)
}

Error Handling and Export

} catch (e) {
    console.log(e)
}
module.exports = remoteGet