This commit is contained in:
Julian Brammer 2024-06-06 22:01:46 +02:00
parent aa22b25486
commit 5e504e42ac
2 changed files with 62 additions and 45 deletions

View File

@ -1,27 +1,27 @@
basic_functions = require("lib.basic-functions") basic = require("lib.basic")
hash = require("lib.hash") hash = require("lib.hash")
--aes = require("lib.aes") --aes does not yet work with require --aes = require("lib.aes") --aes does not yet work with require
os.loadAPI("brulijam/lib/aes.lua") os.loadAPI("brulijam/lib/aes.lua")
function create_keyfile() function createKeyfile()
basic_functions.clear_display() basic.clearDisplay()
write("Please enter shared secret for networking: ") write("Please enter shared secret for networking: ")
local pw_hash = hash.digestStr(read("*") .. "salty salt") local pwHash = hash.digestStr(read("*") .. "salty salt")
basic_functions.write_to_file("brulijam/files/networking-key.txt", pw_hash) basic.writeToFile("brulijam/files/networking-key.txt", pwHash)
basic_functions.clear_display() basic.clearDisplay()
end end
local function get_keyfile() local function getKeyfile()
local pw_hash = basic_functions.read_file_oneline("brulijam/files/networking-key.txt") local pwHash = basic.readFileOneline("brulijam/files/networking-key.txt")
return pw_hash return pwHash
end end
local function setup() local function setup()
own_label = os.computerLabel() ownLabel = os.computerLabel()
peripheral.find("modem", rednet.open) peripheral.find("modem", rednet.open)
if not fs.exists "brulijam/files/networking-key.txt" then if not fs.exists "brulijam/files/networking-key.txt" then
create_keyfile() createKeyfile()
end end
end end
@ -29,60 +29,74 @@ end
String target ist label des Computers. zb "computer1", "miner1", "farmer1", etc String target ist label des Computers. zb "computer1", "miner1", "farmer1", etc
String message ist einfach der Text der Übertragen werden soll String message ist einfach der Text der Übertragen werden soll
]] ]]
function send_message(to, what, payload) function sendMessage(to, what, payload)
setup() setup()
local keyfile = get_keyfile() local keyfile = getKeyfile()
local msg_table = {} local msgTable = {}
msg_table[0] = own_label msgTable[0] = ownLabel
msg_table[1] = to msgTable[1] = to
msg_table[2] = what msgTable[2] = what
msg_table[3] = payload msgTable[3] = payload
local enc_msg = aes.encrypt(keyfile, textutils.serialize(msg_table)) local encMsg = aes.encrypt(keyfile, textutils.serialize(msgTable))
rednet.broadcast(enc_msg) rednet.broadcast(encMsg)
end end
function send_file(to, origin_path, target_path) function sendLog(to, targetPath, logMsg)
setup() setup()
local keyfile = get_keyfile() local keyfile = getKeyfile()
local payload_table = {} local payloadTable = {}
print("sending " .. origin_path .. " to " .. to) payloadTable[0] = targetPath
payloadTable[1] = logMsg
local file_content = basic_functions.read_file_all(origin_path) sendMessage(to, "log", payloadTable)
payload_table[0] = target_path end
payload_table[1] = file_content
send_message(to, "file_transfer", payload_table) function sendFile(to, originPath, targetPath)
setup()
local keyfile = getKeyfile()
local payloadTable = {}
print("sending " .. originPath .. " to " .. to)
local fileContent = basic.readFileAll(originPath)
payloadTable[0] = targetPath
payloadTable[1] = fileContent
sendMessage(to, "file_transfer", payloadTable)
sleep(0.5) sleep(0.5)
end end
function receive_message() function receiveMessage()
setup() setup()
local keyfile = get_keyfile() local keyfile = getKeyfile()
local id, rec_msg, prot = rednet.receive() local id, recMsg, prot = rednet.receive()
local pcall_success, decr_msg = pcall(aes.decrypt, keyfile, rec_msg) local pcallSuccess, decrMsg = pcall(aes.decrypt, keyfile, recMsg)
if pcall_success and decr_msg ~= nil then if pcallSuccess and decrMsg ~= nil then
local rec_table = textutils.unserialize(decr_msg) local recTable = textutils.unserialize(decrMsg)
local from = rec_table[0] local from = recTable[0]
local to = rec_table[1] local to = recTable[1]
local what = rec_table[2] local what = recTable[2]
local payload = rec_table[3] local payload = recTable[3]
if to == own_label or to == "all" then if to == ownLabel or to == "all" then
if what == "file_transfer" then if what == "file_transfer" then
basic_functions.write_to_file(payload[0], payload[1]) basic.writeToFile(payload[0], payload[1])
return "event: file transfer received" return "event: file transfer received"
elseif what == "log" then
basic.appendToFileNl(payload[0], payload[1])
return "event: log received"
elseif what == "execute_script" then elseif what == "execute_script" then
shell.run(payload) shell.run(payload)
return return
elseif what == "script_command" then elseif what == "script_command" then
return rec_table[3] return recTable[3]
end end
return rec_table return recTable
end end
else else
return "event: unencrypted message" return "event: unencrypted message"
@ -90,9 +104,10 @@ function receive_message()
end end
return { return {
send_message = send_message, sendMessage = sendMessage,
send_file = send_file, sendFile = sendFile,
receive_message = receive_message, sendLog = sendLog,
receiveMessage = receiveMessage,
} }

View File

@ -1,4 +1,5 @@
basic = require("lib.basic") basic = require("lib.basic")
networking = require("lib.networking")
oreList = { oreList = {
[0] = "minecraft:gold_ore", [0] = "minecraft:gold_ore",
@ -78,6 +79,7 @@ local function savePositionToFile()
posX, posY, posZ = gps.locate() posX, posY, posZ = gps.locate()
sleep(1) sleep(1)
basic.appendToFileNl("/brulijam/files/chests-turtle-miner-fibo.txt", "Chest at: " .. posX .. ", " .. posY .. ", " .. posZ) basic.appendToFileNl("/brulijam/files/chests-turtle-miner-fibo.txt", "Chest at: " .. posX .. ", " .. posY .. ", " .. posZ)
networking.sendLog("all", "/brulijam/logs/chests-to-collect.txt", posX .. ", " .. posY .. ", " .. posZ)
turtle.equipRight() turtle.equipRight()
end end
end end