Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------IPnet SubRouter v1.1-------
- ---------------Program---------------
- --------------by Creator-------------
- --Variables--
- local mainRouter = {}
- local configuration = {}
- local debugOpt = true
- local Clients = {}
- local textutilsserialize = textutils.serialize
- local textutilsunserialize = textutils.unserialize
- local publicPrime = 625210769
- local base = 11
- local secretInt = math.random(100,9999)
- local secrets = {}
- local version = "1.1"
- local localChannel = nil
- local myHalfOfTheSecret = nil
- local tMyHalfofTheSecret = nil
- --Too many variables--
- --Functions--
- local function clear()
- term.setCursorPos(1,1)
- term.clear()
- end
- local function baseToPowerMod(mBase,power,modulus)
- --This function was taken from Anavrins' Diffie-Hellman proof of concept
- --Full code here:http://pastebin.com/H3kZHZBA
- local remainder = mBase
- print("mbase")
- print(mBase)
- print(remainder)
- for i = 1, power-1 do
- remainder = remainder * remainder
- if remainder >= modulus then
- remainder = remainder % modulus
- end
- end
- return remainder
- end
- local function debugPrint(txt)
- if debugOpt then
- print(txt)
- end
- end
- local function getModemSide()
- local sides = {"bottom","right","left","front","back","top"}
- local side = {}
- for i,v in pairs(rs.getSides()) do
- if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
- debugPrint("side:")
- debugPrint(v)
- return v
- end
- end
- debugPrint("Got modem side")
- if #side == 0 then return nil end
- end
- local function getIP()
- local x , y , z = gps.locate()
- ip = {x,y,z,os.getComputerID()}
- debugPrint("Got ip: ")
- debugPrint(ip)
- return textutilsserialize(ip)
- end
- local function setConfig()
- local configs = {}
- configs = textutilsunserialize(getIP())
- configs[5] = getModemSide()
- local configFile = fs.open("config","w")
- configFile.write(textutilsserialize(configs))
- configFile.close()
- debugPrint("Wrote configs: ")
- debugPrint(configs)
- end
- local function setUp()
- if fs.exists("config") then
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- debugPrint("File exists")
- else
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- debugPrint("File does not exist")
- end
- if tempConfigs[5] == nil then
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- elseif peripheral.isPresent(tempConfigs[5]) == false then
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- end
- if tempConfigs[5] == nil then
- print("No modem attached")
- error()
- end
- modem = peripheral.wrap(tempConfigs[5])
- local idChannelMain = 65524
- local msgChannelMain = 65525
- local idCannelClient = 65522
- local msgChannelClient = 65523
- modem.open(idChannelMain)
- modem.open(msgChannelMain)
- modem.open(idCannelClient)
- modem.open(msgChannelClient)
- debugPrint("Opened channels")
- debugPrint(idChannelMain)
- debugPrint(msgChannelMain)
- debugPrint(idCannelClient)
- debugPrint(msgChannelClient)
- configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
- end
- local function IPsender(a,b,c,d,e,f,g)
- local serializedMsgBuffer = {a,b,c,d,e,f,g}
- local serializedMsg = textutilsserialize(serializedMsgBuffer)
- modem.transmit(65525,1,serializedMsg)
- end
- local function identificationMainRouter()
- serilaizedConfig = textutilsserialize(configuration)
- local mainRouterNotFound = true
- modem.transmit(65524,6,serilaizedConfig)
- while mainRouterNotFound do
- local event, side, ch1, ch2, msg, distance = nil, nil, nil, nil, nil, nil
- repeat
- event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- until ch1 == 65524
- if pcall(textutilsunserialize,msg) then
- if ch2 == 5 then
- mainRouter = textutilsunserialize(msg)
- debugPrint("Channel 2 valid")
- mainRouterNotFound = false
- local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = nil, nil, nil, nil, nil, nil
- repeat
- eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
- until ch1Two == 65524 and ch2Two == 23
- debugPrint("msg")
- debugPrint(msgTwo)
- localChannel = msgTwo
- debugPrint("Opened on channel "..localChannel)
- modem.open(65505)
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
- while tCh1 ~= 65505 or tCh2 ~= 1 do
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- end
- publicPrime = textutilsunserialize(tMsg)[1]
- base = textutilsunserialize(tMsg)[2]
- debugPrint("yay")
- debugPrint(base)
- debugPrint(secretInt)
- debugPrint(publicPrime)
- tMyHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
- debugPrint("yay")
- local buffer = textutilsunserialize(tMsg)
- tSharedSecret = baseToPowerMod(buffer[3],secretInt,publicPrime)
- modem.transmit(65505,2,tMyHalfofTheSecret)
- print(tSharedSecret)
- modem.close(65505)
- elseif ch2 == 7 then
- modem.transmit(65524,8,serilaizedConfig)
- mainRouter = textutilsunserialize(msg)
- debugPrint("Channel 2 valid")
- mainRouterNotFound = false
- local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = nil, nil, nil, nil, nil, nil
- repeat
- eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
- until ch1Two == 65524 and ch2Two == 23
- debugPrint("msg")
- debugPrint(msgTwo)
- localChannel = msgTwo
- debugPrint("Opened on channel "..localChannel)
- modem.open(65505)
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
- while tCh1 ~= 65505 or tCh2 ~= 1 do
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- end
- publicPrime = textutilsunserialize(tMsg)[1]
- base = textutilsunserialize(tMsg)[2]
- debugPrint("yay")
- debugPrint(base)
- debugPrint(secretInt)
- debugPrint(publicPrime)
- tMyHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
- debugPrint("yay")
- local buffer = textutilsunserialize(tMsg)
- tSharedSecret = baseToPowerMod(buffer[3],secretInt,publicPrime)
- modem.transmit(65505,2,tMyHalfofTheSecret)
- print(tSharedSecret)
- modem.close(65505)
- end
- end
- end
- debugPrint("subRouter")
- debugPrint(textutilsserialize(subRouter))
- debugPrint("Local ch")
- debugPrint(localChannel)
- end
- local function identificationClients()
- local localChannel
- local iBuffer
- serilaizedConfig = textutilsserialize(configuration)
- local mainRouterNotFound = true
- modem.transmit(65522,11,serilaizedConfig)
- while mainRouterNotFound do
- local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- if pcall(textutilsunserialize,msg) then
- if ch2 == 10 and ch1 == 65522 then
- modem.transmit(65522,9,serilaizedConfig)
- subRouterIsregistered = false
- debugPrint("Identification message is: ")
- debugPrint(msg)
- local unserializedMsg = {}
- unserializedMsg = textutilsunserialize(msg)
- for i,v in pairs(Clients) do
- if v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
- subRouterIsregistered = true
- v9 = v[9]
- iBuffer = i
- end
- end
- if iBuffer == nil then iBuffer = 1 end
- local msgBufferTemp = textutilsunserialize(msg)
- debugPrint("Return msg")
- debugPrint(msg)
- local msgBuffer = {
- msgBufferTemp[1],
- msgBufferTemp[2],
- msgBufferTemp[3],
- msgBufferTemp[4],
- configuration[1],
- configuration[2],
- configuration[3],
- configuration[4],
- #Clients + 60001,
- }
- debugPrint(textutilsserialize(msgBuffer))
- if subRouterIsregistered == false then
- Clients[#Clients + 1] = msgBuffer
- modem.transmit(65526,1,textutilsserialize(Clients))
- modem.transmit(65523,18,textutilsserialize({msgBufferTemp,msgBuffer[9]}))
- else
- modem.transmit(65523,18,textutilsserialize({msgBufferTemp,v9}))
- end
- sleep(.05)
- modem.open(65515)
- modem.transmit(65515,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
- while tCh1 ~= 65515 or tCh2 ~= 2 do
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- end
- sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
- debugPrint(sharedSecret)
- modem.close(65515)
- secrets[iBuffer] = sharedSecret
- elseif ch2 == 12 then
- local unserializedMsg = {}
- unserializedMsg = textutilsunserialize(msg)
- subRouterIsregistered = false
- for i,v in pairs(Clients) do
- if v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
- subRouterIsregistered = true
- v9 = v[9]
- iBuffer = i
- end
- end
- if iBuffer == nil then iBuffer = 1 end
- local msgBufferTemp = textutilsunserialize(msg)
- debugPrint("Return msg")
- debugPrint(msg)
- local msgBuffer = {
- msgBufferTemp[1],
- msgBufferTemp[2],
- msgBufferTemp[3],
- msgBufferTemp[4],
- configuration[1],
- configuration[2],
- configuration[3],
- configuration[4],
- #Clients + 60001,
- }
- debugPrint(textutilsserialize(msgBuffer))
- if subRouterIsregistered == false then
- Clients[#Clients + 1] = msgBuffer
- modem.transmit(65526,1,textutilsserialize(Clients))
- modem.transmit(65523,18,textutilsserialize({msgBufferTemp,msgBuffer[9]}))
- else
- modem.transmit(65523,18,textutilsserialize({msgBufferTemp,v9}))
- end
- sleep(.05)
- modem.open(65515)
- modem.transmit(65515,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
- while tCh1 ~= 65515 or tCh2 ~= 2 do
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- end
- sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
- debugPrint(sharedSecret)
- modem.close(65515)
- secrets[iBuffer] = sharedSecret
- end
- end
- end
- debugPrint("subRouter")
- debugPrint(textutilsserialize(subRouter))
- debugPrint("Local ch")
- debugPrint(localChannel)
- if fs.exists("confTemp") then
- fs.delete("confTemp")
- end
- confTemp = fs.open("confTemp","w")
- confTemp.write(textutilsserialize({configuration[1],configuration[2],configuration[3],configuration[4],subRouter[1],subRouter[2],subRouter[3],subRouter[4],localChannel}))
- confTemp.close()
- end
- function messageForwarder()
- while true do
- local event, side, ch1, ch2, msg, distance = nil, nil, nil, nil, nil, nil
- repeat
- event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- until (ch1 == 65523 and ch2 == 1) or (ch1 == 65525 and ch1 == 1)
- if pcall(textutilsunserialize,msg) then
- local message = textutilsunserialize(msg)
- local destination = message[1]
- debugPrint("dest coord")
- debugPrint(textutilsserialize(destination))
- if ch1 == 65523 then
- local onLocalNetwork = false
- local whichClient = 0
- for i,v in pairs(Clients) do
- debugPrint(textutilsserialize(v))
- 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
- onLocalNetwork = true
- whichClient = i
- end
- end
- if onLocalNetwork == true then
- modem.transmit(Clients[whichClient][9],1,textutilsserialize({destination,message[2],message[6],message[7]}))
- else
- modem.transmit(65525,1,textutilsserialize({destination,message[2],mainRouter,configuration,message[5],message[6],message[7]}))
- end
- elseif ch1 == 65525 then
- local onLocalNetwork = false
- local whichClient = 0
- for i,v in pairs(Clients) do
- debugPrint(textutilsserialize(v))
- if v[1] == destination[1] and v[2] == destination[2] and v[3] == destination[3] and v[4] == destination[4] then
- onLocalNetwork = true
- whichClient = i
- end
- end
- if onLocalNetwork == true then
- modem.transmit(Clients[whichClient][9],1,textutilsserialize({destination,message[2],message[6],message[7]}))
- end
- end
- debugPrint("msg forwarded")
- end
- end
- end
- --Code--
- clear()
- setUp()
- myHalfOfTheSecret = baseToPowerMod(base,secretInt,publicPrime)
- parallel.waitForAll(identificationClients,identificationMainRouter,messageForwarder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement