From 51ab2d13a3b61d556ff88d4261fd468a1f65142e Mon Sep 17 00:00:00 2001 From: lxsang Date: Thu, 26 Nov 2020 20:04:10 +0100 Subject: [PATCH] update Antunnel --- Antunnel/README.md | 3 +++ Antunnel/build/debug/README.md | 3 +++ Antunnel/build/debug/main.js | 2 +- Antunnel/build/debug/package.json | 2 +- Antunnel/build/debug/scheme.html | 5 ---- Antunnel/build/release/Antunnel.zip | Bin 7448 -> 6940 bytes Antunnel/coffees/Antunnel.coffee | 38 ++++++++++++++-------------- Antunnel/package.json | 2 +- packages.json | 2 +- 9 files changed, 29 insertions(+), 28 deletions(-) delete mode 100644 Antunnel/build/debug/scheme.html diff --git a/Antunnel/README.md b/Antunnel/README.md index 73320c0..2909c16 100644 --- a/Antunnel/README.md +++ b/Antunnel/README.md @@ -3,3 +3,6 @@ `Antunnel` is a client side API that allows AntOS applications to talk to server side applications via the [`antd-tunnel-pligin`](https://github.com/lxsang/antd-tunnel-plugin) plugin using a single websocket API. + +## Changes log +- v0.1.3-a Remove magic number in the frame to reduce frame overhead diff --git a/Antunnel/build/debug/README.md b/Antunnel/build/debug/README.md index 73320c0..2909c16 100644 --- a/Antunnel/build/debug/README.md +++ b/Antunnel/build/debug/README.md @@ -3,3 +3,6 @@ `Antunnel` is a client side API that allows AntOS applications to talk to server side applications via the [`antd-tunnel-pligin`](https://github.com/lxsang/antd-tunnel-plugin) plugin using a single websocket API. + +## Changes log +- v0.1.3-a Remove magic number in the frame to reduce frame overhead diff --git a/Antunnel/build/debug/main.js b/Antunnel/build/debug/main.js index 11a7fac..604cfc1 100644 --- a/Antunnel/build/debug/main.js +++ b/Antunnel/build/debug/main.js @@ -1 +1 @@ -(function(){var e,n,t,s,i;(t=class e{constructor(){this.header={sid:0,cid:0,type:0,size:0},this.data=void 0}as_raw(){var n,t,s;return s=21+this.header.size,(n=new Uint8Array(s)).set(e.MAGIC_START,0),n[4]=this.header.type,t=e.bytes_of(this.header.cid),n.set(t,5),t=e.bytes_of(this.header.sid),n.set(t,9),t=e.bytes_of(this.header.size),n.set(t,13),this.data&&n.set(this.data,17),n.set(e.MAGIC_END,this.header.size+17),n.buffer}}).decode=function(e){return new Promise((function(n,s){var i;return i=new t,t.int_from(t.MAGIC_START,0)!==t.int_from(e,0)?s("Unmatch message begin magic number"):(i.header.type=e[4],i.header.cid=t.int_from(e,5),i.header.sid=t.int_from(e,9),i.header.size=t.int_from(e,13),i.data=e.slice(17,17+i.header.size),t.int_from(t.MAGIC_END,0)!==t.int_from(e,17+i.header.size)?s("Unmatch message end magic number"):n(i))}))},t.bytes_of=function(e){var n;return(n=new Uint8Array(4))[0]=255&e,e>>=8,n[1]=255&e,e>>=8,n[2]=255&e,e>>=8,n[3]=255&e,n},t.int_from=function(e,n){return e[n]|e[n+1]<<8|e[n+2]<<16|e[n+3]<<24},t.OK=0,t.ERROR=1,t.DATA=6,t.CLOSE=5,t.SUBSCRIBE=2,t.UNSUBSCRIBE=3,t.CTRL=7,t.MAGIC_END=[65,78,84,68],t.MAGIC_START=[68,84,78,65],s=class{constructor(e){this.channel=e,this.id=void 0,this.channel_id=void 0,this.onmessage=void 0,this.onerror=void 0,this.onopen=void 0,this.onclose=void 0,this.tunnel=void 0,this.is_opened=!1}send(e,n){if(this.tunnel){if(this.is_opened)return this.tunnel.send(this.genmsg(e,n));this.onerror&&this.onerror("Channel is not opened yet")}else this.onerror&&this.onerror("Tunnel is not opened")}genmsg(e,n){var s;return(s=new t).header.sid=this.id,s.header.cid=this.channel_id,s.header.type=e,s.header.size=n?n.length:0,s.data=n,s}close(e){if(this.is_opened=!1,this.tunnel)return this.tunnel.unsubscribe(this,e)}},e=class{constructor(e){this.uri=e,this.socket=void 0,this.pending={},this.subscribers={},this.onclose=void 0}ready(){return new Promise((e,n)=>this.uri?void 0!==this.socket?e():(console.log("Connect to "+this.uri),this.socket=new WebSocket(this.uri),this.socket.binaryType="arraybuffer",this.socket.onmessage=e=>this.process(e),this.socket.onclose=e=>{var n,t,s,i;for(n in this.socket=void 0,t=this.pending)(i=t[n]).tunnel=void 0,i.onclose&&i.onclose();for(n in s=this.subscribers)(i=s[n]).tunnel=void 0,i.is_opened=!1,i.onclose&&i.onclose();if(this.pending={},this.subscribe={},this.onclose())return this.onclose()},this.socket.onerror=e=>{var n,t,s,i,r;for(n in t=this.pending)(r=t[n]).onerror&&r.onerror(e.toString());for(n in i=[],s=this.subscribers)(r=s[n]).onerror&&i.push(r.onerror(e.toString()));return i},this.socket.onopen=n=>e()):n())}process(e){return t.decode(new Uint8Array(e.data)).then(e=>{var n,s;switch(n=(e,n)=>{var t;if(!(t=this.pending[e.header.sid]))return(t=this.subscribers[e.header.sid])&&t[n]?t[n](e):void 0;t[n]&&t[n](e)},e.header.type){case t.OK:if(!(s=this.pending[e.header.sid]))return n(e,"onmessage");if(delete this.pending[e.header.sid],s.id=t.int_from(e.data,0),s.channel_id=e.header.cid,this.subscribers[s.id]=s,s.is_opened=!0,s.onopen)return s.onopen();break;case t.DATA:return n(e,"onmessage");case t.ERROR:return n(e,"onerror");case t.UNSUBSCRIBE:if(!(s=this.subscribers[e.header.sid]))return;return s.close(!0);default:return console.error(`Message of type ${e.header.type} is unsupported`,e)}}).catch(e=>{var n,t,s,i,r;for(n in t=this.pending)(r=t[n]).onerror&&r.onerror(e);for(n in i=[],s=this.subscribers)(r=s[n]).onerror&&i.push(r.onerror(e));return i})}subscribe(e){return this.ready().then(()=>{for(e.tunnel=this,e.id=Math.floor(1e5*Math.random())+1;this.subscribers[e.id]||this.pending[e.id];)e.id=Math.floor(1e5*Math.random())+1;return this.pending[e.id]=e,this.send(e.genmsg(t.SUBSCRIBE,(new TextEncoder).encode(e.channel)))}).catch((function(n){if(e.onerror)return e.onerror(n.toString())}))}unsubscribe(e,n){return this.ready().then(()=>{if(this.subscribers[e.id])return n||this.send(e.genmsg(t.UNSUBSCRIBE,void 0)),e.onclose&&e.onclose(),delete this.subscribers[e.id],e.tunnel=void 0,e.is_opened=!1}).catch((function(n){if(e.onerror)return e.onerror(n.toString())}))}send(e){return this.socket.send(e.as_raw())}close(){if(console.log("Close connection to "+this.uri),this.socket&&this.socket.close(),this.onclose())return this.onclose()}},(i=this).Antunnel||(i.Antunnel={tunnel:void 0,init:function(n){return new Promise((function(t,s){return i.Antunnel.tunnel?t(i.Antunnel.tunnel):(i.Antunnel.tunnel=new e(n),i.Antunnel.tunnel.onclose=function(){return i.Antunnel.tunnel=void 0},i.Antunnel.tunnel.ready().then((function(){return t(i.Antunnel.tunnel)})).catch((function(e){return s(e)})))}))},Subscriber:s,Msg:t}),n=class extends OS.application.BaseService{constructor(e){super("AntunnelService",e),this.text=__("Tunnel"),this.iconclass="fa fa-close",this.is_connect=!1,this.nodes=[{text:__("Connect"),id:1},{text:__("Disconnect"),id:2},{text:__("Enter uri"),id:3},{text:__("Exit"),id:4}],this.onchildselect=e=>this.action(e)}init(){return this.systemsetting.system.tunnel_uri&&this.start(),this.watch(1500,()=>{var e;if(e=!1,void 0!==Antunnel.tunnel&&(e=!0),e!==this.is_connect)return this.is_connect=e,this.iconclass="fa fa-circle",this.is_connect||(this.iconclass="fa fa-close"),this.update()}),OS.onexit("cleanupAntunnel",()=>(Antunnel.tunnel&&Antunnel.tunnel.close(),this.quit()))}action(e){var n;switch(n=()=>this._gui.openDialog("PromptDialog",{title:__("Tunnel uri"),label:__("Please enter tunnel uri"),value:"wss://localhost/tunnel"}).then(e=>{if(e&&""!==e)return this.systemsetting.system.tunnel_uri=e,this.start()}),e.data.item.data.id){case 1:if(this.is_connect)return;return this.systemsetting.system.tunnel_uri?this.start():n();case 2:if(Antunnel.tunnel)return Antunnel.tunnel.close();break;case 3:return Antunnel.tunnel&&Antunnel.tunnel.close(),n();case 4:return Antunnel.tunnel&&Antunnel.tunnel.close(),this.quit()}}start(){if(this.systemsetting.system.tunnel_uri&&!Antunnel.tunnel)return Antunnel.init(this.systemsetting.system.tunnel_uri).then(e=>this.notify(__("Tunnel now connected to the server at: {0}",this.systemsetting.system.tunnel_uri))).catch(e=>(Antunnel.tunnel&&Antunnel.tunnel.close(),this.error(__("Unable to connect to the tunnel: {0}",e.toString()),e)))}awake(){}},this.OS.register("AntunnelService",n)}).call(this); \ No newline at end of file +(function(){var e,n,t,s,i;(t=class e{constructor(){this.header={sid:0,cid:0,type:0,size:0},this.data=void 0}as_raw(){var n,t,s;return s=13+this.header.size,(n=new Uint8Array(s))[0]=this.header.type,t=e.bytes_of(this.header.cid),n.set(t,1),t=e.bytes_of(this.header.sid),n.set(t,5),t=e.bytes_of(this.header.size),n.set(t,9),this.data&&n.set(this.data,13),n.buffer}}).decode=function(e){return new Promise((function(n,s){var i;return(i=new t).header.type=e[0],i.header.cid=t.int_from(e,1),i.header.sid=t.int_from(e,5),i.header.size=t.int_from(e,9),i.data=e.slice(13,13+i.header.size),n(i)}))},t.bytes_of=function(e){var n;return(n=new Uint8Array(4))[0]=255&e,e>>=8,n[1]=255&e,e>>=8,n[2]=255&e,e>>=8,n[3]=255&e,n},t.int_from=function(e,n){return e[n]|e[n+1]<<8|e[n+2]<<16|e[n+3]<<24},t.OK=0,t.ERROR=1,t.DATA=6,t.CLOSE=5,t.SUBSCRIBE=2,t.UNSUBSCRIBE=3,t.CTRL=7,s=class{constructor(e){this.channel=e,this.id=void 0,this.channel_id=void 0,this.onmessage=void 0,this.onerror=void 0,this.onopen=void 0,this.onclose=void 0,this.tunnel=void 0,this.is_opened=!1}send(e,n){if(this.tunnel){if(this.is_opened)return this.tunnel.send(this.genmsg(e,n));this.onerror&&this.onerror("Channel is not opened yet")}else this.onerror&&this.onerror("Tunnel is not opened")}genmsg(e,n){var s;return(s=new t).header.sid=this.id,s.header.cid=this.channel_id,s.header.type=e,s.header.size=n?n.length:0,s.data=n,s}close(e){if(this.is_opened=!1,this.tunnel)return this.tunnel.unsubscribe(this,e)}},e=class{constructor(e){this.uri=e,this.socket=void 0,this.pending={},this.subscribers={},this.onclose=void 0}ready(){return new Promise((e,n)=>this.uri?void 0!==this.socket?e():(console.log("Connect to "+this.uri),this.socket=new WebSocket(this.uri),this.socket.binaryType="arraybuffer",this.socket.onmessage=e=>this.process(e),this.socket.onclose=e=>{var n,t,s,i;for(n in this.socket=void 0,t=this.pending)(i=t[n]).tunnel=void 0,i.onclose&&i.onclose();for(n in s=this.subscribers)(i=s[n]).tunnel=void 0,i.is_opened=!1,i.onclose&&i.onclose();if(this.pending={},this.subscribe={},this.onclose())return this.onclose()},this.socket.onerror=e=>{var n,t,s,i,r;for(n in t=this.pending)(r=t[n]).onerror&&r.onerror(e.toString());for(n in i=[],s=this.subscribers)(r=s[n]).onerror&&i.push(r.onerror(e.toString()));return i},this.socket.onopen=n=>e()):n())}process(e){return t.decode(new Uint8Array(e.data)).then(e=>{var n,s;switch(n=(e,n)=>{var t;if(!(t=this.pending[e.header.sid]))return(t=this.subscribers[e.header.sid])&&t[n]?t[n](e):void 0;t[n]&&t[n](e)},e.header.type){case t.OK:if(!(s=this.pending[e.header.sid]))return n(e,"onmessage");if(delete this.pending[e.header.sid],s.id=t.int_from(e.data,0),s.channel_id=e.header.cid,this.subscribers[s.id]=s,s.is_opened=!0,s.onopen)return s.onopen();break;case t.DATA:return n(e,"onmessage");case t.ERROR:return n(e,"onerror");case t.UNSUBSCRIBE:if(!(s=this.subscribers[e.header.sid]))return;return s.close(!0);default:return console.error(`Message of type ${e.header.type} is unsupported`,e)}}).catch(e=>{var n,t,s,i,r;for(n in t=this.pending)(r=t[n]).onerror&&r.onerror(e);for(n in i=[],s=this.subscribers)(r=s[n]).onerror&&i.push(r.onerror(e));return i})}subscribe(e){return this.ready().then(()=>{for(e.tunnel=this,e.id=Math.floor(1e5*Math.random())+1;this.subscribers[e.id]||this.pending[e.id];)e.id=Math.floor(1e5*Math.random())+1;return this.pending[e.id]=e,this.send(e.genmsg(t.SUBSCRIBE,(new TextEncoder).encode(e.channel)))}).catch((function(n){if(e.onerror)return e.onerror(n.toString())}))}unsubscribe(e,n){return this.ready().then(()=>{if(this.subscribers[e.id])return n||this.send(e.genmsg(t.UNSUBSCRIBE,void 0)),e.onclose&&e.onclose(),delete this.subscribers[e.id],e.tunnel=void 0,e.is_opened=!1}).catch((function(n){if(e.onerror)return e.onerror(n.toString())}))}send(e){return this.socket.send(e.as_raw())}close(){if(console.log("Close connection to "+this.uri),this.socket&&this.socket.close(),this.onclose())return this.onclose()}},(i=this).Antunnel||(i.Antunnel={tunnel:void 0,init:function(n){return new Promise((function(t,s){return i.Antunnel.tunnel?t(i.Antunnel.tunnel):(i.Antunnel.tunnel=new e(n),i.Antunnel.tunnel.onclose=function(){return i.Antunnel.tunnel=void 0},i.Antunnel.tunnel.ready().then((function(){return t(i.Antunnel.tunnel)})).catch((function(e){return s(e)})))}))},Subscriber:s,Msg:t}),n=class extends OS.application.BaseService{constructor(e){super("AntunnelService",e),this.text=__("Tunnel"),this.iconclass="fa fa-close",this.is_connect=!1,this.nodes=[{text:__("Connect"),id:1},{text:__("Disconnect"),id:2},{text:__("Enter uri"),id:3},{text:__("Exit"),id:4}],this.onchildselect=e=>this.action(e)}init(){return this.systemsetting.system.tunnel_uri&&this.start(),this.watch(1500,()=>{var e;if(e=!1,void 0!==Antunnel.tunnel&&(e=!0),e!==this.is_connect)return this.is_connect=e,this.iconclass="fa fa-circle",this.is_connect||(this.iconclass="fa fa-close"),this.update()}),OS.onexit("cleanupAntunnel",()=>(Antunnel.tunnel&&Antunnel.tunnel.close(),this.quit()))}action(e){var n;switch(n=()=>this._gui.openDialog("PromptDialog",{title:__("Tunnel uri"),label:__("Please enter tunnel uri"),value:"wss://localhost/tunnel"}).then(e=>{if(e&&""!==e)return this.systemsetting.system.tunnel_uri=e,this.start()}),e.data.item.data.id){case 1:if(this.is_connect)return;return this.systemsetting.system.tunnel_uri?this.start():n();case 2:if(Antunnel.tunnel)return Antunnel.tunnel.close();break;case 3:return Antunnel.tunnel&&Antunnel.tunnel.close(),n();case 4:return Antunnel.tunnel&&Antunnel.tunnel.close(),this.quit()}}start(){if(this.systemsetting.system.tunnel_uri&&!Antunnel.tunnel)return Antunnel.init(this.systemsetting.system.tunnel_uri).then(e=>this.notify(__("Tunnel now connected to the server at: {0}",this.systemsetting.system.tunnel_uri))).catch(e=>(Antunnel.tunnel&&Antunnel.tunnel.close(),this.error(__("Unable to connect to the tunnel: {0}",e.toString()),e)))}awake(){}},this.OS.register("AntunnelService",n)}).call(this); \ No newline at end of file diff --git a/Antunnel/build/debug/package.json b/Antunnel/build/debug/package.json index 8ef7a35..c559f0c 100644 --- a/Antunnel/build/debug/package.json +++ b/Antunnel/build/debug/package.json @@ -9,7 +9,7 @@ "author": "Xuan Sang LE", "email": "xsang.le@lxsang.me" }, - "version":"0.1.2-a", + "version":"0.1.3-a", "category":"Library", "iconclass":"fa fa-adn", "mimes":["none"], diff --git a/Antunnel/build/debug/scheme.html b/Antunnel/build/debug/scheme.html deleted file mode 100644 index 5b3a472..0000000 --- a/Antunnel/build/debug/scheme.html +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/Antunnel/build/release/Antunnel.zip b/Antunnel/build/release/Antunnel.zip index 8e96191d4d7bd41b1ea6f4debb56a8d531e00d2d..7130894631e146709642c3b303c3bd55b95d6f91 100644 GIT binary patch delta 512 zcmbPXHOEXMz?+$civa}kW>f`+y+6Wf#>l{6#yE-bLp_(WvVwC)VqSV`u|iIMI+w0O znSq|6p0RGCLQraMep#wQZen_7vO->IZc=KILS~*qNk*zdT2W$dszOP=LQ!f;X)=rt zR9%#jnwY|cYT^G`OW*m6GcbTKJJ1oiiJ5tNS;ZP@rFqGdrI?zT42>txXHsX1Hkf>$ z$&$%XbMi+fDaPW-Jj_~?RF+CL+nE81D-mGjO1YuG7@l3wBK0GU#?Y$Gx+VzevI6C!ZMqm@iD{ZvY%#_VY!>z{JIW4(9YXD8 z51veVF{{yo|Dr#@yGQ*2N=%H$CdQiwXJ{!4?QAAH^E~hOyzi&X$0et@=JN*;Uw`%k zkEi74^Pkz7?@6}pnh6%DCbb&N`p#}lZ?$4wOpIn%Vn>Q;12j2LXonDJ$Wj*@)YRm3 zJT6Nn;JQgQImIuAPW2czJhHi<#MZLIt(w*5u5<+SL5@{Bls1V(VVBw1>Qp^8s?=up z-15Dl+_AMj!mZ_kN9-`(+-Rb2X1@k{EJ3k{-i8iCqogsFdgg=>azbuLcrPXs?nUwL zv}yy5fV!T4xcXpwtCZg?De$PPpYwhJ38M+zqB2DDWh@4;Lm>-NM)j)XM=U5P4G#e{C}w3}9hj>U9a z(>yG&DDT@5`3bRfS~DdJY+J8`RE0VwlBL&iO(Gqu3T-*M6v67TQ~Ig}8kAEN z!+q+z?4+YZ%w~@|;%sq*+=Ee;*8=wop@C{Qa1A1fEGxm>i4&eItJmr5cva$2mfNa_ zgwwib{3W}(8O@H{r-`UImh>55{z+Qf~6 zzy4oW1AK6juP=Q&_!$ZbLel*=-Ez(vCf07A - length = 21 + @header.size + length = 13 + @header.size arr = new Uint8Array(length) - arr.set(Msg.MAGIC_START, 0) - arr[4] = @header.type + #arr.set(Msg.MAGIC_START, 0) + arr[0] = @header.type bytes = Msg.bytes_of @header.cid - arr.set(bytes,5) + arr.set(bytes,1) bytes = Msg.bytes_of @header.sid - arr.set(bytes,9) + arr.set(bytes,5) bytes = Msg.bytes_of @header.size - arr.set(bytes,13) + arr.set(bytes,9) if @data - arr.set(@data, 17) - arr.set(Msg.MAGIC_END, @header.size + 17) + arr.set(@data, 13) + # arr.set(Msg.MAGIC_END, @header.size + 13) arr.buffer Msg.decode = (raw) -> new Promise (resolve, reject) -> msg = new Msg() - if(Msg.int_from(Msg.MAGIC_START, 0) != Msg.int_from(raw, 0)) - return reject("Unmatch message begin magic number") - msg.header.type = raw[4] - msg.header.cid = Msg.int_from(raw, 5) - msg.header.sid = Msg.int_from(raw,9) - msg.header.size = Msg.int_from(raw, 13) - msg.data = raw.slice(17, 17+msg.header.size) - if(Msg.int_from(Msg.MAGIC_END, 0) != Msg.int_from(raw, 17+msg.header.size)) - return reject("Unmatch message end magic number") + #if(Msg.int_from(Msg.MAGIC_START, 0) != Msg.int_from(raw, 0)) + # return reject("Unmatch message begin magic number") + msg.header.type = raw[0] + msg.header.cid = Msg.int_from(raw, 1) + msg.header.sid = Msg.int_from(raw,5) + msg.header.size = Msg.int_from(raw, 9) + msg.data = raw.slice(13, 13+msg.header.size) + #if(Msg.int_from(Msg.MAGIC_END, 0) != Msg.int_from(raw, 17+msg.header.size)) + # return reject("Unmatch message end magic number") resolve msg @@ -63,8 +63,8 @@ Msg.CLOSE = 5 Msg.SUBSCRIBE = 2 Msg.UNSUBSCRIBE = 3 Msg.CTRL = 7 -Msg.MAGIC_END = [ 0x41, 0x4e, 0x54, 0x44] -Msg.MAGIC_START = [0x44, 0x54, 0x4e, 0x41 ] +#Msg.MAGIC_END = [ 0x41, 0x4e, 0x54, 0x44] +#Msg.MAGIC_START = [0x44, 0x54, 0x4e, 0x41 ] class Subscriber constructor: (@channel) -> diff --git a/Antunnel/package.json b/Antunnel/package.json index 8ef7a35..c559f0c 100644 --- a/Antunnel/package.json +++ b/Antunnel/package.json @@ -9,7 +9,7 @@ "author": "Xuan Sang LE", "email": "xsang.le@lxsang.me" }, - "version":"0.1.2-a", + "version":"0.1.3-a", "category":"Library", "iconclass":"fa fa-adn", "mimes":["none"], diff --git a/packages.json b/packages.json index 027619f..055eaaa 100644 --- a/packages.json +++ b/packages.json @@ -23,7 +23,7 @@ "description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Antunnel/README.md", "category": "Library", "author": "Xuan Sang LE", - "version": "0.1.2-a", + "version": "0.1.3-a", "download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/Antunnel/build/release/Antunnel.zip" }, {