Skip to content

Commit

Permalink
T
Browse files Browse the repository at this point in the history
  • Loading branch information
caracal-js committed Jul 27, 2021
1 parent 64857c5 commit 7650c5c
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 320 deletions.
2 changes: 1 addition & 1 deletion demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ssl = {
const server = https.createServer(ssl);
const Corrosion = require('../');
const proxy = new Corrosion({
codec: 'base64',
codec: 'xor',
});

server.on('request', (request, response) => {
Expand Down
12 changes: 5 additions & 7 deletions lib/browser/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ function createDocumentRewriter(ctx) {
value: args[1],
node: that,
});
const flags = [];
switch(handler) {
case 'url':
Reflect.apply(target, that, [`corrosion-${args[0]}`, args[1]]);
Expand Down Expand Up @@ -122,13 +121,14 @@ function createDocumentRewriter(ctx) {
Object.defineProperty(ctx.window.Element.prototype, html, {
get: new Proxy(descriptor.get, {
apply: (target, that, args) => {
const html = Reflect.apply(target, that, args);
return html == 'innerHTML' && that.tagName != 'SCRIPT' ? ctx.html.source(html, ctx.meta) : html;
const body = Reflect.apply(target, that, args);
if (!body || html == 'innerHTML' && that.tagName == 'SCRIPT') return body;
return ctx.html.source(body, ctx.meta);
},
}),
set: new Proxy(descriptor.set, {
apply(target, that, [ val ]) {
return Reflect.apply(target, that, [ ctx.html.process(val, ctx.meta) ]);
return Reflect.apply(target, that, [ val ? ctx.html.process(val.toString(), ctx.meta) : val, ]);
},
}),
});
Expand Down Expand Up @@ -234,9 +234,7 @@ function createDocumentRewriter(ctx) {
val.$corrosion.init();
val.$corrosion.meta = ctx.meta;
};
} catch(e) {
//console.log(e);
};
} catch(e) {};
};
return val;
},
Expand Down
8 changes: 8 additions & 0 deletions lib/browser/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ function createHttpRewriter(ctx = {}) {
}),
});
};
if (ctx.window.open) {
ctx.window.open = new Proxy(ctx.window.open, {
apply: (target, that, args) => {
if (args[0]) args[0] = ctx.url.wrap(args[0], ctx.meta);
return Reflect.apply(target, that, args)
},
});
};
if (ctx.window.fetch) {
ctx.window.fetch = new Proxy(ctx.window.fetch, {
apply: (target, that, args) => {
Expand Down
159 changes: 81 additions & 78 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const createDocumentRewriter = require('./document');
const createHistoryRewriter = require('./history');
const createHttpRewriter = require('./http');
const createLocation = require('./location');
const createStorageRewriter = require('./storage');
const createWorkerRewriter = require('./worker');
const defaultConfig = {
prefix: '/service/',
Expand Down Expand Up @@ -41,97 +40,54 @@ class Corrosion extends require('../rewrite') {
this.rewriteStorage = createStorageRewriter(this);
if (!this.serviceWorker && this.window.document.currentScript) this.window.document.currentScript.remove();
};
get windowParent() {
get parent() {
if (this.serviceWorker) return false;
try {
return this.window.parent.$corrosion ? this.window.parent : this.window;
} catch(e) {
return this.window;
};
};
get windowTop() {
get top() {
if (this.serviceWorker) return false;
try {
return this.window.top.$corrosion ? this.window.top : this.window;
return this.window.top.$corrosion ? this.window.top : this.parent;
} catch(e) {
return this.window;
return this.parent;
};
};
init() {
this.rewriteHttp();
this.rewriteDocument();
this.rewriteHistory();
this.rewriteWorker();
//this.rewriteStorage();
this.window.Location = createLocation.Location;
this.window.$corrosionGet = this.get.bind(this);
this.window.$corrosionSet = this.set.bind(this);
this.window.$corrosionCall = this.call.bind(this)
this.window.$corrosionGetIdentifier = this.getIdentifier.bind(this);
this.window.$corrosionSetIdentifier = this.setIdentifier.bind(this);
this.window.$corrosionGet$ = this.get$.bind(this);
this.window.$corrosionSet$ = this.set$.bind(this);
this.window.$corrosionGet$m = this.get$m.bind(this);
this.window.$corrosionSet$m = this.set$m.bind(this);
this.window.$corrosionCall$m = this.call$m.bind(this);
};
get(obj, key) {
try {
if (this.window != this.window.parent && obj == this.window.parent) {
if (this.window.parent.$corrosion) return this.window.parent.$corrosionGet(obj, key);
else obj = this.window;
};
if (this.window != this.window.top && obj == this.window.top) {
if (this.window.top.$corrosion) return this.window.top.$corrosionGet(obj, key);
else obj = this.window;
};
} catch(e) {
obj = this.window;
get$m(obj, key) {
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) {
return this.parent.$corrosion.get$m(this.parent, key);
};
let ret = obj[key];
if (!ret) return ret;
if (obj == this.window && key == 'location' || obj == this.window.document && key == 'location') return this.location;
if (key == Symbol.iterator || key == Symbol.toPrimitive) return ret.bind(obj);
if (ret == Function.prototype.apply) return ret.bind(obj);
return ret;
};
call(obj, key, args) {
try {
if (this.window != this.window.parent && obj == this.window.parent) {
if (this.window.parent.$corrosion) return this.window.parent.$corrosionCall(obj, key, args);
else obj = this.window;
};
if (this.window != this.window.top && obj == this.window.top) {
if (this.window.top.$corrosion) return this.window.top.$corrosionCall(obj, key, args);
else obj = this.window;
};
} catch(e) {
obj = this.window;
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) {
return this.top.$corrosion.get$m(this.top, key);
};
return obj[key](...args)
if (obj == this.window && key == 'location' || !this.serviceWorker && obj == this.window.document && key == 'location') return this.location;
if (!this.serviceWorker && obj == this.window && key == 'parent' && this.window != this.window.parent) return this.parent;
if (!this.serviceWorker && obj == this.window && key == 'top' && this.window != this.window.top) return this.top;
return obj[key];
};
set(obj, key, val, operator) {
/* if (obj == this.window || obj == this.window.document) {
switch(key) {
case 'location':
return this.location.href = val;
};
};*/
try {
if (this.window != this.window.parent && obj == this.window.parent) {
if (this.window.parent.$corrosion) return this.window.parent.$corrosionSet(obj, key, val, operator);
else obj = this.window;
obj = this.window;
};
if (this.window != this.window.top && obj == this.window.top) {
if (this.window.top.$corrosion) return this.window.top.$corrosionSet(obj, key, val, operator);
else obj = this.window;
obj = this.window;
};
} catch(e) {
obj = this.window;
set$m(obj, key, val, operator) {
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) {
return this.parent.$corrosion.set$m(this.parent, key, val, operator);
};
if (obj == this.window || obj == this.document) {
if (key == 'location') obj = this;
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) {
return this.top.$corrosion.set$m(this.top, key, val, operator);
};
if (obj == this.window.parent) obj = this.windowParent;
if (obj == this.window.top) obj = this.windowTop;

if (obj == this.window && key == 'location' || !this.serviceWorker && obj == this.window.document && key == 'location') obj = this;
switch(operator) {
case '+=':
return obj[key] += val;
Expand Down Expand Up @@ -172,17 +128,64 @@ class Corrosion extends require('../rewrite') {
return obj[key] = val;
};
};
setIdentifier(identifier, val) {
if (identifier == this.window.location) return this.location.href = val;
if (identifier == this.window.parent) return this.windowParent = val;
if (identifier == this.window.top) return this.windowTop = val;
return identifier = val;
call$m(obj, key, args) {
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) {
return this.parent.$corrosion.call$m(this.parent, key, args);
};
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) {
return this.top.$corrosion.call$m(this.top, key, args);
};
return obj[key](...args);
};
getIdentifier(identifier) {
if (identifier == this.window.location) return this.location;
if (identifier == this.window.parent) return this.windowParent;
if (identifier == this.window.top) return this.windowTop;
return identifier;
get$(obj) {
if (obj == this.window.location) return this.location;
if (!this.serviceWorker && obj == this.window.parent) return this.parent;
if (!this.serviceWorker && obj == this.window.top) return this.top;
return obj;
};
set$(obj, val, operator) {
if (obj == this.window.location) return this.set$(this.location, val, operator);
if (!this.serviceWorker && this.window != this.window.parent && obj == this.window.parent) return this.parent.set$(this.parent, val, operator);
if (!this.serviceWorker && this.window != this.window.top && obj == this.window.top) return this.top.set$(this.top, val, operator);
switch(operator) {
case '+=':
return obj += val;
case '-=':
return obj -= val;
case '*=':
return obj *= val;
case '/=':
return obj /= val;
case '%=':
return obj %= val;
case '**=':
return obj **= val;
case '<<=':
return obj <<= val;
case '>>=':
return obj >>= val;
case '>>>=':
return obj >>>= val;
case '&=':
return obj &= val;
case '^=':
return obj ^= val;
case '|=':
return obj |= val;
case '&&=':
return obj &&= val;
case '||=':
return obj ||= val;
case '??=':
return obj ??= val;
case '++':
return obj++;
case '--':
return obj--;
case '=':
default:
return obj = val;
};
};
updateLocation() {
this._url = new URL(this.url.unwrap(this.window.location.href, this.meta));
Expand Down
Loading

0 comments on commit 7650c5c

Please sign in to comment.