MCPcopy
hub / github.com/nodejs/undici / parseResponseAddress

Function parseResponseAddress

lib/core/socks5-utils.js:116–181  ·  view source on GitHub ↗

* Parse address from SOCKS5 response * @param {Buffer} buffer - Buffer containing the address * @param {number} offset - Starting offset in buffer * @returns {{address: string, port: number, bytesRead: number}}

(buffer, offset = 0)

Source from the content-addressed store, hash-verified

114 * @returns {{address: string, port: number, bytesRead: number}}
115 */
116function parseResponseAddress (buffer, offset = 0) {
117 if (buffer.length < offset + 1) {
118 throw new InvalidArgumentError('Buffer too small to contain address type')
119 }
120
121 const addressType = buffer[offset]
122 let address
123 let currentOffset = offset + 1
124
125 switch (addressType) {
126 case 0x01: { // IPv4
127 if (buffer.length < currentOffset + 6) {
128 throw new InvalidArgumentError('Buffer too small for IPv4 address')
129 }
130 address = Array.from(buffer.subarray(currentOffset, currentOffset + 4)).join('.')
131 currentOffset += 4
132 break
133 }
134
135 case 0x03: { // Domain
136 if (buffer.length < currentOffset + 1) {
137 throw new InvalidArgumentError('Buffer too small for domain length')
138 }
139 const domainLength = buffer[currentOffset]
140 currentOffset += 1
141
142 if (buffer.length < currentOffset + domainLength + 2) {
143 throw new InvalidArgumentError('Buffer too small for domain address')
144 }
145 address = buffer.subarray(currentOffset, currentOffset + domainLength).toString('utf8')
146 currentOffset += domainLength
147 break
148 }
149
150 case 0x04: { // IPv6
151 if (buffer.length < currentOffset + 18) {
152 throw new InvalidArgumentError('Buffer too small for IPv6 address')
153 }
154 // Convert buffer to IPv6 string
155 const parts = []
156 for (let i = 0; i < 8; i++) {
157 const value = buffer.readUInt16BE(currentOffset + i * 2)
158 parts.push(value.toString(16))
159 }
160 address = parts.join(':')
161 currentOffset += 16
162 break
163 }
164
165 default:
166 throw new InvalidArgumentError(`Invalid address type: ${addressType}`)
167 }
168
169 // Parse port
170 if (buffer.length < currentOffset + 2) {
171 throw new InvalidArgumentError('Buffer too small for port')
172 }
173 const port = buffer.readUInt16BE(currentOffset)

Callers 1

socks5-utils.jsFile · 0.85

Calls 2

toStringMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…