-
-
Notifications
You must be signed in to change notification settings - Fork 3
Server factory integration #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
robertsLando
merged 25 commits into
moscajs:master
from
getlarge:server-factory-integration
Oct 27, 2020
Merged
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
6098aeb
update dependencies
getlarge cd5b668
refactor protocol decoder and export extractSocketDetails
getlarge efeef3d
update example, tests and docs
getlarge 8ab1350
update types
getlarge 3a5a81e
change lib path and update test
getlarge a98ee24
quick fix
getlarge fa6a946
fix port getters for Proxy v2
getlarge 7fc3cbc
Update createServer signature
getlarge e43176b
Fix test end
getlarge 0ae0871
update doc
getlarge 5306908
fix test issues with node 10
getlarge 81f6521
chore: drop nodejs 8, added nodejs 14
robertsLando ace99b9
refactor tests
getlarge 27a1d23
minor improvements
getlarge 0780d46
Merge branch 'server-factory-integration' of https://github.com/getla…
getlarge 07e7f88
chore(ci): disable parallel tests
robertsLando bea03af
chore(ci): removed parallel from coverall
robertsLando 31d027e
Fix test not ending
getlarge 835a412
Merge branch 'server-factory-integration' of https://github.com/getla…
getlarge c789d19
Reenable parallel tests
getlarge 7310ac2
test fix
robertsLando ddfabb9
udpate aedes
getlarge 18f9212
replace spread operator
getlarge 52ca790
Merge branch 'server-factory-integration' of https://github.com/getla…
getlarge 0982081
fix package.json
getlarge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,151 @@ | ||
| var protocolDecoder = require('./lib/protocol-decoder') | ||
| 'use strict' | ||
|
|
||
| var proxyProtocol = require('proxy-protocol-js') | ||
| var forwarded = require('forwarded') | ||
|
|
||
| var v1ProxyProtocolSignature = Buffer.from('PROXY ', 'utf8') | ||
| var v2ProxyProtocolSignature = Buffer.from([ | ||
| 0x0d, | ||
| 0x0a, | ||
| 0x0d, | ||
| 0x0a, | ||
| 0x00, | ||
| 0x0d, | ||
| 0x0a, | ||
| 0x51, | ||
| 0x55, | ||
| 0x49, | ||
| 0x54, | ||
| 0x0a | ||
| ]) | ||
|
|
||
| function isValidV1ProxyProtocol (buffer) { | ||
| for (var i = 0; i < v1ProxyProtocolSignature.length; i++) { | ||
| if (buffer[i] !== v1ProxyProtocolSignature[i]) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| function isValidV2ProxyProtocol (buffer) { | ||
| for (var i = 0; i < v2ProxyProtocolSignature.length; i++) { | ||
| if (buffer[i] !== v2ProxyProtocolSignature[i]) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // from https://stackoverflow.com/questions/57077161/how-do-i-convert-hex-buffer-to-ipv6-in-javascript | ||
| function parseIpV6Array (ip) { | ||
| var ipHex = Buffer.from(ip).toString('hex') | ||
| return ipHex.match(/.{1,4}/g) | ||
| .map((val) => val.replace(/^0+/, '')) | ||
| .join(':') | ||
| .replace(/0000:/g, ':') | ||
| .replace(/:{2,}/g, '::') | ||
| } | ||
|
|
||
| function getProtoIpFamily (ipFamily) { | ||
| if (ipFamily && ipFamily.endsWith('4')) { | ||
| return 4 | ||
| } else if (ipFamily && ipFamily.endsWith('6')) { | ||
| return 6 | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| function extractHttpDetails (req, socket) { | ||
| const details = {} | ||
| var headers = req && req.headers ? req.headers : null | ||
| if (headers) { | ||
| if (headers['x-forwarded-for']) { | ||
| var addresses = forwarded(req) | ||
| details.ipAddress = headers['x-real-ip'] ? headers['x-real-ip'] : addresses[addresses.length - 1] | ||
| details.serverIpAddress = addresses[0] | ||
| } | ||
| if (headers['x-real-ip']) { | ||
| details.ipAddress = headers['x-real-ip'] | ||
| } | ||
| details.port = socket._socket.remotePort | ||
| details.ipFamily = getProtoIpFamily(socket._socket.remoteFamily) | ||
| details.isWebsocket = true | ||
| } | ||
| return details | ||
| } | ||
|
|
||
| function extractProxyDetails (buffer) { | ||
| const details = {} | ||
| var proxyProto | ||
| if (isValidV1ProxyProtocol(buffer)) { | ||
| proxyProto = proxyProtocol.V1BinaryProxyProtocol.parse(buffer) | ||
| if (proxyProto && proxyProto.source && proxyProto.data) { | ||
| details.ipFamily = getProtoIpFamily(proxyProto.inetProtocol) | ||
| details.ipAddress = proxyProto.source.ipAddress | ||
| details.port = proxyProto.source.port | ||
| details.serverIpAddress = proxyProto.destination.ipAddress | ||
| details.data = proxyProto.data | ||
| details.isProxy = 1 | ||
| } | ||
| } else if (isValidV2ProxyProtocol(buffer)) { | ||
| proxyProto = proxyProtocol.V2ProxyProtocol.parse(buffer) | ||
| if (proxyProto && proxyProto.proxyAddress && proxyProto.data) { | ||
| if (proxyProto.proxyAddress instanceof proxyProtocol.IPv4ProxyAddress) { | ||
| details.ipAddress = proxyProto.proxyAddress.sourceAddress.address.join('.') | ||
| details.port = proxyProto.proxyAddress.sourcePort | ||
| details.serverIpAddress = proxyProto.proxyAddress.destinationAddress.address.join('.') | ||
| details.ipFamily = 4 | ||
| } else if (proxyProto.proxyAddress instanceof proxyProtocol.IPv6ProxyAddress) { | ||
| details.ipAddress = parseIpV6Array(proxyProto.proxyAddress.sourceAddress.address) | ||
| details.port = proxyProto.proxyAddress.sourcePort | ||
| details.serverIpAddress = parseIpV6Array(proxyProto.proxyAddress.destinationAddress.address) | ||
| details.ipFamily = 6 | ||
| } | ||
| details.isProxy = 2 | ||
| if (Buffer.isBuffer(proxyProto.data)) { | ||
| details.data = proxyProto.data | ||
| } else { | ||
| details.data = Buffer.from(proxyProto.data) | ||
| } | ||
| } | ||
| } | ||
| return details | ||
| } | ||
|
|
||
| function extractSocketDetails (socket) { | ||
| const details = {} | ||
| if (socket._socket && socket._socket.address) { | ||
| details.isWebsocket = true | ||
| details.ipAddress = socket._socket.remoteAddress | ||
| details.port = socket._socket.remotePort | ||
| details.serverIpAddress = socket._socket.address().address | ||
| details.ipFamily = getProtoIpFamily(socket._socket.remoteFamily) | ||
| } else if (socket.address) { | ||
| details.ipAddress = socket.remoteAddress | ||
| details.port = socket.remotePort | ||
| details.serverIpAddress = socket.address().address | ||
| details.ipFamily = getProtoIpFamily(socket.remoteFamily) | ||
| } | ||
| return details | ||
| } | ||
|
|
||
| function protocolDecoder (conn, buffer, req) { | ||
| var proto = {} | ||
| if (!buffer) return proto | ||
| var socket = conn.socket || conn | ||
| proto.isProxy = 0 | ||
| proto.isWebsocket = false | ||
| proto = { ...proto, ...extractHttpDetails(req, socket), ...extractProxyDetails(buffer) } | ||
|
|
||
| if (!proto.ipAddress) { | ||
| proto = { ...proto, ...extractSocketDetails(socket) } | ||
| } | ||
|
|
||
| return proto | ||
| } | ||
|
|
||
| module.exports = { | ||
| extractSocketDetails, | ||
| protocolDecoder | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.