# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1684936586 -7200
# Node ID 71243bedb2b022afd89a6cefc18ab66901457698
# Parent  67190744b1eb0030bf2e8bceafb32f378bb2414b
mod_s2sout_override: New module for overriding s2s connections

This takes advantage of the new event added in Prosody rev d5f322dd424b
which enables a cleaner way to override the connection using a resolver.

diff -r 67190744b1eb -r 71243bedb2b0 mod_s2sout_override/README.md
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_s2sout_override/README.md	Wed May 24 15:56:26 2023 +0200
@@ -0,0 +1,37 @@
+---
+summary: Override s2s connection targets
+---
+
+This module replaces [mod_s2soutinjection] and uses more modern and
+reliable methods for overriding connection targets.
+
+# Configuration
+
+Enable the module as usual, then specify a map of XMPP remote hostnames
+to URIs like `"tcp://host.example:port"`, to have Prosody connect there
+instead of doing normal DNS SRV resolution.
+
+Currently only the `tcp://` scheme is supported.  A future version could
+support more methods including Direct TLS, alternate SRV lookup targets
+or even UNIX sockets.
+
+```lua
+-- Global section
+modules_enabled = {
+    -- other global modules
+    "s2sout_override";
+}
+
+s2sout_override = {
+    ["example.com"] = "tcp://other.host.example:5299";
+    ["xmpp.example.net"] = "tcp://localhost:5999";
+}
+```
+
+# Compatibility
+
+Prosody version   status
+---------------   ----------
+0.12.4            Will work
+0.12.3            Will not work
+0.11              Will not work
diff -r 67190744b1eb -r 71243bedb2b0 mod_s2sout_override/mod_s2sout_override.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_s2sout_override/mod_s2sout_override.lua	Wed May 24 15:56:26 2023 +0200
@@ -0,0 +1,16 @@
+--% requires: s2sout-pre-connect-event
+
+local url = require"socket.url";
+local basic_resolver = require "net.resolvers.basic";
+
+local override_for = module:get_option(module.name, {}); -- map of host to "tcp://example.com:5269"
+
+module:hook("s2sout-pre-connect", function(event)
+	local override = override_for[event.session.to_host];
+	if type(override) == "string" then
+		override = url.parse(override);
+	end
+	if type(override) == "table" and override.scheme == "tcp" and type(override.host) == "string" then
+		event.resolver = basic_resolver.new(override.host, tonumber(override.port) or 5269, override.scheme, {});
+	end
+end);