Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dns = {}
- local component = require("component")
- local event = require("event")
- local ser = require("serialization")
- local computer = require("computer")
- local thread = require("thread")
- local modem = component.modem
- local data = component.data
- function dns.dnsConnection(address, port)
- if address then
- local Connection = {address = address, publicKey = nil, privateKey = nil, key = nil, port = (port or 65535)}
- function Connection:dns(domain)
- if not modem then return nil, "MAT1:Network card could not be found" end
- local dnsRequest = ser.serialize({ReqType = "DNS", Parameters = {Domain = domain}})
- if modem.send(self.address, self.port, dnsRequest) then
- modem.open(self.port)
- local ret, err
- local thr = thread.create(function()
- local _, _, remoteAddr, _, _, message = event.pull("modem_message")
- if remoteAddr == self.address then
- tbl = ser.unserialize(message)
- if tbl then
- if tbl.RespType == "DNS" then
- if type(tbl.Parameters.Address) == "string" then
- ret = tbl.Parameters.Address
- else
- err = "Address type is wrong"
- end
- elseif tbl.RespType == "DNSERROR" then
- err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
- else
- err = "INT5:DNS response unknown"
- end
- else
- err = "INT4:DNS data could not be unserialized"
- end
- else
- err = "INT3:Response was recieved from wrong host"
- end
- end)
- if not thr:join(5) then
- thr:kill()
- end
- modem.close(self.port)
- if not ret and not err then
- return nil, "INT2:DNS Server did not respond"
- end
- return ret, err
- else
- return nil, "INT1:Modem message could not be sent"
- end
- end
- function Connection:secdns()
- if not modem then return false, "MAT1:Network card could not be found" end
- if not data then return false, "MAT2:Data card could not be found" end
- if not (self.publicKey and self.privateKey) then
- self.publicKey, self.privateKey = data.generateKeyPair()
- end
- local dnsRequest = ser.serialize({ReqType = "SECDNS", Parameters = {Key = self.publicKey.serialize()}})
- if modem.send(self.address, self.port, dnsRequest) then
- modem.open(self.port)
- local key, err
- local thr = thread.create(function()
- local _, _, remoteAddr, _, _, message = event.pull("modem_message")
- if remoteAddr == self.address then
- tbl = ser.unserialize(message)
- if tbl then
- if tbl.RespType == "SECDNS" then
- if type(tbl.Parameters.Key) == "string" then
- key = tbl.Parameters.Key
- else
- err = "Key type is wrong"
- end
- elseif tbl.RespType == "DNSERROR" then
- err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
- else
- err = "INT5:DNS response unknown"
- end
- else
- err = "INT4:DNS data could not be unserialized"
- end
- else
- err = "INT3:Response was recieved from wrong host"
- end
- end)
- if not thr:join(5) then
- thr:kill()
- end
- modem.close(self.port)
- if not key and not err then
- return false, "INT2:DNS Server did not respond"
- elseif not key then
- return false, err
- end
- local serverKey = data.deserializeKey(key, "ec-public")
- if(not serverKey) then
- return false, "INT6:Server key could not be unserialized"
- end
- self.key = data.md5(data.sha256(data.ecdh(self.privateKey, serverKey)))
- return true
- else
- return false, "INT1:Modem message could not be sent"
- end
- end
- function Connection:adddns(domain, address, owner, password)
- if not modem then return false, "MAT1:Network card could not be found" end
- if not data then return false, "MAT2:Data card could not be found" end
- if not self.key then return false, "INT7:SECDND not registered" end
- local iv = data.random(16)
- local dnsRequest = ser.serialize({ ReqType = "ADDDNS", Parameters = { Domain = domain, Address = address, Owner = owner, Password = { Password = data.encrypt(password, self.key, iv), IV = iv } } })
- if modem.send(self.address, self.port, dnsRequest) then
- modem.open(self.port)
- local rep, err = false, nil
- local thr = thread.create(function()
- local _, _, remoteAddr, _, _, message = event.pull("modem_message")
- if remoteAddr == self.address then
- tbl = ser.unserialize(message)
- if tbl then
- if tbl.RespType == "ADDDNS" then
- rep = true
- elseif tbl.RespType == "DNSERROR" then
- err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
- else
- err = "INT5:DNS response unknown"
- end
- else
- err = "INT4:DNS data could not be unserialized"
- end
- else
- err = "INT3:Response was recieved from wrong host"
- end
- end)
- if not thr:join(5) then
- thr:kill()
- end
- modem.close(self.port)
- if not rep and not err then
- return false, "INT2:DNS Server did not respond"
- end
- return rep, err
- else
- return false, "INT1:Modem message could not be sent"
- end
- end
- function Connection:deldns(domain, password)
- if not modem then return false, "MAT1:Network card could not be found" end
- if not data then return false, "MAT2:Data card could not be found" end
- if not self.key then return false, "INT7:SECDNS not registered" end
- local iv = data.random(16)
- local dnsRequest = ser.serialize({ ReqType = "DELDNS", Parameters = { Domain = domain, Password = { Password = data.encrypt(password, self.key, iv), IV = iv } } })
- if modem.send(self.address, self.port, dnsRequest) then
- modem.open(self.port)
- local rep, err = false, nil
- local thr = thread.create(function()
- local _, _, remoteAddr, _, _, message = event.pull("modem_message")
- if remoteAddr == self.address then
- tbl = ser.unserialize(message)
- if tbl then
- if tbl.RespType == "DELDNS" then
- rep = true
- elseif tbl.RespType == "DNSERROR" then
- err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
- else
- err = "INT5:DNS response unknown"
- end
- else
- err = "INT4:DNS data could not be unserialized"
- end
- else
- err = "INT3:Response was recieved from wrong host"
- end
- end)
- if not thr:join(5) then
- thr:kill()
- end
- modem.close(self.port)
- if not rep and not err then
- return false, "INT2:DNS Server did not respond"
- end
- return rep, err
- else
- return false, "INT1:Modem message could not be sent"
- end
- end
- function Connection:owndns(domain, password)
- if not modem then return false, "MAT1:Network card could not be found" end
- if not data then return false, "MAT2:Data card could not be found" end
- if not self.key then return false, "INT7:SECDND not registered" end
- local iv = data.random(16)
- local dnsRequest = ser.serialize({ ReqType = "OWNDNS", Parameters = { Domain = domain, Password = { Password = data.encrypt(password, self.key, iv), IV = iv } } })
- if modem.send(self.address, self.port, dnsRequest) then
- modem.open(self.port)
- local rep, err = nil, nil
- local thr = thread.create(function()
- local _, _, remoteAddr, _, _, message = event.pull("modem_message")
- if remoteAddr == self.address then
- tbl = ser.unserialize(message)
- if tbl then
- if tbl.RespType == "OWNDNS" then
- rep = tbl.Parameters.Owner
- elseif tbl.RespType == "DNSERROR" then
- err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
- else
- err = "INT5:DNS response unknown"
- end
- else
- err = "INT4:DNS data could not be unserialized"
- end
- else
- err = "INT3:Response was recieved from wrong host"
- end
- end)
- if not thr:join(5) then
- thr:kill()
- end
- modem.close(self.port)
- if not rep and not err then
- return nil, "INT2:DNS Server did not respond"
- end
- if not rep then
- return rep, err
- end
- return data.decrypt(rep.Owner, self.key, rep.IV)
- else
- return false, "INT1:Modem message could not be sent"
- end
- end
- return Connection
- else
- return nil, "INT0:Address cannot be nil"
- end
- end
- return dns
Advertisement
Add Comment
Please, Sign In to add comment