-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (107 loc) · 3.27 KB
/
Copy pathindex.js
File metadata and controls
122 lines (107 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import express from "express";
import morgan from "morgan";
import { fileURLToPath } from "url";
import * as fs from "fs";
import * as rfs from "rotating-file-stream";
import * as path from "path";
import { createProxyMiddleware } from "http-proxy-middleware";
import { constants } from "buffer";
import axios from "axios";
import "dotenv/config";
import { Agent, createServer } from "https";
// Create Express Server
const app = express();
app.set("trust proxy", true);
// Configuration
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
const PORT = 8080;
const HOST = "localhost";
const API_SERVICE_URL = process.env.API_SERVICE_URL;
var jSession = "";
const options = {
key: fs.readFileSync("./certificate/private.key"),
cert: fs.readFileSync("./certificate/certificate.pem"),
secureOptions:
constants.SSL_OP_NO_TLSv1 |
constants.SSL_OP_NO_TLSv1_1 |
constants.SSL_OP_NO_SSLv2 |
constants.SSL_OP_NO_SSLv3,
};
const getjSession = () => {
const URL = `${API_SERVICE_URL}/login`;
const BODY = {
auth_user: process.env.AUTH_USER,
password: process.env.PASSWORD,
};
const getSEN = async () => {
const rawResponse = await axios.post(URL, BODY, {
// proxy: {
// protocol: 'http',
// host: 'nseproxy.sce.com',
// port: 8080,
// auth: {
// username: process.env.PROXY_USER,
// password: process.env.PROXY_PASSWORD
// }
// }
});
jSession = rawResponse.headers.get("Set-Cookie")[0].split(";")[0];
console.log(jSession);
};
getSEN();
};
//Obtain the session ID and refresh it once in 30 mins
if (jSession == "") {
getjSession();
setInterval(getjSession, 1_800_000);
}
// Logging
const pad = (num) => (num > 9 ? "" : "0") + num;
const generator = (time, index) => {
if (!time) return "file.log";
var month = time.getFullYear() + "" + pad(time.getMonth() + 1);
var day = pad(time.getDate());
var hour = pad(time.getHours());
var minute = pad(time.getMinutes());
return `${month}/${month}${day}-${hour}${minute}-${index}-file.log`;
};
const accessLogStream = rfs.createStream(generator, {
interval: "1d", // rotate daily
path: path.join(__dirname, "logs"),
size: "10M", // rotate every 10 MegaBytes written
});
app.use(
morgan("[:date[clf]] :referrer :req[header] :method :url - :status", {
stream: accessLogStream,
})
);
//Check if JSESSION is acquired
app.get("/jsession", (req, res, next) => {
res.send("session id: " + jSession);
});
// Redirect all requests to SEN Cloud
app.use(
"",
createProxyMiddleware({
target: API_SERVICE_URL,
changeOrigin: true,
agent: new Agent({
maxSockets: Infinity,
keepAlive: true,
}),
pathRewrite: { "^/enmanager": "" },
onProxyReq: (proxyReq, req, res) => {
proxyReq.setHeader("Cookie", jSession);
},
onError: (err, req, res) => {
console.log(err);
},
})
);
// Start Proxy on HTTP 8080
app.listen(PORT, HOST, () => {
console.log(`Starting Proxy at ${HOST}:${PORT}`);
});
// Start Proxy on HTTPS 443
// createServer(options, app).listen(443)