Skip to content

Commit 78d5926

Browse files
authored
fix: proxy code to support both variants of proxy protocol (#1076)
1 parent e204134 commit 78d5926

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.38.1]
8+
9+
- Fix proxy protocol to support both variants
10+
711
## [1.38.0]
812

913
- Implement advanced copy functions

src/proxyConnect.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,28 @@ export class ProxyConnect extends EventEmitter {
152152
})
153153
}
154154

155+
/**
156+
* The proxy protocol changed over time and initially the response was just XML.
157+
* Later with re-implementation it follows the same spec as DBGp: [data length] [NULL] [xml] [NULL]
158+
* @param data Recieved data
159+
*/
160+
private _extractPacket(data: Buffer): string {
161+
let packetData = data
162+
const nullByteIndex = data.indexOf(0)
163+
if (nullByteIndex !== -1) {
164+
const lastPiece = data.subarray(0, nullByteIndex)
165+
const dataLength = parseInt(decode(lastPiece, ENCODING))
166+
if (!!dataLength && dataLength <= data.length - nullByteIndex - 1) {
167+
packetData = data.subarray(nullByteIndex + 1, nullByteIndex + 1 + dataLength)
168+
}
169+
}
170+
return decode(packetData, ENCODING)
171+
}
172+
155173
/** Parse data from response server and emit the relevant notification. */
156174
private _responseStrategy(data: Buffer) {
157175
try {
158-
const documentElement = this._parser.parseFromString(decode(data, ENCODING), 'application/xml')
176+
const documentElement = this._parser.parseFromString(this._extractPacket(data), 'application/xml')
159177
.documentElement!
160178
const isSuccessful = documentElement.getAttribute('success') === '1'
161179
const error = documentElement.firstChild

src/test/proxy.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ describe('ProxyConnect', () => {
1111
const err = `<proxy${cmd} success="${success}"><error id="${id}"><message>${msg}</message></error></proxy${cmd}>`
1212
return encode(`<?xml version="1.0" encoding="UTF-8"?>\n${err}`, ENCODING)
1313
}
14+
function _dbgpWrap(raw: Buffer): Buffer {
15+
return Buffer.concat([encode(raw.length.toString() + '\0', ENCODING), raw, Buffer.from([0])])
16+
}
1417

1518
const host = 'host'
1619
const port = 9001
@@ -78,7 +81,7 @@ describe('ProxyConnect', () => {
7881
})
7982
})
8083

81-
it('should be registered', (done: Mocha.Done) => {
84+
it('should be registered (new protocol)', (done: Mocha.Done) => {
8285
conn.on('log_response', (str: string) => {
8386
assert.equal(str, msgs.registerSuccess)
8487
done()
@@ -87,7 +90,7 @@ describe('ProxyConnect', () => {
8790
conn.sendProxyInitCommand().catch((err: Error) => {
8891
done(err)
8992
})
90-
testSocket.emit('data', _xml('init', 1))
93+
testSocket.emit('data', _dbgpWrap(_xml('init', 1)))
9194
testSocket.emit('close', false)
9295
})
9396

0 commit comments

Comments
 (0)