network sniffer

This commit is contained in:
kepler155c@gmail.com 2019-02-23 18:36:17 -05:00
parent 1c859029f1
commit 8ae3b33374
5 changed files with 81 additions and 13 deletions

View File

@ -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

View File

@ -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

View File

@ -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

69
sys/apps/sniff.lua Normal file
View File

@ -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

View File

@ -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)