Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
MajGenRelativity

Basic Wired Networking Protocol

Recommended Posts

This is a basic wired networking protocol that allows you to set up a rudimentary chat application over a WIRED network. This is the foundation for a far FAR more robust protocol I am developing

 

local term = require("term")
local component = require("component")
local event = require("event")
local m = component.modem
local str="0"
 
-- determine what channel people want to listen on
term.write("What channel do you want to chat on? \n")
local channel=io.read()
m.open(tonumber(channel))
-- begin event
event.listen("modem_message", function(_,_,from,port,_,message)
 
-- decipher, and print
local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
local function decrypt(str)
        str = str:gsub("%d",function(a) return string.char(cipher:find(a,nil,true)+47) end)
        str = str:gsub("%l",function(a) return string.char(cipher:find(a,nil,true)+86) end)
        return str
end
if string.sub(tostring(message), 1, 9)=="encrypted" then
str=tostring(message)
message=decrypt(str)
local strlngth=string.len(tostring(message))
message=string.sub(tostring(message), 10, strlngth)
print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
 
else
print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
end
end)
 
-- send a message
term.write("is this going to be encrypted? \n")
 
-- register cipher
local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
 
local function encrypt(str)
  str = str:gsub("%d",function(a) a=a:byte()-47 return cipher:sub(a,a) end)
  str = str:gsub("%l",function(a) a=a:byte()-86 return cipher:sub(a,a) end)
  return str
end
-- determine whether encryption should be used
local useEC=io.read()
if useEC=="yes" then
  term.write("Encryption activated \n")
end
while true do
  str=io.read()
  if tostring(str)=="endme" then
    os.exit()
  end
  if useEC=="yes" then
    str=encrypt(str)
    str="encrypted" .. " " .. str
  end
-- send message
m.broadcast(tonumber(channel), str)
end
Link to post
Share on other sites

This is almost exactly what I was looking for! However, I'm trying to make it so that a User can enter a username, and have it displayed when sending messages. I'm still a Script Kiddie when it comes to Lua, so I'm not sure how I can get this working easily.

 

EDIT:

I'm slowly improving this code into an easy to use, somewhat 'complete' chat program. The one thing I'm not sure how to do is make it so that a user's entered name shows up when a message is sent/received, instead of the computer's address. I may re-write initial questions to include asking for a username, or possibly checking for a file that has the user's set name in it. I will report back with results.

Edited by Xlaits
Adding information, updating information
Link to post
Share on other sites

After a long debugging process, here is what has become of this code. It is FAR from perfect, but it works. So far, the only bugs I have seen are when sending and receiving, the cursor is super unreliable, and one one of my computers, it sends incomplete or broken messages. The port also remains listening, which I MAY OR MAY NOT have fixed. I'm unsure.

 

I didn't to any new commenting, and I'll fix that when I have some time on my hands. For now, I'll give you the short of it. The program will first check for a file, /home/.uname.txt and if it exists, reads the first 16 bytes of it for the username. If it's not found, it will create it, and prompt the user for a username, which it writes in the newly created file. It still contains the Encryption methods, however it no longer prints the address of the computer it is receiving from. Instead, it prints a user readable <Username>: "Message" to the channel. I had to edit the quit codes to reflect this. I did realize later on that I would need to separate all the computers on the network, to prevent message feedback. I think I'm going to change that by making the receive event and it's print function check for the username, and refuse to print the feedback messages.

 

Lemme know what you guys think, or if you can optimize my terrible, buggy code.

 

local term = require("term")
local component = require("component")
local event = require("event")
local m = component.modem
local str="0"
local userfile = "/home/.uname.txt"
local fs = require("filesystem")
local uname = fs.exists(userfile)
 
-- determine what channel people want to listen on
term.clear()
if uname then
  local handler = fs.open(userfile)
  user = handler:read(16)
  handler:close()
else
  term.write("You do not have a user profile. \n")
  term.write("Please enter a 16 character user name: ")
  local handler = fs.open(userfile, "w")
  handler:write(io.read())
  local handler = fs.open(userfile)
  user = handler:read(16)
  term.write("\n Username set to " .. user .. ". \n")
  handler:close()
end
term.write("What channel do you want to chat on: ")
local channel = io.read()
m.open(tonumber(channel))

-- begin event
function ModemFunction(_,_,from,port,_,message_)
event.listen("modem_message", ModemFunction)

-- decipher, and print
local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
local function decrypt(str)
        str = str:gsub("%d",function(a) return string.char(cipher:find(a,nil,true)+47) end)
        str = str:gsub("%l",function(a) return string.char(cipher:find(a,nil,true)+86) end)
        return str
end
if string.sub(tostring(message), 1, 9)=="encrypted" then
str=tostring(message)
message=decrypt(str)
local strlngth=string.len(tostring(message))
message=string.sub(tostring(message), 10, strlngth)
print(tostring(message))
 
else
print(tostring(message))
end
end
 
-- send a message
term.write("is this going to be encrypted? (yes/no) \n")
 
-- register cipher
local cipher = "1234567890qwertyuiopasdfghjklzxcvbnm"
 
local function encrypt(str)
  str = str:gsub("%d",function(a) a=a:byte()-47 return cipher:sub(a,a) end)
  str = str:gsub("%l",function(a) a=a:byte()-86 return cipher:sub(a,a) end)
  return str
end

-- determine whether encryption should be used
local useEC=io.read()
if useEC=="yes" then
  term.clear()
  term.write("Encryption activated \n")
  sleep(5)
  term.clear()
end
while true do
  str= user .. ": " .. io.read()
  if tostring(str)== user .. ": " .. "endme" then
    term.clear()
    event.ignore("modem_message", ModemFunction)
    os.exit()
  end
  if useEC=="yes" then
    str= user .. ": " .. encrypt(str)
    str="encrypted" .. " " .. str
  end

-- send message
m.broadcast(tonumber(channel), str)
end

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.