add tunnel plugin package

This commit is contained in:
Dany LE
2021-11-28 14:10:21 +01:00
parent 0df49e387c
commit 031f78c926
19 changed files with 1484 additions and 7 deletions

View File

@ -0,0 +1,7 @@
# Antunnel Plugins
Aditional Plugins for Antunnel library.
This package provides also the Typescript declaration file for
application Development.
## Change logs
- v.0.1.0: Antunnel API declaration and broadcast plugin

519
AntunnelPlugins/build/debug/main.d.ts vendored Normal file
View File

@ -0,0 +1,519 @@
/**
* This namespace describe the Antunnel API
* used by o ther application
*/
declare namespace Antunnel {
/**
* Tunnel message type
*
* @export
* @enum {number}
*/
enum AntunnelMSGType {
OK = 0,
SUBSCRIBE = 2,
UNSUBSCRIBE = 3,
ERROR = 1,
DATA = 6,
CTRL = 7,
CLOSE = 5,
PING = 8
}
/**
* Main tunnel core handle API
*
* @export
* @interface AntunnelAPI
*/
interface AntunnelAPI {
/**
* Close the socket connection attached to the
* current handle
*
* @memberof AntunnelAPI
*/
close(): void;
/**
* register a subscriber to the handle
*
* @param {SubscriberInterface} sub
* @memberof AntunnelAPI
*/
subscribe(sub: SubscriberInterface): void;
/**
* Remove a subscriber from the handle
*
* @param {SubscriberInterface} sub
* @param {boolean} b notify the backend ?
* @memberof AntunnelAPI
*/
unsubscribe(sub: SubscriberInterface, b: boolean): void;
}
/**
* Singleton instance to the current core tunnel handle
*/
var tunnel: AntunnelAPI;
/**
* A tunnel frame header
*
* @export
* @interface AntunnelMSGHeader
*/
interface AntunnelMSGHeader {
/**
* Client ID allocated by the backend
*
* @type {number}
* @memberof AntunnelMSGHeader
*/
cid: number;
/**
* Subscriber ID allocated by Antunnel frontend
*
* @type {number}
* @memberof AntunnelMSGHeader
*/
sid: number;
/**
* Payload size
*
* @type {number}
* @memberof AntunnelMSGHeader
*/
size: number;
/**
* Message type
*
* @type {AntunnelMSGType}
* @memberof AntunnelMSGHeader
*/
type: AntunnelMSGType;
}
/**
* Tunnel frame format
*
* @export
* @interface AntunnelMSG
*/
interface AntunnelMSG {
/**
* frame header
*
* @type {AntunnelMSGHeader}
* @memberof AntunnelMSG
*/
header: AntunnelMSGHeader;
/**
* raw data
*
* @type {Uint8Array}
* @memberof AntunnelMSG
*/
data: Uint8Array;
/**
* helper function that convert the
* entire frame to byte array
*
* @memberof AntunnelMSG
*/
as_raw(): void;
}
/**
* Static members of Msg class
*
* @export
* @interface AntunnelMSGMeta
*/
interface AntunnelMSGMeta {
/**
* constructor
*/
new (): AntunnelMSG;
/**
* convert number to array (network byte order)
*
* @param {number} x
* @param {number} [s]
* @return {*} {Uint8Array}
* @memberof AntunnelMSGMeta
*/
bytes_of(x: number, s?: number): Uint8Array;
/**
* convert network byte order array to number
*
* @param {Uint8Array} data
* @param {number} offset
* @param {number} [s]
* @return {*} {number}
* @memberof AntunnelMSGMeta
*/
int_from(data: Uint8Array, offset: number, s?: number): number;
OK: AntunnelMSGType;
ERROR: AntunnelMSGType;
DATA: AntunnelMSGType;
CLOSE: AntunnelMSGType;
SUBSCRIBE: AntunnelMSGType;
UNSUBSCRIBE: AntunnelMSGType;
CTRL: AntunnelMSGType;
PING: AntunnelMSGType;
}
var Msg: AntunnelMSGMeta;
/**
* Interface of a Subscriber class
*
* @export
* @interface SubscriberInterface
*/
interface SubscriberInterface {
/**
* Subscriber ID allocated by Antunnel API
*
* @type {number}
* @memberof SubscriberInterface
*/
id: number;
/**
* Channel ID/ Client ID allocated by backend
*
* @type {number}
* @memberof SubscriberInterface
*/
channel_id: number;
/**
* Called when a channel is opened for
* this subscriber
*
* @memberof SubscriberInterface
*/
onopen: () => void;
/**
* Error handle callback
*
* @memberof SubscriberInterface
*/
onerror: (d: string | AntunnelMSG) => void;
/**
* Message callback
*
* @memberof SubscriberInterface
*/
onmessage: (m: AntunnelMSG) => void;
/**
* Control messqge callback
*
* @memberof SubscriberInterface
*/
onctrl: (m: AntunnelMSG) => void;
/**
* Subscriber close callback
*
* @memberof SubscriberInterface
*/
onclose: () => void;
/**
* Send a message to backend
*
* @memberof SubscriberInterface
*/
send: (t: AntunnelMSGType, arr: Uint8Array) => void;
close: () => void;
}
/**
* Static member of a subscriber
*
* @export
* @interface SubscriberInterfaceMeta
*/
interface SubscriberInterfaceMeta {
new (channel: string): SubscriberInterface;
}
var Subscriber: SubscriberInterfaceMeta;
/**
* Core handle initialization
*/
var init: (u: string) => void;
}
/**
* Broadcast plugin for Antunnel to communication
* with the backend Antunnel broadcast publisher
*/
declare namespace Antunnel {
/**
* Broadcast control message type
*
* @export
* @enum {number}
*/
enum BroadcastCTRLType {
SUBSCRIBE = 10,
UNSUBSCRIBE = 11,
QUERY = 12
}
/**
* Group state
*
* @export
* @enum {number}
*/
enum BroadcastGroupState {
INIT = 0,
SUBSCRIBED = 1,
UNSUBSCRIBED = 2
}
/**
* Broadcast control message
*
* @export
* @interface BroadcastCTRLMsg
*/
interface BroadcastCTRLMsg {
/**
* Message type
*
* @type {BroadcastCTRLType}
* @memberof BroadcastCTRLMsg
*/
type: BroadcastCTRLType;
/**
* Group name
*
* @type {string}
* @memberof BroadcastCTRLMsg
*/
group?: string;
/**
* User name
*
* @type {string}
* @memberof BroadcastCTRLMsg
*/
user: string;
/**
* group id - allocated by backend
*
* @type {number}
* @memberof BroadcastCTRLMsg
*/
id: number;
}
/**
* Broadcast group handle
*
* @export
* @class BroadcastGroup
*/
class BroadcastGroup {
/**
* Group name
*
* @type {string}
* @memberof BroadcastGroup
*/
groupname: string;
/**
* Group id allocated by backend
*
* @type {number}
* @memberof BroadcastGroup
*/
id: number;
/**
* Active users of the group
*
* @type {Set<string>}
* @memberof BroadcastGroup
*/
users: Set<string>;
/**
* Called when grouped is opened by backend
*
* @memberof BroadcastGroup
*/
onready: () => void;
/**
* Called whe a message is sent to group
*
* @memberof BroadcastGroup
*/
onmessage: (data: Uint8Array) => void;
/**
* Called when user added to the group
*
* @memberof BroadcastGroup
*/
onuseradd: (user: string) => void;
/**
* Called when user is removed from the group
*
* @memberof BroadcastGroup
*/
onuserdel: (user: string) => void;
/**
* Called when handle owner left the group
*
* @memberof BroadcastGroup
*/
onclose: () => void;
/**
* Owner of this handle
*
* @type {string}
* @memberof BroadcastGroup
*/
user: string;
/**
* reference to the attached Broadcast manage
*
* @type {BroadcastManager}
* @memberof BroadcastGroup
*/
mgr: BroadcastManager;
/**
* Current state of the handle
*
* @type {BroadcastGroupState}
* @memberof BroadcastGroup
*/
state: BroadcastGroupState;
/**
* Creates an instance of BroadcastGroup.
* @param {string} name
* @memberof BroadcastGroup
*/
constructor(name: string);
/**
* Leave the group
*
* @return {*} {void}
* @memberof BroadcastGroup
*/
close(): void;
/**
* Query all users in the group
*
* @return {*} {void}
* @memberof BroadcastGroup
*/
refresh(): void;
/**
* Send a message to the group
*
* @param {Uint8Array} data
* @memberof BroadcastGroup
*/
send(data: Uint8Array): void;
}
/**
* Broadcast Manager
* Managing all group handles created by the current
* user
*
* @export
* @class BroadcastManager
*/
class BroadcastManager {
/**
* Reference to Antunnel subscriber
*
* @private
* @type {SubscriberInterface}
* @memberof BroadcastManager
*/
private sub;
/**
* channel name
*
* @private
* @type {string}
* @memberof BroadcastManager
*/
private channel;
/**
* Reference to the global tunnel handle
*
* @private
* @type {AntunnelAPI}
* @memberof BroadcastManager
*/
private tunnel;
/**
* list of all registered group handles
*
* @private
* @type {{[prop: number]: BroadcastGroup}}
* @memberof BroadcastManager
*/
private groups;
/**
* temporary list of group handles that wait for
* an connection confirmation from the backend
*
* @private
* @type {GenericObject<BroadcastGroup>}
* @memberof BroadcastManager
*/
private pendings;
/**
* Creates an instance of BroadcastManager.
* @param {string} channel
* @memberof BroadcastManager
*/
constructor(channel: string);
/**
* Connect to the broadcast channel
*
* @private
* @param {(d: any)=> void} resolve
* @memberof BroadcastManager
*/
private connect;
/**
* Perform setup of the manager:
* - Check if Antunnel API is available
* - Connect to the tunnel if the global tunnel does not exists
* - Subscribe to th e broadcast channel if not done
*
* @private
* @return {*} {Promise<any>}
* @memberof BroadcastManager
*/
private setup;
/**
* Remove a group handle from the manager
*
* @param {number} gid
* @memberof BroadcastManager
*/
unsubscribe(gid: number): void;
/**
* Query users in the specific group
*
* @param {number} gid group id
* @memberof BroadcastManager
*/
query(gid: number): void;
/**
* Register a group to the manager
*
* @param {BroadcastGroup} group
* @memberof BroadcastManager
*/
subscribe(group: BroadcastGroup): void;
/**
*CLeanup the manager
*
* @memberof BroadcastManager
*/
teardown(): void;
/**
* Send a message to a specific group
*
* @param {number} gid
* @param {Uint8Array} data
* @memberof BroadcastManager
*/
send(gid: number, data: Uint8Array): void;
}
}

View File

@ -0,0 +1 @@
var Antunnel;!function(e){let s;!function(e){e[e.OK=0]="OK",e[e.SUBSCRIBE=2]="SUBSCRIBE",e[e.UNSUBSCRIBE=3]="UNSUBSCRIBE",e[e.ERROR=1]="ERROR",e[e.DATA=6]="DATA",e[e.CTRL=7]="CTRL",e[e.CLOSE=5]="CLOSE",e[e.PING=8]="PING"}(s=e.AntunnelMSGType||(e.AntunnelMSGType={}))}(Antunnel||(Antunnel={})),function(e){let s,t;!function(e){e[e.SUBSCRIBE=10]="SUBSCRIBE",e[e.UNSUBSCRIBE=11]="UNSUBSCRIBE",e[e.QUERY=12]="QUERY"}(s=e.BroadcastCTRLType||(e.BroadcastCTRLType={})),function(e){e[e.INIT=0]="INIT",e[e.SUBSCRIBED=1]="SUBSCRIBED",e[e.UNSUBSCRIBED=2]="UNSUBSCRIBED"}(t=e.BroadcastGroupState||(e.BroadcastGroupState={})),e.BroadcastGroup=class{constructor(e){this.groupname=e,this.users=new Set,this.onmessage=void 0,this.onready=void 0,this.onuseradd=void 0,this.onuserdel=void 0,this.onclose=void 0,this.user=OS.setting.user.name,this.mgr=void 0,this.state=t.INIT}close(){this.mgr&&this.id&&this.mgr.unsubscribe(this.id)}refresh(){this.mgr&&this.id&&this.mgr.query(this.id)}send(e){this.mgr.send(this.id,e)}},e.BroadcastManager=class{constructor(e){this.sub=void 0,this.channel=e,this.tunnel=void 0,this.groups={},this.pendings={}}connect(n){this.sub=new e.Subscriber(this.channel),this.sub.onopen=()=>{OS.announcer.osinfo(__("Subscriber {0}: Connected to the {1} channel",this.sub.id,this.channel)),n(!0)},this.sub.onerror=e=>{let s=e;e.data&&(s=new TextDecoder("utf-8").decode(e.data)),OS.announcer.oserror(__("Subscriber {0}: Error from the {1} channel: {2}",this.sub.id,this.channel,s),void 0)},this.sub.onmessage=s=>{if(s.data){let t=e.Msg.int_from(s.data.slice(0,4),0,4),n=this.groups[t];if(!n)return;n.onmessage&&n.onmessage(s.data.slice(4))}},this.sub.onctrl=n=>{let i={user:void 0,group:void 0,type:n.data[0],id:void 0};switch(i.type){case s.SUBSCRIBE:case s.UNSUBSCRIBE:let r=n.data[1]+2;if(i.user=new TextDecoder("utf-8").decode(n.data.slice(2,r)),i.id=e.Msg.int_from(n.data,r,4),r+=4,i.group=new TextDecoder("utf-8").decode(n.data.slice(r)),i.type===s.SUBSCRIBE){let e=this.pendings[i.group];if(e&&e.user===i.user&&(e.id=i.id,e.onready(),this.pendings[i.group]=void 0,delete this.pendings[i.group],this.groups[i.id]=e,e.state=t.SUBSCRIBED),e=this.groups[i.id],!e)return;e.users.add(i.user),e.onuseradd&&e.onuseradd(i.user)}else{let e=this.groups[i.id];if(!e)return;e.user===i.user?(OS.announcer.osinfo(__("Subcriber {0}: leave group {1}",this.sub.id,i.group)),this.groups[i.id]=void 0,delete this.groups[i.id],e.state=t.UNSUBSCRIBED,e.onclose&&e.onclose()):(e.users.delete(i.user),e.onuserdel&&e.onuserdel(i.user))}break;case s.QUERY:i.id=e.Msg.int_from(n.data,1,4),i.user=new TextDecoder("utf-8").decode(n.data.slice(5));let o=this.groups[i.id];if(!o)return;o.users.has(i.user)||(o.users.add(i.user),o.onuseradd&&o.onuseradd(i.user))}},this.sub.onclose=()=>{OS.announcer.osinfo(__("Subscriber {0}: Connection to {1} closed",this.sub.id,this.channel)),this.sub=void 0},this.tunnel.subscribe(this.sub)}setup(){return new Promise(async(s,t)=>{try{if(!e)throw new Error(__("Library not fould: %s","Antunnel").__());if(e.tunnel)this.tunnel=e.tunnel;else{await OS.GUI.pushService("Antunnel/AntunnelService");let s=OS.setting.system.tunnel_uri;if(!s)throw new Error(__("Unable to connect to: %s","Antunnel").__());await e.init(s),this.tunnel=e.tunnel}this.sub?s(!0):this.connect(s)}catch(s){e.tunnel&&e.tunnel.close(),t(__e(s))}})}unsubscribe(t){let n=new Uint8Array(5);n[0]=s.UNSUBSCRIBE,n.set(e.Msg.bytes_of(t,4),1),this.sub.send(e.Msg.CTRL,n)}query(t){let n=new Uint8Array(5);n[0]=s.QUERY,n.set(e.Msg.bytes_of(t,4),1),this.sub.send(e.Msg.CTRL,n)}subscribe(t){this.setup().then(n=>{let i=new Uint8Array(t.groupname.length+1);i[0]=s.SUBSCRIBE,i.set((new TextEncoder).encode(t.groupname),1),this.sub.send(e.Msg.CTRL,i),t.mgr=this,this.pendings[t.groupname]=t}).catch(e=>{OS.announcer.oserror(__("Unable to subscribe to group {0}: {1}",t.groupname,e.toString()),e)})}teardown(){this.sub&&this.sub.close(),this.groups={},this.pendings={}}send(s,t){let n=new Uint8Array(t.length+4);n.set(e.Msg.bytes_of(s,4),0),n.set(t,4),this.sub.send(e.Msg.DATA,n)}}}(Antunnel||(Antunnel={}));

View File

@ -0,0 +1,15 @@
{
"pkgname": "AntunnelPlugins",
"name":"Antunnel Plugins",
"description":"Additional plugins for the Antunnel library",
"info":{
"author": "Dany LE",
"email": "mrsang@iohub.dev"
},
"version":"0.1.0-a",
"category":"Library",
"iconclass":"fa fa-cog",
"mimes":["none"],
"dependencies":["Antunnel@0.2.0-b"],
"locale": {}
}