cozzimoto

Dynamic Rednet Connection Client-side B:b1

Jul 29th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. -- rednet security client
  2. -- Build Beta 1
  3.  
  4. MODEM = peripheral.wrap("top")
  5.  
  6. local decodePort = function(TAB)
  7. -TAKEN OUT
  8. end
  9.  
  10. -- lets find the server
  11. local serverFound = false
  12. local connected = false
  13. local targetServ = "bankSer"
  14. local targetPort = nil
  15. local count = 8
  16. local status = 1
  17. local credentials = {cid = os.getComputerID(), name = os.getComputerLabel(), protocol = nil}
  18.   print("Searching..")
  19.  
  20. while not serverFound and count < 13 do -- 120 public ports
  21.   MODEM.open(count)
  22.   print("opened: "..count)
  23.  
  24.   if status == 1 then
  25.     credentials.protocol = "_ping"
  26.     MODEM.transmit(count,count,textutils.serialize(credentials))
  27.    
  28.     local T = os.startTimer(0.5)
  29.     os.sleep(0)
  30.     local ev = { os.pullEvent() }
  31.     if ev[1] == "modem_message" then
  32.       local iC = ev[3]
  33.       local rC = ev[4]
  34.       local str = ev[5]
  35.    
  36.       if str == "_pong" then
  37.         status = 2
  38.         print("PINGED!")
  39.       end
  40.      
  41.       os.sleep(0)
  42.       if status == 2 then
  43.         credentials.protocol = "whoareyou?"
  44.         MODEM.transmit(count,count,textutils.serialize(credentials))
  45.        
  46.         local T = os.startTimer(0.4)
  47.         os.sleep(0)
  48.         local ev = { os.pullEvent() }
  49.         if ev[1] == "modem_message" then
  50.           local iC = ev[3]
  51.           local rC = ev[4]
  52.           local str = ev[5]
  53.    
  54.           if str == targetServ then
  55.             status = 3
  56.             serverFound = true
  57.             targetPort = count
  58.             print("Found Target Server, Requesting to Connect")
  59.           end
  60.         end
  61.       end
  62.     end  
  63.   end
  64.  
  65.   credentials.protocol = nil
  66.   count = count + 1
  67.   os.sleep(0.4)
  68. end
  69.  
  70. print("Search ended, trying to connect")
  71.  
  72. local attempts = 0
  73. while serverFound and not connected and attempts < 3 do
  74.   if status == 3 then
  75.     credentials.protocol = "connectionRequest"
  76.     MODEM.transmit(targetPort,targetPort,textutils.serialize(credentials))
  77.    
  78.     local T = os.startTimer(0.4)
  79.     os.sleep(0)
  80.     local ev = { os.pullEvent() }
  81.     if ev[1] == "modem_message" then
  82.       local iC = ev[3]
  83.       local rC = ev[4]
  84.       local pkg = textutils.unserialize(ev[5])
  85.      
  86.       targetPort = tonumber(decodePort(pkg))
  87.       print(targetPort)
  88.       connected = true
  89.      
  90.     end
  91.   end
  92.  
  93.   attempts = attempts + 1
  94. end
  95.  
  96. print("ENDOF program")
Advertisement
Add Comment
Please, Sign In to add comment