Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cryptKey = {1, 2, 3, 4, 5}
- local modemPort = 199
- local component = require("component")
- local event = require("event")
- local modem = component.modem
- local ser = require ("serialization")
- local term = require("term")
- local function convert( chars, dist, inv )
- return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
- end
- local function crypt(str,k,inv)
- local enc= "";
- for i=1,#str do
- if(#str-k[5] >= i or not inv)then
- for inc=0,3 do
- if(i%4 == inc)then
- enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
- break;
- end
- end
- end
- end
- if(not inv)then
- for i=1,k[5] do
- enc = enc .. string.char(math.random(32,126));
- end
- end
- return enc;
- end
- --// exportstring( string )
- --// returns a "Lua" portable version of the string
- local function exportstring( s )
- s = string.format( "%q",s )
- -- to replace
- s = string.gsub( s,"\\\n","\\n" )
- s = string.gsub( s,"\r","\\r" )
- s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
- return s
- end
- --// The Save Function
- function saveTable( tbl,filename )
- local charS,charE = " ","\n"
- local file,err
- -- create a pseudo file that writes to a string and return the string
- if not filename then
- file = { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
- charS,charE = "",""
- -- write table to tmpfile
- elseif filename == true or filename == 1 then
- charS,charE,file = "","",io.tmpfile()
- -- write table to file
- -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
- else
- file,err = io.open( filename, "w" )
- if err then return _,err end
- end
- -- initiate variables for save procedure
- local tables,lookup = { tbl },{ [tbl] = 1 }
- file:write( "return {"..charE )
- for idx,t in ipairs( tables ) do
- if filename and filename ~= true and filename ~= 1 then
- file:write( "-- Table: {"..idx.."}"..charE )
- end
- file:write( "{"..charE )
- local thandled = {}
- for i,v in ipairs( t ) do
- thandled[i] = true
- -- escape functions and userdata
- if type( v ) ~= "userdata" then
- -- only handle value
- if type( v ) == "table" then
- if not lookup[v] then
- table.insert( tables, v )
- lookup[v] = #tables
- end
- file:write( charS.."{"..lookup[v].."},"..charE )
- elseif type( v ) == "function" then
- file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
- else
- local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
- file:write( charS..value..","..charE )
- end
- end
- end
- for i,v in pairs( t ) do
- -- escape functions and userdata
- if (not thandled[i]) and type( v ) ~= "userdata" then
- -- handle index
- if type( i ) == "table" then
- if not lookup[i] then
- table.insert( tables,i )
- lookup[i] = #tables
- end
- file:write( charS.."[{"..lookup[i].."}]=" )
- else
- local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
- file:write( charS..index.."=" )
- end
- -- handle value
- if type( v ) == "table" then
- if not lookup[v] then
- table.insert( tables,v )
- lookup[v] = #tables
- end
- file:write( "{"..lookup[v].."},"..charE )
- elseif type( v ) == "function" then
- file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
- else
- local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
- file:write( value..","..charE )
- end
- end
- end
- file:write( "},"..charE )
- end
- file:write( "}" )
- -- Return Values
- -- return stringtable from string
- if not filename then
- -- set marker for stringtable
- return file.str.."--|"
- -- return stringttable from file
- elseif filename == true or filename == 1 then
- file:seek ( "set" )
- -- no need to close file, it gets closed and removed automatically
- -- set marker for stringtable
- return file:read( "*a" ).."--|"
- -- close file and return 1
- else
- file:close()
- return 1
- end
- end
- --// The Load Function
- function loadTable( sfile )
- local tables, err, _
- -- catch marker for stringtable
- if string.sub( sfile,-3,-1 ) == "--|" then
- tables,err = loadstring( sfile )
- else
- tables,err = loadfile( sfile )
- end
- if err then return _,err
- end
- tables = tables()
- for idx = 1,#tables do
- local tolinkv,tolinki = {},{}
- for i,v in pairs( tables[idx] ) do
- if type( v ) == "table" and tables[v[1]] then
- table.insert( tolinkv,{ i,tables[v[1]] } )
- end
- if type( i ) == "table" and tables[i[1]] then
- table.insert( tolinki,{ i,tables[i[1]] } )
- end
- end
- -- link values, first due to possible changes of indices
- for _,v in ipairs( tolinkv ) do
- tables[idx][v[1]] = v[2]
- end
- -- link indices
- for _,v in ipairs( tolinki ) do
- tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
- end
- end
- return tables[1]
- end
- term.clear()
- print("Security server")
- print("---------------------------------------------------------------------------")
- local userTable = loadTable("userlist.txt")
- local doorTable = loadTable("doorlist.txt")
- if userTable == nil then
- userTable = {}
- end
- if doorTable == nil then
- doorTable = {}
- end
- function checkUser(user)
- for key, value in pairs(userTable) do
- if value.name == user then
- return true, not value.blocked, tonumber(value.level)
- end
- end
- return false
- end
- function checkLevel(id)
- for key, value in pairs(doorTable) do
- if key == id then
- return tonumber(value)
- end
- end
- return 0
- end
- while true do
- if modem.isOpen(modemPort) == false then
- modem.open(modemPort)
- end
- local _, _, from, port, _, command, msg = event.pull("modem_message")
- local data = crypt(msg, cryptKey, true)
- term.write(from .. ":" .. port .. ":" .. command)
- if command == "updateuser" then
- userTable = ser.unserialize(data)
- term.write("\n")
- saveTable(userTable, "userlist.txt")
- elseif command == "setlevel" then
- term.write(" " .. data .. "\n")
- doorTable[from] = data
- saveTable(doorTable, "doorlist.txt")
- elseif command == "checkuser" then
- term.write(":" .. data .. ":")
- local cu, isBlocked, level = checkUser(data)
- if cu == true then -- user found
- if isBlocked == false then
- data = crypt("false", cryptKey)
- term.write("blocked\n")
- modem.send(from, port, data)
- else
- local cl = checkLevel(from)
- if cl > level then
- data = crypt("false", cryptKey)
- term.write("level too low\n")
- modem.send(from, port, data)
- else
- data = crypt("true", cryptKey)
- term.write("ok\n")
- modem.send(from, port, data)
- end
- end
- else
- data = crypt("false", cryptKey)
- term.write("not found\n")
- modem.send(from, port, data)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement