Compare commits
2
Commits
a64c72d4fc
...
c16da21d63
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c16da21d63 | ||
|
|
b01b0d4eb5 |
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
function hash(str)
|
||||||
|
local hash = 0
|
||||||
|
for i = 1, #str do
|
||||||
|
hash = (hash * 31 + string.byte(str, i)) % 65536
|
||||||
|
end
|
||||||
|
return string.format("%04X", hash)
|
||||||
|
end
|
||||||
+39
-9
@@ -1,5 +1,6 @@
|
|||||||
local fullPath = shell.getRunningProgram()
|
local fullPath = shell.getRunningProgram()
|
||||||
local dir = fullPath:match("^(.*/)") or ""
|
local dir = fullPath:match("^(.*/)") or ""
|
||||||
|
local args = {...}
|
||||||
|
|
||||||
local config = dofile(dir..'taskguard.cfg')
|
local config = dofile(dir..'taskguard.cfg')
|
||||||
|
|
||||||
@@ -13,14 +14,7 @@ local function split(str, divider)
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local function hash(str)
|
dofile(dir..'hash.lua')
|
||||||
local hash = 0
|
|
||||||
for i = 1, #str do
|
|
||||||
hash = (hash * 31 + string.byte(str, i)) % 65536
|
|
||||||
end
|
|
||||||
return string.format("%04X", hash)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function check_for_outdated_sessions()
|
local function check_for_outdated_sessions()
|
||||||
local expired = 0
|
local expired = 0
|
||||||
@@ -57,10 +51,10 @@ local function validate_key(card_holder, card_clearance, random_chars)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if args[1] == "serve" then
|
||||||
rednet.open(config.modem_side)
|
rednet.open(config.modem_side)
|
||||||
rednet.host('tg', config.name)
|
rednet.host('tg', config.name)
|
||||||
print("Server started, waiting for requests...")
|
print("Server started, waiting for requests...")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local id, msg, proto = rednet.receive('tg')
|
local id, msg, proto = rednet.receive('tg')
|
||||||
check_for_outdated_sessions()
|
check_for_outdated_sessions()
|
||||||
@@ -116,3 +110,39 @@ while true do
|
|||||||
print("Transaction finished")
|
print("Transaction finished")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elseif args[1] == "add_card" then
|
||||||
|
if args[2] then
|
||||||
|
local new_card = args[2]
|
||||||
|
local new_card_parts = split(new_card, ',')
|
||||||
|
local card_holder = new_card_parts[1]
|
||||||
|
|
||||||
|
local key_file = fs.open(card_holder..'.key', 'w')
|
||||||
|
key_file.write(new_card)
|
||||||
|
key_file.close()
|
||||||
|
|
||||||
|
local trusted_keys = fs.open(dir..config.key_file, 'a')
|
||||||
|
local card_hash = hash(new_card)
|
||||||
|
trusted_keys.write(card_hash)
|
||||||
|
trusted_keys.close()
|
||||||
|
end
|
||||||
|
elseif args[1] == "remove_card" then
|
||||||
|
if args[2] then
|
||||||
|
local key_hash = args[2]
|
||||||
|
local trusted_keys = fs.open(dir..config.key_file, 'r')
|
||||||
|
local new_key_table = {}
|
||||||
|
while true do
|
||||||
|
local trusted_hash = trusted_keys.readLine()
|
||||||
|
if trusted_hash ~= key_hash then
|
||||||
|
table.insert(new_key_table, trusted_hash)
|
||||||
|
end
|
||||||
|
if not trusted_hash then break end -- Stop when EOF reached
|
||||||
|
end
|
||||||
|
trusted_keys.close()
|
||||||
|
|
||||||
|
local trusted_keys = fs.open(dir..config.key_file, 'w+')
|
||||||
|
for _, entry in ipairs(new_key_table) do
|
||||||
|
trusted_keys.write(entry .. '\n')
|
||||||
|
end
|
||||||
|
trusted_keys.close()
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in new issue
Block a user