Advertisement
cadergator10

Security Server

Jun 18th, 2021 (edited)
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local cryptKey = {1, 2, 3, 4, 5}
  2. local modemPort = 199
  3.  
  4. local lockDoors = false
  5. local forceOpen = false
  6.  
  7. local component = require("component")
  8. local event = require("event")
  9. local modem = component.modem
  10. local ser = require ("serialization")
  11. local term = require("term")
  12. local ios = require("io")
  13.  
  14. local version = "5.0"
  15.  
  16. local redstone = {}
  17.  
  18. local function convert( chars, dist, inv )
  19.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  20. end
  21.  
  22.  
  23. local function crypt(str,k,inv)
  24.   local enc= "";
  25.   for i=1,#str do
  26.     if(#str-k[5] >= i or not inv)then
  27.       for inc=0,3 do
  28.     if(i%4 == inc)then
  29.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  30.       break;
  31.     end
  32.       end
  33.     end
  34.   end
  35.   if(not inv)then
  36.     for i=1,k[5] do
  37.       enc = enc .. string.char(math.random(32,126));
  38.     end
  39.   end
  40.   return enc;
  41. end
  42.  
  43. --// exportstring( string )
  44. --// returns a "Lua" portable version of the string
  45. local function exportstring( s )
  46.     s = string.format( "%q",s )
  47.     -- to replace
  48.     s = string.gsub( s,"\\\n","\\n" )
  49.     s = string.gsub( s,"\r","\\r" )
  50.     s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  51.     return s
  52. end
  53. --// The Save Function
  54. function saveTable(  tbl,filename )
  55.     local tableFile = assert(io.open(filename, "w"))
  56.   tableFile:write(ser.serialize(tbl))
  57.   tableFile:close()
  58. end
  59.  
  60. --// The Load Function
  61. function loadTable( sfile )
  62.     local tableFile = io.open(sfile)
  63.     if tableFile ~= nil then
  64.         return ser.unserialize(tableFile:read("*all"))
  65.     else
  66.         return nil
  67.     end
  68. end
  69.  
  70.  
  71. term.clear()
  72. print("Security server version: " .. version)
  73. print("---------------------------------------------------------------------------")
  74.  
  75. local userTable = loadTable("userlist.txt")
  76. local doorTable = loadTable("doorlist.txt")
  77. if userTable == nil then
  78.   userTable = {}
  79. end
  80. if doorTable == nil then
  81.   doorTable = {}
  82. end
  83.  
  84. function getUserName(user)
  85.    for key, value in pairs(userTable) do
  86.     if value.uuid == user then
  87.       return value.name
  88.     end
  89.   end
  90.    return "NilUser"
  91. end
  92.  
  93. function checkUser(user)
  94.   for key, value in pairs(userTable) do
  95.     if value.uuid == user then
  96.       return true, not value.blocked, tonumber(value.level), value.staff
  97.     end
  98.   end
  99.   return false
  100. end
  101.  
  102. function checkMtf(user)
  103.   for key, value in pairs(userTable) do
  104.     if value.uuid == user then
  105.       return true, not value.blocked, value.mtf, value.staff
  106.     end
  107.   end
  108.   return false
  109. end
  110.  
  111. function checkGoi(user)
  112.   for key, value in pairs(userTable) do
  113.     if value.uuid == user then
  114.       return true, not value.blocked, value.goi, value.staff
  115.     end
  116.   end
  117.   return false
  118. end
  119.  
  120. function checkSec(user)
  121.   for key, value in pairs(userTable) do
  122.     if value.uuid == user then
  123.       return true, not value.blocked, value.sec, value.staff
  124.     end
  125.   end
  126.   return false
  127. end
  128.  
  129. function checkUserA(user)
  130.   for key, value in pairs(userTable) do
  131.     if value.uuid == user then
  132.       return true, not value.blocked, tonumber(value.armory), value.staff
  133.     end
  134.   end
  135.   return false
  136. end
  137.  
  138. function checkUserD(user)
  139.   for key, value in pairs(userTable) do
  140.     if value.uuid == user then
  141.       return true, not value.blocked, tonumber(value.department), value.staff
  142.     end
  143.   end
  144.   return false
  145. end
  146.  
  147. function checkLevel(id)
  148.   for key, value in pairs(doorTable) do
  149.     if key == id then
  150.       return tonumber(value)
  151.     end
  152.   end
  153.   return 0
  154. end
  155.  
  156. function checkInt(user)
  157.   for key, value in pairs(userTable) do
  158.     if value.uuid == user then
  159.       return true, not value.blocked, value.int, value.staff
  160.     end
  161.   end
  162.   return false
  163. end
  164.  
  165. function checkStaff(user)
  166.   for key, value in pairs(userTable) do
  167.     if value.uuid == user then
  168.       return true, not value.blocked, value.staff
  169.     end
  170.   end
  171.   return false
  172. end
  173.  
  174. function checkLink(user)
  175.   for key, value in pairs(userTable) do
  176.     if value.link == user then
  177.       return true, not value.blocked, value.name
  178.     end
  179.   end
  180.   return false
  181. end
  182.  
  183.   if modem.isOpen(198) == false then
  184.     modem.open(198)
  185.   end
  186.   if modem.isOpen(197) == false then
  187.     modem.open(197)
  188.   end
  189. redstone = {}
  190. redstone["lock"] = false
  191. redstone["forceopen"] = false
  192. while true do
  193.   if modem.isOpen(modemPort) == false then
  194.     modem.open(modemPort)
  195.   end
  196.  
  197.   local _, _, from, port, _, command, msg, bypassLock = event.pull("modem_message")
  198.   local data = msg
  199.   if command == "updatedoors" then
  200.    
  201.   else
  202.     data = crypt(msg, cryptKey, true)
  203.   end
  204.   local thisUserName = getUserName(data)
  205.   if command == "updatedoors" then
  206.      os.execute("pastebin get -f QxnRYA3B ctrl.lua")
  207.      local filetemp = ios.open("/mnt/b93/ctrl.lua","r")
  208.      local file = filetemp:read("*a")
  209.      data = crypt(tostring(file),cryptKey)
  210.      modem.broadcast(198, "update", data)
  211.      os.execute("pastebin get -f sHfsrt2M ctrlE.lua")
  212.      filetemp = ios.open("/mnt/b93/ctrlE.lua","r")
  213.      file = filetemp:read("*a")
  214.      data = crypt(tostring(file),cryptKey)
  215.      modem.broadcast(197, "update", data)
  216.      term.write("Updating door command received\n")
  217.   elseif command == "updateuser" then
  218.     userTable = ser.unserialize(data)
  219.     term.write("Updated userlist received\n")
  220.     saveTable(userTable, "userlist.txt")
  221.   elseif command == "setlevel" then
  222.     term.write("Received level from door: " .. data .. "\n")
  223.     doorTable[from] = data
  224.     saveTable(doorTable, "doorlist.txt")
  225.   elseif command == "redstoneUpdated" then
  226.         term.write("Redstone has been updated")
  227.         local newRed = ser.unserialize(data)
  228.         if newRed["lock"] ~= redstone["lock"] then
  229.             lockDoors = newRed["lock"]
  230.         else
  231.            
  232.         end
  233.         if newRed["forceopen"] ~= redstone["forceopen"] then
  234.             forceopen = newRed["forceopen"]
  235.             if forceopen == true then
  236.                 data = crypt("open",cryptKey)
  237.                 modem.broadcast(198,"forceopen",data)
  238.                 modem.broadcast(197,"forceopen",data)
  239.             else
  240.                 data = crypt("close",cryptKey)
  241.                 modem.broadcast(198,"forceopen",data)
  242.                 modem.broadcast(197,"forceopen",data)
  243.             end
  244.         else
  245.            
  246.         end
  247.         redstone = newRed
  248.   elseif command == "checkuser" then
  249.     if lockDoors == true and bypassLock ~= 1 then
  250.         term.write("Doors have been locked. Unable to open door\n")
  251.         data = crypt("locked", cryptKey)
  252.         modem.send(from, port, data)
  253.     else
  254.     term.write("Checking " .. thisUserName .. "'s access level:")
  255.     local cu, isBlocked, level, isStaff = checkUser(data)
  256.     if cu == true then          -- user found
  257.         if isBlocked == false then
  258.             data = crypt("false", cryptKey)
  259.             term.write(" user is blocked\n")
  260.             modem.send(from, port, data)
  261.         else
  262.             local cl = checkLevel(from)
  263.             if cl > level then
  264.                 if isStaff == true then
  265.                     data = crypt("true", cryptKey)
  266.                     term.write(" access granted due to staff\n")
  267.                     modem.send(from, port, data)        
  268.                 else
  269.                     data = crypt("false", cryptKey)
  270.                     term.write(" user level is too low\n")
  271.                     modem.send(from, port, data)            
  272.                 end
  273.             else
  274.                 data = crypt("true", cryptKey)
  275.                 term.write(" access granted\n")
  276.                 modem.send(from, port, data)
  277.             end
  278.         end
  279.     else
  280.       data = crypt("false", cryptKey)
  281.       term.write(" user not found\n")
  282.       modem.send(from, port, data)
  283.     end
  284.     end
  285.     elseif command == "checkMtf" then
  286.         if lockDoors == true and bypassLock ~= 1 then
  287.         term.write("Doors have been locked. Unable to open door\n")
  288.         data = crypt("locked", cryptKey)
  289.         modem.send(from, port, data)
  290.     else
  291.         term.write("Checking if user " .. thisUserName .. " is MTF:")
  292.         local cu, isBlocked, isMtf, isStaff = checkMtf(data)
  293.         if cu == true then
  294.             if isBlocked == false then
  295.             data = crypt("false", cryptKey)
  296.             term.write(" user is blocked\n")
  297.             modem.send(from, port, data)
  298.             else
  299.             if isMtf == true then
  300.             data = crypt("true", cryptKey)
  301.             term.write(" access granted\n")
  302.             modem.send(from, port, data)
  303.             else
  304.                 if isStaff == true then
  305.                     data = crypt("true", cryptKey)
  306.                     term.write(" access granted due to staff\n")
  307.                     modem.send(from, port, data)        
  308.                 else
  309.                     data = crypt("false", cryptKey)
  310.                     term.write(" access denied\n")
  311.                     modem.send(from, port, data)          
  312.                 end
  313.          end
  314.          end
  315.                 else
  316.                 data = crypt("false", cryptKey)
  317.                 term.write(" user not found\n")
  318.                 modem.send(from, port, data)
  319.          end
  320.       end  
  321.         elseif command == "checkgoi" then
  322.         if lockDoors == true and bypassLock ~= 1 then
  323.         term.write("Doors have been locked. Unable to open door\n")
  324.         data = crypt("locked", cryptKey)
  325.         modem.send(from, port, data)
  326.         else
  327.         term.write("Checking if user " .. thisUserName .. " is GOI:")
  328.         local cu, isBlocked, isGoi, isStaff = checkGoi(data)
  329.         if cu == true then
  330.             if isBlocked == false then
  331.             data = crypt("false", cryptKey)
  332.             term.write(" user is blocked\n")
  333.             modem.send(from, port, data)
  334.             else
  335.             if isGoi == true then
  336.             data = crypt("true", cryptKey)
  337.             term.write(" access granted\n")
  338.             modem.send(from, port, data)
  339.             else
  340.             if isStaff == true then
  341.                     data = crypt("true", cryptKey)
  342.                     term.write(" access granted due to staff\n")
  343.                     modem.send(from, port, data)        
  344.                 else
  345.                     data = crypt("false", cryptKey)
  346.                     term.write(" access denied\n")
  347.                     modem.send(from, port, data)          
  348.                 end
  349.          end
  350.          end
  351.                 else
  352.                 data = crypt("false", cryptKey)
  353.                 term.write(" user not found\n")
  354.                 modem.send(from, port, data)
  355.          end
  356.       end  
  357.     elseif command == "checksec" then
  358.         if lockDoors == true and bypassLock ~= 1 then
  359.             term.write("Doors have been locked. Unable to open door\n")
  360.             data = crypt("locked", cryptKey)
  361.             modem.send(from, port, data)
  362.         else
  363.             term.write("Checking if user " .. thisUserName .. " has Security pass:")
  364.             local cu, isBlocked, isSec, isStaff = checkSec(data)
  365.             if cu == true then
  366.                 if isBlocked == false then
  367.                     data = crypt("false", cryptKey)
  368.                     term.write(" user is blocked\n")
  369.                     modem.send(from, port, data)
  370.                 else
  371.                     if isSec == true then
  372.                         data = crypt("true", cryptKey)
  373.                         term.write(" access granted\n")
  374.                         modem.send(from, port, data)
  375.                     else
  376.                         if isStaff == true then
  377.                     data = crypt("true", cryptKey)
  378.                     term.write(" access granted due to staff\n")
  379.                     modem.send(from, port, data)        
  380.                 else
  381.                     data = crypt("false", cryptKey)
  382.                     term.write(" access denied\n")
  383.                     modem.send(from, port, data)          
  384.                 end
  385.                     end
  386.                 end
  387.             else
  388.                 data = crypt("false", cryptKey)
  389.                 term.write(" user not found\n")
  390.                 modem.send(from, port, data)
  391.             end
  392.         end  
  393.     elseif command == "checkarmor" then
  394.         if lockDoors == true and bypassLock ~= 1 then
  395.         term.write("Doors have been locked. Unable to open door\n")
  396.         data = crypt("locked", cryptKey)
  397.         modem.send(from, port, data)
  398.     else
  399.     term.write("Checking " .. thisUserName .. "'s armory level:")
  400.     local cu, isBlocked, level, isStaff = checkUserA(data)
  401.     if cu == true then          -- user found
  402.       if isBlocked == false then
  403.     data = crypt("false", cryptKey)
  404.     term.write(" user is blocked\n")
  405.     modem.send(from, port, data)
  406.       else
  407.     local cl = checkLevel(from)
  408.     if cl > level then
  409.                 if isStaff == true then
  410.                     data = crypt("true", cryptKey)
  411.                     term.write(" access granted due to staff\n")
  412.                     modem.send(from, port, data)        
  413.                 else
  414.                     data = crypt("false", cryptKey)
  415.                     term.write(" users level too low\n")
  416.                     modem.send(from, port, data)        
  417.                 end
  418.     else
  419.       data = crypt("true", cryptKey)
  420.       term.write(" access granted\n")
  421.       modem.send(from, port, data)
  422.     end
  423.       end
  424.     else
  425.       data = crypt("false", cryptKey)
  426.       term.write(" user not found\n")
  427.       modem.send(from, port, data)
  428.     end
  429.     end
  430.     elseif command == "checkdepartment" then
  431.         if lockDoors == true and bypassLock ~= 1 then
  432.         term.write("Doors have been locked. Unable to open door\n")
  433.         data = crypt("locked", cryptKey)
  434.         modem.send(from, port, data)
  435.     else
  436.     term.write("Checking " .. thisUserName .. "'s department:")
  437.     local cu, isBlocked, level, isStaff = checkUserD(data)
  438.     if cu == true then          -- user found
  439.       if isBlocked == false then
  440.     data = crypt("false", cryptKey)
  441.     term.write(" user is blocked\n")
  442.     modem.send(from, port, data)
  443.       else
  444.     local cl = checkLevel(from)
  445.     if cl ~= level and level ~= 5 then
  446.       if isStaff == true then
  447.                     data = crypt("true", cryptKey)
  448.                     term.write(" access granted due to staff\n")
  449.                     modem.send(from, port, data)        
  450.                 else
  451.                     data = crypt("false", cryptKey)
  452.                     term.write(" incorrect department\n")
  453.                     modem.send(from, port, data)    
  454.                 end
  455.     else
  456.       data = crypt("true", cryptKey)
  457.       if cl == 5 then
  458.         term.write(" O5 clearance granted\n")
  459.         modem.send(from, port, data)
  460.       else
  461.         term.write(" access granted\n")
  462.         modem.send(from, port, data)                
  463.       end
  464.     end
  465.       end
  466.     else
  467.       data = crypt("false", cryptKey)
  468.       term.write(" user not found\n")
  469.       modem.send(from, port, data)
  470.     end
  471.    end
  472.        elseif command == "checkint" then
  473.         if false == true and bypassLock ~= 1 then
  474.         term.write("Doors have been locked. Unable to open door\n")
  475.         else
  476.         term.write("Checking if user " .. thisUserName .. " has Intercom pass:")
  477.         local cu, isBlocked, isInt, isStaff = checkInt(data)
  478.         if cu == true then
  479.             if isBlocked == false then
  480.             data = crypt("false", cryptKey)
  481.             term.write(" user is blocked\n")
  482.             modem.send(from, port, data)
  483.             else
  484.             if isInt == true then
  485.             data = crypt("true", cryptKey)
  486.             term.write(" access granted\n")
  487.             modem.send(from, port, data)
  488.             else
  489.                 if isStaff == true then
  490.                     data = crypt("true", cryptKey)
  491.                     term.write(" access granted due to staff\n")
  492.                     modem.send(from, port, data)        
  493.                 else
  494.                     data = crypt("false", cryptKey)
  495.                     term.write(" access denied\n")
  496.                     modem.send(from, port, data)
  497.                 end
  498.          end
  499.          end
  500.                 else
  501.                 data = crypt("false", cryptKey)
  502.                 term.write(" user not found\n")
  503.                 modem.send(from, port, data)
  504.          end
  505.       end  
  506.       elseif command == "checkstaff" then
  507.         if false == true then
  508.         term.write("WHY DOES THIS RUN??? IM SAD :(\n")
  509.         data = crypt("locked", cryptKey)
  510.         modem.send(from, port, data)
  511.         else
  512.         term.write("Checking if user " .. thisUserName .. " is Staff:")
  513.         local cu, isBlocked, isStaff = checkStaff(data)
  514.         if cu == true then
  515.             if isBlocked == false then
  516.             data = crypt("false", cryptKey)
  517.             term.write(" user is blocked\n")
  518.             modem.send(from, port, data)
  519.             else
  520.                 if isStaff == true then
  521.                     data = crypt("true", cryptKey)
  522.                     term.write(" access granted\n")
  523.                     modem.send(from, port, data)        
  524.                 else
  525.                     data = crypt("false", cryptKey)
  526.                     term.write(" access denied\n")
  527.                     modem.send(from, port, data)
  528.                 end
  529.          end
  530.                 else
  531.                 data = crypt("false", cryptKey)
  532.                 term.write(" user not found\n")
  533.                 modem.send(from, port, data)
  534.          end
  535.       end  
  536.     elseif command == "checkLinked" then
  537.         if false == true then
  538.         term.write("DONT RUN or i b sad ;-;\n")
  539.         else
  540.         term.write("Checking test tablet is linked to a user:")
  541.         local cu, isBlocked, thisName = checkLink(data)
  542.         local dis = {}
  543.         if cu == true then
  544.             if isBlocked == false then
  545.             dis["status"] = false
  546.             dis["reason"] = 2
  547.             data = crypt(ser.serialize(dis), cryptKey)
  548.             term.write(" user " .. thisName .. "is blocked\n")
  549.             modem.send(from, port, data)
  550.             else
  551.             dis["status"] = true
  552.             dis["name"] = thisName
  553.             data = crypt(ser.serialize(dis), cryptKey)
  554.             term.write(" tablet is connected to " .. thisName .. "\n")
  555.             modem.send(from, port, data)
  556.          end
  557.                 else
  558.                 dis["status"] = false
  559.                 dis["reason"] = 1
  560.                 data = crypt(ser.serialize(dis), cryptKey)
  561.                 term.write(" tablet not linked\n")
  562.                 modem.send(from, port, data)
  563.          end
  564.       end
  565.    end
  566. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement