Skip to content

Commit 80e1d83

Browse files
committed
imp web rate limiter
1 parent 031ed7c commit 80e1d83

5 files changed

Lines changed: 33 additions & 4 deletions

File tree

OpenFlow/src/LoginProvider.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,30 @@ export class LoginProvider {
180180
});
181181
app.get("/dashboardauth", async (req: any, res: any, next: any) => {
182182
const span: Span = Logger.otel.startSpan("LoginProvider.user");
183+
let remoteip: string = req.connection.remoteAddress;
184+
if (req.headers["X-Forwarded-For"] != null) remoteip = req.headers["X-Forwarded-For"];
185+
if (req.headers["X-real-IP"] != null) remoteip = req.headers["X-real-IP"];
186+
if (req.headers["x-forwarded-for"] != null) remoteip = req.headers["x-forwarded-for"];
187+
if (req.headers["x-real-ip"] != null) remoteip = req.headers["x-real-ip"];
188+
if (!NoderedUtil.IsNullEmpty(remoteip)) span.setAttribute("remoteip", remoteip);
183189
try {
190+
if (req.user) {
191+
const user: TokenUser = TokenUser.From(req.user);
192+
span.setAttribute("username", user.username);
193+
if (user != null) {
194+
const allowed = user.roles.filter(x => x.name == "dashboardusers" || x.name == "admins");
195+
if (allowed.length > 0) {
196+
Logger.instanse.info("dashboardauth: Authorized " + user.username + " for " + req.url);
197+
return res.send({
198+
status: "success",
199+
display_status: "Success",
200+
message: "Connection OK"
201+
});
202+
} else {
203+
console.warn("dashboardauth: " + user.username + " is not member of 'dashboardusers' for " + req.url);
204+
}
205+
}
206+
}
184207
const authorization: string = req.headers.authorization;
185208
if (!NoderedUtil.IsNullEmpty(authorization) && authorization.indexOf(" ") > 1 &&
186209
(authorization.toLocaleLowerCase().startsWith("bearer") || authorization.toLocaleLowerCase().startsWith("jwt"))) {
@@ -222,6 +245,7 @@ export class LoginProvider {
222245
// const [login, password] = new Buffer(b64auth, 'base64').toString().split(':')
223246
const [login, password] = Buffer.from(b64auth, "base64").toString().split(':')
224247
if (login && password) {
248+
span.setAttribute("username", login);
225249
const user = await Auth.ValidateByPassword(login, password, span);
226250
if (user != null) {
227251
const allowed = user.roles.filter(x => x.name == "dashboardusers" || x.name == "admins");

OpenFlow/src/WebServer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ const BaseRateLimiter = new RateLimiterMemory({
3030
});
3131

3232
const rateLimiter = (req: express.Request, res: express.Response, next: express.NextFunction): void => {
33+
let remoteip: string = req.connection.remoteAddress;
34+
if (req.headers["X-Forwarded-For"] != null) remoteip = req.headers["X-Forwarded-For"] as string;
35+
if (req.headers["X-real-IP"] != null) remoteip = req.headers["X-real-IP"] as string;
36+
if (req.headers["x-forwarded-for"] != null) remoteip = req.headers["x-forwarded-for"] as string;
37+
if (req.headers["x-real-ip"] != null) remoteip = req.headers["x-real-ip"] as string;
3338
BaseRateLimiter
34-
.consume(req.ip)
39+
.consume(remoteip)
3540
.then((e) => {
3641
// console.info("API_O_RATE_LIMIT consumedPoints: " + e.consumedPoints + " remainingPoints: " + e.remainingPoints);
3742
next();

OpenFlowNodeRED/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openiap/nodered",
3-
"version": "1.2.62",
3+
"version": "1.2.63",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.62
1+
1.2.63

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openiap/openflow",
3-
"version": "1.2.62",
3+
"version": "1.2.63",
44
"description": "Simple wrapper around NodeRed, RabbitMQ and MongoDB to support a more scaleable NodeRed implementation.\r Also the \"backend\" for [OpenRPA](https://github.com/skadefro/OpenRPA)",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)