forked from prestonelam2003/CobaltEssentials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CE-RCON.lua
160 lines (109 loc) · 3.28 KB
/
CE-RCON.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
160
--Copyright (C) 2020, Preston Elam (CobaltTetra) ALL RIGHTS RESERVED
--COBALTESSENTIALS IS PROTECTED UNDER AN GPLv3 LICENSE
local socket
local server
local magicChar = string.char(0xff, 0xff, 0xff, 0xff)
local waitingForReply = false
local rconClients = {}
local clientCount = 0
RegisterEvent("startRCON","startRCON")
function startRCON(port, path, cpath)
package.path = path
package.cpath = cpath
local rconClients = {}
socket = require("socket")
utils = require("CobaltUtils")
server = socket.udp()
server:settimeout(0.1)
server:setsockname('0.0.0.0', port)
RegisterEvent("keepAlive","keepAlive")
RegisterEvent("RCONsend","RCONsend")
RegisterEvent("RCONreply","RCONreply")
CreateThread("listenRCON",500)
CElog("RCON open on port " .. port,"RCON")
end
function listenRCON()
--if waitingForReply == false then
local message, ip, port = server:receivefrom()
if message ~= nil and message:sub(1,4) == magicChar then
local ID = checkClient(ip, port)
-- print("'".. message .. "'")
-- print("'".. ip .. "'")
-- print("'".. port .. "'")
message = message:sub(5)
local s, e = message:find(" ")
local prefix = message:sub(1,s-1)
message = message:sub(s+1)
local s, e = message:find(" ")
local password = message:sub(1,s-1)
message = message:sub(s+1)
message = message:sub(1,message:len()-1)
-- print("'".. prefix .. "'")
-- print("'".. password .. "'")
-- print("'".. message .. "'")
TriggerGlobalEvent("onRconCommand", ID, message, password, prefix)
waitingForReply = true
end
--end
end
function RCONsend(rconID, message)
local splitMes = split(message,"\n")
if splitMes[2] ~= nil then
splitMes[1] = splitMes[1] .. "..."
end
CElog("RCON > " .. rconID .. ": " .. splitMes[1],"RCON")
if rconID == "R-1" then
for k,v in pairs(rconClients) do
server:sendto(magicChar .. "print " .. message , v.ip, v.port)
end
else
server:sendto(magicChar .. "print " .. message , rconClients[rconID].ip, rconClients[rconID].port)
end
end
function keepAlive(rconID)
server:sendto(magicChar .. "print keepAlive" , rconClients[rconID].ip, rconClients[rconID].port)
end
function checkClient(ip, port)
local clientID
--loop through all current rconClients
for ID, client in pairs(rconClients) do
if client.ip == ip and client.port == port then
clientID = client.ID
break
end
end
local client = {}
if clientID then
return clientID
else
--generate a new client table/object for the client
client.ip = ip
client.port = port
client.ID = "R" .. clientCount
clientCount = clientCount + 1 --increment clientCount
--add the client to it's respective tables.
rconClients[client.ID] = client
TriggerGlobalEvent("onNewRconClient", client.ID, client.ip, client.port)
return client.ID
end
end
function RCONreply(reply)
if reply == nil then
reply = ""
else
local splitReply = split(reply,"\n")
if splitReply[2] ~= nil then
splitReply[1] = splitReply[1] .. "..."
end
CElog("RCON REPLY: " .. splitReply[1],"RCON")
end
server:sendto(magicChar .. "print" .. reply , ip, port)
waitingForReply = false
end
function split(s, sep)
local fields = {}
local sep = sep or " "
local pattern = string.format("([^%s]+)", sep)
string.gsub(s, pattern, function(c) fields[#fields + 1] = c end)
return fields
end