Skip to content

Commit df640b9

Browse files
authored
feat: TSPlus support (#73)
1 parent b8e9387 commit df640b9

File tree

9 files changed

+121
-74
lines changed

9 files changed

+121
-74
lines changed

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
"lodash.isundefined": "^3.0.1",
114114
"lodash.minby": "^4.6.0",
115115
"lodash.result": "^4.5.2",
116+
"lodash.split": "^4.4.2",
116117
"lodash.tonumber": "^4.0.3",
117118
"micromodal": "^0.4.6",
118119
"multistream": "^4.1.0",
@@ -129,4 +130,4 @@
129130
"url": "^0.11.0",
130131
"utf-8-validate": "^5.0.4"
131132
}
132-
}
133+
}

src/channel/channel.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,9 @@ module.exports = class ZitiChannel {
800800
* @param {*} data
801801
*/
802802
async _recvSend(data) {
803-
804-
this._ctx.logger.debug('_recvSend -> sentLen[%o] bufferedLen[%o]', data.byteLength, this._zws._ws.bufferedAmount);
805-
803+
if (!isUndefined(this._zws)) {
804+
this._ctx.logger.debug('_recvSend -> sentLen[%o] bufferedLen[%o]', data.byteLength, this._zws._ws.bufferedAmount);
805+
}
806806
}
807807

808808

@@ -998,7 +998,25 @@ module.exports = class ZitiChannel {
998998
if (len > 2000) {
999999
len = 2000;
10001000
}
1001-
this._ctx.logger.debug("recv <- data (first 2000): %s", String.fromCharCode.apply(null, bodyView).substring(0, len));
1001+
let dbgStr = String.fromCharCode.apply(null, bodyView).substring(0, len);
1002+
this._ctx.logger.debug("recv <- data (first 2000): %s", dbgStr);
1003+
1004+
//temp debugging
1005+
// if (dbgStr.includes("var openMe = (window.parent")) {
1006+
1007+
// let str = String.fromCharCode.apply(null, bodyView).substring(0, bodyView.length);
1008+
1009+
// str = str.replace('var openMe = (window.parent', 'debugger; var openMe = (window.parent');
1010+
1011+
// // str = str.replace('document.cookie = a;', 'document.cookie = a; debugger');
1012+
1013+
// // this._ctx.logger.debug("============== DEBUG INJECT: %s", str);
1014+
1015+
// bodyView = new TextEncoder("utf-8").encode(str);
1016+
// }
1017+
1018+
// }
1019+
10021020
}
10031021
}
10041022

src/client.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,51 @@ class ZitiClient {
6666

6767
CookieInterceptor.init(); // Hijack the `document.cookie` object
6868

69-
CookieInterceptor.write.use( async function ( cookie ) {
69+
CookieInterceptor.write.use( function ( cookie ) {
7070

71-
console.log('=====> CookieInterceptor sees write of Cookie: ', cookie);
71+
(async function() { // we use an IIFE because we need to run some await calls, and we cannot make
72+
// our write.use() an async func because it will then return a Promise,
73+
// which would cause Cookie storage in the browser to get corrupted.
74+
75+
console.log('=====> CookieInterceptor sees write of Cookie: ', cookie);
7276

73-
const release = await ziti._cookiemutex.acquire();
74-
75-
let zitiCookies = await ls.getWithExpiry(zitiConstants.get().ZITI_COOKIES);
76-
if (isNull(zitiCookies)) {
77-
zitiCookies = {}
78-
}
79-
console.log('=====> CookieInterceptor ZITI_COOKIES (before): ', zitiCookies);
80-
81-
let name = cookie.substring(0, cookie.indexOf("="));
82-
let value = cookie.substring(cookie.indexOf("=") + 1);
83-
let cookie_value = value.substring(0, value.indexOf(";"));
84-
let parts = value.split(";");
85-
let cookiePath;
86-
let expires;
87-
for (let j = 0; j < parts.length; j++) {
88-
let part = parts[j].trim();
89-
part = part.toLowerCase();
90-
if ( part.startsWith("path") ) {
91-
cookiePath = part.substring(part.indexOf("=") + 1);
92-
}
93-
else if ( part.startsWith("expires") ) {
94-
expires = new Date( part.substring(part.indexOf("=") + 1) );
77+
const release = await ziti._cookiemutex.acquire();
78+
79+
let zitiCookies = await ls.getWithExpiry(zitiConstants.get().ZITI_COOKIES);
80+
if (isNull(zitiCookies)) {
81+
zitiCookies = {}
9582
}
96-
else if ( part.startsWith("httponly") ) {
97-
httpOnly = true;
83+
// console.log('=====> CookieInterceptor ZITI_COOKIES (before): ', zitiCookies);
84+
85+
let name = cookie.substring(0, cookie.indexOf("="));
86+
let value = cookie.substring(cookie.indexOf("=") + 1);
87+
let cookie_value = value.substring(0, value.indexOf(";"));
88+
let parts = value.split(";");
89+
let cookiePath;
90+
let expires;
91+
for (let j = 0; j < parts.length; j++) {
92+
let part = parts[j].trim();
93+
part = part.toLowerCase();
94+
if ( part.startsWith("path") ) {
95+
cookiePath = part.substring(part.indexOf("=") + 1);
96+
}
97+
else if ( part.startsWith("expires") ) {
98+
expires = new Date( part.substring(part.indexOf("=") + 1) );
99+
}
100+
else if ( part.startsWith("httponly") ) {
101+
httpOnly = true;
102+
}
98103
}
99-
}
100-
101-
zitiCookies[name] = cookie_value;
102-
103-
console.log('=====> CookieInterceptor ZITI_COOKIES (after): ', zitiCookies);
104-
105-
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
106-
107-
release();
104+
105+
zitiCookies[name] = cookie_value;
106+
107+
// console.log('=====> CookieInterceptor ZITI_COOKIES (after): ', zitiCookies);
108+
109+
await ls.setWithExpiry(zitiConstants.get().ZITI_COOKIES, zitiCookies, new Date(8640000000000000));
110+
111+
release();
112+
113+
})()
108114

109115
return cookie;
110116
});
@@ -903,7 +909,7 @@ if (!zitiConfig.serviceWorker.active) {
903909
* Service Worker 'message' handler'
904910
*/
905911
navigator.serviceWorker.addEventListener('message', event => {
906-
console.log('----- Client received msg from serviceWorker: ', event);
912+
console.log('----- Client received msg from serviceWorker: ', event.data.command);
907913

908914
if (event.data.command === 'initClient') { _onMessage_initClient( event ); }
909915
else if (event.data.command === 'generateKeyPair') { _onMessage_generateKeyPair( event ); }

src/context/context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ ZitiContext.prototype.shouldRouteOverZiti = async function(url) {
644644
let port = parsedURL.port;
645645

646646
if (port === '') {
647-
if (parsedURL.protocol === 'https:') {
647+
if ((parsedURL.protocol === 'https:') || (parsedURL.protocol === 'wss:')) {
648648
port = 443;
649649
} else {
650650
port = 80;

src/http/request.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const isNull = require('lodash.isnull');
3232
const clone = HttpBody.clone;
3333
var pjson = require('../../package.json');
3434
const isUndefined = require('lodash.isundefined');
35+
const _split = require('lodash.split');
36+
const forEach = require('lodash.foreach');
3537

3638

3739
const INTERNALS = Symbol('HttpRequest internals');
@@ -223,41 +225,49 @@ HttpRequest.prototype.getRequestOptions = async function() {
223225
headers.set('Host', parsedURL.hostname);
224226
}
225227

226-
let cookies = Cookies.get();
228+
let cookieObject = {};
227229

228-
let cookieValue = '';
229-
for (const cookie in cookies) {
230-
if (cookies.hasOwnProperty(cookie)) {
231-
if (cookies[cookie] !== '') {
232-
cookieValue += cookie + '=' + cookies[cookie] + ';';
233-
}
234-
}
230+
// Obtain all Cookie KV pairs from the incoming Cookie header
231+
if (headers.has('Cookie')) {
232+
let cookieHdr = headers.get('Cookie');
233+
let cookies = _split(cookieHdr, ';' );
234+
forEach(cookies, function( cookie ) {
235+
let cookieKV = _split(cookie, '=' );
236+
cookieObject[cookieKV[0]] = cookieKV[1];
237+
});
235238
}
236-
if (!isUndefined(cookies)) {
237-
if (cookieValue !== '') {
238-
headers.set('Cookie', cookieValue);
239-
}
240-
}
241-
if (cookieValue === '') {
242-
243-
let zitiCookies = await ls.getWithExpiry(zitiConstants.get().ZITI_COOKIES);
244-
245-
if (!isNull(zitiCookies)) {
246-
247-
for (const cookie in zitiCookies) {
248-
if (zitiCookies.hasOwnProperty(cookie)) {
249-
cookieValue += cookie + '=' + zitiCookies[cookie] + ';';
250239

251-
if (cookie === 'MMCSRF') {
252-
headers.set('X-CSRF-Token', zitiCookies[cookie]);
253-
}
240+
// Obtain all Cookie KV pairs from the browser Cookie cache
241+
let browserCookies = Cookies.get();
242+
for (const cookie in browserCookies) {
243+
if (browserCookies.hasOwnProperty( cookie )) {
244+
cookieObject[cookie] = browserCookies[cookie];
245+
if (cookie.includes('CSRF')) {
246+
headers.set('X-CSRF-Token', browserCookies[cookie]);
247+
}
248+
}
249+
}
250+
251+
// Obtain all Cookie KV pairs from the Ziti Cookie cache
252+
let zitiCookies = await ls.getWithExpiry(zitiConstants.get().ZITI_COOKIES);
253+
if (!isNull(zitiCookies)) {
254+
for (const cookie in zitiCookies) {
255+
if (zitiCookies.hasOwnProperty(cookie)) {
256+
if (zitiCookies[cookie] !== 'null') {
257+
cookieObject[cookie] = zitiCookies[cookie];
254258
}
255259
}
256-
257-
headers.set('Cookie', cookieValue);
258260
}
259261
}
260-
262+
263+
// set the Cookie header
264+
let cookieHeaderValue = '';
265+
for (const cookie in cookieObject) {
266+
if (cookieObject.hasOwnProperty(cookie)) {
267+
cookieHeaderValue += cookie + '=' + cookieObject[cookie] + ';';
268+
}
269+
}
270+
headers.set('Cookie', cookieHeaderValue);
261271

262272
// HTTP-network-or-cache fetch steps 2.4-2.7
263273
let contentLengthValue = null;

src/http/ziti-websocket-wrapper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ class ZitiWebSocketWrapper extends EventEmitter {
374374
options = {};
375375
}
376376

377+
if (typeof data === 'make-socket.io-think-we-are-native') { /* nop */ }
378+
377379
if (typeof data === 'number') data = data.toString();
378380

379381
if (this.readyState !== ZitiWebSocketWrapper.OPEN) {
@@ -569,7 +571,7 @@ async function initAsClient(websocket, address, protocols, options) {
569571
serviceName = await ziti._ctx.shouldRouteOverZiti( newUrl );
570572

571573
if (isUndefined(serviceName)) { // If we have no serviceConfig associated with the hostname:port, do not intercept
572-
ziti._ctx.logger.warn('ZitiWebSocketWrapper(): no associated serviceConfig, bypassing intercept of [%s]', url);
574+
ziti._ctx.logger.warn('ZitiWebSocketWrapper(): no associated serviceConfig, bypassing intercept of [%s]', address);
573575
opts.createConnection = isSecure ? tlsConnect : netConnect;
574576
opts.host = parsedUrl.hostname.startsWith('[')
575577
? parsedUrl.hostname.slice(1, -1)
@@ -589,7 +591,7 @@ async function initAsClient(websocket, address, protocols, options) {
589591

590592
} else { // the request is targeting the raw internet
591593

592-
ziti._ctx.logger.warn('ZitiWebSocketWrapper(): no associated serviceConfig, bypassing intercept of [%s]', url);
594+
ziti._ctx.logger.warn('ZitiWebSocketWrapper(): no associated serviceConfig, bypassing intercept of [%s]', address);
593595
opts.createConnection = isSecure ? tlsConnect : netConnect;
594596
opts.host = parsedUrl.hostname.startsWith('[')
595597
? parsedUrl.hostname.slice(1, -1)
@@ -624,7 +626,7 @@ async function initAsClient(websocket, address, protocols, options) {
624626

625627

626628
opts.headers.Cookie = cookieString;
627-
629+
628630
opts.timeout = opts.handshakeTimeout;
629631
if (!opts.timeout) {
630632
opts.timeout = handshakeTimeout;

src/http/ziti-xhr.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ function ZitiXMLHttpRequest () {
288288

289289
response.blob().then(async function(blob) {
290290
self.responseBodyText = await blob.text();
291+
self.responseText = self.responseBodyText;
291292
self.response = self.responseBodyText;
292293
sendFlag = false;
293294
setState(self.DONE);

src/ui/identity_modal/css.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ exports.inject = () => {
8989
border-top-left-radius: 0;
9090
border-top-right-radius: 0;
9191
}
92+
93+
::placeholder {
94+
color: #9e9e9e;
95+
}
9296
9397
.form-signin-button {
9498
background-image: linear-gradient(to bottom right, #082481 , #e00043);

0 commit comments

Comments
 (0)