Here's my client version (with the encryption password editted.)
Code: Select all
-- client.lua
Status, ErrCode = pcall( function ()
require "enet"
require "aeslua"
local host = enet.host_create()
local server = host:connect("0.0.0.0:65927")
while true do
local event = host:service(100)
while event do
if event.type == "receive" then
print("Got message: ", tostring(aeslua.decrypt("secretpassword",tostring(event.data))), tostring(event.peer))
--event.peer:send( aeslua.encrypt("secretpassword","%$>CMD notepad") )
io.write(">")
a = io.read()
a = "%$>" .. a
pcall(function () a=aeslua.encrypt("secretpassword",a) end)
event.peer:send(a)
elseif event.type == "connect" then
print(tostring(event.peer) .. " connected.")
event.peer:send( "ping" )
elseif event.type == "disconnect" then
print(tostring(event.peer) .. " disconnected.")
end
event = host:service()
end
end end)
print(Status, ErrCode)
while true do
end
And here's my server version (with the encryption code in another file, but it it's the same.) (I also figured, if I had destroyed the host, and set it back to local host, it might follow my dynamic internal IP, and dynamic external IP(don't know much about networks, just preparing my script for IP changes))
Code: Select all
-- server.lua
require "enet"
require "aeslua"
require "Settings"
local host = enet.host_create("0.0.0.0:65927")
Last_Disconnect = 0
PEER = false
while true do
Key = "%$>"
key = Key
local event = host:service(Settings.timeout)
while event do
if event.type == "receive" then
PEER = true
Last_Disconnect = love.timer.getTime()
local Dstat, Derror = pcall(function () Data = aeslua.decrypt(Settings.encryption_code,tostring(event.data)) end)
if Dstat == false then print(tostring(event.peer).." sent invalid packet.") event.peer:send("INCORRECT ENCRYPTION CODE")
else
print("Got message: ", Data, tostring(event.peer))
if string.sub(Data,1,#Key)==Key then
Data = tostring(string.sub(Data,#key+1,#Data))
print(string.sub(Data,1, 4))
if string.sub(Data,1, 4)=="LUA " then
local Command = string.sub(Data,5,#Data)
local status, ErrorCode = pcall(loadstring(Command))
local Command = nil
event.peer:send( aeslua.encrypt(Settings.encryption_code,"STATUS: "..tostring(status)..";\n Error: "..(tostring(ErrorCode) or "(no error)").."\n" ))
status = nil
ErrorCode = nil
elseif string.sub(Data,1, 4)=="CMD " then
local Command = string.sub(Data,5,#Data)
local status, ErrorCode = pcall(function () Args = os.execute(Command) end)
local Command = nil
event.peer:send( aeslua.encrypt(Settings.encryption_code,"STATUS: "..tostring(status)..";\n Error: "..(tostring(ErrorCode) or "(no error)")..";\n RETURNED: "..(Args or "(nothing returned)").."\n"))
Args = nil
status = nil
ErrorCode = nil
Command = nil
else
event.peer:send( aeslua.encrypt(Settings.encryption_code,"ERROR EXECUTING COMMAND") )
end
else
event.peer:send( aeslua.encrypt(Settings.encryption_code,"ERROR INITIALIZING COMMAND") )
end
end
elseif event.type == "connect" then
print(tostring(event.peer) .. " connected.")
PEER = true
Last_Disconnect = love.timer.getTime()
elseif event.type == "disconnect" then
print(tostring(event.peer) .. " disconnected.")
PEER = false
Last_Disconnect = love.timer.getTime()
end
event = host:service()
end
if (love.timer.getTime()-Last_Disconnect)>=Settings.reconnect_frequency and PEER==false then
print("reconnecting IP (since running on a dynamic IP)")
Last_Disconnect = love.timer.getTime()
host:destroy()
host = enet.host_create("localhost:65927")
end
end-