Skip to content

Commit

Permalink
fix: #113 ip detection seperated to single function and refactored (#118
Browse files Browse the repository at this point in the history
)

* fix: #113 ip detection seperated to single function and refactored
  • Loading branch information
mabunixda authored Sep 27, 2022
1 parent 3900896 commit a7c5c36
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
18 changes: 17 additions & 1 deletion alexa/alexa-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ module.exports = {
) ||
false,
bri_default: process.env.BRI_DEFAULT || 254,
prefixUUID: 'f6543a06-da50-11ba-8d8f-'
prefixUUID: 'f6543a06-da50-11ba-8d8f-',

AlexaIPAddress: function (req) {
if (req.headers['x-forwarded-for'] !== undefined) {
return req.headers['x-forwarded-for']
}
if (req.socket.remoteAddress !== undefined) {
return req.socket.remoteAddress
}
if (req.connection.remoteAddress !== undefined) {
return req.connection.remoteAddress
}
if ((req.connection.socket) && (req.connection.socket.remoteAddress)) {
return req.connection.socket.remoteAddress
}
return undefined
}
}
11 changes: 2 additions & 9 deletions alexa/alexa-home-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ module.exports = function (RED) {
}

RED.httpNode.use(function (req, res, next) {
const alexaIp = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress ||
undefined

req.alexaIp = alexaIp

req.headers.alexaIp = alexaHome.AlexaIPAddress(req)
return next()
})

Expand Down Expand Up @@ -473,7 +466,7 @@ module.exports = function (RED) {
payload: request.body
}

msg.alexa_ip = request.alexa_ip
msg.alexa_ip = alexaHome.AlexaIPAddress(request)

if (alexaHome.isDebug) {
const httpHeader = Object.keys(request.headers)
Expand Down
13 changes: 3 additions & 10 deletions alexa/alexa-hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const express = require('express')
const bodyParser = require('body-parser')
require('./alexa-home')
const alexaHome = require('./alexa-home')
/**
* Hub to create communication with alexa devices
* @constructor
Expand Down Expand Up @@ -56,15 +56,8 @@ AlexaHub.prototype.createServer = function (protocol, options) {
})

app.use(function (req, res, next) {
const alexaIp = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress ||
undefined

req.alexaIp = alexaIp

node.controller.log('Request data: ' + alexaIp + '-' +
req.headers.alexaIp = alexaHome.AlexaIPAddress(req)
node.controller.log('Request data: ' + req.alexaIp + '-' +
node.port + '/' +
req.method + ' -> ' +
req.url)
Expand Down

0 comments on commit a7c5c36

Please sign in to comment.