Skip to content

Commit

Permalink
feat: MSFT RDS/RDP (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect authored Nov 14, 2023
1 parent a2ca03a commit 5ab70a7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"@auth0/auth0-spa-js": "^2.0.4",
"@azure/msal-browser": "^2.38.0",
"@babel/runtime": "^7.17.9",
"@openziti/ziti-browzer-core": "^0.31.2",
"@openziti/ziti-browzer-core": "^0.32.0",
"bowser": "^2.11.0",
"cookie-interceptor": "^1.0.0",
"core-js": "^3.22.8",
Expand Down
23 changes: 12 additions & 11 deletions src/http/ziti-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { isEqual } from "lodash-es";

function ZitiXMLHttpRequest () {

Expand Down Expand Up @@ -142,8 +143,6 @@ function ZitiXMLHttpRequest () {
*/
this.open = function(method, url, async, user, password) {

// console.log(`XHR: method=${method}, url=${url}, async=${async}`);

if (!async) {
window.zitiBrowzerRuntime.synchronousXHREncounteredEventHandler({});
}
Expand Down Expand Up @@ -231,8 +230,6 @@ function ZitiXMLHttpRequest () {
}
}

console.log(str);

return str;
};

Expand Down Expand Up @@ -284,13 +281,17 @@ function ZitiXMLHttpRequest () {
let self = this;

response.blob().then(async function(blob) {
self.responseBodyText = await blob.text();
// console.log(`XHR: responseBodyText=${self.responseBodyText}`);
self.responseText = self.responseBodyText;
self.response = self.responseBodyText;
self.responseXML = self.responseBodyText;
self.responseURL = settings.url;
self.responseType = '';
if (isEqual(blob.type, 'application/pkix-cert')) {
self.response = await blob.arrayBuffer();
}
else {
self.responseBodyText = await blob.text();
self.responseText = self.responseBodyText;
self.response = self.responseBodyText;
self.responseXML = self.responseBodyText;
self.responseURL = settings.url;
self.responseType = '';
}

// create the (potential) XML DOM object
try {
Expand Down
99 changes: 57 additions & 42 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ class ZitiBrowzerRuntime {
/**
* Instantiate the Ziti browZer Runtime.
*
* Use 'zitiConfig' values passed to us from the Ziti HTTP Agent.
* Use 'zitiConfig' values passed to us from the Ziti BrowZer Bootstrapper.
*
*/
if (isUndefined(window.zitiBrowzerRuntime)) {
Expand Down Expand Up @@ -1700,8 +1700,6 @@ if (isUndefined(window.zitiBrowzerRuntime)) {
});
}

console.log('now inside async IIFE to initialize the runtime and register the SW');

const loadedViaHTTPAgent = document.getElementById('from-ziti-browzer-bootstrapper');

const loadedViaSW = document.getElementById('from-ziti-browzer-sw');
Expand All @@ -1718,8 +1716,6 @@ if (isUndefined(window.zitiBrowzerRuntime)) {
*/
let initResults = await zitiBrowzerRuntime.initialize({loadedViaHTTPAgent: (loadedViaHTTPAgent ? true : false)});

console.log('returned from call to zitiBrowzerRuntime.initialize -- initResults:', initResults);

if (initResults.authenticated && !initResults.loadedViaHTTPAgent) {

/**
Expand Down Expand Up @@ -1910,6 +1906,10 @@ if (isUndefined(window.zitiBrowzerRuntime)) {

}

// else if (event.data.type === 'GET_BASEURI') {
// event.ports[0].postMessage( document.baseURI );
// }

else if (event.data.type === 'CACHE_UPDATED') {
const {updatedURL} = event.data.payload;
zitiBrowzerRuntime.logger.info(`A newer version of ${updatedURL} is available!`);
Expand Down Expand Up @@ -2192,6 +2192,9 @@ if (isUndefined(window.zitiBrowzerRuntime)) {

}

// Gather list of Services right up front, since some WebSocket intercept logic needs it
await zitiBrowzerRuntime.zitiContext.fetchServices();

})();

}
Expand Down Expand Up @@ -2231,6 +2234,11 @@ const zitiFetch = async ( urlObj, opts ) => {
url = urlObj;
}

window.zitiBrowzerRuntime.logger.trace( 'zitiFetch: entered for URL: ', url);

//TEMP TEST... always attempt to go thru SW
return window._ziti_realFetch(urlObj, opts);

if (url.match( regexZBWASM )) { // the request seeks z-b-r/wasm
window.zitiBrowzerRuntime.logger.trace('zitiFetch: seeking Ziti z-b-r/wasm, bypassing intercept of [%s]', url);
return window._ziti_realFetch(urlObj, opts);
Expand All @@ -2240,23 +2248,33 @@ const zitiFetch = async ( urlObj, opts ) => {

// window.zitiBrowzerRuntime.noActiveChannelDetectedEnabled = true;

window.zitiBrowzerRuntime.logger.trace( 'zitiFetch: entered for URL: ', url);

let serviceName;

// We want to intercept fetch requests that target the Ziti HTTP Agent... that is...
// We want to intercept fetch requests that target the Ziti BrowZer Bootstrapper... that is...
// ...we want to intercept any request from the web app that targets the server from which the app was loaded.

if (url.match( regexZBWASM )) { // the request seeks z-b-r/wasm
window.zitiBrowzerRuntime.logger.trace('zitiFetch: seeking Ziti z-b-r/wasm, bypassing intercept of [%s]', url);
return window._ziti_realFetch(urlObj, opts);
}
else if (url.match( regex )) { // yes, the request is targeting the Ziti HTTP Agent
else if (url.match( regex )) { // yes, the request is targeting the Ziti BrowZer Bootstrapper

var newUrl = new URL( url );
newUrl.hostname = window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service;
newUrl.port = window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.port;
window.zitiBrowzerRuntime.logger.trace( 'zitiFetch: transformed URL: ', newUrl.toString());

var pathnameArray = newUrl.pathname.split('/');
var targetpathnameArray = window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.path.split('/');

if (!isEqual(pathnameArray[1], targetpathnameArray[1])) {

// if (!newUrl.pathname.startsWith(window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.path)) {
newUrl.pathname = window.zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.path + newUrl.pathname;
newUrl.pathname = newUrl.pathname.replace('//','/');
// }
window.zitiBrowzerRuntime.logger.trace( 'zitiFetch: transformed URL: ', newUrl.toString());
}

serviceName = await window.zitiBrowzerRuntime.zitiContext.shouldRouteOverZiti( newUrl );

Expand All @@ -2271,22 +2289,11 @@ const zitiFetch = async ( urlObj, opts ) => {

} else if ( (url.match( regexSlash )) || ((url.match( regexDotSlash ))) ) { // the request starts with a slash, or dot-slash

if ( url.match( regexDotSlash ) ) {
url = url.slice(1); // remove the 'dot'
}

let newUrl;
let baseURIUrl = new URL( document.baseURI );
let newUrl = new URL( `${baseURIUrl.origin}${url}` );

if (baseURIUrl.hostname === zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host) {
newUrl = new URL( 'https://' +
zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service +
':' +
(zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.port ? zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.port : 443) +
url
);
} else {
let baseURI = document.baseURI.replace(/\.\/$/, '');
newUrl = new URL( baseURI + url );
newUrl.hostname = zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service;
}
zitiBrowzerRuntime.logger.debug( 'zitiFetch: transformed URL: ', newUrl.toString());

Expand All @@ -2304,30 +2311,38 @@ const zitiFetch = async ( urlObj, opts ) => {

// We have a 'relative' URL

let href;
let baseURIUrl = new URL( document.baseURI );
let newUrl = new URL( `${baseURIUrl.origin}${url}` );
newUrl.pathname = url;

if (url.includes('/')) {
href = `${window.location.origin}${zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.path}${url}`;
} else {
const substrings = window.location.pathname.split('/');
let pathname = substrings.length === 1
? window.location.pathname // delimiter is not part of the string
: substrings.slice(0, -1).join('/');
href = `${window.location.origin}${pathname}/${url}`;
if (newUrl.hostname === zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host) {
newUrl.hostname = zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service;
}

let href;

// if (url.includes('/')) {
// href = `${window.location.origin}${zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.path}/${url}`;
// } else {
// const substrings = window.location.pathname.split('/');
// let pathname = substrings.length === 1
// ? window.location.pathname // delimiter is not part of the string
// : substrings.slice(0, -1).join('/');
// href = `${window.location.origin}${pathname}/${url}`;
// }

let newUrl;
let baseURIUrl = new URL( href );
if (baseURIUrl.hostname === zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host) {
// let newUrl;
// let baseURIUrl = new URL( href );
// if (baseURIUrl.hostname === zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.self.host) {

newUrl = new URL( href );
newUrl.hostname = zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service;
newUrl.port = zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.port;
// newUrl = new URL( href );
// newUrl.hostname = zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.service;
// newUrl.port = zitiBrowzerRuntime.zitiConfig.browzer.bootstrapper.target.port;

} else {
let baseURI = document.baseURI.replace(/\.\/$/, '');
newUrl = new URL( baseURI + url );
}
// } else {
// let baseURI = document.baseURI.replace(/\.\/$/, '');
// newUrl = new URL( baseURI + url );
// }
zitiBrowzerRuntime.logger.debug( 'zitiFetch: transformed URL: ', newUrl.toString());

serviceName = await zitiBrowzerRuntime.zitiContext.shouldRouteOverZiti( newUrl );
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1195,10 +1195,10 @@
"@types/emscripten" "^1.39.6"
"@wasmer/wasi" "^1.0.2"

"@openziti/ziti-browzer-core@^0.31.2":
version "0.31.2"
resolved "https://registry.yarnpkg.com/@openziti/ziti-browzer-core/-/ziti-browzer-core-0.31.2.tgz#67caf3b2730034f7aedf8949e14cf6b6c77c11a8"
integrity sha512-rSDwmRsIfm5rI9lds3Cge9rErwkVX9nalfF1vR3pZXuUNzU2npHNMURAYVGObNgXff8b4+KThIiciLUmLpAsTw==
"@openziti/ziti-browzer-core@^0.32.0":
version "0.32.0"
resolved "https://registry.yarnpkg.com/@openziti/ziti-browzer-core/-/ziti-browzer-core-0.32.0.tgz#f2e9eff5224e495121bcb18de6e19ad43511cc5e"
integrity sha512-HnXyHywB9lJGppgWpsGysO+x57h5NbMBgxI25TPHyNMsZZehUynszoYEdIrQWZwrtUO/J+jBcaNB9pLdteVC4w==
dependencies:
"@openziti/libcrypto-js" "^0.18.2"
"@openziti/ziti-browzer-edge-client" "^0.6.2"
Expand Down

0 comments on commit 5ab70a7

Please sign in to comment.