2020-08-06 23:21:42 +02:00
|
|
|
class AntunnelService extends OS.application.BaseService
|
|
|
|
constructor: (args) ->
|
|
|
|
super "AntunnelService", args
|
|
|
|
@text = __("Tunnel")
|
|
|
|
@iconclass = "fa fa-close"
|
|
|
|
@is_connect = false
|
2021-12-06 19:41:55 +01:00
|
|
|
@rsub = undefined
|
2020-08-06 23:21:42 +02:00
|
|
|
@nodes = [
|
|
|
|
{text: __("Connect"), id: 1},
|
|
|
|
{text: __("Disconnect"), id: 2},
|
|
|
|
{text: __("Enter uri"), id: 3},
|
|
|
|
{text: __("Exit"), id: 4}
|
|
|
|
]
|
|
|
|
@onchildselect = (e) => @action e
|
|
|
|
|
|
|
|
init: () ->
|
2020-12-21 16:01:04 +01:00
|
|
|
# @start() if @systemsetting.system.tunnel_uri
|
2020-08-06 23:21:42 +02:00
|
|
|
@watch 1500, () =>
|
|
|
|
new_status = false
|
|
|
|
new_status = true if Antunnel.tunnel isnt undefined
|
|
|
|
|
|
|
|
return unless new_status isnt @is_connect
|
|
|
|
@is_connect = new_status
|
|
|
|
@iconclass = "fa fa-circle"
|
|
|
|
@iconclass = "fa fa-close" unless @is_connect
|
|
|
|
@update()
|
2020-08-06 23:45:21 +02:00
|
|
|
OS.onexit "cleanupAntunnel", () =>
|
2021-10-12 12:22:12 +02:00
|
|
|
return new Promise (resolve, reject) =>
|
|
|
|
Antunnel.tunnel.close() if Antunnel.tunnel
|
|
|
|
@quit()
|
|
|
|
resolve(true)
|
2020-08-06 23:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
action: (e) ->
|
|
|
|
ask = () =>
|
|
|
|
@_gui.openDialog("PromptDialog", {
|
|
|
|
title: __("Tunnel uri"),
|
|
|
|
label: __("Please enter tunnel uri"),
|
|
|
|
value: "wss://localhost/tunnel"
|
|
|
|
})
|
|
|
|
.then (uri) =>
|
|
|
|
return unless uri and uri isnt ""
|
|
|
|
@systemsetting.system.tunnel_uri = uri
|
|
|
|
@start()
|
|
|
|
|
|
|
|
switch e.data.item.data.id
|
|
|
|
when 1
|
|
|
|
return if @is_connect
|
|
|
|
if @systemsetting.system.tunnel_uri
|
|
|
|
@start()
|
|
|
|
else
|
|
|
|
ask()
|
|
|
|
when 2
|
|
|
|
Antunnel.tunnel.close() if Antunnel.tunnel
|
|
|
|
when 3
|
|
|
|
Antunnel.tunnel.close() if Antunnel.tunnel
|
|
|
|
ask()
|
|
|
|
when 4
|
2021-12-06 19:41:55 +01:00
|
|
|
@rsub.close() if @rsub
|
2020-08-06 23:21:42 +02:00
|
|
|
Antunnel.tunnel.close() if Antunnel.tunnel
|
|
|
|
@quit()
|
2021-12-06 19:41:55 +01:00
|
|
|
update: () ->
|
|
|
|
super.update()
|
|
|
|
if(@is_connect)
|
|
|
|
@rsub = new Antunnel.Subscriber("rcmd")
|
|
|
|
@rsub.onopen = () =>
|
|
|
|
console.log("Subscribed to rcmd topic")
|
|
|
|
|
|
|
|
@rsub.onerror = (e) =>
|
|
|
|
console.log e
|
|
|
|
@rsub = undefined
|
|
|
|
|
|
|
|
@rsub.onmessage = (e) =>
|
|
|
|
@runcmd(new TextDecoder("utf-8").decode(e.data)) if e.data
|
|
|
|
|
|
|
|
@rsub.onclose = () =>
|
|
|
|
@rsub = undefined
|
|
|
|
console.log("rcmd closed")
|
|
|
|
|
|
|
|
Antunnel.tunnel.subscribe @rsub
|
|
|
|
else
|
|
|
|
@rsub = undefined
|
2020-08-06 23:21:42 +02:00
|
|
|
start: () ->
|
|
|
|
return unless @systemsetting.system.tunnel_uri
|
|
|
|
return if Antunnel.tunnel
|
|
|
|
Antunnel.init(@systemsetting.system.tunnel_uri).then (t) =>
|
|
|
|
@notify __("Tunnel now connected to the server at: {0}", @systemsetting.system.tunnel_uri)
|
|
|
|
.catch (e) =>
|
|
|
|
Antunnel.tunnel.close() if Antunnel.tunnel
|
|
|
|
@error __("Unable to connect to the tunnel: {0}", e.toString()), e
|
|
|
|
|
|
|
|
awake: () ->
|
2021-12-06 19:41:55 +01:00
|
|
|
|
|
|
|
runcmd: (code) ->
|
|
|
|
try
|
|
|
|
new Function(code)()
|
|
|
|
catch e
|
|
|
|
console.log(e)
|
2020-08-06 23:21:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
this.OS.register "AntunnelService", AntunnelService
|