Advertisement
programcreator

Network RAM

Jan 27th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. modemSide = "back"
  2. modem = peripheral.wrap(modemSide)
  3. modem.open(93)
  4. turtle = {}
  5. testForExist = {}
  6. notExists = true
  7. secretPass = "Ehfks87-i"
  8. secretPass2 = "sgxyg&%UI33%^&HS34DH-N"
  9. turtlePort = 512
  10.  
  11. function clear()
  12.   term.clear()
  13.   term.setCursorPos(1,1)
  14. end
  15.  
  16. function makeString(l)
  17.         if l < 1 then return nil end -- Check for l < 1
  18.         local s = "" -- Start string
  19.         for i = 1, l do
  20.                 s = s .. string.char(math.random(32, 126)) -- Generate random number from 32 to 126, turn it into character and add to string
  21.         end
  22.         return s -- Return string
  23. end
  24.  
  25. function authentificationServer(port)
  26.     authPass = false
  27.     rndString = tostring(makeString(math.random(8,20)))
  28.     modem.transmit(57,port,textutils.serialize({"authentification",encrypt.encrypt(rndString,secretPass)}))
  29.     qevent, qside, qch1, qch2, qmsg = os.pullEvent("modem_message")
  30.     qmsgTab = textutils.unserialize(qmsg)
  31.     if encrypt.decrypt(qmsgTab[2],secretPass2) == rndString then
  32.         authPass = true
  33.     end
  34.     return authPass
  35. end
  36.  
  37. function authentificationClient(port)
  38.     qevent, qside, qch1, qch2, qmsg = os.pullEvent("modem_message")
  39.     modem.transmit(65529,port,encrypt.encrypt(encrypt.decrypt(qmsg,secretPass),secretPass2))
  40. end
  41.  
  42. clear()
  43.  
  44. while true do
  45.     notExists = true
  46.     event, side, ch1, ch2, msg = os.pullEvent("modem_message")
  47.     clear()
  48.    
  49.     print("h")
  50.     msgTab = textutils.unserialize(msg)
  51.     if msgTab[1] == "turtle" and ch2 == 1 then
  52.         print("1st cond")
  53.         if authentificationServer(turtlePort) == true then
  54.             print("h")
  55.             for i,v in pairs(turtle) do
  56.                 print(i..v)
  57.                 testForExist = textutils.unserialize(turtle[i])
  58.                 if testForExist[1] == msgTab[2] then
  59.                     notExists = false
  60.                 end
  61.             end
  62.             print("i")
  63.             if notExists == true then
  64.                 turtle[#turtle + 1] = textutils.serialize({msgTab[2],msgTab[3],msgTab[4],msgTab[5]})
  65.             end
  66.         end
  67.     elseif msgTab[1] == "turtle" and ch2 == 2 then
  68.         print("msg")
  69.         modem.transmit(324,1,textutils.serialize(turtle))
  70.     end
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement