From 05c99b583a1b5b10e45da7f5a4dd9cd6b6b14f3e Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Mon, 9 Oct 2017 13:08:38 -0400 Subject: [PATCH] friendlier networking + adding tabs --- sys/apis/socket.lua | 56 ++++++++++++++++++++++++--------------- sys/apis/ui.lua | 17 ++++++++++-- sys/apps/Help.lua | 9 +++---- sys/apps/Network.lua | 62 ++++++++++++++++++++++++++++++++++++++++---- sys/apps/System.lua | 12 +++++++-- sys/apps/telnet.lua | 4 +-- sys/apps/vnc.lua | 4 +-- 7 files changed, 124 insertions(+), 40 deletions(-) diff --git a/sys/apis/socket.lua b/sys/apis/socket.lua index 4be73cf..ab71673 100644 --- a/sys/apis/socket.lua +++ b/sys/apis/socket.lua @@ -5,13 +5,13 @@ local Util = require('util') local device = _G.device local os = _G.os -local transport = _G.transport +--local transport = _G.transport local socketClass = { } function socketClass:read(timeout) - local data, distance = transport.read(self) + local data, distance = _G.transport.read(self) if data then return data, distance end @@ -27,7 +27,7 @@ function socketClass:read(timeout) local e, id = os.pullEvent() if e == 'transport_' .. self.sport then - data, distance = transport.read(self) + data, distance = _G.transport.read(self) if data then os.cancelTimer(timerId) return data, distance @@ -44,7 +44,7 @@ end function socketClass:write(data) if self.connected then - transport.write(self, { + _G.transport.write(self, { type = 'DATA', seq = self.wseq, data = data, @@ -55,7 +55,7 @@ end function socketClass:ping() if self.connected then - transport.write(self, { + _G.transport.write(self, { type = 'PING', seq = self.wseq, }) @@ -72,7 +72,7 @@ function socketClass:close() self.connected = false end device.wireless_modem.close(self.sport) - transport.close(self) + _G.transport.close(self) end local Socket = { } @@ -126,23 +126,29 @@ function Socket.connect(host, port) local e, id, sport, dport, msg = os.pullEvent() if e == 'modem_message' and sport == socket.sport and - msg.dhost == socket.shost and - msg.type == 'CONN' then - - socket.dport = dport - socket.connected = true - Logger.log('socket', 'connection established to %d %d->%d', - host, socket.sport, socket.dport) + msg.dhost == socket.shost then os.cancelTimer(timerId) - transport.open(socket) + if msg.type == 'CONN' then - return socket + socket.dport = dport + socket.connected = true + Logger.log('socket', 'connection established to %d %d->%d', + host, socket.sport, socket.dport) + + _G.transport.open(socket) + + return socket + elseif msg.type == 'REJE' then + return false, 'Password not set on target or not trusted' + end end until e == 'timer' and id == timerId socket:close() + + return false, 'Connection timed out' end local function trusted(msg, port) @@ -176,13 +182,14 @@ function Socket.server(port) msg.dhost == os.getComputerID() and msg.type == 'OPEN' then + local socket = newSocket(msg.shost == os.getComputerID()) + socket.dport = dport + socket.dhost = msg.shost + socket.wseq = msg.wseq + socket.rseq = msg.rseq + if trusted(msg, port) then - local socket = newSocket(msg.shost == os.getComputerID()) - socket.dport = dport - socket.dhost = msg.shost socket.connected = true - socket.wseq = msg.wseq - socket.rseq = msg.rseq socket.transmit(socket.dport, socket.sport, { type = 'CONN', dhost = socket.dhost, @@ -190,9 +197,16 @@ function Socket.server(port) }) Logger.log('socket', 'Connection established %d->%d', socket.sport, socket.dport) - transport.open(socket) + _G.transport.open(socket) return socket end + + socket.transmit(socket.dport, socket.sport, { + type = 'REJE', + dhost = socket.dhost, + shost = socket.shost, + }) + socket:close() end end end diff --git a/sys/apis/ui.lua b/sys/apis/ui.lua index b468d0f..2f1ee9f 100644 --- a/sys/apis/ui.lua +++ b/sys/apis/ui.lua @@ -2045,7 +2045,7 @@ function UI.MenuBar:init(args) end self:addButtons(self.buttons) - if self.showBackButton then + if self.showBackButton then -- need to remove table.insert(self.children, UI.MenuItem({ x = UI.term.width - 2, width = 3, @@ -2086,7 +2086,9 @@ function UI.MenuBar:addButtons(buttons) end end end - self:initChildren() + if self.parent then + self:initChildren() + end end function UI.MenuBar:getActive(menuItem) @@ -2129,6 +2131,13 @@ function UI.DropMenuItem:init(args) UI.Button.init(self, UI:getDefaults(UI.DropMenuItem, args)) end +function UI.DropMenuItem:eventHandler(event) + if event.type == 'button_activate' then + self.parent:hide() + end + return UI.Button.eventHandler(self, event) +end + --[[-- DropMenu --]]-- UI.DropMenu = class(UI.MenuBar) UI.DropMenu.defaults = { @@ -3028,6 +3037,10 @@ function UI.TextArea:setText(text) self:draw() end +function UI.TextArea:focus() + -- allow keyboard scrolling +end + function UI.TextArea:draw() self:clear() self:setCursorPos(1, 1) diff --git a/sys/apps/Help.lua b/sys/apps/Help.lua index f585f0e..eaccfd7 100644 --- a/sys/apps/Help.lua +++ b/sys/apps/Help.lua @@ -3,8 +3,9 @@ _G.requireInjector() local UI = require('ui') local Util = require('util') -local colors = _G.colors -local help = _G.help +local colors = _G.colors +local help = _G.help +local multishell = _ENV.multishell multishell.setTitle(multishell.getCurrent(), 'Help') UI:configure('Help', ...) @@ -55,10 +56,6 @@ local topicPage = UI.Page { }, } -function topicPage.helpText:focus() - -- let the help text get focused so we consume key strokes -end - function topicPage:eventHandler(event) if event.type == 'back' then UI:setPreviousPage() diff --git a/sys/apps/Network.lua b/sys/apps/Network.lua index cb20acd..a069c51 100644 --- a/sys/apps/Network.lua +++ b/sys/apps/Network.lua @@ -28,10 +28,17 @@ end local page = UI.Page { menuBar = UI.MenuBar { buttons = { - { text = 'Telnet', event = 'telnet' }, - { text = 'VNC', event = 'vnc' }, - { text = 'Trust', event = 'trust' }, - { text = 'Reboot', event = 'reboot' }, + { text = 'Connect', dropdown = { + { text = 'Telnet t', event = 'telnet' }, + { text = 'VNC v', event = 'vnc' }, + UI.MenuBar.spacer, + { text = 'Reboot r', event = 'reboot' }, + } }, + { text = 'Trust', dropdown = { + { text = 'Establish', event = 'trust' }, + { text = 'Remove', event = 'untrust' }, + } }, + { text = 'Help', event = 'help' }, }, }, grid = UI.ScrollingGrid { @@ -43,6 +50,9 @@ local page = UI.Page { }, notification = UI.Notification { }, accelerators = { + t = 'telnet', + v = 'vnc', + r = 'reboot', q = 'quit', c = 'clear', }, @@ -85,20 +95,62 @@ function page:eventHandler(event) args = { t.id }, title = t.label, }) + elseif event.type == 'clear' then + Util.clear(network) + page.grid:update() + page.grid:draw() + elseif event.type == 'trust' then shell.openForegroundTab('trust ' .. t.id) + + elseif event.type == 'untrust' then + local trustList = Util.readTable('usr/.known_hosts') or { } + trustList[t.id] = nil + Util.writeTable('usr/.known_hosts', trustList) + elseif event.type == 'reboot' then sendCommand(t.id, 'reboot') + elseif event.type == 'shutdown' then sendCommand(t.id, 'shutdown') end end - if event.type == 'quit' then + if event.type == 'help' then + UI:setPage(UI.Dialog { + title = 'Network Help', + height = 10, + backgroundColor = colors.white, + text = UI.TextArea { + x = 2, y = 2, + backgroundColor = colors.white, + value = [[ +In order to connect to another computer: + + 1. The target computer must have a password set (run 'password' from the shell prompt). + 2. From this computer, click trust and enter the password for that computer. + +This only needs to be done once. + ]], + }, + accelerators = { + q = 'cancel', + } + }) + elseif event.type == 'quit' then Event.exitPullEvents() end UI.Page.eventHandler(self, event) end +function page.menuBar:getActive(menuItem) + local t = page.grid:getSelected() + if menuItem.event == 'untrust' then + local trustList = Util.readTable('usr/.known_hosts') or { } + return t and trustList[t.id] + end + return not not t +end + function page.grid:getRowTextColor(row, selected) if not row.active then return colors.orange diff --git a/sys/apps/System.lua b/sys/apps/System.lua index b21826c..906dcba 100644 --- a/sys/apps/System.lua +++ b/sys/apps/System.lua @@ -13,6 +13,14 @@ local shell = _ENV.shell multishell.setTitle(multishell.getCurrent(), 'System') UI:configure('System', ...) +local mcVersion = _G._MC_VERSION or 'unknown' +if _G._HOST then + local version = _G._HOST:match('%S+ %S+ %((%S.+)%)') + if version then + mcVersion = version:match('Minecraft (%S+)') or version + end +end + local env = { path = shell.path(), aliases = shell.aliases(), @@ -89,7 +97,7 @@ local systemPage = UI.Page { { name = '', value = '' }, { name = 'CC version', value = Util.getVersion() }, { name = 'Lua version', value = _VERSION }, - { name = 'MC version', value = _G._MC_VERSION or 'unknown' }, + { name = 'MC version', value = mcVersion }, { name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) }, { name = 'Computer ID', value = tostring(os.getComputerID()) }, { name = 'Day', value = tostring(os.day()) }, @@ -123,7 +131,7 @@ if settings then grid = UI.Grid { y = 1, values = values, - --autospace = true, + autospace = true, sortColumn = 'name', columns = { { heading = 'Setting', key = 'name' }, diff --git a/sys/apps/telnet.lua b/sys/apps/telnet.lua index ce83d3a..20702ac 100644 --- a/sys/apps/telnet.lua +++ b/sys/apps/telnet.lua @@ -23,10 +23,10 @@ if not remoteId then end print('connecting...') -local socket = Socket.connect(remoteId, 23) +local socket, msg = Socket.connect(remoteId, 23) if not socket then - error('Unable to connect to ' .. remoteId .. ' on port 23') + error(msg) end local ct = Util.shallowCopy(term.current()) diff --git a/sys/apps/vnc.lua b/sys/apps/vnc.lua index 2c00df1..df5acae 100644 --- a/sys/apps/vnc.lua +++ b/sys/apps/vnc.lua @@ -25,10 +25,10 @@ end multishell.setTitle(multishell.getCurrent(), 'VNC-' .. remoteId) print('connecting...') -local socket = Socket.connect(remoteId, 5900) +local socket, msg = Socket.connect(remoteId, 5900) if not socket then - error('Unable to connect to ' .. remoteId .. ' on port 5900') + error(msg) end local function writeTermInfo()