Mathilde411

BDNS.lua

Dec 21st, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. local DNS_PORT = 65535
  2. local VERSION = "1.0"
  3.  
  4. local computer = require("computer")
  5. local component = require("component")
  6. local event = require("event")
  7. local thread = require("thread")
  8. local ser = require("serialization")
  9. local term = require("term")
  10. local io = require("io")
  11. local filesystem = require("filesystem")
  12.  
  13. local modem = component.modem
  14. local data = component.data
  15.  
  16. local secureConnections = {}
  17. local attempts = {}
  18. local logFile = io.open("/home/dnslogs.log","w")
  19.  
  20. local function strip(str)
  21.     while (string.byte(str, string.len(str)) == 10 or string.byte(str, string.len(str)) == 13) do
  22.         str = string.gsub(str, "\n", "")
  23.         str = string.gsub(str, "\r", "")
  24.     end
  25.     return str
  26. end
  27.  
  28. local function exists(path)
  29.    local f=io.open(path,"r")
  30.    if f~=nil then io.close(f) return true else return false end
  31. end
  32.  
  33. local function log(log)
  34.     local msg = "[" .. os.date("%c") .. "]" .. log
  35.     print(msg)
  36.     logFile:write(msg .. "\n")
  37.     logFile:flush()
  38. end
  39.  
  40. local function DNS(domain)
  41.     local path = "/home/domains/" .. domain .. ".domain"
  42.     if exists(path) then
  43.         l = io.lines(path)
  44.         return l(), l()
  45.     end
  46. end
  47.  
  48. local function getPass()
  49.     local path = "/home/password.pass"
  50.     if exists(path) then
  51.         return io.open(path, "r"):read("*a")
  52.     end
  53. end
  54.  
  55. local function deleteDomain(domain)
  56.     local path = "/home/domains/" .. domain .. ".domain"
  57.     if exists(path) then
  58.         os.remove(path)
  59.         return true
  60.     else
  61.         return false
  62.     end
  63. end
  64.  
  65. local function createDomain(domain, address, owner)
  66.     local path = "/home/domains/" .. domain .. ".domain"
  67.     if not exists(path) then
  68.         local f = io.open(path, "w")
  69.         f:write(address .. "\n" .. owner)
  70.         f:close()
  71.         return true
  72.     else
  73.         return false
  74.     end
  75. end
  76.  
  77. local function checkPassword(addr, password)
  78.     local key = secureConnections[addr]
  79.     if key then
  80.         if type(password.Password) == "string" and type(password.IV) == "string" then
  81.             if not attempts[addr] then
  82.                 attempts[addr] = {NextTry = 0, TriesLeft = 2}
  83.             end
  84.  
  85.             if computer.uptime() >= attempts[addr].NextTry then
  86.                 local decPass = data.decrypt(password.Password, key, password.IV)
  87.                 if data.sha256(decPass) == getPass() then
  88.                     attempts[addr].TriesLeft = 2
  89.                     return true
  90.                 else
  91.                     if attempts[addr].TriesLeft > 0 then
  92.                         attempts[addr].TriesLeft = attempts[addr].TriesLeft - 1
  93.                     else
  94.                         attempts[addr].NextTry = computer.uptime() + 600
  95.                     end
  96.  
  97.                     return false, 25, "Bad password"
  98.                 end
  99.             else
  100.                 return false, 24, "Blocked by filter"
  101.             end
  102.         else
  103.             return false, 22, "Bad password structure"
  104.         end
  105.     else
  106.         return false, 21, "Encryption key not registered"
  107.     end
  108. end
  109.  
  110. local function finalResp(field, addr, message, param)
  111.     log("[" .. addr .. "][" .. field:upper() .. "][INFO] " .. message)
  112.     local mess = ser.serialize({RespType = field:upper(), Parameters = param})
  113.     modem.send(addr, DNS_PORT, mess)
  114. end
  115.  
  116. local function finalErr(field, addr, code, message)
  117.     log("[" .. addr .. "][" .. field:upper() .. "][ERROR][" .. tostring(code) .. "] " .. message)
  118.     local mess = ser.serialize({RespType = "DNSERROR", Parameters = {Error = {Code = code, Message = message}}})
  119.     modem.send(addr, DNS_PORT, mess)
  120. end
  121.  
  122. local function DNSResp(remoteAddr, domain)
  123.     local addr = DNS(domain)
  124.     if addr then
  125.         finalResp("DNS", remoteAddr, "Domain " .. domain .. " --> " .. addr, {Address = addr})
  126.     else
  127.         finalErr("DNS", remoteAddr, 11, "No such domain")
  128.     end
  129. end
  130.  
  131. local function DNSSec(remoteAddr, key, publicKey, privateKey)
  132.     local userKey = data.deserializeKey(key, "ec-public")
  133.     if userKey then
  134.         secureConnections[remoteAddr] = data.md5(data.sha256(data.ecdh(privateKey, userKey)))
  135.         finalResp("SECDNS", remoteAddr, "Encryption key was succesfully created !", {Key = publicKey.serialize()})
  136.     else
  137.         finalErr("SECDNS", remoteAddr, 20, "Public key could not be read")
  138.     end
  139. end
  140.  
  141. local function AddDNS(remoteAddr, encrPass, domain, domAddress, owner)
  142.     val, code, err = checkPassword(remoteAddr, encrPass)
  143.     if val then
  144.         if createDomain(domain, domAddress, owner) then
  145.             finalResp("ADDDNS", remoteAddr, "Domain " .. domain .. " was successfully created", {Message = "Success"})
  146.         else
  147.             finalErr("ADDDNS", remoteAddr, 12, "Domain already exists")
  148.         end
  149.     else
  150.         finalErr("ADDDNS", remoteAddr, code, err)
  151.     end
  152. end
  153.  
  154. local function DelDNS(remoteAddr, encrPass, domain)
  155.     val, code, err = checkPassword(remoteAddr, encrPass)
  156.     if val then
  157.         if deleteDomain(domain) then
  158.             finalResp("DELDNS", remoteAddr, "Domain " .. domain .. " was successfully deleted", {Message = "Success"})
  159.         else
  160.             finalErr("DELDNS", remoteAddr, 11, "No such domain")
  161.         end
  162.     else
  163.         finalErr("DELDNS", remoteAddr, code, err)
  164.     end
  165. end
  166.  
  167. local function OwnDNS(remoteAddr, encrPass, domain)
  168.     val, code, err = checkPassword(remoteAddr, encrPass)
  169.     if val then
  170.         local key = secureConnections[remoteAddr]
  171.         _, owner = DNS(domain)
  172.         if owner then
  173.             local iv = data.random(16)
  174.             finalResp("OWNDNS", remoteAddr, domain .. " owner was succesfully sent", {Owner = { Owner = data.encrypt(owner, key, iv), IV = iv}})
  175.         else
  176.             finalErr("OWNDNS", remoteAddr, 11, "No such domain")
  177.         end
  178.     else
  179.         finalErr("OWNDNS", remoteAddr, code, err)
  180.     end
  181. end
  182.  
  183. local function network()
  184.     modem.open(DNS_PORT)
  185.     publicKey, privateKey = data.generateKeyPair()
  186.     log(" --BDNS v" .. VERSION .. "--")
  187.     while true do
  188.         local event, localAddr, remoteAddr, port, dist, message = event.pull("modem_message")
  189.         local request = ser.unserialize(message)
  190.         if port == DNS_PORT and type(request) == "table" and type(request.ReqType) == "string" and type(request.Parameters) == "table" then
  191.             if request.ReqType:upper() == "DNS" and type(request.Parameters.Domain) == "string" then
  192.                 -- {ReqType = "DNS", Parameters = {Domain = "domain.dom"}}
  193.                 DNSResp(remoteAddr, request.Parameters.Domain)
  194.             elseif request.ReqType:upper() == "SECDNS" and type(request.Parameters.Key) == "string" then
  195.                 -- {ReqType = "SECDNS", Parameters = {Key = "public key"}}
  196.                 DNSSec(remoteAddr, request.Parameters.Key, publicKey, privateKey)
  197.             elseif request.ReqType:upper() == "ADDDNS" and type(request.Parameters.Password) == "table" and type(request.Parameters.Domain) == "string" and type(request.Parameters.Address) == "string" and type(request.Parameters.Owner) == "string" then
  198.                 -- { ReqType = "ADDDNS", Parameters = { Domain = "domain.dom", Address = "a6cc03...", Owner = "lolo", Password = { Password = "encrypted password", IV = "random 16 bytes IV" } } }
  199.                 AddDNS(remoteAddr, request.Parameters.Password, request.Parameters.Domain, request.Parameters.Address, request.Parameters.Owner)
  200.             elseif request.ReqType:upper() == "DELDNS" and type(request.Parameters.Password) == "table" and type(request.Parameters.Domain) == "string" then
  201.                 -- { ReqType = "DELDNS", Parameters = { Domain = "domain.dom", Password = { Password = "encrypted password", IV = "random 16 bytes IV" } } }
  202.                 DelDNS(remoteAddr, request.Parameters.Password, request.Parameters.Domain)
  203.             elseif request.ReqType:upper() == "OWNDNS" and type(request.Parameters.Password) == "table" and type(request.Parameters.Domain) == "string" then
  204.                 -- { ReqType = "OWNDNS", Parameters = { Domain = "domain.dom", Password = { Password = "encrypted password", IV = "random 16 bytes IV" } } }
  205.                 OwnDNS(remoteAddr, request.Parameters.Password, request.Parameters.Domain)
  206.             else
  207.                 finalErr("DNS", remoteAddr, 10, "Malformed request")
  208.             end
  209.         end
  210.     end
  211. end
  212.  
  213. term.clear()
  214.  
  215. if not filesystem.isDirectory("/home/domains") then
  216.     filesystem.makeDirectory("/home/domains")
  217. end
  218.  
  219. if not exists("/home/password.pass") then
  220.     local pass = nil
  221.     local verif = nil
  222.     while (not pass or not verif or pass ~= verif) do
  223.         print("A password must be set to start the DNS Server")
  224.         term.write("Password: ")
  225.         pass = term.read({pwchar="*"})
  226.         pass = strip(pass)
  227.         print()
  228.         term.write("Verification: ")
  229.         verif = term.read({pwchar="*"})
  230.         verif = strip(verif)
  231.         term.clear()
  232.     end
  233.     local f = io.open("/home/password.pass", "w")
  234.     f:write(data.sha256(pass))
  235.     f:close()
  236.     print("The DNS password is now set.")
  237.     print("The server will start in 5 seconds.")
  238.     os.sleep(5)
  239.     term.clear()
  240. end
  241.  
  242. network()
Advertisement
Add Comment
Please, Sign In to add comment