Refactor preview server to support server_name and bind_address
This commit is contained in:
parent
232aca91bc
commit
6aa7ce741a
38 changed files with 2023 additions and 50 deletions
|
@ -0,0 +1,52 @@
|
|||
module Middleman
|
||||
class DnsResolver
|
||||
# Use network name server to resolve ips and names
|
||||
class BasicNetworkResolver
|
||||
private
|
||||
|
||||
attr_reader :resolver, :timeouts
|
||||
|
||||
public
|
||||
|
||||
def initialize(opts={})
|
||||
@timeouts = opts.fetch(:timeouts, 2)
|
||||
end
|
||||
|
||||
# Get names for ip
|
||||
#
|
||||
# @param [#to_s] ip
|
||||
# The ip to resolve into names
|
||||
#
|
||||
# @return [Array]
|
||||
# Array of Names
|
||||
def getnames(ip)
|
||||
resolver.getnames(ip.to_s).map(&:to_s)
|
||||
rescue Resolv::ResolvError, Errno::EADDRNOTAVAIL
|
||||
[]
|
||||
end
|
||||
|
||||
# Get ips for name
|
||||
#
|
||||
# @param [#to_s] name
|
||||
# The name to resolve into ips
|
||||
#
|
||||
# @return [Array]
|
||||
# Array of ipaddresses
|
||||
def getaddresses(name)
|
||||
resolver.getaddresses(name.to_s).map(&:to_s)
|
||||
rescue Resolv::ResolvError, Errno::EADDRNOTAVAIL
|
||||
[]
|
||||
end
|
||||
|
||||
# Set timeout for lookup
|
||||
#
|
||||
# @param [Integer] value
|
||||
# The timeout value
|
||||
def timeouts=(timeouts)
|
||||
return if RUBY_VERSION < '2'
|
||||
|
||||
resolver.timeouts = timeouts
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,63 @@
|
|||
module Middleman
|
||||
class DnsResolver
|
||||
# Use network name server to resolve ips and names
|
||||
class HostsResolver
|
||||
private
|
||||
|
||||
attr_reader :resolver
|
||||
|
||||
public
|
||||
|
||||
def initialize(opts={})
|
||||
# using the splat operator works around a non-existing HOSTSRC variable
|
||||
# using nil as input does not work, but `*[]` does and then Resolv::Hosts
|
||||
# uses its defaults
|
||||
@resolver = opts.fetch(:resolver, Resolv::Hosts.new(*hosts_file))
|
||||
end
|
||||
|
||||
# Get names for ip
|
||||
#
|
||||
# @param [#to_s] ip
|
||||
# The ip to resolve into names
|
||||
#
|
||||
# @return [Array]
|
||||
# Array of Names
|
||||
def getnames(ip)
|
||||
resolver.getnames(ip.to_s).map(&:to_s)
|
||||
rescue Resolv::ResolvError
|
||||
[]
|
||||
end
|
||||
|
||||
# Get ips for name
|
||||
#
|
||||
# @param [#to_s] name
|
||||
# The name to resolve into ips
|
||||
#
|
||||
# @return [Array]
|
||||
# Array of ipaddresses
|
||||
def getaddresses(name)
|
||||
resolver.getaddresses(name.to_s).map(&:to_s)
|
||||
rescue Resolv::ResolvError
|
||||
[]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Path to hosts file
|
||||
#
|
||||
# This looks for MM_HOSTSRC in your environment
|
||||
#
|
||||
# @return [Array]
|
||||
# This needs to be an array, to make the splat operator work
|
||||
#
|
||||
# @example
|
||||
# # <ip> <hostname>
|
||||
# 127.0.0.1 localhost.localhost localhost
|
||||
def hosts_file
|
||||
return [ENV['MM_HOSTSRC']] if ENV.key?('MM_HOSTSRC') && File.file?(ENV['MM_HOSTSRC'])
|
||||
|
||||
[]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,44 @@
|
|||
require 'middleman-core/dns_resolver/basic_network_resolver'
|
||||
|
||||
module Middleman
|
||||
class DnsResolver
|
||||
# Use network name server to resolve ips and names
|
||||
class LocalLinkResolver < BasicNetworkResolver
|
||||
def initialize(opts={})
|
||||
super
|
||||
|
||||
@timeouts = opts.fetch(:timeouts, 1)
|
||||
@resolver = opts.fetch(:resolver, Resolv::MDNS.new(nameserver_config))
|
||||
|
||||
self.timeouts = timeouts
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Hosts + Ports for MDNS resolver
|
||||
#
|
||||
# This looks for MM_MDNSRC in your environment. If you are going to use
|
||||
# IPv6-addresses: Make sure you do not forget to add the port at the end.
|
||||
#
|
||||
# MM_MDNSRC=ip:port ip:port
|
||||
#
|
||||
# @return [Hash]
|
||||
# Returns the configuration for the nameserver
|
||||
#
|
||||
# @example
|
||||
# export MM_MDNSRC="224.0.0.251:5353 ff02::fb:5353"
|
||||
#
|
||||
def nameserver_config
|
||||
return unless ENV.key?('MM_MDNSRC') && ENV['MM_MDNSRC']
|
||||
|
||||
address, port = ENV['MM_MDNSRC'].split(/:/)
|
||||
|
||||
{
|
||||
nameserver_port: [[address, port.to_i]]
|
||||
}
|
||||
rescue StandardError
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,42 @@
|
|||
require 'middleman-core/dns_resolver/basic_network_resolver'
|
||||
|
||||
module Middleman
|
||||
class DnsResolver
|
||||
# Use network name server to resolve ips and names
|
||||
class NetworkResolver < BasicNetworkResolver
|
||||
def initialize(opts={})
|
||||
super
|
||||
|
||||
@resolver = opts.fetch(:resolver, Resolv::DNS.new(nameserver_config))
|
||||
self.timeouts = timeouts
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Hosts + Ports for MDNS resolver
|
||||
#
|
||||
# This looks for MM_MDNSRC in your environment. If you are going to use
|
||||
# IPv6-addresses: Make sure you do not forget to add the port at the end.
|
||||
#
|
||||
# MM_MDNSRC=ip:port ip:port
|
||||
#
|
||||
# @return [Hash]
|
||||
# Returns the configuration for the nameserver
|
||||
#
|
||||
# @example
|
||||
# export MM_MDNSRC="224.0.0.251:5353 ff02::fb:5353"
|
||||
#
|
||||
def nameserver_config
|
||||
return unless ENV.key?('MM_DNSRC') && ENV['MM_DNSRC']
|
||||
|
||||
address, port = ENV['MM_DNSRC'].split(/:/)
|
||||
|
||||
{
|
||||
nameserver_port: [[address, port.to_i]]
|
||||
}
|
||||
rescue StandardError
|
||||
{}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue