Advertisement
programcreator

IPnet: Client

Jan 31st, 2015
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.16 KB | None | 0 0
  1. -----------IPnet Client v1.0---------
  2. -----------------API-----------------
  3. --------------by Creator-------------
  4.  
  5. --Variables--
  6. local secretInt = math.random(100,9999)
  7. local base = nil
  8. local publicPrime = nil
  9. local subRouter = {}
  10. local configuration = {}
  11. local debugOpt = false
  12. local localChannel = 0
  13. local textutilsserialize = textutils.serialize
  14. local textutilsunserialize = textutils.unserialize
  15. local sharedSecret = nil
  16. local myHalfofTheSecret = nil
  17. --Too many variables--
  18.  
  19. --Functions--
  20. local function clear()
  21.     term.setCursorPos(1,1)
  22.     term.clear()
  23. end
  24.  
  25. function baseToPowerMod(base,power,modulus)
  26.   --This function was taken from Anavrins' Diffie-Hellman proof of concept
  27.   --Full code here:http://pastebin.com/H3kZHZBA
  28.   local remainder = base
  29.   for i = 1, power-1 do
  30.     remainder = remainder * remainder
  31.     if remainder >= modulus then
  32.       remainder = remainder % modulus
  33.     end
  34.   end
  35.   return remainder
  36. end
  37.  
  38. local function debugPrint(txt)
  39.     if debugOpt then
  40.         print(txt)
  41.     end
  42. end
  43.  
  44. local function getModemSide()
  45.     local sides = {"bottom","right","left","front","back","top"}
  46.     local side = {}
  47.     for i,v in pairs(rs.getSides()) do
  48.         if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
  49.             debugPrint("side:")
  50.             debugPrint(v)
  51.             return v
  52.         end
  53.     end
  54.     debugPrint("Got modem side")
  55.     if #side == 0 then return nil end
  56. end
  57.  
  58. local function getIP()
  59.     local x , y , z = gps.locate()
  60.     ip = {x,y,z,os.getComputerID()}
  61.     debugPrint("Got ip: ")
  62.     debugPrint(ip)
  63.     return textutilsserialize(ip)
  64. end
  65.  
  66. local function setConfig()
  67.     local configs = {}
  68.     configs = textutilsunserialize(getIP())
  69.     configs[5] = getModemSide()
  70.    
  71.     local configFile = fs.open("config","w")
  72.     configFile.write(textutilsserialize(configs))
  73.     configFile.close()
  74.     debugPrint("Wrote configs: ")
  75.     debugPrint(configs)
  76. end
  77.  
  78. local function setUp()
  79.     if fs.exists("config") then
  80.         configFile = fs.open("config","r")
  81.         tempConfigs = textutilsunserialize(configFile.readAll())
  82.         configFile.close()
  83.         debugPrint("File exists")
  84.     else
  85.         setConfig()
  86.         configFile = fs.open("config","r")
  87.         tempConfigs = textutilsunserialize(configFile.readAll())
  88.         configFile.close()
  89.         debugPrint("File does not exist")
  90.     end
  91.     if tempConfigs[5] == nil then
  92.         setConfig()
  93.         configFile = fs.open("config","r")
  94.         tempConfigs = textutilsunserialize(configFile.readAll())
  95.         configFile.close()
  96.     elseif peripheral.isPresent(tempConfigs[5]) == false then
  97.         setConfig()
  98.         configFile = fs.open("config","r")
  99.         tempConfigs = textutilsunserialize(configFile.readAll())
  100.         configFile.close()
  101.     elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
  102.         setConfig()
  103.         configFile = fs.open("config","r")
  104.         tempConfigs = textutilsunserialize(configFile.readAll())
  105.         configFile.close()
  106.     end
  107.    
  108.     if tempConfigs[5] == nil then
  109.         debugPrint("No modem attached")
  110.         error()
  111.     end
  112.    
  113.     modem = peripheral.wrap(tempConfigs[5])
  114.     local idChannel = 65522
  115.     local msgChannel = 65523
  116.     modem.open(idChannel)
  117.     modem.open(msgChannel)
  118.     debugPrint("Opened channels:")
  119.     debugPrint(idChannel)
  120.     debugPrint(msgChannel)
  121.     configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
  122.     debugPrint("configuration")
  123.     debugPrint(textutilsserialize(configuration))
  124. end
  125.  
  126. local function identificationSubRouter()
  127.     local localChannel
  128.     serilaizedConfig = textutilsserialize(configuration)
  129.     local mainRouterNotFound = true
  130.     modem.transmit(65522,10,serilaizedConfig)
  131.     while mainRouterNotFound do
  132.         local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  133.         if  pcall(textutilsunserialize,msg) then
  134.             if ch2 == 9 then
  135.                 subRouter = textutilsunserialize(msg)
  136.                 debugPrint("Channel 2 valid")
  137.                 mainRouterNotFound = false
  138.                 local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
  139.                 while ch2Two ~= 18 do
  140.                     eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
  141.                 end
  142.                 debugPrint("msg")
  143.                 debugPrint(msgTwo)
  144.                 local unserializedMsgTwo = textutilsunserialize(msgTwo)
  145.                 localChannel = unserializedMsgTwo[2]
  146.                 debugPrint("Opened on channel "..localChannel)
  147.                 modem.open(65515)
  148.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
  149.                 while tCh1 ~= 65515 or tCh2 ~= 1 do
  150.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  151.                 end
  152.                 publicPrime = textutilsunserialize(tMsg)[1]
  153.                 base = textutilsunserialize(tMsg)[2]
  154.                 myHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
  155.                 sharedSecret = baseToPowerMod(textutilsunserialize(tMsg)[3],secretInt,publicPrime)
  156.                 modem.transmit(65515,2,myHalfofTheSecret)
  157.                 print(sharedSecret)
  158.                 modem.close(65515)
  159.             elseif ch2 == 11 then
  160.                 modem.transmit(65522,12,serilaizedConfig)
  161.                 subRouter = textutilsunserialize(msg)
  162.                 debugPrint("Channel 2 valid")
  163.                 mainRouterNotFound = false
  164.                 local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
  165.                 while ch2Two ~= 18 do
  166.                     eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
  167.                 end
  168.                 debugPrint("msg")
  169.                 debugPrint(msgTwo)
  170.                 local unserializedMsgTwo = textutilsunserialize(msgTwo)
  171.                 localChannel = unserializedMsgTwo[2]
  172.                 debugPrint("Opened on channel "..localChannel)
  173.                 modem.open(65515)
  174.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
  175.                 while tCh1 ~= 65515 or tCh2 ~= 1 do
  176.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  177.                 end
  178.                 publicPrime = textutilsunserialize(tMsg)[1]
  179.                 base = textutilsunserialize(tMsg)[2]
  180.                 myHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
  181.                 sharedSecret = baseToPowerMod(textutilsunserialize(tMsg)[3],secretInt,publicPrime)
  182.                 modem.transmit(65515,2,myHalfofTheSecret)
  183.                 print(sharedSecret)
  184.                 modem.close(65515)
  185.             end
  186.         end
  187.     end
  188.     debugPrint("subRouter")
  189.     debugPrint(textutilsserialize(subRouter))
  190.     debugPrint("Local ch")
  191.     debugPrint(localChannel)
  192.     if fs.exists("confTemp") then
  193.         fs.delete("confTemp")
  194.     end
  195.     confTemp = fs.open("confTemp","w")
  196.     confTemp.write(textutilsserialize({configuration[1],configuration[2],configuration[3],configuration[4],subRouter[1],subRouter[2],subRouter[3],subRouter[4],localChannel}))
  197.     confTemp.close()
  198. end
  199.  
  200. function send(destPc,msg,port)
  201.     local modemSideFile = fs.open("config","r")
  202.     local modemSide = textutilsunserialize(modemSideFile.readAll())
  203.     modemSideFile.close()
  204.     modem = peripheral.wrap(modemSide[5])
  205.     if type(destPc) == "table" then
  206.         if #destPc == 4 then
  207.             local configTempFile = fs.open("confTemp","r")
  208.             local confTemp = textutilsunserialize(configTempFile.readAll())
  209.             configTempFile.close()
  210.             local msgNum = math.random(1,5000)
  211.             modem.transmit(65523,1,textutilsserialize({destPc,{confTemp[1],confTemp[2],confTemp[3],confTemp[4]},{confTemp[5],confTemp[6],confTemp[7],confTemp[8]},{confTemp[1],confTemp[2],confTemp[3],confTemp[4]},msgNum,msg,port}))
  212.         else
  213.             debugPrint("Your destination IP is a table with the following content: x, y, z and ID of the receiver.")
  214.         end
  215.     else
  216.         debugPrint("Your destination IP is a table with the following content: x, y, z and ID of the receiver.")
  217.     end
  218. end
  219.  
  220. function receive()
  221.     local event, side, ch1, ch2, msg, distance
  222.     local modemSideFile = fs.open("config","r")
  223.     local modemSide = textutilsunserialize(modemSideFile.readAll())
  224.     modemSideFile.close()
  225.     modem = peripheral.wrap(modemSide[5])
  226.     local configTempFile = fs.open("confTemp","r")
  227.     local confTemp = textutilsunserialize(configTempFile.readAll())
  228.     configTempFile.close()
  229.     modem.open(confTemp[9])
  230.     local continueBool = true
  231.     while continueBool == true do
  232.         event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  233.         debugPrint("recieved msg")
  234.         debugPrint(ch1)
  235.         debugPrint(ch2)
  236.         debugPrint(msg)
  237.         debugPrint(textutilsserialize(confTemp))
  238.         pcall(textutilsunserialize,msg)
  239.         if ch1 == confTemp[9] and ch2 == 1 then
  240.             continueBool = false
  241.         end
  242.     end
  243.     modem.close(confTemp[9])
  244.     return msg
  245. end
  246.  
  247. function toReturn()
  248.     clear()
  249.     setUp()
  250.     identificationSubRouter()
  251.     modem.close(65522)
  252.     modem.close(65523)
  253.     return textutilsserialize(configuration)
  254. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement