Mathilde411

dns.lua

Dec 21st, 2019
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.41 KB | None | 0 0
  1. local dns = {}
  2.  
  3. local component = require("component")
  4. local event = require("event")
  5. local ser = require("serialization")
  6. local computer = require("computer")
  7. local thread = require("thread")
  8.  
  9. local modem = component.modem
  10. local data = component.data
  11.  
  12. function dns.dnsConnection(address, port)
  13.     if address then
  14.         local Connection = {address = address, publicKey = nil, privateKey = nil, key = nil, port = (port or 65535)}
  15.  
  16.         function Connection:dns(domain)
  17.             if not modem then return nil, "MAT1:Network card could not be found" end
  18.    
  19.             local dnsRequest = ser.serialize({ReqType = "DNS", Parameters = {Domain = domain}})
  20.             if modem.send(self.address, self.port, dnsRequest) then
  21.                 modem.open(self.port)
  22.                 local ret, err
  23.                 local thr = thread.create(function()
  24.                     local _, _, remoteAddr, _, _, message = event.pull("modem_message")
  25.                     if remoteAddr == self.address then
  26.                         tbl = ser.unserialize(message)
  27.                         if tbl then
  28.                             if tbl.RespType == "DNS" then
  29.                                 if type(tbl.Parameters.Address) == "string" then
  30.                                     ret = tbl.Parameters.Address
  31.                                 else
  32.                                     err = "Address type is wrong"
  33.                                 end
  34.                             elseif tbl.RespType == "DNSERROR" then
  35.                                 err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
  36.                             else
  37.                                 err = "INT5:DNS response unknown"
  38.                             end
  39.                         else
  40.                             err = "INT4:DNS data could not be unserialized"
  41.                         end
  42.                     else
  43.                         err = "INT3:Response was recieved from wrong host"
  44.                     end
  45.                 end)
  46.                 if not thr:join(5) then
  47.                     thr:kill()
  48.                 end
  49.  
  50.                 modem.close(self.port)
  51.  
  52.                 if not ret and not err then
  53.                     return nil, "INT2:DNS Server did not respond"
  54.                 end
  55.                 return ret, err
  56.             else
  57.                 return nil, "INT1:Modem message could not be sent"
  58.             end
  59.         end
  60.  
  61.         function Connection:secdns()
  62.             if not modem then return false, "MAT1:Network card could not be found" end
  63.             if not data then return false, "MAT2:Data card could not be found" end
  64.            
  65.             if not (self.publicKey and self.privateKey) then
  66.                 self.publicKey, self.privateKey = data.generateKeyPair()
  67.             end
  68.  
  69.             local dnsRequest = ser.serialize({ReqType = "SECDNS", Parameters = {Key = self.publicKey.serialize()}})
  70.             if modem.send(self.address, self.port, dnsRequest) then
  71.                 modem.open(self.port)
  72.                 local key, err
  73.                 local thr = thread.create(function()
  74.                     local _, _, remoteAddr, _, _, message = event.pull("modem_message")
  75.                     if remoteAddr == self.address then
  76.                         tbl = ser.unserialize(message)
  77.                         if tbl then
  78.                             if tbl.RespType == "SECDNS" then
  79.                                 if type(tbl.Parameters.Key) == "string" then
  80.                                     key = tbl.Parameters.Key
  81.                                 else
  82.                                     err = "Key type is wrong"
  83.                                 end
  84.                             elseif tbl.RespType == "DNSERROR" then
  85.                                 err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
  86.                             else
  87.                                 err = "INT5:DNS response unknown"
  88.                             end
  89.                         else
  90.                             err = "INT4:DNS data could not be unserialized"
  91.                         end
  92.                     else
  93.                         err = "INT3:Response was recieved from wrong host"
  94.                     end
  95.                 end)
  96.                 if not thr:join(5) then
  97.                     thr:kill()
  98.                 end
  99.  
  100.                 modem.close(self.port)
  101.  
  102.                 if not key and not err then
  103.                     return false, "INT2:DNS Server did not respond"
  104.                 elseif not key then
  105.                     return false, err
  106.                 end
  107.  
  108.                 local serverKey = data.deserializeKey(key, "ec-public")
  109.                 if(not serverKey) then
  110.                     return false, "INT6:Server key could not be unserialized"
  111.                 end
  112.                 self.key = data.md5(data.sha256(data.ecdh(self.privateKey, serverKey)))
  113.                 return true
  114.             else
  115.                 return false, "INT1:Modem message could not be sent"
  116.             end
  117.         end
  118.  
  119.         function Connection:adddns(domain, address, owner, password)
  120.             if not modem then return false, "MAT1:Network card could not be found" end
  121.             if not data then return false, "MAT2:Data card could not be found" end
  122.  
  123.             if not self.key then return false, "INT7:SECDND not registered" end
  124.  
  125.             local iv = data.random(16)
  126.             local dnsRequest = ser.serialize({ ReqType = "ADDDNS", Parameters = { Domain = domain, Address = address, Owner = owner, Password = { Password = data.encrypt(password, self.key, iv), IV = iv } } })
  127.             if modem.send(self.address, self.port, dnsRequest) then
  128.                 modem.open(self.port)
  129.                 local rep, err = false, nil
  130.                 local thr = thread.create(function()
  131.                     local _, _, remoteAddr, _, _, message = event.pull("modem_message")
  132.                     if remoteAddr == self.address then
  133.                         tbl = ser.unserialize(message)
  134.                         if tbl then
  135.                             if tbl.RespType == "ADDDNS" then
  136.                                 rep = true
  137.                             elseif tbl.RespType == "DNSERROR" then
  138.                                 err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
  139.                             else
  140.                                 err = "INT5:DNS response unknown"
  141.                             end
  142.                         else
  143.                             err = "INT4:DNS data could not be unserialized"
  144.                         end
  145.                     else
  146.                         err = "INT3:Response was recieved from wrong host"
  147.                     end
  148.                 end)
  149.                 if not thr:join(5) then
  150.                     thr:kill()
  151.                 end
  152.  
  153.                 modem.close(self.port)
  154.  
  155.                 if not rep and not err then
  156.                     return false, "INT2:DNS Server did not respond"
  157.                 end
  158.  
  159.                 return rep, err
  160.             else
  161.                 return false, "INT1:Modem message could not be sent"
  162.             end
  163.         end
  164.  
  165.         function Connection:deldns(domain, password)
  166.             if not modem then return false, "MAT1:Network card could not be found" end
  167.             if not data then return false, "MAT2:Data card could not be found" end
  168.  
  169.             if not self.key then return false, "INT7:SECDNS not registered" end
  170.  
  171.             local iv = data.random(16)
  172.             local dnsRequest = ser.serialize({ ReqType = "DELDNS", Parameters = { Domain = domain, Password = { Password = data.encrypt(password, self.key, iv), IV = iv } } })
  173.             if modem.send(self.address, self.port, dnsRequest) then
  174.                 modem.open(self.port)
  175.                 local rep, err = false, nil
  176.                 local thr = thread.create(function()
  177.                     local _, _, remoteAddr, _, _, message = event.pull("modem_message")
  178.                     if remoteAddr == self.address then
  179.                         tbl = ser.unserialize(message)
  180.                         if tbl then
  181.                             if tbl.RespType == "DELDNS" then
  182.                                 rep = true
  183.                             elseif tbl.RespType == "DNSERROR" then
  184.                                 err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
  185.                             else
  186.                                 err = "INT5:DNS response unknown"
  187.                             end
  188.                         else
  189.                             err = "INT4:DNS data could not be unserialized"
  190.                         end
  191.                     else
  192.                         err = "INT3:Response was recieved from wrong host"
  193.                     end
  194.                 end)
  195.                 if not thr:join(5) then
  196.                     thr:kill()
  197.                 end
  198.  
  199.                 modem.close(self.port)
  200.  
  201.                 if not rep and not err then
  202.                     return false, "INT2:DNS Server did not respond"
  203.                 end
  204.  
  205.                 return rep, err
  206.             else
  207.                 return false, "INT1:Modem message could not be sent"
  208.             end
  209.         end
  210.  
  211.         function Connection:owndns(domain, password)
  212.             if not modem then return false, "MAT1:Network card could not be found" end
  213.             if not data then return false, "MAT2:Data card could not be found" end
  214.  
  215.             if not self.key then return false, "INT7:SECDND not registered" end
  216.  
  217.             local iv = data.random(16)
  218.             local dnsRequest = ser.serialize({ ReqType = "OWNDNS", Parameters = { Domain = domain, Password = { Password = data.encrypt(password, self.key, iv), IV = iv } } })
  219.             if modem.send(self.address, self.port, dnsRequest) then
  220.                 modem.open(self.port)
  221.                 local rep, err = nil, nil
  222.                 local thr = thread.create(function()
  223.                     local _, _, remoteAddr, _, _, message = event.pull("modem_message")
  224.                     if remoteAddr == self.address then
  225.                         tbl = ser.unserialize(message)
  226.                         if tbl then
  227.                             if tbl.RespType == "OWNDNS" then
  228.                                 rep = tbl.Parameters.Owner
  229.                             elseif tbl.RespType == "DNSERROR" then
  230.                                 err = "DNS" .. tostring(tbl.Parameters.Error.Code) .. ":" .. tbl.Parameters.Error.Message
  231.                             else
  232.                                 err = "INT5:DNS response unknown"
  233.                             end
  234.                         else
  235.                             err = "INT4:DNS data could not be unserialized"
  236.                         end
  237.                     else
  238.                         err = "INT3:Response was recieved from wrong host"
  239.                     end
  240.                 end)
  241.                 if not thr:join(5) then
  242.                     thr:kill()
  243.                 end
  244.  
  245.                 modem.close(self.port)
  246.  
  247.                 if not rep and not err then
  248.                     return nil, "INT2:DNS Server did not respond"
  249.                 end
  250.  
  251.                 if not rep then
  252.                     return rep, err
  253.                 end
  254.  
  255.                 return data.decrypt(rep.Owner, self.key, rep.IV)
  256.             else
  257.                 return false, "INT1:Modem message could not be sent"
  258.             end
  259.         end
  260.  
  261.         return Connection
  262.     else
  263.         return nil, "INT0:Address cannot be nil"
  264.     end
  265. end
  266.  
  267. return dns
Advertisement
Add Comment
Please, Sign In to add comment