Advertisement
PlowmanPlow

ComputerCraft - Security System

May 15th, 2017
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.61 KB | None | 0 0
  1. local owner = {ign="", uuid=""}
  2. local hasOwner = false
  3. local compID = os.getComputerID()
  4. local compLabel = os.getComputerLabel()
  5. local secret = ""
  6. local password = ""
  7. local proxRadius = 0
  8.  
  9. local sensor = peripheral.find("playerSensor")
  10. if sensor == nil then
  11.    error("No player sensor found. Is one attached?")
  12. end
  13.  
  14. function round(num, idp)
  15.   local mult = 10^(idp or 0)
  16.   return math.floor(num * mult + 0.5) / mult
  17. end
  18.  
  19. local function setCompLabel()
  20.    term.clear()
  21.    term.setCursorPos(1,1)
  22.    print("This computer is not labeled. Unlabeled computers")
  23.    print("will lose all their information when broken. Set")
  24.    print("a label here to ensure that doesn't happen.")
  25.    print()
  26.    local myLabel = ""
  27.    while myLabel == "" do
  28.       print("Enter a computer label:")
  29.       print()
  30.       myLabel = read()
  31.    end
  32.    compLabel = myLabel
  33.    os.setComputerLabel(myLabel)
  34.    return
  35. end
  36.  
  37. local function getProxRadius()
  38.    local filePR = 0
  39.    if fs.exists("proxradius") then
  40.       local file = fs.open("proxradius", "r")
  41.       filePR = tonumber(file.readAll())
  42.       file.close()
  43.       if filePR < 1 or filePR > 64 then
  44.          fs.delete("proxradius")
  45.          filePR = 0
  46.       end
  47.    end
  48.    return filePR
  49. end
  50.  
  51. local function setProxRadius()
  52.    term.clear()
  53.    term.setCursorPos(1,1)
  54.    print("This system will scan for players within")
  55.    print("a defined radius. The minimum radius is")
  56.    print("1 block, and the maximum is 64 blocks.")
  57.    print()
  58.    local scanRadius = 0
  59.    while scanRadius < 1 or scanRadius > 64 do
  60.      print("Please enter the scanning radius between 1 and 64:")
  61.      print()
  62.      scanRadius = math.floor(tonumber(read()))
  63.    end
  64.    proxRadius = scanRadius
  65.    local file = fs.open("proxradius", "w")
  66.    file.write(proxRadius)
  67.    file.close()
  68. end
  69.  
  70. local function getSecret()
  71.    if fs.exists("secret") then
  72.       local file = fs.open("secret", "r")
  73.       secret = file.readAll()
  74.       file.close()
  75.    else
  76.       term.clear()
  77.       term.setCursorPos(1,1)
  78.       print("In order to help protect this")
  79.       print("computer's data you must enter")
  80.       print("a secret word or passphrase below.")
  81.       print("You do not need to remember this.")
  82.       print()
  83.       print("Enter your secret:")
  84.       secret = read("*")
  85.       local file = fs.open("secret", "w")
  86.       file.write(secret)
  87.       file.close()
  88.    end
  89.    return
  90. end
  91.  
  92. local function getTime()
  93.    local response = http.get("http://www.circlecraft.info/ccapi/cctime.php")
  94.    local contents = response.readAll()
  95.    response.close()
  96.    return contents
  97. end
  98.  
  99. local function getOwner(ign)
  100.    local response = http.get("http://www.circlecraft.info/ccapi/secsys_getowner.php?ign=" .. ign)
  101.    local contents = response.readAll()
  102.    response.close()
  103.    if contents == "Error: invalid request" then
  104.       error("Error: getOwner failed with invalid request")
  105.       os.exit()
  106.    end
  107.    local retData = {ign="", uuid=""}
  108.    local data = textutils.unserialize(contents)
  109.    if data.uuid ~= "" then
  110.       retData = data
  111.    end
  112.    return retData
  113. end
  114.  
  115. local function getComputerOwner()
  116.    local response = http.get("http://www.circlecraft.info/ccapi/secsys_getcompowner.php?id=" .. compID)
  117.    local contents = response.readAll()
  118.    response.close()
  119.    if contents == "Error: invalid request" then
  120.       error("Error: getOwner failed with invalid request")
  121.       os.exit()
  122.    end
  123.    local data = textutils.unserialize(contents)
  124.    if data.ign ~= "secsys_unowned" then
  125.       owner.ign = data.ign
  126.       owner.uuid = data.uuid
  127.       hasOwner = true
  128.    end
  129. end
  130.  
  131. local function proxLog(ign, distance)
  132.    local response = http.post("http://www.circlecraft.info/ccapi/secsys_proxlog.php",
  133.              "compid=" .. compID .. "&" ..
  134.              "ign=" .. textutils.urlEncode(tostring(ign)) .. "&" ..
  135.              "distance=" .. distance .. "&" ..
  136.              "secret=" .. textutils.urlEncode(tostring(secret)))
  137.    return
  138. end
  139.  
  140. local function createOwner(ign)
  141.    term.clear()
  142.    term.setCursorPos(1,1)
  143.    print("It seems that this is your first time")
  144.    print("registering a computer with this system.")
  145.    print()
  146.    local goodPass = false
  147.    local pass1 = ""
  148.    while goodPass == false do
  149.       print("Enter a password for your account: ")
  150.       pass1 = read("*")
  151.       print("Confirm the password for your account: ")
  152.       local pass2 = read("*")
  153.       if pass1 ~= pass2 and pass1 ~= "" then
  154.          print("Error: passwords do not match!!")
  155.          print()
  156.       else
  157.          goodPass = true
  158.       end
  159.    end
  160.    password = pass1
  161.    local response = http.post("http://www.circlecraft.info/ccapi/secsys_createowner.php",
  162.              "ign=" .. tostring(ign) .. "&" ..
  163.              "pass=" .. textutils.urlEncode(tostring(pass1)))
  164.    local data = textutils.unserialize(response.readAll())
  165.    if data.errorNum > 0 then
  166.       error("Error saving new owner account")
  167.    end
  168.    return data
  169. end
  170.  
  171. local function setComputerOwner()
  172.    term.clear()
  173.    term.setCursorPos(1,1)
  174.    print("Select the owner of this computer from")
  175.    print("the list of players below:")
  176.    print()
  177.    local players = sensor.getNearbyPlayers(2)
  178.    for index, player in ipairs(players) do
  179.       print(index .. ". " .. player.player)
  180.    end
  181.    print()
  182.    print("Choose a number from the list above:")
  183.    local chosen = false
  184.    while chosen == false do
  185.       local event, p1, p2, p3, p4 = os.pullEvent("key")
  186.       local keyVal = tonumber(p1) - 1
  187.       if keyVal <= #players then
  188.          os.pullEvent("char")
  189.          owner.ign = players[keyVal].player
  190.          local newOwner = getOwner(owner.ign)
  191.          if newOwner.ign == "" then
  192.             newOwner = createOwner(owner.ign)
  193.          else
  194.             print("Enter the password for your account: ")
  195.             print()
  196.             password = read("*")
  197.          end
  198.          print()
  199.          local response = http.post("http://www.circlecraft.info/ccapi/secsys_setowner.php",
  200.                    "id=" .. tostring(compID) .. "&" ..
  201.                    "owner=" .. tostring(owner.ign) .. "&" ..
  202.                    "pass=" .. textutils.urlEncode(tostring(password)) .. "&" ..
  203.                    "secret=" .. textutils.urlEncode(tostring(secret)))
  204.          local contents = response.readAll()
  205.          local data = textutils.unserialize(contents)
  206.          if data.errorNum == 1 then
  207.             print("Invalid request, rebooting...")
  208.             sleep(2)
  209.             os.reboot()
  210.          elseif data.errorNum == 2 or data.errorNum == 4 then
  211.             print("Couldn't verify account, rebooting...")
  212.             sleep(2)
  213.             os.reboot()
  214.          elseif data.errorNum == 3 then
  215.             print("Couldn't save owner data, rebooting...")
  216.             sleep(2)
  217.             os.reboot()
  218.          elseif data.errorNum == 4 then
  219.             print("Computer already registered, rebooting...")
  220.             sleep(2)
  221.             os.reboot()
  222.          elseif data.errorNum == 0 then
  223.             owner.uuid = data.uuid
  224.             print("Found player UUID: " .. data.uuid)
  225.             print("Set owner to: " .. data.ign)
  226.             sleep(2)
  227.          else
  228.             error(contents)
  229.          end
  230.          return
  231.       end
  232.    end
  233. end
  234.  
  235. local function showMain()
  236.    term.clear()
  237.    term.setCursorPos(1,1)
  238.    print("Player Proximity Security System")
  239.    print()
  240.    print("Current Owner: " .. owner.ign)
  241.    print("Current Scan Radius: " .. proxRadius)
  242.    print()
  243.    print("Press 'q' to exit")
  244. --   print("Press 'o' to change owner")
  245.    print()
  246.    print("Visit this url to view this computer's log:")
  247.    print()
  248.    print("https://www.circlecraft.info/secsys/")
  249.    print()
  250.    print("Select ID# " .. compID .. " from the menu in your browser")
  251. end
  252.  
  253. if compLabel == nil then setCompLabel() end
  254. getComputerOwner()
  255. getSecret()
  256. if hasOwner == false then setComputerOwner() end
  257. proxRadius = getProxRadius()
  258. if proxRadius == 0 then setProxRadius() end
  259. showMain()
  260. local pollTimer = os.startTimer(0)
  261. while true do
  262.    local event, p1, p2, p3, p4 = os.pullEvent()
  263.    if event == "key" then
  264.       os.pullEvent("char")
  265.       if p1 == keys.q then
  266.          term.setCursorPos(1,11)
  267.          return
  268. --      elseif p1 == keys.o then
  269. --         setOwner()
  270. --         showMain()
  271.       end
  272.    elseif event == "timer" and p1 == pollTimer then
  273.       local players = sensor.getNearbyPlayers(proxRadius)
  274.       for index, player in ipairs(players) do
  275.          if player.player ~= owner.ign then
  276.             proxLog(player.player, player.distance)
  277. --            print("Player: " .. player.player .. "  Distance: " .. round(player.distance, 1))
  278.          end
  279.       end
  280.       pollTimer = os.startTimer(5)
  281.    end
  282. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement