Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------IPnet Client v1.0---------
- -----------------API-----------------
- --------------by Creator-------------
- --Variables--
- local secretInt = math.random(100,9999)
- local base = nil
- local publicPrime = nil
- local subRouter = {}
- local configuration = {}
- local debugOpt = false
- local localChannel = 0
- local textutilsserialize = textutils.serialize
- local textutilsunserialize = textutils.unserialize
- local sharedSecret = nil
- local myHalfofTheSecret = nil
- --Too many variables--
- --Functions--
- local function clear()
- term.setCursorPos(1,1)
- term.clear()
- end
- function baseToPowerMod(base,power,modulus)
- --This function was taken from Anavrins' Diffie-Hellman proof of concept
- --Full code here:http://pastebin.com/H3kZHZBA
- local remainder = base
- 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
- debugPrint("No modem attached")
- error()
- end
- modem = peripheral.wrap(tempConfigs[5])
- local idChannel = 65522
- local msgChannel = 65523
- modem.open(idChannel)
- modem.open(msgChannel)
- debugPrint("Opened channels:")
- debugPrint(idChannel)
- debugPrint(msgChannel)
- configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
- debugPrint("configuration")
- debugPrint(textutilsserialize(configuration))
- end
- local function identificationSubRouter()
- local localChannel
- serilaizedConfig = textutilsserialize(configuration)
- local mainRouterNotFound = true
- modem.transmit(65522,10,serilaizedConfig)
- while mainRouterNotFound do
- local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- if pcall(textutilsunserialize,msg) then
- if ch2 == 9 then
- subRouter = textutilsunserialize(msg)
- debugPrint("Channel 2 valid")
- mainRouterNotFound = false
- local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
- while ch2Two ~= 18 do
- eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
- end
- debugPrint("msg")
- debugPrint(msgTwo)
- local unserializedMsgTwo = textutilsunserialize(msgTwo)
- localChannel = unserializedMsgTwo[2]
- debugPrint("Opened on channel "..localChannel)
- modem.open(65515)
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
- while tCh1 ~= 65515 or tCh2 ~= 1 do
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- end
- publicPrime = textutilsunserialize(tMsg)[1]
- base = textutilsunserialize(tMsg)[2]
- myHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
- sharedSecret = baseToPowerMod(textutilsunserialize(tMsg)[3],secretInt,publicPrime)
- modem.transmit(65515,2,myHalfofTheSecret)
- print(sharedSecret)
- modem.close(65515)
- elseif ch2 == 11 then
- modem.transmit(65522,12,serilaizedConfig)
- subRouter = textutilsunserialize(msg)
- debugPrint("Channel 2 valid")
- mainRouterNotFound = false
- local eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
- while ch2Two ~= 18 do
- eventTwo, sideTwo, ch1Two, ch2Two, msgTwo, distanceTwo = os.pullEvent("modem_message")
- end
- debugPrint("msg")
- debugPrint(msgTwo)
- local unserializedMsgTwo = textutilsunserialize(msgTwo)
- localChannel = unserializedMsgTwo[2]
- debugPrint("Opened on channel "..localChannel)
- modem.open(65515)
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil
- while tCh1 ~= 65515 or tCh2 ~= 1 do
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- end
- publicPrime = textutilsunserialize(tMsg)[1]
- base = textutilsunserialize(tMsg)[2]
- myHalfofTheSecret = baseToPowerMod(base,secretInt,publicPrime)
- sharedSecret = baseToPowerMod(textutilsunserialize(tMsg)[3],secretInt,publicPrime)
- modem.transmit(65515,2,myHalfofTheSecret)
- print(sharedSecret)
- modem.close(65515)
- 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 send(destPc,msg,port)
- local modemSideFile = fs.open("config","r")
- local modemSide = textutilsunserialize(modemSideFile.readAll())
- modemSideFile.close()
- modem = peripheral.wrap(modemSide[5])
- if type(destPc) == "table" then
- if #destPc == 4 then
- local configTempFile = fs.open("confTemp","r")
- local confTemp = textutilsunserialize(configTempFile.readAll())
- configTempFile.close()
- local msgNum = math.random(1,5000)
- 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}))
- else
- debugPrint("Your destination IP is a table with the following content: x, y, z and ID of the receiver.")
- end
- else
- debugPrint("Your destination IP is a table with the following content: x, y, z and ID of the receiver.")
- end
- end
- function receive()
- local event, side, ch1, ch2, msg, distance
- local modemSideFile = fs.open("config","r")
- local modemSide = textutilsunserialize(modemSideFile.readAll())
- modemSideFile.close()
- modem = peripheral.wrap(modemSide[5])
- local configTempFile = fs.open("confTemp","r")
- local confTemp = textutilsunserialize(configTempFile.readAll())
- configTempFile.close()
- modem.open(confTemp[9])
- local continueBool = true
- while continueBool == true do
- event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- debugPrint("recieved msg")
- debugPrint(ch1)
- debugPrint(ch2)
- debugPrint(msg)
- debugPrint(textutilsserialize(confTemp))
- pcall(textutilsunserialize,msg)
- if ch1 == confTemp[9] and ch2 == 1 then
- continueBool = false
- end
- end
- modem.close(confTemp[9])
- return msg
- end
- function toReturn()
- clear()
- setUp()
- identificationSubRouter()
- modem.close(65522)
- modem.close(65523)
- return textutilsserialize(configuration)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement