مراية لـ
https://github.com/postalserver/postal.git
تم المزامنة 2025-11-30 21:32:30 +00:00
refactor: refactor DNS resolution
This commit also adds some of tests for the Domain model. It was during the writing of these tests that the DNS resolution refactoring requirement became apparent.
هذا الالتزام موجود في:
@@ -1,36 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Postal
|
||||
class MXLookup
|
||||
|
||||
class << self
|
||||
|
||||
def lookup(domain)
|
||||
records = resolve(domain)
|
||||
records = sort(records)
|
||||
records.map { |m| m[1] }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sort(records)
|
||||
records.sort do |a, b|
|
||||
if a[0] == b[0]
|
||||
[-1, 1].sample
|
||||
else
|
||||
a[0] <=> b[0]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def resolve(domain)
|
||||
Resolv::DNS.open do |dns|
|
||||
dns.timeouts = [10, 5]
|
||||
dns.getresources(domain, Resolv::DNS::Resource::IN::MX).map { |m| [m.preference.to_i, m.exchange.to_s] }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -19,26 +19,13 @@ module Postal
|
||||
header = "by #{our_hostname} with #{method.to_s.upcase}; #{Time.now.utc.rfc2822}"
|
||||
|
||||
if server.nil? || server.privacy_mode == false
|
||||
hostname = resolve_hostname(ip_address)
|
||||
hostname = DNSResolver.local.ip_to_hostname(ip_address)
|
||||
header = "from #{helo} (#{hostname} [#{ip_address}]) #{header}"
|
||||
end
|
||||
|
||||
header
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def resolve_hostname(ip_address)
|
||||
Resolv::DNS.open do |dns|
|
||||
dns.timeouts = [10, 5]
|
||||
begin
|
||||
dns.getname(ip_address)
|
||||
rescue StandardError
|
||||
ip_address
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -225,7 +225,7 @@ module Postal
|
||||
|
||||
def servers
|
||||
@options[:servers] || self.class.relay_hosts || @servers ||= begin
|
||||
mx_servers = MXLookup.lookup(@domain)
|
||||
mx_servers = DNSResolver.local.mx(@domain).map(&:last)
|
||||
if mx_servers.empty?
|
||||
mx_servers = [@domain] # This will be resolved to an A or AAAA record later
|
||||
end
|
||||
@@ -243,16 +243,13 @@ module Postal
|
||||
|
||||
def lookup_ip_address(type, hostname)
|
||||
records = []
|
||||
Resolv::DNS.open do |dns|
|
||||
dns.timeouts = [10, 5]
|
||||
case type
|
||||
when :a
|
||||
records = dns.getresources(hostname, Resolv::DNS::Resource::IN::A)
|
||||
when :aaaa
|
||||
records = dns.getresources(hostname, Resolv::DNS::Resource::IN::AAAA)
|
||||
end
|
||||
case type
|
||||
when :a
|
||||
records = DNSResolver.local.a(hostname)
|
||||
when :aaaa
|
||||
records = DNSResolver.local.aaaa(hostname)
|
||||
end
|
||||
records.first&.address&.to_s&.downcase
|
||||
records.first&.to_s&.downcase
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
المرجع في مشكلة جديدة
حظر مستخدم