Secure Static IP for Node.js Multi-Protocol (HTTPS & SFTP)

Learn how to securely route multiple protocols (HTTPS and SFTP) in Node.js through a QuotaGuard Shield proxy.

Prerequisites

npm install https-proxy-agent request ssh2-sftp-client socks
curl https://s3.amazonaws.com/quotaguard/qgtunnel-latest.tar.gz | tar xz
curl https://s3.amazonaws.com/quotaguard/qgpass-latest.tar.gz | tar xz

Instructions

Run example
QUOTAGUARDSHIELD_URL=... bin/qgtunnel bin/qgpass node app.js
Test in Docker
docker build -t qg-shield-node-https-sftp-example .
docker run -e QUOTAGUARDSHIELD_URL=... qg-shield-node-https-sftp-example

Code Samples

App.js
import url from 'url';
import { HttpsProxyAgent } from 'https-proxy-agent';
import request from 'request';
import { readFile } from "fs";
import SFTPClient from 'ssh2-sftp-client';

const testEndpoint = 'https://ip.quotaguard.com';
const proxy = "http://localhost:8080"; // Corrected typo in localhost
const agent = new HttpsProxyAgent(proxy);
const options = {
  uri: url.parse(testEndpoint),
  agent
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log('Response from "ip.quotaguard.com": ', body);
  } else {
    console.log('error: ', error);
  }
}

request(options, callback);

agent.destroy();

const host = 'test.rebex.net';
const port = 22; // default SSH/SFTP port on remote server

const client = new SFTPClient();
client.connect({
  host,
  port,
  username: 'demo',
  password: 'password'
}).then(() => {
  console.log("Connected over SFTP, downloading readme.txt");
  return client.get("readme.txt", "readme.txt");
})
.then(() => {
  console.log("File Downloaded!");
  readFile("readme.txt", function(err, data){
    if (err) {
      console.error(err);
    } else {
      console.log(data.toString('utf8'));
    }
    client.end(); // Ensure the client exits after reading the file
    process.exit(0);
  });
})
.catch(err => {
  console.error(err.message);
  client.end(); // Ensure the client exits on error
});
Docker File
FROM node:latest

WORKDIR /app/

COPY package.json /app/package.json
RUN npm install

COPY app.js /app/app.js

# Download and extract qgpass
RUN curl https://s3.amazonaws.com/quotaguard/qgpass-latest.tar.gz | tar xz -C /app/
ENV QGPASS_DEBUG true

RUN curl https://s3.amazonaws.com/quotaguard/qgtunnel-latest.tar.gz | tar xz
ENV QGTUNNEL_DEBUG true

ENTRYPOINT [ "/app/bin/qgtunnel", "/app/bin/qgpass", "node", "app.js" ]