diff --git a/sys/apis/socket.lua b/sys/apis/socket.lua index a2d2d7a..75ae3a3 100644 --- a/sys/apis/socket.lua +++ b/sys/apis/socket.lua @@ -131,23 +131,21 @@ function Socket.connect(host, port) os.cancelTimer(timerId) if msg.type == 'CONN' then - 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 + socket:close() 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 diff --git a/sys/apis/util.lua b/sys/apis/util.lua index 2150840..658f875 100644 --- a/sys/apis/util.lua +++ b/sys/apis/util.lua @@ -119,14 +119,8 @@ function Util.signum(num) end end -function Util.clamp(lo, num, hi) - if num <= lo then - return lo - elseif num >= hi then - return hi - else - return num - end +function Util.clamp(num, low, high) + return num < low and low or num > high and high or num end -- http://lua-users.org/wiki/SimpleRound diff --git a/sys/apps/Network.lua b/sys/apps/Network.lua index 517c98f..5e17d46 100644 --- a/sys/apps/Network.lua +++ b/sys/apps/Network.lua @@ -128,6 +128,13 @@ local function sendCommand(host, command) end end +function page.ports:eventHandler(event) + if event.type == 'grid_select' then + shell.openForegroundTab('sniff ' .. event.selected.port) + end + return UI.SlideOut.eventHandler(self, event) +end + function page.ports.grid:update() local function findConnection(port) for _,socket in pairs(_G.transport.sockets) do diff --git a/sys/apps/sniff.lua b/sys/apps/sniff.lua new file mode 100644 index 0000000..ada3d77 --- /dev/null +++ b/sys/apps/sniff.lua @@ -0,0 +1,69 @@ +local Event = require('event') +local Terminal = require('terminal') +local Util = require('util') + +local colors = _G.colors +local modem = _G.device.wireless_modem +local term = _G.term +local textutils = _G.textutils + +local terminal = Terminal.window(term.current()) +terminal.setMaxScroll(300) +local oldTerm = term.redirect(terminal) + +local function syntax() + error('Syntax: sniff [port]') +end + +local port = ({ ... })[1] or syntax() +port = tonumber(port) or syntax() + +Event.on('modem_message', + function(_, _, dport, _, data, _) + if dport == port then + terminal.scrollBottom() + terminal.setTextColor(colors.white) + print(textutils.serialize(data)) + end + end) + +Event.on('mouse_scroll', function(_, direction) + if direction == -1 then + terminal.scrollUp() + else + terminal.scrollDown() + end +end) + +local function sniffer(_, _, data) + terminal.scrollBottom() + terminal.setTextColor(colors.yellow) + local ot = term.redirect(terminal) + print(textutils.serialize(data)) + term.redirect(ot) +end + +local socket = _G.transport.sockets[port] +if socket then + if not socket.sniffers then + socket.sniffers = { modem.transmit } + socket.transmit = function(...) + for _,v in pairs(socket.sniffers) do + v(...) + end + end + end + table.insert(socket.sniffers, sniffer) +end + +local s, m = pcall(Event.pullEvents) + +if socket then + Util.removeByValue(socket.sniffers, sniffer) +end + +term.redirect(oldTerm) + +if not s and m then + error(m) +end diff --git a/sys/network/proxy.lua b/sys/network/proxy.lua index 46ec14b..6aaddef 100644 --- a/sys/network/proxy.lua +++ b/sys/network/proxy.lua @@ -32,7 +32,7 @@ Event.addRoutine(function() print('proxy: lost connection from ' .. socket.dhost) break end - socket:write({ proxy[data.fn](table.unpack(data.args)) }) + socket:write({ proxy[data[1]](table.unpack(data, 2)) }) end end end)