aedes-protocol-decoder
Advanced tools
Comparing version 2.1.0 to 2.2.0
69
index.js
'use strict' | ||
/** | ||
* @typedef ConnectionDetails | ||
* @property {string} ipAddress | ||
* @property {number} port | ||
* @property {string} serverIpAddress | ||
* @property {number} serverPort | ||
* @property {4 | 6 | 0} ipFamily | ||
* @property {boolean} isWebsocket | ||
* @property {boolean} isTls | ||
* @property {0 | 1 | 2} isProxy | ||
* @property {boolean | undefined} certAuthorized | ||
* @property {object | import('tls').PeerCertificate | undefined} cert | ||
* @property {Buffer | undefined} data | ||
*/ | ||
/** | ||
* @typedef HttpConnection | ||
* @property { import('net').Socket } _socket | ||
*/ | ||
const proxyProtocol = require('proxy-protocol-js') | ||
@@ -46,2 +66,10 @@ const forwarded = require('forwarded') | ||
/** | ||
* | ||
* @param {import('http').IncomingMessage} req | ||
* @param {HttpConnection} socket | ||
* @param {ConnectionDetails} proto | ||
* @returns {ConnectionDetails} | ||
* @todo replace socket arg by req.socket ? | ||
*/ | ||
function extractHttpDetails (req, socket, proto = {}) { | ||
@@ -58,2 +86,4 @@ const headers = req && req.headers ? req.headers : null | ||
} | ||
// ? should we try to parse req.url to get the port first and then fallback to socket.address()?.port ? | ||
proto.serverPort = socket._socket.address()?.port | ||
proto.port = socket._socket.remotePort | ||
@@ -66,2 +96,8 @@ proto.ipFamily = getProtoIpFamily(socket._socket.remoteFamily) | ||
/** | ||
* | ||
* @param {Buffer} buffer | ||
* @param {ConnectionDetails} proto | ||
* @returns {ConnectionDetails} | ||
*/ | ||
function extractProxyDetails (buffer, proto = {}) { | ||
@@ -76,2 +112,3 @@ let proxyProto | ||
proto.serverIpAddress = proxyProto.destination.ipAddress | ||
proto.serverPort = proxyProto.destination.port | ||
proto.data = proxyProto.data | ||
@@ -87,2 +124,3 @@ proto.isProxy = 1 | ||
proto.serverIpAddress = proxyProto.proxyAddress.destinationAddress.address.join('.') | ||
proto.serverPort = proxyProto.proxyAddress.destinationPort | ||
proto.ipFamily = 4 | ||
@@ -93,2 +131,3 @@ } else if (proxyProto.proxyAddress instanceof proxyProtocol.IPv6ProxyAddress) { | ||
proto.serverIpAddress = parseIpV6Array(proxyProto.proxyAddress.destinationAddress.address) | ||
proto.serverPort = proxyProto.proxyAddress.destinationPort | ||
proto.ipFamily = 6 | ||
@@ -103,2 +142,8 @@ } | ||
/** | ||
* | ||
* @param {import('net').Socket | HttpConnection} socket | ||
* @param {ConnectionDetails} proto | ||
* @returns {ConnectionDetails} | ||
*/ | ||
function extractSocketTLSDetails (socket, proto = {}) { | ||
@@ -109,2 +154,3 @@ socket = socket._socket || socket | ||
proto.cert = socket.getPeerCertificate(true) | ||
proto.isTls = true | ||
} | ||
@@ -114,2 +160,8 @@ return proto | ||
/** | ||
* | ||
* @param {import('net').Socket | HttpConnection} socket | ||
* @param {ConnectionDetails} proto | ||
* @returns {ConnectionDetails} | ||
*/ | ||
function extractSocketDetails (socket, proto = {}) { | ||
@@ -121,2 +173,3 @@ if (socket._socket && socket._socket.address) { | ||
proto.serverIpAddress = socket._socket.address().address | ||
proto.serverPort = socket._socket.address().port | ||
proto.ipFamily = getProtoIpFamily(socket._socket.remoteFamily) | ||
@@ -127,2 +180,3 @@ } else if (socket.address) { | ||
proto.serverIpAddress = socket.address().address | ||
proto.serverPort = socket.address().port | ||
proto.ipFamily = getProtoIpFamily(socket.remoteFamily) | ||
@@ -134,8 +188,17 @@ } | ||
/** | ||
* | ||
* @param {import('aedes').Connection} conn | ||
* @param {Buffer} buffer | ||
* @param {import('http').IncomingMessage} req | ||
* @returns {ConnectionDetails} | ||
*/ | ||
function protocolDecoder (conn, buffer, req) { | ||
const proto = {} | ||
const proto = { | ||
isProxy: 0, | ||
isWebsocket: false, | ||
isTls: false | ||
} | ||
if (!buffer) return proto | ||
const socket = conn.socket || conn | ||
proto.isProxy = 0 | ||
proto.isWebsocket = false | ||
extractHttpDetails(req, socket, proto) | ||
@@ -142,0 +205,0 @@ extractProxyDetails(buffer, proto) |
{ | ||
"name": "aedes-protocol-decoder", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Aedes plugin to decode connection frame and validate proxies", | ||
@@ -54,6 +54,6 @@ "main": "index.js", | ||
], | ||
"author": "Get Large <https://github.com/getlarge>", | ||
"author": "Edouard Maleix <https://github.com/getlarge>", | ||
"contributors": [ | ||
{ | ||
"name": "Get Large", | ||
"name": "Edouard Maleix", | ||
"url": "https://github.com/getlarge" | ||
@@ -68,23 +68,23 @@ }, | ||
"devDependencies": { | ||
"@types/node": "^14.0.1", | ||
"@typescript-eslint/eslint-plugin": "^4.28.0", | ||
"@typescript-eslint/parser": "^4.28.0", | ||
"aedes": "^0.46.0", | ||
"aedes-server-factory": "0.1.3", | ||
"faucet": "0.0.1", | ||
"@types/node": "^18.17.0", | ||
"@typescript-eslint/eslint-plugin": "^6.7.0", | ||
"@typescript-eslint/parser": "^6.7.0", | ||
"aedes": "^0.50.0", | ||
"aedes-server-factory": "0.2.1", | ||
"faucet": "0.0.4", | ||
"license-checker": "^25.0.1", | ||
"mqtt": "^4.2.8", | ||
"mqtt-packet": "^7.0.0", | ||
"mqtt": "^5.0.5", | ||
"mqtt-packet": "^8.2.0", | ||
"nyc": "^15.1.0", | ||
"pre-commit": "^1.2.2", | ||
"release-it": "^14.10.0", | ||
"release-it": "^16.1.5", | ||
"snazzy": "^9.0.0", | ||
"standard": "^16.0.3", | ||
"tape": "^5.2.0", | ||
"typescript": "^4.3.4" | ||
"standard": "^17.1.0", | ||
"tape": "^5.6.6", | ||
"typescript": "^5.2.2" | ||
}, | ||
"dependencies": { | ||
"forwarded": "^0.2.0", | ||
"proxy-protocol-js": "^4.0.5" | ||
"proxy-protocol-js": "^4.0.6" | ||
} | ||
} |
@@ -97,2 +97,3 @@ 'use strict' | ||
const port = 4883 | ||
const clientIps = ['::ffff:127.0.0.1', '::1'] | ||
const setup = start({ | ||
@@ -103,3 +104,3 @@ broker: { | ||
client.ip = client.connDetails.ipAddress | ||
t.equal('::ffff:127.0.0.1', client.ip) | ||
t.equal(clientIps.includes(client.ip), true) | ||
} else { | ||
@@ -262,3 +263,4 @@ t.fail('no ip address present') | ||
const clientIp = '::ffff:127.0.0.1' | ||
// local client IPs might resolve slightly differently | ||
const clientIps = ['::ffff:127.0.0.1', '::1'] | ||
const port = 4883 | ||
@@ -271,3 +273,3 @@ | ||
client.ip = client.connDetails.ipAddress | ||
t.equal(clientIp, client.ip) | ||
t.equal(clientIps.includes(client.ip), true) | ||
} else { | ||
@@ -274,0 +276,0 @@ t.fail('no ip address present') |
@@ -16,4 +16,6 @@ /* eslint no-unused-vars: 0 */ | ||
serverIpAddress: string | ||
serverPort: number | ||
isWebsocket: boolean | ||
isProxy: number | ||
isTls: boolean | ||
certAuthorized?: boolean, | ||
@@ -20,0 +22,0 @@ cert?: PeerCertificate | {} | null, |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
37262
871
2
Updatedproxy-protocol-js@^4.0.6