-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.lua
145 lines (127 loc) · 3.66 KB
/
server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
local waitingForIP = false
local ipAddress = nil
local host = nil
local payloads = {}
local headers = {}
local function GetServerIPAddress()
waitingForIP = true
ipAddress = nil
PerformHttpRequest("https://ipinfo.io/ip", function(err, response, headers)
if response[1] == "{" then
ipAddress = json.decode(response).ip
else
ipAddress = response
end
waitingForIP = false
end)
while waitingForIP do
Wait(100)
end
return ipAddress
end
local function EnsureHost()
if not host then
if Logger.ServerName and Logger.ServerName ~= "" then
host = Logger.ServerName
else
host = GetServerIPAddress() .. ":" .. tostring(Logger.ServerPort)
end
end
end
local function HandleGrayLog(message, attributes, permission)
local payload = {
short_message = message,
host = host
}
permission = permission or Logger.StreamFilterValue
if Logger.EnableStreamFilter then
payload["_" .. Logger.StreamFilterKey] = permission
end
if attributes then
for k, v in pairs(attributes) do
payload["_" .. k] = v
end
end
if Logger.Debug then
print("[DEBUG] " .. json.encode(payload))
end
PerformHttpRequest(Logger.Endpoint, nil, "POST", json.encode(payload), headers)
end
local function HandleLokiLog(attributes)
local message = {}
if attributes then
for k, v in pairs(attributes) do
if k ~= 'event' then
message[k] = v
end
end
end
local timestamp = tostring(os.time(os.date("*t")) ) .. "000000000"
local payload = {
stream = {
server = "fivem",
host = host,
type = attributes['event']
},
values = {
{
timestamp,
json.encode(message)
}
}
}
if Logger.Debug then
print("[DEBUG] " .. json.encode(payload))
end
if not payloads[attributes['event']] then
payloads[attributes['event']] = payload
else
local lastIndex = #payloads[attributes['event']].values
payloads[attributes['event']].values[lastIndex+1] = {
timestamp,
json.encode(message)
}
end
end
function LogRaw(message, attributes, permission)
EnsureHost()
if Logger.Target == "GrayLog" then
headers["Content-Type"] = "application/json"
HandleGrayLog(message, attributes, permission)
elseif Logger.Target == "Loki" then
headers["Content-Type"] = "application/json"
HandleLokiLog(attributes)
else
headers = {}
print("[ERROR] Invalid Target: " .. Logger.Target)
end
end
if Logger.Target == 'Loki' then
local function SendData()
local payload = {}
if Logger.Target == "Loki" then
for k,v in pairs(payloads) do
payload[#payload+1] = v
end
payload = { streams = payload }
end
if next(payloads) then
PerformHttpRequest(Logger.Endpoint, function (errorCode, resultData, resultHeaders)
if errorCode and errorCode ~= "204" and errorCode ~= "200" then
print('[DEBUG]', errorCode, " There was an issue posting." )
else
print('SENT LOKI DATA')
end
payloads = {}
end, "POST", json.encode(payload), headers)
end
DataTimer()
end
function DataTimer ()
CreateThread(function()
Wait(Logger.BulkTimer * 1000)
SendData()
end)
end
DataTimer()
end