Advertisement
programcreator

IPnet: SlaveRouter

Jan 31st, 2015
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.75 KB | None | 0 0
  1. ----------IPnet SubRouter v1.1-------
  2. ---------------Program---------------
  3. --------------by Creator-------------
  4. --Variables--
  5. local mainRouter = {}
  6. local configuration = {}
  7. local debugOpt = true
  8. local Clients = {}
  9. local textutilsserialize = textutils.serialize
  10. local textutilsunserialize = textutils.unserialize
  11. local publicPrime = 625210769
  12. local base = 11
  13. local secretInt = math.random(100,9999)
  14. local secrets = {}
  15. local version = "1.1"
  16. local localChannel = nil
  17. local myHalfOfTheSecret = nil
  18. local tMyHalfofTheSecret = nil
  19. --Too many variables--
  20.  
  21. --Functions--
  22. local function clear()
  23.     term.setCursorPos(1,1)
  24.     term.clear()
  25. end
  26.  
  27. local function baseToPowerMod(mBase,power,modulus)
  28.   --This function was taken from Anavrins' Diffie-Hellman proof of concept
  29.   --Full code here:http://pastebin.com/H3kZHZBA
  30.   local remainder = mBase
  31.   print("mbase")
  32.   print(mBase)
  33.   print(remainder)
  34.   for i = 1, power-1 do
  35.     remainder = remainder * remainder
  36.     if remainder >= modulus then
  37.       remainder = remainder % modulus
  38.     end
  39.   end
  40.   return remainder
  41. end
  42.  
  43. local function debugPrint(txt)
  44.     if debugOpt then
  45.         print(txt)
  46.     end
  47. end
  48.  
  49. local function getModemSide()
  50.     local sides = {"bottom","right","left","front","back","top"}
  51.     local side = {}
  52.     for i,v in pairs(rs.getSides()) do
  53.         if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
  54.             debugPrint("side:")
  55.             debugPrint(v)
  56.             return v
  57.         end
  58.     end
  59.     debugPrint("Got modem side")
  60.     if #side == 0 then return nil end
  61. end
  62.  
  63. local function getIP()
  64.     local x , y , z = gps.locate()
  65.     ip = {x,y,z,os.getComputerID()}
  66.     debugPrint("Got ip: ")
  67.     debugPrint(ip)
  68.     return textutilsserialize(ip)
  69. end
  70.  
  71. local function setConfig()
  72.     local configs = {}
  73.     configs = textutilsunserialize(getIP())
  74.     configs[5] = getModemSide()
  75.    
  76.     local configFile = fs.open("config","w")
  77.     configFile.write(textutilsserialize(configs))
  78.     configFile.close()
  79.     debugPrint("Wrote configs: ")
  80.     debugPrint(configs)
  81. end
  82.  
  83. local function setUp()
  84.     if fs.exists("config") then
  85.         configFile = fs.open("config","r")
  86.         tempConfigs = textutilsunserialize(configFile.readAll())
  87.         configFile.close()
  88.         debugPrint("File exists")
  89.     else
  90.         setConfig()
  91.         configFile = fs.open("config","r")
  92.         tempConfigs = textutilsunserialize(configFile.readAll())
  93.         configFile.close()
  94.         debugPrint("File does not exist")
  95.     end
  96.     if tempConfigs[5] == nil then
  97.         setConfig()
  98.         configFile = fs.open("config","r")
  99.         tempConfigs = textutilsunserialize(configFile.readAll())
  100.         configFile.close()
  101.     elseif peripheral.isPresent(tempConfigs[5]) == false then
  102.         setConfig()
  103.         configFile = fs.open("config","r")
  104.         tempConfigs = textutilsunserialize(configFile.readAll())
  105.         configFile.close()
  106.     elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
  107.         setConfig()
  108.         configFile = fs.open("config","r")
  109.         tempConfigs = textutilsunserialize(configFile.readAll())
  110.         configFile.close()
  111.     end
  112.    
  113.     if tempConfigs[5] == nil then
  114.         print("No modem attached")
  115.         error()
  116.     end
  117.    
  118.     modem = peripheral.wrap(tempConfigs[5])
  119.     local idChannelMain = 65524
  120.     local msgChannelMain = 65525
  121.     local idCannelClient = 65522
  122.     local msgChannelClient = 65523
  123.     modem.open(idChannelMain)
  124.     modem.open(msgChannelMain)
  125.     modem.open(idCannelClient)
  126.     modem.open(msgChannelClient)
  127.     debugPrint("Opened channels")
  128.     debugPrint(idChannelMain)
  129.     debugPrint(msgChannelMain)
  130.     debugPrint(idCannelClient)
  131.     debugPrint(msgChannelClient)
  132.    
  133.     configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
  134. end
  135.  
  136. local function IPsender(a,b,c,d,e,f,g)
  137.     local serializedMsgBuffer = {a,b,c,d,e,f,g}
  138.     local serializedMsg = textutilsserialize(serializedMsgBuffer)
  139.     modem.transmit(65525,1,serializedMsg)
  140. end
  141.  
  142. local function identificationMainRouter()
  143.     serilaizedConfig = textutilsserialize(configuration)
  144.     local mainRouterNotFound = true
  145.     modem.transmit(65524,6,serilaizedConfig)
  146.     while mainRouterNotFound do
  147.         local event, side, ch1, ch2, msg, distance = nil, nil, nil, nil, nil, nil
  148.         repeat
  149.             event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  150.         until ch1 == 65524
  151.         if  pcall(textutilsunserialize,msg) then
  152.             if ch2 == 5 then
  153.                 mainRouter = textutilsunserialize(msg)
  154.                 debugPrint("Channel 2 valid")
  155.                 mainRouterNotFound = false
  156.                 local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo =  nil, nil, nil, nil, nil, nil
  157.                 repeat
  158.                     eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
  159.                 until ch1Two == 65524 and ch2Two == 23
  160.                 debugPrint("msg")
  161.                 debugPrint(msgTwo)
  162.                 localChannel = msgTwo
  163.                 debugPrint("Opened on channel "..localChannel)
  164.                 modem.open(65505)
  165.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
  166.                 while tCh1 ~= 65505 or tCh2 ~= 1 do
  167.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  168.                 end
  169.                 publicPrime = textutilsunserialize(tMsg)[1]
  170.                 base = textutilsunserialize(tMsg)[2]
  171.                 debugPrint("yay")
  172.                 debugPrint(base)
  173.                 debugPrint(secretInt)
  174.                 debugPrint(publicPrime)
  175.                 tMyHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
  176.                 debugPrint("yay")
  177.                 local buffer = textutilsunserialize(tMsg)
  178.                 tSharedSecret = baseToPowerMod(buffer[3],secretInt,publicPrime)
  179.                 modem.transmit(65505,2,tMyHalfofTheSecret)
  180.                 print(tSharedSecret)
  181.                 modem.close(65505)
  182.             elseif ch2 == 7 then
  183.                 modem.transmit(65524,8,serilaizedConfig)
  184.                 mainRouter = textutilsunserialize(msg)
  185.                 debugPrint("Channel 2 valid")
  186.                 mainRouterNotFound = false
  187.                 local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo =  nil, nil, nil, nil, nil, nil
  188.                 repeat
  189.                     eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
  190.                 until ch1Two == 65524 and ch2Two == 23
  191.                 debugPrint("msg")
  192.                 debugPrint(msgTwo)
  193.                 localChannel = msgTwo
  194.                 debugPrint("Opened on channel "..localChannel)
  195.                 modem.open(65505)
  196.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
  197.                 while tCh1 ~= 65505 or tCh2 ~= 1 do
  198.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  199.                 end
  200.                 publicPrime = textutilsunserialize(tMsg)[1]
  201.                 base = textutilsunserialize(tMsg)[2]
  202.                 debugPrint("yay")
  203.                 debugPrint(base)
  204.                 debugPrint(secretInt)
  205.                 debugPrint(publicPrime)
  206.                 tMyHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
  207.                 debugPrint("yay")
  208.                 local buffer = textutilsunserialize(tMsg)
  209.                 tSharedSecret = baseToPowerMod(buffer[3],secretInt,publicPrime)
  210.                 modem.transmit(65505,2,tMyHalfofTheSecret)
  211.                 print(tSharedSecret)
  212.                 modem.close(65505)
  213.             end
  214.         end
  215.     end
  216.     debugPrint("subRouter")
  217.     debugPrint(textutilsserialize(subRouter))
  218.     debugPrint("Local ch")
  219.     debugPrint(localChannel)
  220. end
  221.  
  222. local function identificationClients()
  223.     local localChannel
  224.     local iBuffer
  225.     serilaizedConfig = textutilsserialize(configuration)
  226.     local mainRouterNotFound = true
  227.     modem.transmit(65522,11,serilaizedConfig)
  228.     while mainRouterNotFound do
  229.         local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  230.         if  pcall(textutilsunserialize,msg) then
  231.             if ch2 == 10 and ch1 == 65522 then
  232.                 modem.transmit(65522,9,serilaizedConfig)
  233.                 subRouterIsregistered = false
  234.                 debugPrint("Identification message is: ")
  235.                 debugPrint(msg)
  236.                 local unserializedMsg = {}
  237.                 unserializedMsg = textutilsunserialize(msg)
  238.                 for i,v in pairs(Clients) do
  239.                     if  v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
  240.                         subRouterIsregistered = true
  241.                         v9 = v[9]
  242.                         iBuffer = i
  243.                     end
  244.                 end
  245.                 if iBuffer == nil then iBuffer = 1 end
  246.                 local msgBufferTemp = textutilsunserialize(msg)
  247.                 debugPrint("Return msg")
  248.                 debugPrint(msg)
  249.                 local msgBuffer = {
  250.                 msgBufferTemp[1],
  251.                 msgBufferTemp[2],
  252.                 msgBufferTemp[3],
  253.                 msgBufferTemp[4],
  254.                 configuration[1],
  255.                 configuration[2],
  256.                 configuration[3],
  257.                 configuration[4],
  258.                 #Clients + 60001,
  259.                 }
  260.                 debugPrint(textutilsserialize(msgBuffer))
  261.                 if subRouterIsregistered == false then
  262.                     Clients[#Clients + 1] = msgBuffer
  263.                     modem.transmit(65526,1,textutilsserialize(Clients))
  264.                     modem.transmit(65523,18,textutilsserialize({msgBufferTemp,msgBuffer[9]}))
  265.                 else
  266.                     modem.transmit(65523,18,textutilsserialize({msgBufferTemp,v9}))
  267.                 end
  268.                 sleep(.05)
  269.                 modem.open(65515)
  270.                 modem.transmit(65515,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
  271.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
  272.                 while tCh1 ~= 65515 or tCh2 ~= 2 do
  273.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  274.                 end
  275.                 sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
  276.                 debugPrint(sharedSecret)
  277.                 modem.close(65515)
  278.                 secrets[iBuffer] = sharedSecret
  279.             elseif ch2 == 12 then
  280.                 local unserializedMsg = {}
  281.                 unserializedMsg = textutilsunserialize(msg)
  282.                 subRouterIsregistered = false
  283.                 for i,v in pairs(Clients) do
  284.                     if  v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
  285.                         subRouterIsregistered = true
  286.                         v9 = v[9]
  287.                         iBuffer = i
  288.                     end
  289.                 end
  290.                 if iBuffer == nil then iBuffer = 1 end
  291.                 local msgBufferTemp = textutilsunserialize(msg)
  292.                 debugPrint("Return msg")
  293.                 debugPrint(msg)
  294.                 local msgBuffer = {
  295.                 msgBufferTemp[1],
  296.                 msgBufferTemp[2],
  297.                 msgBufferTemp[3],
  298.                 msgBufferTemp[4],
  299.                 configuration[1],
  300.                 configuration[2],
  301.                 configuration[3],
  302.                 configuration[4],
  303.                 #Clients + 60001,
  304.                 }
  305.                 debugPrint(textutilsserialize(msgBuffer))
  306.                 if subRouterIsregistered == false then
  307.                     Clients[#Clients + 1] = msgBuffer
  308.                     modem.transmit(65526,1,textutilsserialize(Clients))
  309.                     modem.transmit(65523,18,textutilsserialize({msgBufferTemp,msgBuffer[9]}))
  310.                 else
  311.                     modem.transmit(65523,18,textutilsserialize({msgBufferTemp,v9}))
  312.                 end
  313.                 sleep(.05)
  314.                 modem.open(65515)
  315.                 modem.transmit(65515,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
  316.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
  317.                 while tCh1 ~= 65515 or tCh2 ~= 2 do
  318.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  319.                 end
  320.                 sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
  321.                 debugPrint(sharedSecret)
  322.                 modem.close(65515)
  323.                 secrets[iBuffer] = sharedSecret
  324.             end
  325.         end
  326.     end
  327.     debugPrint("subRouter")
  328.     debugPrint(textutilsserialize(subRouter))
  329.     debugPrint("Local ch")
  330.     debugPrint(localChannel)
  331.     if fs.exists("confTemp") then
  332.         fs.delete("confTemp")
  333.     end
  334.     confTemp = fs.open("confTemp","w")
  335.     confTemp.write(textutilsserialize({configuration[1],configuration[2],configuration[3],configuration[4],subRouter[1],subRouter[2],subRouter[3],subRouter[4],localChannel}))
  336.     confTemp.close()
  337. end
  338.  
  339. function messageForwarder()
  340.     while true do
  341.         local event, side, ch1, ch2, msg, distance = nil, nil, nil, nil, nil, nil
  342.         repeat
  343.             event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  344.         until (ch1 == 65523 and ch2 == 1) or (ch1 == 65525 and ch1 == 1)
  345.         if pcall(textutilsunserialize,msg) then
  346.             local message = textutilsunserialize(msg)
  347.             local destination = message[1]
  348.             debugPrint("dest coord")
  349.             debugPrint(textutilsserialize(destination))
  350.             if ch1 == 65523 then
  351.                 local onLocalNetwork = false
  352.                 local whichClient = 0
  353.                 for i,v in pairs(Clients) do
  354.                     debugPrint(textutilsserialize(v))
  355.                     if tonumber(v[1]) == tonumber(destination[1]) and tonumber(v[2]) == tonumber(destination[2]) and tonumber(v[3]) == tonumber(destination[3]) and tonumber(v[4]) == tonumber(destination[4]) then
  356.                         onLocalNetwork = true
  357.                         whichClient = i
  358.                     end
  359.                 end
  360.                 if onLocalNetwork == true then
  361.                     modem.transmit(Clients[whichClient][9],1,textutilsserialize({destination,message[2],message[6],message[7]}))
  362.                 else
  363.                     modem.transmit(65525,1,textutilsserialize({destination,message[2],mainRouter,configuration,message[5],message[6],message[7]}))
  364.                 end
  365.             elseif ch1 == 65525 then
  366.                 local onLocalNetwork = false
  367.                 local whichClient = 0
  368.                 for i,v in pairs(Clients) do
  369.                     debugPrint(textutilsserialize(v))
  370.                     if v[1] == destination[1] and v[2] == destination[2] and v[3] == destination[3] and v[4] == destination[4] then
  371.                         onLocalNetwork = true
  372.                         whichClient = i
  373.                     end
  374.                 end
  375.                 if onLocalNetwork == true then
  376.                     modem.transmit(Clients[whichClient][9],1,textutilsserialize({destination,message[2],message[6],message[7]}))
  377.                 end
  378.             end
  379.         debugPrint("msg forwarded")
  380.         end
  381.     end
  382. end
  383.  
  384. --Code--
  385.  
  386. clear()
  387. setUp()
  388.  
  389. myHalfOfTheSecret = baseToPowerMod(base,secretInt,publicPrime)
  390.  
  391. parallel.waitForAll(identificationClients,identificationMainRouter,messageForwarder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement