From 5e504e42aca32f28f52fe5676525c24a47d57910 Mon Sep 17 00:00:00 2001 From: brulijam Date: Thu, 6 Jun 2024 22:01:46 +0200 Subject: [PATCH] a --- src/brulijam/lib/networking.lua | 105 ++++++++++++++++------------- src/brulijam/turtle-miner-fibo.lua | 2 + 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/src/brulijam/lib/networking.lua b/src/brulijam/lib/networking.lua index 79b01ff..96879c7 100755 --- a/src/brulijam/lib/networking.lua +++ b/src/brulijam/lib/networking.lua @@ -1,27 +1,27 @@ -basic_functions = require("lib.basic-functions") +basic = require("lib.basic") hash = require("lib.hash") --aes = require("lib.aes") --aes does not yet work with require os.loadAPI("brulijam/lib/aes.lua") -function create_keyfile() - basic_functions.clear_display() +function createKeyfile() + basic.clearDisplay() write("Please enter shared secret for networking: ") - local pw_hash = hash.digestStr(read("*") .. "salty salt") - basic_functions.write_to_file("brulijam/files/networking-key.txt", pw_hash) - basic_functions.clear_display() + local pwHash = hash.digestStr(read("*") .. "salty salt") + basic.writeToFile("brulijam/files/networking-key.txt", pwHash) + basic.clearDisplay() end -local function get_keyfile() - local pw_hash = basic_functions.read_file_oneline("brulijam/files/networking-key.txt") - return pw_hash +local function getKeyfile() + local pwHash = basic.readFileOneline("brulijam/files/networking-key.txt") + return pwHash end local function setup() - own_label = os.computerLabel() + ownLabel = os.computerLabel() peripheral.find("modem", rednet.open) if not fs.exists "brulijam/files/networking-key.txt" then - create_keyfile() + createKeyfile() end end @@ -29,60 +29,74 @@ end String target ist label des Computers. zb "computer1", "miner1", "farmer1", etc String message ist einfach der Text der Übertragen werden soll ]] -function send_message(to, what, payload) +function sendMessage(to, what, payload) setup() - local keyfile = get_keyfile() - local msg_table = {} - msg_table[0] = own_label - msg_table[1] = to - msg_table[2] = what - msg_table[3] = payload + local keyfile = getKeyfile() + local msgTable = {} + msgTable[0] = ownLabel + msgTable[1] = to + msgTable[2] = what + msgTable[3] = payload - local enc_msg = aes.encrypt(keyfile, textutils.serialize(msg_table)) - rednet.broadcast(enc_msg) + local encMsg = aes.encrypt(keyfile, textutils.serialize(msgTable)) + rednet.broadcast(encMsg) end -function send_file(to, origin_path, target_path) +function sendLog(to, targetPath, logMsg) setup() - local keyfile = get_keyfile() - local payload_table = {} + local keyfile = getKeyfile() + local payloadTable = {} - print("sending " .. origin_path .. " to " .. to) + payloadTable[0] = targetPath + payloadTable[1] = logMsg - local file_content = basic_functions.read_file_all(origin_path) - payload_table[0] = target_path - payload_table[1] = file_content + sendMessage(to, "log", payloadTable) +end - 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) end -function receive_message() +function receiveMessage() setup() - local keyfile = get_keyfile() - local id, rec_msg, prot = rednet.receive() - local pcall_success, decr_msg = pcall(aes.decrypt, keyfile, rec_msg) + local keyfile = getKeyfile() + local id, recMsg, prot = rednet.receive() + local pcallSuccess, decrMsg = pcall(aes.decrypt, keyfile, recMsg) - if pcall_success and decr_msg ~= nil then - local rec_table = textutils.unserialize(decr_msg) + if pcallSuccess and decrMsg ~= nil then + local recTable = textutils.unserialize(decrMsg) - local from = rec_table[0] - local to = rec_table[1] - local what = rec_table[2] - local payload = rec_table[3] + local from = recTable[0] + local to = recTable[1] + local what = recTable[2] + local payload = recTable[3] - if to == own_label or to == "all" then + if to == ownLabel or to == "all" 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" + elseif what == "log" then + basic.appendToFileNl(payload[0], payload[1]) + return "event: log received" elseif what == "execute_script" then shell.run(payload) return elseif what == "script_command" then - return rec_table[3] + return recTable[3] end - return rec_table + return recTable end else return "event: unencrypted message" @@ -90,9 +104,10 @@ function receive_message() end return { - send_message = send_message, - send_file = send_file, - receive_message = receive_message, + sendMessage = sendMessage, + sendFile = sendFile, + sendLog = sendLog, + receiveMessage = receiveMessage, } diff --git a/src/brulijam/turtle-miner-fibo.lua b/src/brulijam/turtle-miner-fibo.lua index 9b56ea8..59a41ad 100644 --- a/src/brulijam/turtle-miner-fibo.lua +++ b/src/brulijam/turtle-miner-fibo.lua @@ -1,4 +1,5 @@ basic = require("lib.basic") +networking = require("lib.networking") oreList = { [0] = "minecraft:gold_ore", @@ -78,6 +79,7 @@ local function savePositionToFile() posX, posY, posZ = gps.locate() sleep(1) 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() end end