Skip to content

Commit

Permalink
introduce to 'target host transformer' to gzip inflator
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect committed Oct 20, 2024
1 parent 65a651c commit bda6bd5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/context/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ class ZitiContext extends EventEmitter {

}

this.targetService = options.target;
this.targetServiceAppData = await this.getConnectAppDataByServiceName (this.targetService.service, this.targetService.scheme);
this.targetServiceHost = await this.getConfigHostByServiceName (this.targetService.service);
this.targetServiceHostAndPort = `${this.targetServiceAppData.dst_hostname}:${this.targetServiceAppData.dst_port}`;
this.bootstrapperHost = options.bootstrapperHost;

this._initialized = true;

this._zitiEnroller = new ZitiEnroller ({
Expand Down Expand Up @@ -2341,7 +2347,7 @@ class ZitiContext extends EventEmitter {
if (isEqual(res.statusCode, 204)) {
response = new HttpResponse(null, response_options);
} else {
let body = res.pipe(new PassThrough( response_options ));
let body = res.pipe(new PassThrough( response_options, { zitiContext: res.socket.zitiContext } ));
response = new HttpResponse(body, response_options);
}

Expand Down
20 changes: 18 additions & 2 deletions src/http/readable-stream/_stream_passthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import { Transform } from './_stream_transform';
import EventEmitter from 'events';
import pako from 'pako';
import { isEqual } from 'lodash-es';
import { isEqual, isUndefined } from 'lodash-es';

class PassThrough extends Transform {

constructor (options) {
constructor (options, zitiOptions) {
super(options);
this.ee = new EventEmitter();
if (options.headers) {
Expand All @@ -23,12 +23,28 @@ class PassThrough extends Transform {
}
}
}
this.zitiContext = zitiOptions.zitiContext;
}

_transform(chunk, encoding, cb) {
if (this.isGzip) {
this.inflator.push(chunk);
chunk = this.inflator.result;

/**
* This logic was added on behalf of Solarwinds Orion, which embeds the host:port of the protected web server in some
* HTML/inline-JS (which is also gzip'ed). We must swap the hostname from the "protected web server" to the boot-strapper,
* or some downstream Orion logic will mismatch when doing some hostname comparisons, which leads it to NOT include
* an XSRF Token header on HTTP requests, which leads to 400's being returned fro teh web server,
* which leads to errors in Orion web UI.
*/
if (!isUndefined(chunk)) {
let decodedChunk = new TextDecoder().decode(chunk);
if (decodedChunk.indexOf(this.zitiContext.targetServiceHostAndPort) != -1) {
decodedChunk = decodedChunk.replaceAll(this.zitiContext.targetServiceHostAndPort, this.zitiContext.bootstrapperHost);
chunk = new TextEncoder().encode(decodedChunk);
}
}
}
cb(null, chunk, this);
};
Expand Down

0 comments on commit bda6bd5

Please sign in to comment.