commit a23c87ba63b0747f2df21d790ba1864773ceb6d2 Author: Gasper Spagnolo Date: Tue Sep 20 10:54:35 2022 +0200 kje za vraga so vejice diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0b00e85 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM node:14-alpine AS development +# Add a work directory +WORKDIR /app + +# Cache and Install dependencies +COPY package.json . +RUN yarn install + +COPY index.js . + +# Expose port +EXPOSE 1234 + +# Start the app +CMD [ "yarn", "start"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..216dd6c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,10 @@ +version: "3.8" + +services: + proski_neki: + container_name: proski_neki + image: proski_neki + build: + context: . + ports: + - 1234:1234 diff --git a/index.js b/index.js new file mode 100644 index 0000000..1bdb89a --- /dev/null +++ b/index.js @@ -0,0 +1,77 @@ +const express = require("express"); +const http = require('http'); +const fetch = require('node-fetch'); +const https = require('https'); +const bodyParser = require('body-parser'); + +const httpsAgent = new https.Agent({ + rejectUnauthorized: false, +}); + +const app = express(); +app.use(bodyParser.urlencoded({ extended: true })); +app.use(bodyParser.json()) + +app.use('/proxy', async (req, res) => { + try { + // check if there is bearer token with value e8a8aead-95b5-47d0-9a10-2b7478f151bd + if (req.headers.authorization != 'Bearer e8a8aead-95b5-47d0-9a10-2b7478f151bd') { + res.status(401).json({ + success: false, + code: 401 + }); + return; + } + // check if there is a proxy-target-url provided in header + if (!req.headers['proxy-target-url']) { + res.status(400).json({ + success: false, + code: 400 + }); + return; + } + // copy over the headers from the request, while removing the authorization and Proxy-Target-URL headers + let headers = {}; + for (let key in req.headers) { + if (key != 'authorization' && key != 'proxy-target-url') { + headers[key] = req.headers[key]; + } + } + headers['host'] = null; + // print request body + // console.log(req.body); + let response = await fetch(req.headers['proxy-target-url'], { + method: req.method, + headers: headers, + body: req.method == 'GET' ? null : JSON.stringify(req.body), + agent: httpsAgent + }); + // return the response, with the response header, status code and body + console.log(`[${new Date().toLocaleString('bs-Latn-BA')}] ${req.method} ${req.headers['proxy-target-url']}: ${response.status}`); + res.status(response.status).json({ + success: true, + headers: response.headers, + status: response.status, + data: req.headers['proxy-target-url'].includes('https://najava.hzinfra.hr/hzinfo/default.asp') ? await response.buffer() : await response.text(), + sent: { + url: req.headers['proxy-target-url'], + method: req.method, + headers: headers, + body: req.body, + } + }); + + } catch (e) { + console.log(e); + res.status(500).json({ + success: false, + code: 500 + }) + return; + } +}) + + +http.createServer(app).listen(1234, async () => { + console.log("Server is running on port 1234"); +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..df2dcd9 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "prometko-api", + "version": "0.0.1", + "description": "API which contains all the API procedures used in the extended Prometko and MestniPromet project", + "main": "index.js", + "scripts": { + "test": "exit 0", + "start": "node index.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/vekejsn/prometko-api.git" + }, + "author": "vekejsn ", + "license": "ISC", + "bugs": { + "url": "https://github.com/vekejsn/prometko-api/issues" + }, + "homepage": "https://github.com/vekejsn/prometko-api#readme", + "dependencies": { + "cors": "^2.8.5", + "express": "^4.18.1", + "node-fetch": "2.6.1" + } +}