diff options
author | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
---|---|---|
committer | sss <sss@dark-alexandr.net> | 2023-01-17 00:38:19 +0300 |
commit | cc3f33db7a8d3c4ad373e646b199808e01bc5d9b (patch) | |
tree | ec09d690c7656ab5f2cc72607e05fb359c24d8b2 /www/js/webrdp-log.js |
added webrdp public code
Diffstat (limited to 'www/js/webrdp-log.js')
-rw-r--r-- | www/js/webrdp-log.js | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/www/js/webrdp-log.js b/www/js/webrdp-log.js new file mode 100644 index 0000000..62f17f6 --- /dev/null +++ b/www/js/webrdp-log.js @@ -0,0 +1,147 @@ +/** BSD-2-Clause license + * + * Copyright (c) 2018-2023 NST <www.newinfosec.ru>, smake <smake at ya dot ru>. + * + */ + +webrdp.o2s = function(obj, depth) { + depth = depth || []; + if (depth.contains(obj)) { + return '{SELF}'; + } + switch (typeof(obj)) { + case 'undefined': + return 'undefined'; + case 'string': + return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"'; + case 'array': + var string = []; + depth.push(obj); + for (var i = 0; i < obj.length; ++i) { + string.push(webrdp.o2s(obj[i], depth)); + } + depth.pop(); + return '[' + string + ']'; + case 'object': + case 'hash': + var string = []; + depth.push(obj); + var isE = (obj instanceof UIEvent); + Object.each(obj, function(v, k) { + if (v instanceof HTMLElement) { + string.push(k + '={HTMLElement}'); + } else if (isE && (('layerX' == k) || ('layerY' == k) ('view' == k))) { + string.push(k + '=!0'); + } else { + try { + var vstr = webrdp.o2s(v, depth); + if (vstr) { + string.push(k + '=' + vstr); + } + } catch (error) { + string.push(k + '=??E??'); + } + } + }); + depth.pop(); + return '{' + string + '}'; + case 'number': + case 'boolean': + return '' + obj; + case 'null': + return 'null'; + } + return null; +}; + +webrdp.Log = new Class({ + initialize: function() { + this.ws = null; + this.history = []; + this.loglevel = null; + this.debuglevel = 0; + }, + _p: function(pfx, a) { + var line = ''; + var i; + for (i = 0; i < a.length; ++i) { + switch (typeof(a[i])) { + case 'string': + case 'number': + case 'boolean': + case 'null': + line += a[i] + ' '; + break; + default: + line += webrdp.o2s(a[i]) + ' '; + break; + } + } + if (0 < line.length) { + this.ws.send(pfx + line); + } + }, + drop: function() { + }, + debug: function() { + if (this.loglevel == "debug") { + var entry = { + "date": new Date(), + "logtype": arguments.callee.name, + "direction": arguments[0], + "data": arguments[1] + } + this.history.push(entry); + } + }, + info: function() { + if (this.ws) { + var a = Array.prototype.slice.call(arguments); + a.unshift('I:'); + this._p.apply(this, a); + } + }, + warn: function() { + if (this.ws) { + var a = Array.prototype.slice.call(arguments); + a.unshift('W:'); + this._p.apply(this, a); + } + }, + err: function() { + if (this.ws) { + var a = Array.prototype.slice.call(arguments); + a.unshift('E:'); + this._p.apply(this, a); + } + }, + show: function(count) { + if (!count) { + var count = 30; + } + var first = this.history.length - count > 0 ? this.history.length - count : 0; + for (var i = first; i < this.history.length; i++) { + var h = this.history[i]; + var header = '[' + h.date.toLocaleTimeString('ru') + '.' + h.date.getMilliseconds() + ']'; + header += '[' + h.logtype + '] ' + h.direction + ':'; + console.log(header); + console.hex(h.data); + } + }, + setWS: function(_ws) { + this.ws = _ws; + } +}); + + + +/** + * For debugging + */ + +console.hex = (d) => console.log((Object(d).buffer instanceof ArrayBuffer ? new Uint8Array(d.buffer) : +typeof d === 'string' ? (new TextEncoder('utf-8')).encode(d) : +new Uint8ClampedArray(d)).reduce((p, c, i, a) => p + (i % 16 === 0 ? i.toString(16).padStart(6, 0) + ' ' : ' ') + +c.toString(16).padStart(2, 0) + (i === a.length - 1 || i % 16 === 15 ? +' '.repeat((15 - i % 16) * 3) + Array.from(a).splice(i - i % 16, 16).reduce((r, v) => +r + (v > 31 && v < 127 || v > 159 ? String.fromCharCode(v) : '.'), ' ') + '\n' : ''), '')); |