Alakazard12

Bank server

Apr 26th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. local BANK_CHANNEL = 1329
  2.  
  3.  
  4. local function convert(chars,dist,inv)
  5.     local charInt = string.byte(chars);
  6.     for i=1,dist do
  7.         if(inv)then charInt = charInt - 1; else charInt = charInt + 1; end
  8.         if(charInt<32)then
  9.             if(inv)then charInt = 126; else charInt = 126; end
  10.         elseif(charInt>126)then
  11.             if(inv)then charInt = 32; else charInt = 32; end
  12.         end
  13.     end
  14.     return string.char(charInt);
  15. end
  16.  
  17. local function crypt(str,k,inv)
  18.     local enc= "";
  19.     for i=1,#str do
  20.         if(#str-k[5] >= i or not inv)then
  21.             for inc=0,3 do
  22.                 if(i%4 == inc)then
  23.                     enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  24.                     break;
  25.                 end
  26.             end
  27.         end
  28.     end
  29.     if(not inv)then
  30.         for i=1,k[5] do
  31.             enc = enc .. string.char(math.random(32,126));
  32.         end
  33.     end
  34.     return enc;
  35. end
  36.  
  37. local modem
  38.  
  39. local function getModem()
  40.     for i,v in pairs(peripheral.getNames()) do
  41.         if peripheral.getType(v) == "modem" then
  42.             modem = peripheral.wrap(v)
  43.             modem.open(BANK_CHANNEL)
  44.             break
  45.         end
  46.     end
  47. end
  48.  
  49. local keys = {}
  50. local logs = {}
  51. local users = {}
  52.  
  53. local function loadLogs()
  54.     if fs.exists("logs") then
  55.         local file = fs.open("log", "r")
  56.         logs = textutils.unserialize(file.readAll())
  57.         file.close()
  58.     end
  59. end
  60.  
  61. local function loadUsers()
  62.     if fs.exists("users") then
  63.         local file = fs.open("users", "r")
  64.         users = textutils.unserialize(file.readAll())
  65.         file.close()
  66.     end
  67. end
  68.  
  69. local function saveUsers()
  70.     local file = fs.open("users", "w")
  71.     file.write(textutils.serialize(users))
  72.     file.close()
  73. end
  74.  
  75. local function saveLogs()
  76.     local file = fs.open("log", "w")
  77.     file.write(textutils.serialize(logs))
  78.     file.close()
  79. end
  80.  
  81. local function log(text)
  82.     table.insert(logs, text)
  83.     saveLogs()
  84.     print(text)
  85. end
  86.  
  87. local function generateKey()
  88.     return {math.random(1, 255), math.random(1, 255), math.random(1, 255), math.random(1, 255), math.random(1, 255)}
  89. end
  90.  
  91. local function receiveData()
  92.     local event, side, from, id, data
  93.     while true do
  94.         event, side, from, id, data = os.pullEvent("modem_message")
  95.         if from == BANK_CHANNEL then
  96.             if data == "@!)#njsdf)(#84njd)" then -- Renew key
  97.                 keys[id] = generateKey()
  98.                 modem.transmit(BANK_CHANNEL, BANK_CHANNEL, textutils.serialize(keys[id]));
  99.             elseif keys[id] then
  100.                 data = crypt(data, keys[id], true)
  101.                 print(data)
  102.                 data = textutils.unserialize(data)
  103.                 if type(data) == "table" then
  104.                     return id, data
  105.                 end
  106.             end
  107.         end
  108.     end
  109. end
  110.  
  111. local function sendMessage(text, id)
  112.     modem.transmit(BANK_CHANNEL, BANK_CHANNEL, crypt(textutils.serialize({id, text}), myKey))
  113. end
  114.  
  115. local function main() -- The main function
  116.     getModem()
  117.     loadLogs()
  118.     loadUsers()
  119.     if modem == nil then
  120.         log("No modem attached")
  121.         return
  122.     end
  123.  
  124.     while true do
  125.         pcall(function()
  126.             local id, data = receiveData()
  127.            
  128.             if data[1] == "createUser" then
  129.                 if data[2] and data[3] then
  130.                     if not users[data[2]:lower()] then
  131.                         users[data[2]:lower()] = {data[3], 0}
  132.                         sendMessage({true}, id)
  133.                         log("Created user '" .. data[2] .. "'")
  134.                     else
  135.                         sendMessage({false, "User exists"}, id)
  136.                     end
  137.                 else
  138.                     sendMessage({false, "Username or password empty"})
  139.                 end
  140.             end
  141.         end)
  142.     end
  143. end
  144.  
  145. main()
Add Comment
Please, Sign In to add comment