-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
206 lines (191 loc) · 6.33 KB
/
index.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'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')
const forwarded = require('forwarded')
const v1ProxyProtocolSignature = Buffer.from('PROXY ', 'utf8')
const v2ProxyProtocolSignature = Buffer.from('0d0a0d0a000d0a515549540a', 'hex')
function isValidV1ProxyProtocol (buffer) {
for (let i = 0; i < v1ProxyProtocolSignature.length; i++) {
if (buffer[i] !== v1ProxyProtocolSignature[i]) {
return false
}
}
return true
}
function isValidV2ProxyProtocol (buffer) {
for (let 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) {
const 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
}
/**
*
* @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 = {}) {
const headers = req && req.headers ? req.headers : null
if (headers) {
if (headers['x-forwarded-for']) {
const addresses = forwarded(req)
proto.ipAddress = headers['x-real-ip'] ? headers['x-real-ip'] : addresses[addresses.length - 1]
proto.serverIpAddress = addresses[0]
}
if (headers['x-real-ip']) {
proto.ipAddress = headers['x-real-ip']
}
// ? 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
proto.ipFamily = getProtoIpFamily(socket._socket.remoteFamily)
proto.isWebsocket = true
}
return proto
}
/**
*
* @param {Buffer} buffer
* @param {ConnectionDetails} proto
* @returns {ConnectionDetails}
*/
function extractProxyDetails (buffer, proto = {}) {
let proxyProto
if (isValidV1ProxyProtocol(buffer)) {
proxyProto = proxyProtocol.V1BinaryProxyProtocol.parse(buffer)
if (proxyProto && proxyProto.source && proxyProto.data) {
proto.ipFamily = getProtoIpFamily(proxyProto.inetProtocol)
proto.ipAddress = proxyProto.source.ipAddress
proto.port = proxyProto.source.port
proto.serverIpAddress = proxyProto.destination.ipAddress
proto.serverPort = proxyProto.destination.port
proto.data = proxyProto.data
proto.isProxy = 1
}
} else if (isValidV2ProxyProtocol(buffer)) {
proxyProto = proxyProtocol.V2ProxyProtocol.parse(buffer)
if (proxyProto && proxyProto.proxyAddress && proxyProto.data) {
if (proxyProto.proxyAddress instanceof proxyProtocol.IPv4ProxyAddress) {
proto.ipAddress = proxyProto.proxyAddress.sourceAddress.address.join('.')
proto.port = proxyProto.proxyAddress.sourcePort
proto.serverIpAddress = proxyProto.proxyAddress.destinationAddress.address.join('.')
proto.serverPort = proxyProto.proxyAddress.destinationPort
proto.ipFamily = 4
} else if (proxyProto.proxyAddress instanceof proxyProtocol.IPv6ProxyAddress) {
proto.ipAddress = parseIpV6Array(proxyProto.proxyAddress.sourceAddress.address)
proto.port = proxyProto.proxyAddress.sourcePort
proto.serverIpAddress = parseIpV6Array(proxyProto.proxyAddress.destinationAddress.address)
proto.serverPort = proxyProto.proxyAddress.destinationPort
proto.ipFamily = 6
}
proto.isProxy = 2
proto.data = Buffer.isBuffer(proxyProto.data) ? proxyProto.data : Buffer.from(proxyProto.data)
}
}
return proto
}
/**
*
* @param {import('net').Socket | HttpConnection} socket
* @param {ConnectionDetails} proto
* @returns {ConnectionDetails}
*/
function extractSocketTLSDetails (socket, proto = {}) {
socket = socket._socket || socket
if (socket.getPeerCertificate && typeof socket.getPeerCertificate === 'function') {
proto.certAuthorized = socket.authorized
proto.cert = socket.getPeerCertificate(true)
proto.isTls = true
}
return proto
}
/**
*
* @param {import('net').Socket | HttpConnection} socket
* @param {ConnectionDetails} proto
* @returns {ConnectionDetails}
*/
function extractSocketDetails (socket, proto = {}) {
if (socket._socket && socket._socket.address) {
proto.isWebsocket = true
proto.ipAddress = socket._socket.remoteAddress
proto.port = socket._socket.remotePort
proto.serverIpAddress = socket._socket.address().address
proto.serverPort = socket._socket.address().port
proto.ipFamily = getProtoIpFamily(socket._socket.remoteFamily)
} else if (socket.address) {
proto.ipAddress = socket.remoteAddress
proto.port = socket.remotePort
proto.serverIpAddress = socket.address().address
proto.serverPort = socket.address().port
proto.ipFamily = getProtoIpFamily(socket.remoteFamily)
}
extractSocketTLSDetails(socket, proto)
return proto
}
/**
*
* @param {import('aedes').Connection} conn
* @param {Buffer} buffer
* @param {import('http').IncomingMessage} req
* @returns {ConnectionDetails}
*/
function protocolDecoder (conn, buffer, req) {
const proto = {
isProxy: 0,
isWebsocket: false,
isTls: false
}
if (!buffer) return proto
const socket = conn.socket || conn
extractHttpDetails(req, socket, proto)
extractProxyDetails(buffer, proto)
if (!proto.ipAddress) {
extractSocketDetails(socket, proto)
} else {
extractSocketTLSDetails(socket, proto)
}
return proto
}
module.exports = {
extractSocketDetails,
protocolDecoder
}