Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Initial code for simple proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Sep 18, 2019
1 parent f394688 commit d15795b
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .cleanup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"disabled": ["jssource.py"]
}
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: "hive"
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package-lock.json

/node_modules
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("prettier-config-hive");
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceRoot}/app.js",
"env": {
"NODE_ENV": "development"
}
}
]
}
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM hivesolutions/alpine_dev:latest

LABEL version="1.0"
LABEL maintainer="Platforme <[email protected]>"

EXPOSE 8080

ENV HOST 0.0.0.0
ENV PORT 8080
ENV NODE_ENV production

ADD app.js /app/
ADD package.json /app/
ADD lib /app/lib

WORKDIR /app

RUN apk update && apk add nodejs npm
RUN npm install

CMD ["/usr/bin/node", "/app/app.js"]
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# simple-proxy
Simple proxy pipeline proxy service
# Simple (HTTP) Proxy

Simple HTTP proxy pipeline service.
76 changes: 76 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// requires the multiple libraries
const bodyParser = require("body-parser");
const express = require("express");
const process = require("process");
const util = require("hive-js-util");
const info = require("./package");
const lib = require("./lib");
const request = require("request");

// builds the initial application object to be used
// by the application for serving
const app = express();

// ensures that the content type is exposed in the request
// object and that the body is parsed accordingly (JSON
// and URL encoding)
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

process.on("SIGINT", function() {
process.exit();
});

process.on("SIGTERM", function() {
process.exit();
});

process.on("exit", () => {
util.Logging.info("Exiting on user's request");
lib.destroy();
});

app.get("/info", (req, res, next) => {
res.json({
name: info.name,
version: info.version,
node: process.version
});
});

app.all("*", (req, res, next) => {
async function clojure() {
lib.verifyKey(req);
await new Promise(function(resolve, reject) {
try {
// constructs the initial options object with the
// processed headers and query string
const options = {
baseUrl: lib.TARGET,
uri: req.path,
method: req.method,
headers: lib.proxyHeaders(req),
qs: req.query,
forever: true,
pool: { maxSockets: Infinity }
};

// runs the changed request with the transformed values so
// that they become compliant with the RIPE API
request(options)
.pipe(res)
.on("finish", resolve)
.on("error", reject);
} catch (err) {
reject(err);
}
});
}
clojure().catch(next);
});

app.listen(lib.PORT, lib.HOSTNAME, () => {
lib.startLogging();
util.Logging.info("Listening on " + lib.HOSTNAME + ":" + String(lib.PORT));
lib.init();
});
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const proxy = require("./proxy");
const util = require("./util");

Object.assign(module.exports, proxy);
Object.assign(module.exports, util);
3 changes: 3 additions & 0 deletions lib/proxy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const proxy = require("./proxy");

Object.assign(module.exports, proxy);
21 changes: 21 additions & 0 deletions lib/proxy/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function proxyHeaders(req) {
const headers = Object.assign({}, req.headers || {});

const skip = ["connection", "content-length", "host"];
skip.forEach(header => {
delete headers[header];
});

if (!headers["x-forwarded-for"] !== undefined) {
headers["x-forwarded-for"] = req.connection.remoteAddress;
}
if (!headers["x-forwarded-proto"] !== undefined) {
headers["x-forwarded-proto"] = req.protocol;
}

return headers;
}

module.exports = {
proxyHeaders: proxyHeaders
};
29 changes: 29 additions & 0 deletions lib/util/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const config = require("./config");

const init = () => {};

const destroy = () => {};

/**
* Verifies that the key present in the request matches
* the one defined in the current configuration, ensuring
* that proper security measures are in place.
*
* @param {any} req The request to retrieve the key.
*/
const verifyKey = req => {
if (!config.KEY) {
return;
}
const _key = req.query.key || req.headers["X-Proxy-Key"] || null;
if (config.KEY === _key) {
return;
}
throw new Error("Invalid key");
};

module.exports = {
init: init,
destroy: destroy,
verifyKey: verifyKey
};
34 changes: 34 additions & 0 deletions lib/util/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const util = require("hive-js-util");

const HOSTNAME = process.env.HOST ? process.env.HOST : "127.0.0.1";
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 3000;
const KEY = process.env.PROXY_KEY ? process.env.PROXY_KEY : null;
const TARGET = process.env.PROXY_TARGET
? process.env.PROXY_TARGET
: "https://sandbox.platforme.com/";
const SDK_URL = process.env.RIPE_SDK_URL
? process.env.RIPE_SDK_URL
: "https://sandbox.platforme.com/api/";

const startLogging = () => {
const logger = util.Logging.getLogger(undefined, {
level: util.Logging.constants.DEBUG
});

if (util.Logging.ConsolaHandler.isReady()) {
logger.addHandler(new util.Logging.ConsolaHandler());
logger.setFormatter(new util.Logging.SimpleFormatter("{asctime} {message}"));
} else {
logger.addHandler(new util.Logging.StreamHandler());
logger.setFormatter(new util.Logging.SimpleFormatter());
}
};

module.exports = {
HOSTNAME: HOSTNAME,
PORT: PORT,
KEY: KEY,
SDK_URL: SDK_URL,
TARGET: TARGET,
startLogging: startLogging
};
5 changes: 5 additions & 0 deletions lib/util/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NotImplementedError extends Error {}

module.exports = {
NotImplementedError: NotImplementedError
};
7 changes: 7 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const base = require("./base");
const config = require("./config");
const errors = require("./errors");

Object.assign(module.exports, base);
Object.assign(module.exports, config);
Object.assign(module.exports, errors);
53 changes: 53 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "simple-proxy",
"version": "0.1.0",
"description": "Simple HTTP proxy for node.js",
"keywords": [
"http",
"proxy",
"simple"
],
"homepage": "https://github.com/ripe-tech/simple-proxy#readme",
"bugs": {
"url": "https://github.com/ripe-tech/simple-proxy/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ripe-tech/simple-proxy.git"
},
"license": "Apache-2.0",
"author": "Platforme",
"files": [
"app.js",
"lib/**/*.css",
"lib/**/*.js",
"test/**/*.js"
],
"main": "app.js",
"scripts": {
"lint": "eslint \"./**/*.js\"",
"lint-fix": "eslint \"./**/*.js\" --fix",
"prettier": "prettier \"./**/*.{js,json}\" --write",
"pretty": "npm run prettier && npm run lint-fix",
"start": "node app.js",
"test": "mocha --recursive",
"upgrade": "npx sort-package-json && ncu -u"
},
"dependencies": {
"consola": "^2.10.1",
"express": "^4.17.1",
"hive-js-util": "^0.2.1",
"request": "^2.88.0",
"ripe-sdk": "^1.6.43",
"uuid": "^3.3.3"
},
"devDependencies": {
"eslint": "^6.4.0",
"eslint-config-hive": "^0.1.22",
"mocha": "^6.2.0",
"npm-check-updates": "^3.1.23",
"prettier": "^1.18.2",
"prettier-config-hive": "^0.1.1",
"sort-package-json": "^1.22.1"
}
}
14 changes: 14 additions & 0 deletions test/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const assert = require("assert");

describe("Array", function() {
describe("#indexOf()", function() {
it("should return -1 when the value is not present", () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(4));
});
});
describe("#length", function() {
it("should return proper length", () => {
assert.strictEqual(3, [1, 2, 3].length);
});
});
});

0 comments on commit d15795b

Please sign in to comment.