Remote assistance improvements

- Add status page with service log
- Use a different service name to avoid conflicts with other n2n instances
- Split key into community and key, alphanumeric
- Add connection script download
This commit is contained in:
emanuele-f 2018-11-28 17:56:45 +01:00
parent d1f353a4c5
commit de5a58ecf3
9 changed files with 184 additions and 44 deletions

View file

@ -8,7 +8,9 @@ require "lua_utils"
local os_utils = require("os_utils")
local SERVICE_NAME = "n2n"
local N2N_INTERFACE_NAME = "n2n_assistance"
local DEVICE_IP = "192.168.166.1"
local ASSISTANT_IP = "192.168.166.10"
local SUPERNODE_ADDRESS = "dns.ntop.org:7777"
local CONF_DIR = dirs.workingdir.."/n2n"
local CONF_FILE = CONF_DIR .. "/edge.conf"
@ -20,7 +22,7 @@ local remote_assistance = {}
-- ########################################################
function remote_assistance.checkAvailable()
ntop.setCache(IS_AVAILABLE_KEY, ternary(os_utils.hasService(SERVICE_NAME), "1", "0"))
ntop.setCache(IS_AVAILABLE_KEY, ternary(os_utils.hasService(SERVICE_NAME, N2N_INTERFACE_NAME), "1", "0"))
end
-- ########################################################
@ -43,9 +45,22 @@ end
-- ########################################################
function remote_assistance.createConfig(community, key)
local function getUidGid()
local prefs = ntop.getPrefs()
if not isEmptyString(prefs.user) then
-- uid=999(ntopng) gid=999(ntopng) groups=999(ntopng)
local res = os_utils.execWithOutput("id " .. prefs.user) or ""
local uid = res:gmatch("uid=(%d+)")()
local gid = res:gmatch("gid=(%d+)")()
return uid, gid
end
end
-- ########################################################
function remote_assistance.createConfig(community, key)
if not ntop.mkdir(CONF_DIR) then
return false
end
@ -56,21 +71,16 @@ function remote_assistance.createConfig(community, key)
return false
end
f:write("-d=n2n0\n")
f:write("-d=".. N2N_INTERFACE_NAME .."\n")
f:write("-l=".. SUPERNODE_ADDRESS .."\n")
f:write("-c=".. community .."\n")
f:write("-k=".. key .."\n")
if not isEmptyString(prefs.user) then
-- uid=999(ntopng) gid=999(ntopng) groups=999(ntopng)
local res = os_utils.execWithOutput("id " .. prefs.user) or ""
local uid = res:gmatch("uid=(%d+)")()
local gid = res:gmatch("gid=(%d+)")()
local uid, gid = getUidGid()
if((uid ~= nil) and (gid ~= nil)) then
f:write("-u=".. uid .."\n");
f:write("-g=".. gid .."\n");
end
if((uid ~= nil) and (gid ~= nil)) then
f:write("-u=".. uid .."\n");
f:write("-g=".. gid .."\n");
end
f:write("-a=".. DEVICE_IP .."\n")
@ -91,16 +101,22 @@ end
function remote_assistance.enableAndStart()
ntop.setPref("ntopng.prefs.remote_assistance.enabled", "1")
ntop.setPref("ntopng.prefs.remote_assistance.expires_on", tostring(os.time() + REMOTE_ASSISTANCE_EXPIRATION))
os_utils.enableService(SERVICE_NAME)
return os_utils.restartService(SERVICE_NAME)
os_utils.enableService(SERVICE_NAME, N2N_INTERFACE_NAME)
return os_utils.restartService(SERVICE_NAME, N2N_INTERFACE_NAME)
end
-- ########################################################
function remote_assistance.disableAndStop()
ntop.delCache("ntopng.prefs.remote_assistance.enabled")
os_utils.disableService(SERVICE_NAME)
return os_utils.stopService(SERVICE_NAME)
os_utils.disableService(SERVICE_NAME, N2N_INTERFACE_NAME)
return os_utils.stopService(SERVICE_NAME, N2N_INTERFACE_NAME)
end
-- ########################################################
function remote_assistance.restart()
return os_utils.restartService(SERVICE_NAME, N2N_INTERFACE_NAME)
end
-- ########################################################
@ -119,13 +135,46 @@ end
-- ########################################################
function remote_assistance.getStatus()
return os_utils.serviceStatus(SERVICE_NAME)
return os_utils.serviceStatus(SERVICE_NAME, N2N_INTERFACE_NAME)
end
-- ########################################################
--! @brief Return the log trace of the remote assistance service (n2n)
--! @param rows the number of lines to return
--| @note lines are retuned in reverse order (most recent line first)
--! @return the log trace
function remote_assistance.log(rows)
return os_utils.ntopctlCmd(SERVICE_NAME, "log", N2N_INTERFACE_NAME, "|tail -n", rows, "|tac")
end
-- ########################################################
function remote_assistance.getInterfaceName()
return N2N_INTERFACE_NAME
end
-- ########################################################
function remote_assistance.getIpAddress()
return DEVICE_IP
end
-- ########################################################
function remote_assistance.getConnectionCommand()
local community = ntop.getPref("ntopng.prefs.remote_assistance.community")
local key = ntop.getPref("ntopng.prefs.remote_assistance.key")
-- sudo "n2n " -d $N2N_IFACE -c $N2N_COMMUNITY -k $N2N_KEY -u `id -u` -g `id -g` -a $MY_IP -f -l $N2N_SUPERNODE
local cmd = {"sudo edge -d ", N2N_INTERFACE_NAME, " -c ", community, " -k ", key, " -u `id -u` -g `id -g` -a ", ASSISTANT_IP, " -f -l ", SUPERNODE_ADDRESS}
return table.concat(cmd)
end
-- ########################################################
function remote_assistance.statusLabel()
local rv = os_utils.serviceStatus(SERVICE_NAME)
local rv = os_utils.serviceStatus(SERVICE_NAME, N2N_INTERFACE_NAME)
local color
local status