forked from thisdp/dgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
159 lines (147 loc) · 5.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function outputDGSMessage(message,title,level,visibleTo) -- level: 3 = info, 2 = warning, 1 = error, default is info
message = "[DGS"..(title and " "..title or "").."] "..message
if type(visibleTo) ~= "table" then
visibleTo = {visibleTo or "console"}
end
local r,g,b = 0,255,0
if level == 2 then
r,g,b = 255, 147, 0
elseif level == 1 then
r,g,b = 255,0,0
end
for i=1,#visibleTo do
local to = visibleTo[i]
if to and (to ~= "console" and (not isElement(to) or getElementType(to) ~= "console")) then
outputChatBox(message,to,r,g,b)
else
outputDebugString(message,getVersion().sortable > "1.5.7-9.20477" and 4 or level,r,g,b)
end
end
end
-----------Config Loader
DGSConfig = {
updateCheck = true, -- Enable:true;Disable:false
updateCheckInterval = 120, -- Minutes
updateCheckNoticeInterval = 120, -- Minutes
updateCommand = "updatedgs", -- Command of update dgs
enableUpdateSystem = true , -- Enable update system
enableMetaBackup = true, -- Backup meta.xml
enableStyleMetaBackup = true, -- Backup style files meta index from meta.xml
enableG2DCMD = true, -- Enable GUI To DGS command line
enableBuiltInCMD = true, -- Enable DGS Built-in CMD /dgscmd
enableTestFile = true, -- Loads DGS Test File (If you want to save some bytes of memory, disable this by set to false)
enableCompatibilityCheck = true, -- Enable compatibility check warnings
enableDebug = true, -- Enable /debugdgs
}
function loadConfig()
if fileExists("config.txt") then
local file = fileOpen ("config.txt")
if file then
local configUpdateRequired = false
local str = fileRead(file,fileGetSize(file))
fileClose(file)
local fnc = loadstring(str)
if fnc then
local dgsConfig = {}
setfenv(fnc,{dgsConfig=dgsConfig})
fnc()
for name,value in pairs(DGSConfig) do
if dgsConfig[name] == nil then
configUpdateRequired = true
else
DGSConfig[name] = dgsConfig[name]
end
end
outputDGSMessage("The config file has been loaded.","Config")
else
configUpdateRequired = true
outputDGSMessage("Invalid config file.","Config",2)
end
if configUpdateRequired then
fileDelete("config.txt")
local file = fileCreate("config.txt")
local str = ""
for k,v in pairs(DGSConfig) do
local value = type(v) == "string" and '"'..v..'"' or tostring(v)
str = str.."\r\ndgsConfig."..k.." = "..value
end
fileWrite(file,str:sub(3))
fileClose(file)
outputDGSMessage("The config file has been updated.","Config")
end
else
outputDGSMessage("Failed to open the config file.","Config",2)
end
else
local file = fileCreate("config.txt")
local str = ""
for k,v in pairs(DGSConfig) do
local value = type(v) == "string" and '"'..v..'"' or tostring(v)
str = str.."\r\ndgsConfig."..k.." = "..value
end
fileWrite(file,str:sub(3))
fileClose(file)
outputDGSMessage("Config file was created.","Config")
end
setElementData(resourceRoot,"DGS-allowCMD",DGSConfig.enableBuiltInCMD)
setElementData(resourceRoot,"DGS-enableDebug",DGSConfig.enableDebug)
setElementData(resourceRoot,"DGS-enableCompatibilityCheck",DGSConfig.enableCompatibilityCheck)
if DGSConfig.enableG2DCMD then
outputDGSMessage("G2D command line is enabled.","Config")
end
end
loadConfig()
-----------Remote Stuff
addEvent("DGSI_RequestQRCode",true)
addEvent("DGSI_RequestIP",true)
addEvent("DGSI_RequestRemoteImage",true)
addEventHandler("DGSI_RequestQRCode",resourceRoot,function(str,w,h,id)
fetchRemote("https://api.qrserver.com/v1/create-qr-code/?size="..w.."x"..h.."&data="..str,{},function(data,info,player,id)
triggerClientEvent(player,"DGSI_ReceiveQRCode",resourceRoot,data,info.success,id)
end,{client,id})
end)
addEventHandler("DGSI_RequestRemoteImage",resourceRoot,function(website,id)
fetchRemote(website,{},function(data,info,player,id)
triggerClientEvent(player,"DGSI_ReceiveRemoteImage",resourceRoot,data,info,id)
end,{client,id})
end)
function getMyIP()
triggerClientEvent(client,"DGSI_ReceiveIP",resourceRoot,getPlayerIP(client))
end
addEventHandler("DGSI_RequestIP",resourceRoot,getMyIP)
setElementData(root,"DGS-ResName",getResourceName(getThisResource()))
function hashFile(fName)
local f = fileOpen(fName)
local fSize = fileGetSize(f)
local fContent = fileRead(f,fSize)
fileClose(f)
return hash("sha256",fContent),fSize
end
function verifyFile()
local xml = xmlLoadFile("meta.xml")
local children = xmlNodeGetChildren(xml)
local DGSRecordedFiles = {}
for index,child in ipairs(children) do
local nodeName = xmlNodeGetName(child)
if nodeName == "file" then
local src = xmlNodeGetAttribute(child,"src")
DGSRecordedFiles[src] = {hashFile(src)}
end
end
setElementData(resourceRoot,"DGSI_FileInfo",DGSRecordedFiles)
end
verifyFile()
addEvent("DGSI_AbnormalDetected",true)
addEventHandler("DGSI_AbnormalDetected",root,function(fData)
local pName = getPlayerName(client)
for fName,fData in pairs(fData) do
outputDGSMessage("Abnormal Detected at '"..fName.."' of player '"..pName.."'","Security")
end
end)
addEventHandler("onElementDataChange",resourceRoot,
function (key,old)
if client and (string.sub(key,0,4) == "DGS-" or key == "DGSI_FileInfo") then
setElementData(source,key,old)
outputDGSMessage("Illegal attempt to modify element data ("..key..") by "..getPlayerName(client),"Security",1)
end
end,false)