Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.4KB

  1. print(pcall(function()
  2. local minitel = require "minitel"
  3. local port = 22
  4. --local logfile = "/boot/termsrv.log"
  5. local oout = io.output()
  6. local pname = os.taskInfo(os.pid()).name
  7. local function sread(self, len)
  8. while true do
  9. local d=self.sock:read(len)
  10. if d then
  11. return d
  12. end
  13. coroutine.yield()
  14. end
  15. end
  16. local function swrite(self, data)
  17. while self.flushing do
  18. coroutine.yield()
  19. end
  20. if data and data:len() > 0 then
  21. self.wb = self.wb .. (data or "")
  22. end
  23. end
  24. local function sclose(self)
  25. self.sock:close()
  26. end
  27. local function sflush(self)
  28. self.flushing = true
  29. self.sock:write(self.wb)
  30. self.wb = ""
  31. self.flushing = false
  32. end
  33. while true do
  34. local sock = minitel.listen(port)
  35. print(string.format("Connection from %s:%d",sock.addr,sock.port))
  36. os.spawn(function() _G.worked = {pcall(function()
  37. local fh = {}
  38. fh.sock = sock
  39. fh.read = sread
  40. fh.write = swrite
  41. fh.close = sclose
  42. fh.flush = sflush
  43. fh.wb = ""
  44. io.input(fh)
  45. io.output(fh)
  46. fh:write(string.format("Connected to %s on port %d\n",os.getenv("HOSTNAME"),sock.port))
  47. local pid = os.spawnfile("/boot/exec/shell.lua")
  48. repeat
  49. coroutine.yield()
  50. if fh.wb:len() > 0 then
  51. fh:flush()
  52. end
  53. until sock.state ~= "open" or not os.taskInfo(pid)
  54. sock:close()
  55. os.kill(pid)
  56. oout:write(string.format("Session %s:%d ended",sock.addr,sock.port))
  57. end)} end,string.format(pname.." [%s:%d]",sock.addr,sock.port))
  58. end
  59. end))