SSH server: redesign menu; add to dispatcher (#7765)

pull/7785/head
hius07 3 years ago committed by GitHub
parent 2df54251a7
commit e66d294bce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,7 @@
local BD = require("ui/bidi") local BD = require("ui/bidi")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local Device = require("device") local Device = require("device")
local Dispatcher = require("dispatcher")
local InfoMessage = require("ui/widget/infomessage") -- luacheck:ignore local InfoMessage = require("ui/widget/infomessage") -- luacheck:ignore
local InputDialog = require("ui/widget/inputdialog") local InputDialog = require("ui/widget/inputdialog")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
@ -27,8 +28,9 @@ local SSH = WidgetContainer:new{
function SSH:init() function SSH:init()
self.SSH_port = G_reader_settings:readSetting("SSH_port") or "2222" self.SSH_port = G_reader_settings:readSetting("SSH_port") or "2222"
self.allow_no_password = false self.allow_no_password = G_reader_settings:isTrue("SSH_allow_no_password")
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
self:onDispatcherRegisterActions()
end end
function SSH:start() function SSH:start()
@ -68,15 +70,15 @@ function SSH:start()
local info = InfoMessage:new{ local info = InfoMessage:new{
timeout = 10, timeout = 10,
-- @translators: %1 is the SSH port, %2 is the network info. -- @translators: %1 is the SSH port, %2 is the network info.
text = T(_("SSH port: %1\n%2"), text = T(_("SSH server started.\n\nSSH port: %1\n%2"),
self.SSH_port, self.SSH_port,
Device.retrieveNetworkInfo and Device:retrieveNetworkInfo() or _("Could not retrieve network info.")), Device.retrieveNetworkInfo and Device:retrieveNetworkInfo() or _("Could not retrieve network info.")),
} }
UIManager:show(info) UIManager:show(info)
else else
local info = InfoMessage:new{ local info = InfoMessage:new{
timeout = 5, icon = "notice-warning",
text = _("Error"), text = _("Failed to start SSH server."),
} }
UIManager:show(info) UIManager:show(info)
end end
@ -88,6 +90,10 @@ end
function SSH:stop() function SSH:stop()
os.execute("cat /tmp/dropbear_koreader.pid | xargs kill") os.execute("cat /tmp/dropbear_koreader.pid | xargs kill")
UIManager:show(InfoMessage:new {
text = T(_("SSH server stopped.")),
timeout = 2,
})
-- Plug the hole in the Kindle's firewall -- Plug the hole in the Kindle's firewall
if Device:isKindle() then if Device:isKindle() then
@ -100,7 +106,15 @@ function SSH:stop()
end end
end end
function SSH:show_port_dialog() function SSH:onToggleSSHServer()
if self:isRunning() then
self:stop()
else
self:start()
end
end
function SSH:show_port_dialog(touchmenu_instance)
self.port_dialog = InputDialog:new{ self.port_dialog = InputDialog:new{
title = _("Choose SSH port"), title = _("Choose SSH port"),
input = self.SSH_port, input = self.SSH_port,
@ -118,13 +132,14 @@ function SSH:show_port_dialog()
text = _("Save"), text = _("Save"),
is_enter_default = true, is_enter_default = true,
callback = function() callback = function()
local value = tonumber(self.port_dialog:getInputText()) local value = tonumber(self.port_dialog:getInputText())
if value then if value and value >= 0 then
self.SSH_port = value self.SSH_port = value
G_reader_settings:saveSetting("SSH_port", self.SSH_port) G_reader_settings:saveSetting("SSH_port", self.SSH_port)
UIManager:close(self.port_dialog) UIManager:close(self.port_dialog)
touchmenu_instance:updateItems()
end end
end end,
}, },
}, },
}, },
@ -138,54 +153,54 @@ function SSH:addToMainMenu(menu_items)
text = _("SSH server"), text = _("SSH server"),
sub_item_table = { sub_item_table = {
{ {
text = _("Start SSH server"), text = _("SSH server"),
keep_menu_open = true, keep_menu_open = true,
checked_func = function() return self:isRunning() end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
self:start() self:onToggleSSHServer()
-- sleeping might not be needed, but it gives the feeling -- sleeping might not be needed, but it gives the feeling
-- something has been done and feedback is accurate -- something has been done and feedback is accurate
ffiutil.sleep(1) ffiutil.sleep(1)
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
end, end,
enabled_func = function() return not self:isRunning() end,
}, },
{ {
text = _("Stop SSH server"), text_func = function()
return T(_("SSH port (%1)"), self.SSH_port)
end,
keep_menu_open = true, keep_menu_open = true,
enabled_func = function() return not self:isRunning() end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
self:stop() self:show_port_dialog(touchmenu_instance)
-- sleeping might not be needed, but it gives the feeling
-- something has been done and feedback is accurate
ffiutil.sleep(1)
touchmenu_instance:updateItems()
end, end,
enabled_func = function() return self:isRunning() end,
}, },
{ {
text = _("Change SSH port"), text = _("SSH public key"),
keep_menu_open = true, keep_menu_open = true,
enabled_func = function() return not self:isRunning() end, enabled_func = function() return not self:isRunning() end,
callback = function() return self:show_port_dialog() end, callback = function()
local info = InfoMessage:new{
timeout = 60,
text = T(_("Put your public SSH keys in\n%1"), BD.filepath(path.."/settings/SSH/authorized_keys")),
}
UIManager:show(info)
end,
}, },
{ {
text = _("Login without password (DANGEROUS)"), text = _("Login without password (DANGEROUS)"),
checked_func = function() return self.allow_no_password end, checked_func = function() return self.allow_no_password end,
enabled_func = function() return not self:isRunning() end, enabled_func = function() return not self:isRunning() end,
callback = function() self.allow_no_password = not self.allow_no_password end,
},
{
text = _("SSH public key"),
keep_menu_open = true,
callback = function() callback = function()
local info = InfoMessage:new{ self.allow_no_password = not self.allow_no_password
timeout = 60, G_reader_settings:flipNilOrFalse("SSH_allow_no_password")
text = T(_("Put your public SSH keys in %1"), BD.filepath(path.."/settings/SSH/authorized_keys")),
}
UIManager:show(info)
end, end,
}, },
} }
} }
end end
function SSH:onDispatcherRegisterActions()
Dispatcher:registerAction("toggle_ssh_server", { category = "none", event = "ToggleSSHServer", title = _("Toggle SSH server"), device = true})
end
return SSH return SSH

Loading…
Cancel
Save