ADD gateway

This commit is contained in:
Nathan Dwarshuis 2024-02-23 20:20:02 -05:00
parent 34967db7ee
commit 8e973c0479
1 changed files with 39 additions and 3 deletions

View File

@ -2,17 +2,52 @@ local i_o = require 'i_o'
local pure = require 'pure'
return function(main_state, config, common, width, point)
local IP_EXE = 'ip'
local TEST_IP = '104.18.114.97' -- ipv4.icanhazip.com
local IP_REGEX = 'via ([^ ]+)'
local DEV_REGEX = 'dev ([^ ]+)'
local ERR = 'Error'
local text_spacing = config.geometry.text_spacing
local __string_match = string.match
i_o.assert_exe_exists(IP_EXE)
-- NOTE conky has a builtin gateway function but it will only output
-- whatever the output of 'ip route' is, which won't pick up changes that
-- some VPNs (like tailscale) will make to the rest of the routing table.
-- More reliable way to test the "default route" is to ask the routing table
-- how it would route to a public IP, which is what we do here.
local ip_cmd = IP_EXE..' route get '..TEST_IP
local get_gateway = function()
local ip_glob = i_o.execute_cmd(ip_cmd)
if ip_glob == nil then
return ERR
else
local ip = __string_match(ip_glob, IP_REGEX)
if ip ~= nil then
return ip
end
local dev = __string_match(ip_glob, DEV_REGEX)
if dev ~= nil then
return dev
end
return ERR
end
end
local mk_stats = function(y)
local obj = common.make_text_rows(
point.x,
y,
width,
text_spacing,
{'Kernel', 'Uptime', 'Last Upgrade', 'Last Sync'}
-- {'Kernel', 'Uptime', 'Last Upgrade', 'Last Sync', 'Gateway'}
{'Kernel', 'Uptime', 'Last Upgrade', 'Gateway'}
)
-- just update this once
common.text_rows_set(obj, 1, i_o.conky('$kernel'))
@ -25,11 +60,12 @@ return function(main_state, config, common, width, point)
"^%d+%s+([^%s]+)%s+([^%s]+).*"
)
common.text_rows_set(obj, 3, last_update)
common.text_rows_set(obj, 4, last_sync)
-- common.text_rows_set(obj, 4, last_sync)
else
common.text_rows_set(obj, 3, 'N/A')
common.text_rows_set(obj, 4, 'N/A')
-- common.text_rows_set(obj, 4, 'N/A')
end
common.text_rows_set(obj, 4, get_gateway())
end
local static = pure.partial(common.text_rows_draw_static, obj)
local dynamic = pure.partial(common.text_rows_draw_dynamic, obj)