mod_s2s_keepalive/mod_s2s_keepalive.lua
author Kim Alvefur <zash@zash.se>
Mon, 24 Aug 2015 23:17:36 +0200
changeset 1788 1656d4fd71d0
parent 1264 2db2c03dfb95
child 3727 427879b46061
permissions -rw-r--r--
mod_cloud_notify: Fix syntax errors and name
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1110
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
local st = require "util.stanza";
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
local keepalive_servers = module:get_option_set("keepalive_servers");
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
local keepalive_interval = module:get_option_number("keepalive_interval", 60);
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local host = module.host;
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local function send_pings()
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     9
	for remote_domain, session in pairs(hosts[host].s2sout) do
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
		if session.type == "s2sout" -- as opposed to _unauthed
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
		and (not(keepalive_servers) or keepalive_servers:contains(remote_domain)) then
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
			session.sends2s(st.iq({ to = remote_domain, type = "get", from = host, id = "keepalive" })
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
				:tag("ping", { xmlns = "urn:xmpp:ping" })
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
			);
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
			-- Note: We don't actually check if this comes back.
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
		end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
	end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	for session in pairs(prosody.incoming_s2s) do
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
		if session.type == "s2sin" -- as opposed to _unauthed
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
		and (not(keepalive_servers) or keepalive_servers:contains(session.from_host)) then
1264
2db2c03dfb95 mod_s2s_keepalive: Don't send directly on the connection, use sends2s
Kim Alvefur <zash@zash.se>
parents: 1110
diff changeset
    22
			session.sends2s " ";
1110
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
			-- If the connection is dead, this should make it time out.
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
		end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
	return keepalive_interval;
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
if module.add_timer then -- 0.9
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	module:add_timer(keepalive_interval, send_pings);
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
else -- 0.8
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
	local timer = require "util.timer";
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
	local unloaded;
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
	timer.add_task(keepalive_interval, function()
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
		if not unloaded then
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
			return send_pings()
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
		end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
	end);
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
	function module.unload()
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
		unloaded = true
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
	end
97e238ce37ce mod_s2s_keepalive: Initial commit, poke s2s connections with pings and whitespace
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
end