Software / code / prosody-modules
Comparison
mod_dnsupdate/mod_dnsupdate.lua @ 6261:8c9eb4b6d02f
Merge updates
| author | Trần H. Trung <xmpp:trần.h.trung@trung.fun> |
|---|---|
| date | Sun, 18 May 2025 20:52:00 +0700 |
| parent | 6257:3ae0d518b739 |
comparison
equal
deleted
inserted
replaced
| 6245:ea58d2893afb | 6261:8c9eb4b6d02f |
|---|---|
| 1 module:set_global(); | 1 module:set_global(); |
| 2 | 2 |
| 3 local config = require "core.configmanager"; | 3 local config = require "core.configmanager"; |
| 4 local modulemanager = require "core.modulemanager"; | |
| 4 local argparse = require "util.argparse"; | 5 local argparse = require "util.argparse"; |
| 5 local dns = require"net.adns".resolver(); | 6 local dns = require"net.adns".resolver(); |
| 6 local async = require "util.async"; | 7 local async = require "util.async"; |
| 7 local set = require "util.set"; | 8 local set = require "util.set"; |
| 8 local nameprep = require"util.encodings".stringprep.nameprep; | 9 local nameprep = require"util.encodings".stringprep.nameprep; |
| 9 local idna_to_ascii = require"util.encodings".idna.to_ascii; | 10 local idna_to_ascii = require"util.encodings".idna.to_ascii; |
| 10 | 11 |
| 11 local virtualhost_services = { "xmpp-client"; "xmpps-client"; "xmpp-server"; "xmpps-server" } | 12 local services = { "xmpp-client"; "xmpps-client"; "xmpp-server"; "xmpps-server" } |
| 12 local component_services = { "xmpp-server"; "xmpps-server" } | |
| 13 | 13 |
| 14 local function validate_dnsname_option(options, option_name, default) | 14 local function validate_dnsname_option(options, option_name, default) |
| 15 local host = options[option_name]; | 15 local host = options[option_name]; |
| 16 if host == nil then return default end | 16 if host == nil then return default end |
| 17 local normalized = nameprep(host); | 17 local normalized = nameprep(host); |
| 54 local ihost = idna_to_ascii(vhost); | 54 local ihost = idna_to_ascii(vhost); |
| 55 if not ihost then | 55 if not ihost then |
| 56 module:log("error", "Host %q fails IDNA", vhost); | 56 module:log("error", "Host %q fails IDNA", vhost); |
| 57 return 1; | 57 return 1; |
| 58 end | 58 end |
| 59 local is_component = config.get(vhost, "component_module"); | 59 if not config.get(vhost, "component_module") and not config.get(vhost, "defined") then |
| 60 if not is_component and not config.get(vhost, "defined") then | |
| 61 module:log("error", "Host %q is not defined in the config", vhost); | 60 module:log("error", "Host %q is not defined in the config", vhost); |
| 62 return 1; | 61 return 1; |
| 63 end | 62 end |
| 64 | |
| 65 local services = virtualhost_services; | |
| 66 if is_component then services = component_services; end | |
| 67 | 63 |
| 68 local domain = validate_dnsname_option(opts, "domain"); | 64 local domain = validate_dnsname_option(opts, "domain"); |
| 69 if not domain then | 65 if not domain then |
| 70 module:log("error", "--domain is required"); | 66 module:log("error", "--domain is required"); |
| 71 return 1; | 67 return 1; |
| 84 ["xmpp-server"] = module:get_option_array("s2s_ports", { 5269 }); | 80 ["xmpp-server"] = module:get_option_array("s2s_ports", { 5269 }); |
| 85 ["xmpps-client"] = module:get_option_array("c2s_direct_tls_ports", {}); | 81 ["xmpps-client"] = module:get_option_array("c2s_direct_tls_ports", {}); |
| 86 ["xmpps-server"] = module:get_option_array("s2s_direct_tls_ports", {}); | 82 ["xmpps-server"] = module:get_option_array("s2s_direct_tls_ports", {}); |
| 87 }; | 83 }; |
| 88 | 84 |
| 89 if opts.multiplex then | 85 local modules_enabled = modulemanager.get_modules_for_host(vhost); |
| 86 if not modules_enabled:contains("c2s") then | |
| 87 configured_ports["xmpp-client"] = {}; | |
| 88 configured_ports["xmpps-client"] = {}; | |
| 89 end | |
| 90 if not modules_enabled:contains("s2s") then | |
| 91 configured_ports["xmpp-server"] = {}; | |
| 92 configured_ports["xmpps-server"] = {}; | |
| 93 end | |
| 94 | |
| 95 if modules_enabled:contains("net_multiplex") then | |
| 90 for opt, ports in pairs(configured_ports) do | 96 for opt, ports in pairs(configured_ports) do |
| 91 ports:append(module:get_option_array(opt:sub(1, 5) == "xmpps" and "ssl_ports" or "ports", {})); | 97 ports:append(module:get_option_array(opt:sub(1, 5) == "xmpps" and "ssl_ports" or "ports", {})); |
| 92 end | 98 end |
| 93 end | 99 end |
| 94 | 100 |
| 100 print("zone", domain); | 106 print("zone", domain); |
| 101 print("server", primary); | 107 print("server", primary); |
| 102 print("ttl " .. tostring(opts.ttl or 60 * 60)); | 108 print("ttl " .. tostring(opts.ttl or 60 * 60)); |
| 103 | 109 |
| 104 for _, service in ipairs(services) do | 110 for _, service in ipairs(services) do |
| 105 local ports = set.new(configured_ports[service]); | 111 local config_ports = set.new(configured_ports[service]); |
| 106 local records = (async.wait_for(existing_srv[service])); | 112 local dns_ports = set.new(); |
| 107 if opts.remove or opts.reset then | 113 |
| 114 if (opts.reset or opts.remove) and not opts.each then | |
| 108 print(("del _%s._tcp.%s IN SRV"):format(service, ihost)); | 115 print(("del _%s._tcp.%s IN SRV"):format(service, ihost)); |
| 109 else | 116 else |
| 117 local records = (async.wait_for(existing_srv[service])); | |
| 110 for _, rr in ipairs(records) do | 118 for _, rr in ipairs(records) do |
| 111 if ports:contains(rr.srv.port) and target == nameprep(rr.srv.target):gsub("%.$", "") then | 119 if target == nameprep(rr.srv.target):gsub("%.$", "") then |
| 112 ports:remove(rr.srv.port) | 120 dns_ports:add(rr.srv.port) |
| 113 elseif not opts.each then | 121 elseif opts.each then |
| 114 print(("del _%s._tcp.%s IN SRV"):format(service, ihost)); | |
| 115 break | |
| 116 else | |
| 117 print(("del _%s._tcp.%s IN SRV %s"):format(service, ihost, rr)); | 122 print(("del _%s._tcp.%s IN SRV %s"):format(service, ihost, rr)); |
| 118 end | 123 end |
| 119 end | 124 end |
| 120 end | 125 end |
| 126 | |
| 121 if not opts.remove then | 127 if not opts.remove then |
| 122 for port in ports do print(("add _%s._tcp.%s IN SRV 1 1 %d %s"):format(service, ihost, port, target)); end | 128 if config_ports:empty() then |
| 129 print(("add _%s._tcp.%s IN SRV 0 0 0 ."):format(service, ihost)); | |
| 130 else | |
| 131 for port in (config_ports - dns_ports) do | |
| 132 print(("add _%s._tcp.%s IN SRV 1 1 %d %s"):format(service, ihost, port, target)); | |
| 133 end | |
| 134 end | |
| 123 end | 135 end |
| 124 if ports:empty() then print(("add _%s._tcp.%s IN SRV 0 0 0 ."):format(service, ihost)); end | |
| 125 end | 136 end |
| 126 | 137 |
| 127 print("show"); | 138 print("show"); |
| 128 print("send"); | 139 print("send"); |
| 129 print("answer"); | 140 print("answer"); |