From f7ba9009305f0e838611211f8717f3d0743c6e86 Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 13:22:57 +0400 Subject: [PATCH 1/9] genotp.lua changes Make genotp more clearly state that the password can be used once but allows permanent access (sorry for not making this clear earlier.) Use a slightly longer password, and allow uppercase characters except for some that could be ambiguous --- sys/apps/genotp.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/sys/apps/genotp.lua b/sys/apps/genotp.lua index cdaf6eb3..e6bd5a15 100644 --- a/sys/apps/genotp.lua +++ b/sys/apps/genotp.lua @@ -1,14 +1,22 @@ local SHA = require("opus.crypto.sha2") -local acceptableCharacters = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"} +local acceptableCharacters = {} +for c = 0, 127 do + local char = string.char(c) + -- exclude potentially ambiguous characters + if char:match("[1-9a-zA-Z]") and char:match("[^OIl]") end + table.insert(acceptableCharacters, char) + end +end local acceptableCharactersLen = #acceptableCharacters - local password = "" -for _i = 1, 8 do +for i = 1, 10 do password = password .. acceptableCharacters[math.random(acceptableCharactersLen)] end os.queueEvent("set_otp", SHA.compute(password)) -print("Your one-time password is: " .. password) \ No newline at end of file +print("This allows one other device to permanently gain access to this device.") +print("Use the trust settings in System to revert this.") +print("Your one-time password is: " .. password) From 8fbcc7b8bca27b5fe137826af90d67629cd82dde Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 18:23:02 +0400 Subject: [PATCH 2/9] Sanitize label in samba.lua Prevent labels from having .., /, *, control characters, or quotes (this generally messes things up) and limit them to a reasonable length. We might possibly also want to do this in snmp.lua, I'm not sure if that will break things though --- sys/apps/network/samba.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sys/apps/network/samba.lua b/sys/apps/network/samba.lua index 50cbfd0a..0cc6121b 100644 --- a/sys/apps/network/samba.lua +++ b/sys/apps/network/samba.lua @@ -59,6 +59,10 @@ local function sambaConnection(socket) print('samba: Connection closed') end +local function sanitizeLabel(computer) + return (computer.id.."_"..computer.label:gsub("[%c%.\"'/%*]", "")):sub(64) +end + Event.addRoutine(function() print('samba: listening on port 139') @@ -79,10 +83,10 @@ Event.addRoutine(function() end) Event.on('network_attach', function(_, computer) - fs.mount(fs.combine('network', computer.label), 'netfs', computer.id) + fs.mount(fs.combine('network', sanitizeLabel(computer)), 'netfs', computer.id) end) Event.on('network_detach', function(_, computer) - print('samba: detaching ' .. computer.label) - fs.unmount(fs.combine('network', computer.label)) + print('samba: detaching ' .. sanitizeLabel(computer)) + fs.unmount(fs.combine('network', sanitizeLabel(computer))) end) From f26f443b9d057dda2f95a5f89971e9703206ebdf Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 18:43:49 +0400 Subject: [PATCH 3/9] Increase discovery message interval, distribute messages Previously on large server(s), there was an issue where because chunkloaded computers all started up at once, so they all had the same discovery message sending timer. This prevents that by starting each off with a random offset. Also increases the interval for sending messages from 15 s to 30 s, so that messages (which are the same, a lot of the time) get sent less often. --- sys/apps/network/snmp.lua | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sys/apps/network/snmp.lua b/sys/apps/network/snmp.lua index 2c48ee15..e5665649 100644 --- a/sys/apps/network/snmp.lua +++ b/sys/apps/network/snmp.lua @@ -152,7 +152,7 @@ local function getSlots() end local function sendInfo() - if os.clock() - infoTimer >= 1 then -- don't flood + if os.clock() - infoTimer >= 5 then -- don't flood infoTimer = os.clock() info.label = os.getComputerLabel() info.uptime = math.floor(os.clock()) @@ -194,16 +194,25 @@ local function sendInfo() end end --- every 10 seconds, send out this computer's info -Event.onInterval(10, function() - sendInfo() +local function cleanNetwork() for _,c in pairs(_G.network) do local elapsed = os.clock()-c.timestamp - if c.active and elapsed > 15 then + if c.active and elapsed > 90 then c.active = false os.queueEvent('network_detach', c) end end +end + +-- every 60 seconds, send out this computer's info +-- send with offset so that messages are evenly distributed and do not all come at once +Event.onTimeout(math.random() * 60, function() + sendInfo() + cleanNetwork() + Event.onInterval(60, function() + sendInfo() + cleanNetwork() + end) end) Event.on('turtle_response', function() @@ -213,4 +222,5 @@ Event.on('turtle_response', function() end end) -Event.onTimeout(1, sendInfo) +-- send info early so that computers show soon after booting +Event.onTimeout(math.random() * 4 + 1, sendInfo) From 76b310d87390f9e764b72fcc711a5849ad53e4e9 Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 19:22:20 +0400 Subject: [PATCH 4/9] oops --- sys/apps/genotp.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/apps/genotp.lua b/sys/apps/genotp.lua index e6bd5a15..9c1abb73 100644 --- a/sys/apps/genotp.lua +++ b/sys/apps/genotp.lua @@ -4,7 +4,7 @@ local acceptableCharacters = {} for c = 0, 127 do local char = string.char(c) -- exclude potentially ambiguous characters - if char:match("[1-9a-zA-Z]") and char:match("[^OIl]") end + if char:match("[1-9a-zA-Z]") and char:match("[^OIl]") then table.insert(acceptableCharacters, char) end end From 0a42551ab760b022589b4e945e0892f2919f9f2d Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 19:22:38 +0400 Subject: [PATCH 5/9] actually set it to 30 --- sys/apps/network/snmp.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sys/apps/network/snmp.lua b/sys/apps/network/snmp.lua index e5665649..39724411 100644 --- a/sys/apps/network/snmp.lua +++ b/sys/apps/network/snmp.lua @@ -197,19 +197,19 @@ end local function cleanNetwork() for _,c in pairs(_G.network) do local elapsed = os.clock()-c.timestamp - if c.active and elapsed > 90 then + if c.active and elapsed > 50 then c.active = false os.queueEvent('network_detach', c) end end end --- every 60 seconds, send out this computer's info +-- every 30 seconds, send out this computer's info -- send with offset so that messages are evenly distributed and do not all come at once -Event.onTimeout(math.random() * 60, function() +Event.onTimeout(math.random() * 30, function() sendInfo() cleanNetwork() - Event.onInterval(60, function() + Event.onInterval(30, function() sendInfo() cleanNetwork() end) @@ -223,4 +223,4 @@ Event.on('turtle_response', function() end) -- send info early so that computers show soon after booting -Event.onTimeout(math.random() * 4 + 1, sendInfo) +Event.onTimeout(math.random() * 2 + 1, sendInfo) From 5f153721ea3ed9a3ea911989fcaf830b438d274e Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 19:26:48 +0400 Subject: [PATCH 6/9] oops --- sys/apps/network/samba.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/apps/network/samba.lua b/sys/apps/network/samba.lua index 0cc6121b..5e8d59c6 100644 --- a/sys/apps/network/samba.lua +++ b/sys/apps/network/samba.lua @@ -60,7 +60,7 @@ local function sambaConnection(socket) end local function sanitizeLabel(computer) - return (computer.id.."_"..computer.label:gsub("[%c%.\"'/%*]", "")):sub(64) + return (computer.id.."_"..computer.label:gsub("[%c%.\"'/%*]", "")):sub(1, 40) end Event.addRoutine(function() From 2597724dab4e2414aba865d37b2e5e9127591390 Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 20:05:16 +0400 Subject: [PATCH 7/9] Fix Util.getVersion --- sys/apps/Overview.lua | 2 +- sys/apps/system/label.lua | 12 ++++++------ sys/init/7.multishell.lua | 2 +- sys/modules/opus/ui.lua | 2 +- sys/modules/opus/util.lua | 19 +++++++++++-------- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/sys/apps/Overview.lua b/sys/apps/Overview.lua index 722127ec..b5edf39b 100644 --- a/sys/apps/Overview.lua +++ b/sys/apps/Overview.lua @@ -51,7 +51,7 @@ local config = { } Config.load('Overview', config) -local extSupport = Util.getVersion() >= 1.76 +local extSupport = Util.supportsExtChars() local applications = { } local buttons = { } diff --git a/sys/apps/system/label.lua b/sys/apps/system/label.lua index ba381092..61d36983 100644 --- a/sys/apps/system/label.lua +++ b/sys/apps/system/label.lua @@ -26,12 +26,12 @@ return UI.Tab { x = 2, y = 5, ex = -2, ey = -2, values = { { name = '', value = '' }, - { name = 'CC version', value = Util.getVersion() }, - { name = 'Lua version', value = _VERSION }, - { name = 'MC version', value = Util.getMinecraftVersion() }, - { name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) }, - { name = 'Computer ID', value = tostring(os.getComputerID()) }, - { name = 'Day', value = tostring(os.day()) }, + { name = 'CC version', value = ("%d.%d"):format(Util.getVersion()) }, + { name = 'Lua version', value = _VERSION }, + { name = 'MC version', value = Util.getMinecraftVersion() }, + { name = 'Disk free', value = Util.toBytes(fs.getFreeSpace('/')) }, + { name = 'Computer ID', value = tostring(os.getComputerID()) }, + { name = 'Day', value = tostring(os.day()) }, }, disableHeader = true, inactive = true, diff --git a/sys/init/7.multishell.lua b/sys/init/7.multishell.lua index 728a3802..b799584c 100644 --- a/sys/init/7.multishell.lua +++ b/sys/init/7.multishell.lua @@ -14,7 +14,7 @@ local parentTerm = _G.device.terminal local w,h = parentTerm.getSize() local overviewId local tabsDirty = false -local closeInd = Util.getVersion() >= 1.76 and '\215' or '*' +local closeInd = Util.supportsExtChars() and '\215' or '*' local multishell = { } _ENV.multishell = multishell diff --git a/sys/modules/opus/ui.lua b/sys/modules/opus/ui.lua index 8451e1d6..c5b412e8 100644 --- a/sys/modules/opus/ui.lua +++ b/sys/modules/opus/ui.lua @@ -44,7 +44,7 @@ function UI:init() tertiary = colors.gray, } } - self.extChars = Util.getVersion() >= 1.76 + self.extChars = Util.supportsExtChars() local function keyFunction(event, code, held) local ie = Input:translate(event, code, held) diff --git a/sys/modules/opus/util.lua b/sys/modules/opus/util.lua index 484231ba..fac9269e 100644 --- a/sys/modules/opus/util.lua +++ b/sys/modules/opus/util.lua @@ -170,16 +170,19 @@ function Util.print(pattern, ...) end function Util.getVersion() - local version + local versionString = _G._HOST or _G._CC_VERSION + local versionMajor, versionMinor = versionString:match("(%d+)%.(%d+)") + -- ex.: 1.89 would return 1, 89 + return versionMajor, versionMinor +end - if _G._CC_VERSION then - version = tonumber(_G._CC_VERSION:match('[%d]+%.?[%d][%d]')) - end - if not version and _G._HOST then - version = tonumber(_G._HOST:match('[%d]+%.?[%d][%d]')) - end +function Util.compareVersion(major, minor) + local currentMajor, currentMinor = Util.getVersion() + return currentMajor > major or currentMajor == major and currentMinor >= minor +end - return version or 1.7 +function Util.supportsExtChars() + return Util.compareVersion(1, 76) end function Util.getMinecraftVersion() From c247097e2011aae499cd2ecbc0e10fefb3efdb6f Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Wed, 28 Dec 2022 20:25:52 +0400 Subject: [PATCH 8/9] oops --- sys/modules/opus/util.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/modules/opus/util.lua b/sys/modules/opus/util.lua index fac9269e..9c455c6c 100644 --- a/sys/modules/opus/util.lua +++ b/sys/modules/opus/util.lua @@ -173,7 +173,7 @@ function Util.getVersion() local versionString = _G._HOST or _G._CC_VERSION local versionMajor, versionMinor = versionString:match("(%d+)%.(%d+)") -- ex.: 1.89 would return 1, 89 - return versionMajor, versionMinor + return tonumber(versionMajor), tonumber(versionMinor) end function Util.compareVersion(major, minor) From 9b502553e064635a27fff50c099151c68e29d824 Mon Sep 17 00:00:00 2001 From: Kan18 <24967425+Kan18@users.noreply.github.com> Date: Sun, 17 Dec 2023 10:14:40 -0500 Subject: [PATCH 9/9] Update json.lua --- sys/modules/opus/json.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sys/modules/opus/json.lua b/sys/modules/opus/json.lua index 04b1cd11..41d0e368 100644 --- a/sys/modules/opus/json.lua +++ b/sys/modules/opus/json.lua @@ -39,7 +39,8 @@ if register_global_module_table then _G[global_module_name] = json end -local _ENV = nil -- blocking globals in Lua 5.2 +-- this was incompatible because we use fs later +--local _ENV = nil -- blocking globals in Lua 5.2 pcall (function() -- Enable access to blocked metatables.