idubs
4/8/2018 - 11:29 PM

Lua script to log net messages sent to the server, may also work client side, but isn't intended to.

Lua script to log net messages sent to the server, may also work client side, but isn't intended to.

netDetour = netDetour or {}

netDetour.enabled = true
netDetour.fileOutput = true
netDetour.whiteList = {
    ExampleNetMessage = true,
}

netDetour.whiteList = table.LowerKeyNames(netDetour.whiteList)

function netDetour.Incoming(len, client)
    local i = net.ReadHeader()
    local strName = util.NetworkIDToString(i)

    if (!strName) then return end

    local func = net.Receivers[strName:lower()]
    if (!func) then return end

    len = len - 16

    func(len, client)

    if netDetour.whiteList[strName:lower()] then return end
    hook.Run("netDetour:Incoming", client, strName)
end

hook.Add("netDetour:Incoming", "netDetour.Incoming", function(ply, netName)
    local time = os.date("%H:%M:%S - %d/%m/%Y" , os.time())
    local data = Format("[%s] - %s (%s) sent %s", time, ply:GetName(), ply:SteamID(), netName)

    print(data)

    if not netDetour.fileOutput then return end

    file.Append("netlog.txt", data .. "\n")
end)

netDetour.original = netDetour.original or net.Incoming

if netDetour.enabled then
    net.Incoming = netDetour.Incoming
else
    net.Incoming = netDetour.original
end