Advertisement
Guest User

server.lua

a guest
May 12th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. local TEMP_FILE_PATH = "/scripts/ssh/.temp"
  2.  
  3. local localPrint = print
  4. local oldPull = os.pullEvent
  5. os.pullEvent = os.pullEventRaw
  6. print = myPrint
  7. function myPrint(str)
  8.  local file = io.open(TEMP_FILE_PATH, "a")
  9.  file:write(str.."\n")
  10.  file:close()
  11. end
  12.  
  13.  
  14. local activeConnections = {}
  15.  
  16. function authenticateUser(username, password)
  17.  return true
  18. end
  19.  
  20. function waitForTermination()
  21.  sleep(1)
  22.  print("Enter the root password to terminate")
  23.  while true do
  24.   local password = read("*")
  25.   if password == "teste" then
  26.    localPrint("Session ended")
  27.    return
  28.   else
  29.   localPrint("Wrong password")
  30.   end
  31.  end
  32. end
  33.  
  34. function waitForConnections()
  35.  local senderID, message = rednet.receive("ssh")
  36.  local i = string.find(message, "|")
  37.  if i ~= nil then
  38.   local username = string.sub(message, 1, i-1)
  39.   local password = string.sub(message, i+1)
  40.   if authenticateUser(username, password) then
  41.    rednet.send(senderID, {true, "Access granted"}, "ssh")
  42.    localPrint("Client with id "..senderID.." connected")
  43.    return senderID
  44.   else
  45.    rednet.send(senderID, {false,""}, "ssh")
  46.    localPrint("Client with id "..senderID.." was not authorized")
  47.    return waitForConnections()
  48.   end
  49.  else
  50.   rednet.send(senderID, {false, "Wrong message format"}, "ssh")
  51.   return waitForConnections()
  52.  end
  53. end
  54.  
  55. function shellServer()
  56.  local clientID = nil
  57.  rednet.open("left")
  58.  localPrint("Running SSH host")
  59.  clientID = waitForConnections()
  60.  while true do
  61.   local file = io.open(TEMP_FILE_PATH, "w")
  62.   file:close()
  63.   local senderID, command = rednet.receive("ssh")
  64.   if senderID == clientID then
  65.    --rednet.send(senderID, {true, "Access authorized"}, "ssh")
  66.    shell.run(command)
  67.    file = io.open(TEMP_FILE_PATH, "r")
  68.    local content = file:read("*a")
  69.    rednet.send(senderID, {true, content}, "ssh")
  70.   else
  71.    rednet.send(senderID, {false, "Access not authorized"}, "ssh")
  72.   end
  73.  end
  74. end
  75.  
  76. parallel.waitForAny(waitForTermination, shellServer)
  77.  
  78. print = localPrint
  79. os.pullEvent = oldPull
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement