Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ini")
- local modLIP_ServerPatchedVersion = ini.serverPatchedVersion
- local reloadUsersTimeout = ini.userlistTimeout
- local rsSide = ini.rsOutput
- local driveLogs = peripheral.wrap(ini.driveLogs)
- local driveUser = peripheral.wrap(ini.driveUser)
- local modem = peripheral.wrap(ini.wirelessModem)
- local wi = peripheral.find("WorldInterface")
- local usersAPI = "/"..driveUser.getMountPath().."/users"
- if not os.loadAPI(usersAPI) then
- error("Users API not found",0)
- return
- end
- local _setOutput = rs.setOutput
- local _setCursorPos = term.setCursorPos
- local _clear,_clearLine = term.clear,term.clearLine
- local _write = term.write
- local _json = textutils.serializeJSON
- local _startTimer = os.startTimer
- local _pullEventRaw = os.pullEventRaw
- local _loadAPI,_unloadAPI = os.loadAPI,os.unloadAPI
- local _format = string.format
- local _concat = table.concat
- local _match = string.match
- local _exists = fs.exists
- local _getFreeSpace = fs.getFreeSpace
- local _open = fs.open
- if not (driveLogs and modem and wi) then
- term.setCursorPos(1,1)
- term.clear()
- print(_format([[drive: %s
- modem: %s
- WorldInterface: %s]],
- driveLogs and "ok" or "missing",
- modem and "ok" or "missing",
- wi and "ok" or "missing")
- )
- error("Missing Peripheral",0)
- return
- end
- --Sun Sep 04 23:39:44 CEST 2016
- 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"}
- local function dateConv(str)
- local month,day,h,m,s,year = _match(str, "%a+ (%a+) (%d+) (%d+):(%d+):(%d+) CEST (%d+)")
- return _format("%s-%s-%s",tostring(year),monthNum[month],tostring(day)),tostring(h),_format("%s:%s:%s",tostring(h),tostring(m),tostring(s))
- end
- if not driveLogs.getDiskLabel() then
- driveLogs.setDiskLabel("acces_logs")
- end
- local root = driveLogs.getMountPath()
- local logBuffer = {}
- local function saveLog()
- local _d,_h = dateConv(wi.getRealDate())
- local path = "/"..root.."/".._d.."_".._h
- local fh = _open(path, _exists(path) and "a" or "w")
- fh.write(_concat(logBuffer, "\n"))
- fh.close()
- logBuffer={}
- end
- local function addLog(k,v,t)
- local _d,_,_t = dateConv(wi.getRealDate())
- logBuffer[#logBuffer+1] = _format(
- "%s_%s|%s:%s|%s",
- _d,_t,
- v and "200" or "403", k,
- type(t)=="table" and _json(t) or tostring(t)
- )
- end
- local w,h = term.getSize()
- local function output(line,str)
- _setCursorPos(1,line)
- _clearLine()
- _write(str)
- end
- local function process(_from, _query, _dist)
- local valid = type(users.key[_query[1]])~="nil" and users.key[_query[1]]
- addLog(_query[1],valid,{ from = _from, query = _query[2], dist = _dist })
- end
- local function logSpace()
- _setOutput(rsSide,_getFreeSpace("/"..root)<1024)
- end
- modem.open(ini.channel)
- _setCursorPos(1,1)
- _clear()
- output(1,"#"..os.getComputerID().." - "..os.getComputerLabel())
- local reloadUsers = _startTimer(reloadUsersTimeout)
- while true do
- logSpace()
- local e, param, to, from, query, dist = _pullEventRaw()
- if e=="terminate" then
- return
- elseif e=="modem_message" and type(query)=="table" and #query==2 then
- process(from, query, dist)
- elseif e=="timer" then
- if #logBuffer>0 then
- saveLog()
- end
- _unloadAPI(usersAPI) _loadAPI(usersAPI)
- reloadUsers = _startTimer(reloadUsersTimeout)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement