1
0
mirror of https://github.com/LDDestroier/CC/ synced 2024-06-14 09:26:51 +00:00

It's progress I guess

I need to fix up client encTransmitting, since it thinks the key is nil when I tested it earlier.
I also need to make sure that I can transmit by name, and not just by computer ID.
This commit is contained in:
LDDestroier 2019-03-04 16:10:41 -05:00 committed by GitHub
parent d4f57d6181
commit 99dcc8f6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,10 +6,12 @@ local config = {
channel = 1024, channel = 1024,
keyPath = fs.combine(mainPath, "keys"), keyPath = fs.combine(mainPath, "keys"),
mailPath = fs.combine(mainPath, "mail"), mailPath = fs.combine(mainPath, "mail"),
apiPath = fs.combine(mainPath, "api") apiPath = fs.combine(mainPath, "api"),
nameFile = fs.combine(mainPath, "names")
} }
local keys = {} local keys = {}
local names = {}
local readFile = function(path) local readFile = function(path)
local file = fs.open(path, "r") local file = fs.open(path, "r")
@ -38,16 +40,32 @@ local getKey = function(ID)
end end
-- get personal key file -- get personal key file
local key = "" keys[yourID] = ""
if fs.exists(fs.combine(config.keyPath, tostring(yourID))) then if fs.exists(fs.combine(config.keyPath, tostring(yourID))) then
key = readFile(fs.combine(config.keyPath, tostring(yourID))) keys[yourID] = readFile(fs.combine(config.keyPath, tostring(yourID)))
else else
for i = 1, 64 do for i = 1, 64 do
key = key .. string.char(math.random(11, 255)) keys[yourID] = keys[yourID] .. string.char(math.random(11, 255))
end end
writeFile(fs.combine(config.keyPath, tostring(yourID)), key) writeFile(fs.combine(config.keyPath, tostring(yourID)), keys[yourID])
end end
local getAllKeys = function()
local list = fs.list(config.keyPath)
local output = {}
for i = 1, #list do
if tonumber(list[i]) then
output[tonumber(list[i])] = getKey(list[i])
end
end
return output
end
keys = getAllKeys()
--print(textutils.serialize(keys))
--error()
local apiData = { local apiData = {
["aes"] = { ["aes"] = {
path = "aes.lua", path = "aes.lua",
@ -117,6 +135,11 @@ local argList = {
local argData, argErrors = interpretArgs({...}, argList) local argData, argErrors = interpretArgs({...}, argList)
local isServer = argData["--server"] local isServer = argData["--server"]
local serverID = argData[1]
if ccemux and (not peripheral.find("modem")) then
ccemux.attach("top", "wireless_modem")
end
local modem local modem
local getModem = function(doNotPickWireless) local getModem = function(doNotPickWireless)
@ -143,83 +166,184 @@ local userIDs = {}
-- all data recorded -- all data recorded
local DATA = {} local DATA = {}
local transmit = function(msg) local transmit = function(msg, msgID)
modem = getModem(onlyUseWiredModems) modem = getModem(onlyUseWiredModems)
modem.transmit(config.channel, config.channel, { modem.transmit(config.channel, config.channel, {
msg = msg, msg = msg,
encrypted = false encrypted = false,
msgID = msgID
}) })
end end
local encTransmit = function(msg, recipient) local encTransmit = function(msg, msgID, recipient)
modem = getModem(onlyUseWiredModems) modem = getModem(onlyUseWiredModems)
modem.transmit(config.channel, config.channel, { if not keys[recipient] then
msg = aes.encrypt(key, msg), error("the fuck, no keys[recipient]")
encrypted = true elseif not msg then
}) error("the fuck, no msg")
else
modem.transmit(config.channel, config.channel, {
msg = aes.encrypt(keys[recipient], msg),
encrypted = true,
msgID = msgID,
recipient = recipient
})
end
end end
local handle = { local receive = function(msgID, specifyCommand, timer)
client = {}, local evt, msg, tID
server = {} if timer then
} tID = os.startTimer(timer)
end
handle.client.findServer = function(recipient) modem = getModem()
local msgID = math.random(1, 2^30)
transmit({
id = yourID,
command = "find_server",
msgID = msgID,
})
local evt, msg
local timerID = os.startTimer(2)
while true do while true do
evt = {os.pullEvent()} evt = {os.pullEvent()}
if evt[1] == "modem_message" then if evt[1] == "modem_message" then
if evt[5].encrypted then if type(evt[5]) == "table" then
msg = evt[5].msg if evt[5].encrypted then
else msg = aes.decrypt(keys[yourID], evt[5].msg)
msg = evt[5].msg else
msg = evt[5].msg
end
if (not msgID) or (evt[5].msgID == msgID) then
if (not specifyCommand) or (msg.command == specifyCommand) then
return msg, evt[5].encrypted, evt[5].msgID
end
end
end end
elseif evt[1] == "timer" and evt[2] == timerID then elseif evt[1] == "timer" and evt[2] == tID then
return false return nil, nil, nil
end end
end end
end end
handle.server.registerID = function(id) local client = {} -- all client-specific commands
local server = {} -- all server-specific commands
---- ----
---- CLIENT COMMANDS ----
---- ----
-- if you want a super duper secure network, manually enter the server ID into this
client.findServer = function(recipient)
local msgID = math.random(1, 2^30)
transmit({
id = yourID,
command = "find_server"
}, msgID)
local reply, isEncrypted = receive(msgID, "find_server_respond", 2)
if type(reply) == "table" then
if reply.server then
return reply.server
end
end
end
client.register = function(srv, username)
local msgID = math.random(1, 2^30)
encTransmit({
id = yourID,
name = username
}, msgID, srv)
end
client.sendMail = function(srv, recipient, subject, message, attachments)
local msgID = math.random(1, 2^30)
encTransmit({
command = "send_mail",
id = yourID,
recipient = recipient,
subject = subject,
message = message,
attachments = attachments
}, msgID, srv)
local reply, isEncrypted = receive(msgID, "send_mail_respond", 2)
return (reply ~= nil and isEncrypted ~= nil)
end
client.getMail = function(srv)
local msgID = math.random(1, 2^30)
encTransmit({
command = "get_mail",
id = yourID,
}, msgID, srv)
local reply, isEncrypted = receive(msgID, "get_mail_respond", 2)
return (isEncrypted and type(reply) == "table") and reply
end
---- ----
---- SERVER COMMANDS ----
---- ----
server.checkValidName = function(name)
if type(name) ~= "string" then
return false
else
return #name >= 3 or #name <= 64
end
end
server.checkRegister = function(id)
-- I make the code this stupid looking in case I add other stipulations
if names[tostring(id)] then
return true
else
return false
end
end
server.registerID = function(id, name)
local path = fs.combine(config.mailPath, id) local path = fs.combine(config.mailPath, id)
if not fs.exists(path) then if not server.checkRegister(id) then
fs.makeDir(path) fs.makeDir(path)
names[id] = tostring(name)
return true, names[id]
else
return false, "name already exists"
end end
end end
-- records a full email to file -- records a full email to file
handle.server.recordMail = function(sender, recipient, message, subject, attachment) server.recordMail = function(sender, _recipient, subject, message, attachments)
-- sender: The person who sends the message
-- recipient: The message will be put in their folder
-- subject: The header of a message
-- message: the fuck you think it is
-- attachment: Contents of a SINGLE file that will be attached to an email
local path = fs.combine(config.mailPath, recipient)
handle.server.registerID(recipient)
local time = os.epoch("utc") local time = os.epoch("utc")
local file = fs.open(fs.combine(path, time), "w") local recipient
file.write(textutils.serialize({
if _recipient == "*" then
recipient = fs.list(config.mailPath)
elseif type(_recipient) ~= "table" then
recipient = {tostring(_recipient)}
end
local msg = textutils.serialize({
sender = id, sender = id,
time = time, time = time,
recipient = recipient,
read = false, read = false,
subject = subject, subject = subject,
message = message, message = message,
attachment = attachment attachments = attachments
})) })
local requiredSpace = #msg + 2
if fs.getFreeSpace(config.mailPath) < requiredSpace then
return false, "Cannot write mail, not enough space!"
end
local path, file
for i = 1, #recipient do
server.registerID(recipient[i])
path = fs.combine(config.mailPath, recipient[i])
file = fs.open(fs.combine(path, tostring(time)), "w")
file.write(msg)
file.close()
end
return true
end end
-- returns every email in an inbox -- returns every email in an ID's inbox
handle.server.getMail = function(id) server.getMail = function(id)
local output, list = {}, {} local output, list = {}, {}
local mails = fs.list(fs.combine(config.mailPath, id)) local mails = fs.list(fs.combine(config.mailPath, tostring(id)))
local file local file
for k,v in pairs(mails) do for k,v in pairs(mails) do
list[v] = k list[v] = k
@ -235,35 +359,111 @@ handle.server.getMail = function(id)
end end
-- receives messages and sends the appropriate response -- receives messages and sends the appropriate response
handle.server.networking = function() server.networking = function(verbose)
local evt, _msg, msg local msg, isEncrypted, msgID
local say = function(text, id)
if verbose then
return print(text .. (id and ("(" .. id .. ")") or ""))
end
end
while true do while true do
evt = {os.pullEvent()}
if evt[1] == "modem_message" then msg, isEncrypted, msgID = receive()
_msg = evt[5]
if type(_msg) == "table" then if not isEncrypted then
if _msg.msg and type(_msg.encrypted) == "boolean" then if msg.command == "find_server" then
if _msg.encrypted then transmit({
msg = _msg.msg command = msg.command .. "_respond",
else server = yourID,
msg = aes.decrypt(key, _msg.msg) }, msgID, msg.id)
end say("find_server found")
if msg then end
elseif msg.id then
-- add more commands if not server.checkRegister(msg.id) then
if msg.command == "find_server" then encTransmit({
-- send the server ID command = msg.command .. "_respond",
result = false,
errorMsg = "not registered"
}, msgID, msg.id)
say("unregistered users can burn in hell")
else
if msg.command == "register" then
if (
type(msg.id) == "number" and
type(msg.name) == "string"
) then
local reply
local result, name = server.registerID(msg.id, msg.name)
if result then
reply = {
command = msg.command .. "_respond",
result = result,
name = name,
}
else
reply = {
command = msg.command .. "_respond",
result = result,
}
end end
encTransmit(reply, msgID, msg.id)
say("user " .. tostring(msg.id) .. " registered as " .. name)
end
elseif msg.command == "find_server" then
encTransmit({
command = msg.command .. "_respond",
server = yourID,
result = true
}, msgID, msg.id)
say("find_server found (aes)")
elseif msg.command == "send_mail" then
if (
msg.recipient and
type(msg.subject) == "string" and
type(msg.message) == "string"
) then
local reply = {
command = msg.command .. "_respond",
result = server.recordMail(msg.id, msg.recipient, msg.subject, msg.message, msg.attachments)
}
encTransmit(reply, msgID, msg.id)
say("mail sent", msg.id)
end
elseif msg.command == "get_mail" then
if (
msg.id
) then
local mail = server.getMail(msg.id)
local reply = {
command = msg.command .. "_respond",
result = true,
mail = mail,
}
encTransmit(reply, msgID, msg.id)
end end
end end
end end
end end
end end
end end
if isServer then if isServer then
handle.server.networking() server.networking(true)
else else
-- make a whole client interface and shit -- make a whole client interface and shit
local srv = client.findServer()
print(srv)
client.register(srv, "buttman")
end end
--[[
server.recordMail(1, 1, "Testing the sysmail.", "Forgive me, but I'm just testing SysMail as it's being made.")
local messages = server.getMail(1)
print(textutils.serialize(messages))
--]]
return {client = client, server = server}