Advertisement
skypop

ServerLog

Sep 10th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. os.loadAPI("ini")
  2. local modLIP_ServerPatchedVersion = ini.serverPatchedVersion
  3. local reloadUsersTimeout = ini.userlistTimeout
  4. local rsSide = ini.rsOutput
  5. local driveLogs = peripheral.wrap(ini.driveLogs)
  6. local driveUser = peripheral.wrap(ini.driveUser)
  7. local modem = peripheral.wrap(ini.wirelessModem)
  8. local wi = peripheral.find("WorldInterface")
  9. local usersAPI = "/"..driveUser.getMountPath().."/users"
  10. if not os.loadAPI(usersAPI) then
  11.     error("Users API not found",0)
  12.     return
  13. end
  14.  
  15. local _setOutput = rs.setOutput
  16. local _setCursorPos = term.setCursorPos
  17. local _clear,_clearLine = term.clear,term.clearLine
  18. local _write = term.write
  19. local _json = textutils.serializeJSON
  20. local _startTimer = os.startTimer
  21. local _pullEventRaw = os.pullEventRaw
  22. local _loadAPI,_unloadAPI = os.loadAPI,os.unloadAPI
  23. local _format = string.format
  24. local _concat = table.concat
  25. local _match = string.match
  26. local _exists = fs.exists
  27. local _getFreeSpace = fs.getFreeSpace
  28. local _open = fs.open
  29.  
  30. if not (driveLogs and modem and wi) then
  31.     term.setCursorPos(1,1)
  32.     term.clear()
  33.     print(_format([[drive: %s
  34. modem: %s
  35. WorldInterface: %s]],
  36.         driveLogs and "ok" or "missing",
  37.         modem and "ok" or "missing",
  38.         wi and "ok" or "missing")
  39.     )
  40.     error("Missing Peripheral",0)
  41.     return
  42. end
  43. --Sun Sep 04 23:39:44 CEST 2016
  44. local monthNum = {["Jan"]="01",["Feb"]="02",["Mar"]="03",["Apr"]="04",["May"]="05",["Jun"]="06",["Jul"]="07",["Aug"]="08",["Sep"]="09",["Oct"]="10",["Nov"]="11",["Dec"]="12"}
  45. local function dateConv(str)
  46.     local month,day,h,m,s,year = _match(str, "%a+ (%a+) (%d+) (%d+):(%d+):(%d+) CEST (%d+)")
  47.     return _format("%s-%s-%s",tostring(year),monthNum[month],tostring(day)),tostring(h),_format("%s:%s:%s",tostring(h),tostring(m),tostring(s))
  48. end
  49.  
  50. if not driveLogs.getDiskLabel() then
  51.     driveLogs.setDiskLabel("acces_logs")
  52. end
  53. local root = driveLogs.getMountPath()
  54.  
  55. local logBuffer = {}
  56. local function saveLog()
  57.     local _d,_h = dateConv(wi.getRealDate())
  58.     local path = "/"..root.."/".._d.."_".._h
  59.     local fh = _open(path, _exists(path) and "a" or "w")
  60.     fh.write(_concat(logBuffer, "\n"))
  61.     fh.close()
  62.     logBuffer={}
  63. end
  64.  
  65. local function addLog(k,v,t)
  66.     local _d,_,_t = dateConv(wi.getRealDate())
  67.     logBuffer[#logBuffer+1] = _format(
  68.         "%s_%s|%s:%s|%s",
  69.         _d,_t,
  70.         v and "200" or "403", k,
  71.         type(t)=="table" and _json(t) or tostring(t)
  72.     )
  73. end
  74.  
  75. local w,h = term.getSize()
  76.  
  77. local function output(line,str)
  78.     _setCursorPos(1,line)
  79.     _clearLine()
  80.     _write(str)
  81. end
  82.  
  83. local function process(_from, _query, _dist)
  84.     local valid = type(users.key[_query[1]])~="nil" and users.key[_query[1]]
  85.     addLog(_query[1],valid,{ from = _from, query = _query[2], dist = _dist })
  86. end
  87.  
  88. local function logSpace()
  89.     _setOutput(rsSide,_getFreeSpace("/"..root)<1024)
  90. end
  91.  
  92. modem.open(ini.channel)
  93. _setCursorPos(1,1)
  94. _clear()
  95. output(1,"#"..os.getComputerID().." - "..os.getComputerLabel())
  96. local reloadUsers = _startTimer(reloadUsersTimeout)
  97. while true do
  98.     logSpace()
  99.     local e, param, to, from, query, dist = _pullEventRaw()
  100.     if e=="terminate" then
  101.         return
  102.     elseif e=="modem_message" and type(query)=="table" and #query==2 then
  103.         process(from, query, dist)
  104.     elseif e=="timer" then
  105.         if #logBuffer>0 then
  106.             saveLog()
  107.         end
  108.         _unloadAPI(usersAPI) _loadAPI(usersAPI)
  109.         reloadUsers = _startTimer(reloadUsersTimeout)
  110.     end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement