cozzimoto

Dynamic Rednet Connection Server-side B:b1

Jul 29th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. -- rednet security server
  2. -- Build Beta 1
  3.  
  4. local serverProperties = {publicPort = 10, serverType = "bankSer"}
  5. local privatePorts = {}
  6. MODEM = peripheral.wrap("top")
  7. MODEM.open(serverProperties.publicPort)
  8.   print("opened: "..serverProperties.publicPort)
  9.  
  10. local RNGen = function()
  11. --TAKEN OUT
  12. end
  13.  
  14. local encodePort = function(RNG)
  15. --TAKEN OUT
  16. end
  17.  
  18. local decodePort = function(TAB)
  19. --TAKEN OUT
  20. end
  21.  
  22. while true do
  23.   local ev = { os.pullEvent() }
  24.   if ev[1] == "modem_message" then
  25.     local iC = ev[3]
  26.     local rC = ev[4]
  27.     local pkg = textutils.unserialize(ev[5])
  28.    
  29.     if type(pkg) == "table" then -- verify if its a table
  30.       if pkg.cid ~= nil then -- pkg must have a cid -computer id-
  31.         if privatePorts[pkg.cid] == nil then -- if cid isnt in memory create a slot
  32.           privatePorts[pkg.cid] = {name = pkg.name, port = 0}
  33.         end
  34.         os.sleep(0)
  35.        
  36.         if pkg.protocol == "_ping" then
  37.           MODEM.transmit(rC,serverProperties.publicPort,"_pong")
  38.             print("pinged from "..pkg.cid)
  39.        
  40.         elseif pkg.protocol == "whoareyou?" then
  41.           MODEM.transmit(rC,serverProperties.publicPort,serverProperties.serverType)
  42.             print("send Server ID to "..pkg.cid)
  43.        
  44.         elseif pkg.protocol == "connectionRequest" then
  45.           local gPort = RNGen()
  46.           local packet = encodePort(gPort)
  47.           privatePorts[pkg.cid].port = gPort
  48.           MODEM.open(gPort)
  49.             print(" opened: "..gPort)
  50.             print(" for cid: "..pkg.cid)
  51.             print(" encoded at: "..textutils.serialize(packet))
  52.           MODEM.transmit(rC,serverProperties.publicPort,textutils.serialize(packet))
  53.        
  54.         elseif pkg.protocol == "connectionVerify" then
  55.           MODEM.transmit(rC,rC,"connectionEstablished")
  56.        
  57.         else -- if pkg.protocol doesnt exist or doesnt match
  58.           MODEM.transmit(rC,serverProperties.publicPort,"error: invalid protocol")
  59.             print("invalid protocol")
  60.         end
  61.       else -- if pkg.cid is nil
  62.         MODEM.transmit(rC,serverProperties.publicPort,"error: invalid Credentials")
  63.           print("invalid Credentials")
  64.       end
  65.     else -- if pkg is a string
  66.       MODEM.transmit(rC,serverProperties.publicPort,"error: invalid packet")
  67.         print("invalid packet..")
  68.     end
  69.   os.sleep(0)
  70.  
  71.   elseif ev[1] == "key" then
  72.     print(textutils.serialize(privatePorts))
  73.     os.sleep(1.5)
  74.   end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment