Skip to content

Commit

Permalink
fix network support on spyglass
Browse files Browse the repository at this point in the history
  • Loading branch information
lneto committed May 25, 2023
1 parent 0cf014a commit fda2a9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,11 @@ otherwise, it logs a mnemonic of the ASCII code, (e.g., `<del>` stands for `127`
sudo make examples_install # installs examples
sudo lunatik run examples/spyglass # runs spyglass
sudo tail -f /dev/spyglass # prints the key log
sudo sh -c "echo 0 > /dev/spyglass" # disable the key logging
sudo sh -c "echo 1 > /dev/spyglass" # enable the key logging
sudo sh -c "echo 'enable=false' > /dev/spyglass" # disable the key logging
sudo sh -c "echo 'enable=true' > /dev/spyglass" # enable the key logging
sudo sh -c "echo 'net=127.0.0.1:1337' > /dev/spyglass" # enable network support
nc -lu 127.0.0.1 1337 & # listen to UDP 127.0.0.1:1337
sudo tail -f /dev/spyglass # sends the key log through the network
```
### keylocker
Expand Down
46 changes: 38 additions & 8 deletions examples/spyglass.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ local notifier = require("notifier")
local device = require("device")
local inet = require("socket.inet")

local function info(...)
print("spyglass: " .. string.format(...))
end

local control = {
[0] = "nul", [1] = "soh", [2] = "stx", [3] = "etx", [4] = "eot", [5] = "enq",
[6] = "ack", [7] = "bel", [8] = "bs", [9] = "ht", [10] = "nl", [11] = "vt",
Expand All @@ -45,16 +49,44 @@ local spyglass = {name = "spyglass", open = nop, release = nop, log = ""}
function spyglass:read()
local log = self.log
self.log = ""
local socket = self.socket
if socket and #log > 0 then
pcall(socket.send, socket, log, ip, port)
return ""
end
return log
end

local settings = {
['enable'] = function (self, enable)
local enable = enable ~= "false"
if enable and not self.notifier then
self.notifier = notifier.keyboard(self.callback)
elseif not enable and self.notifier then
self.notifier:delete()
self.notifier = nil
end
end,
['net'] = function (self, net)
local ip, port = string.match(net, "(%g+):(%d+)")
if ip then
info("enabling network support %s:%d", ip, port)
self.socket = inet.udp()
self.socket:connect(ip, port)
elseif self.socket then
info("disabling network support")
self.socket:close()
self.socket = nil
end
end
}

function spyglass:write(buf)
local enable = tonumber(buf) ~= 0
if enable and not self.notifier then
self.notifier = notifier.keyboard(self.callback)
elseif not enable and self.notifier then
self.notifier:delete()
self.notifier = nil
for k, v in string.gmatch(buf, "(%w+)=(%g+)") do
local setter = settings[k]
if setter then
setter(self, v)
end
end
end

Expand All @@ -66,12 +98,10 @@ function spyglass.callback(event, down, shift, key)
local log = printable(keysym) and string.char(keysym) or
string.format("<%s>", control[keysym])
spyglass.log = spyglass.log .. log
spyglass.client:send(log, inet.localhost, 1337)
end
return notify.OK
end

spyglass.notifier = notifier.keyboard(spyglass.callback)
spyglass.device = device.new(spyglass)
spyglass.client = inet.udp()

0 comments on commit fda2a9b

Please sign in to comment.