mod_pinger/mod_pinger.lua
author Matthew Wild <mwild1@gmail.com>
Tue, 26 Jan 2016 19:15:00 +0000
changeset 2038 256a5e3591db
child 2675 80b6c63cb559
permissions -rw-r--r--
mod_pinger: Added from /files/
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2038
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
local new_watchdog = require "util.watchdog".new;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local filters = require "util.filters";
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
local st = require "util.stanza";
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local idle_timeout = module:get_option_number("c2s_idle_timeout", 300);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local ping_timeout = module:get_option_number("c2s_ping_timeout",  30);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
function update_watchdog(data, session)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
	session.idle_watchdog:reset();
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	session.idle_pinged = nil;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
	return data;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
function check_session(watchdog)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
	local session = watchdog.session;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
	if not session.idle_pinged then
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		session.idle_pinged = true;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
               if session.smacks and not session.awaiting_ack then
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
                       session.send(st.stanza("r", { xmlns = "urn:xmpp:sm:2" })) -- TODO: hardcoded sm:2
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
               else
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
                       session.send(st.iq({ type = "get", from = module.host, id = "idle-check" })
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
                               :tag("ping", { xmlns = "urn:xmpp:ping" }));
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
               end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		return ping_timeout; -- Call us again after ping_timeout
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
	else
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
		module:log("info", "Client %q silent for too long, closing...", session.full_jid);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
		session:close("connection-timeout");
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
function watch_session(session)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
	if not session.idle_watchdog
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
	and not session.requests then -- Don't watch BOSH connections (BOSH already has timeouts)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
		session.idle_watchdog = new_watchdog(idle_timeout, check_session);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
		session.idle_watchdog.session = session;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
		filters.add_filter(session, "bytes/in", update_watchdog);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
	end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
function unwatch_session(session)
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
	if session.idle_watchdog then
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
		session.idle_watchdog:cancel();
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
		session.idle_watchdog = nil;
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
		filters.remove_filter(session, "bytes/in", update_watchdog);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
	end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
end
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
module:hook("resource-bind", function (event) watch_session(event.session); end);
256a5e3591db mod_pinger: Added from /files/
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
module:hook("resource-unbind", function (event) unwatch_session(event.session); end);