Skip to content

Commit 4cc873d

Browse files
committed
fix bug when registering with domain name
1 parent 8497e5d commit 4cc873d

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

resources/libraries/update_hosts_file.rb

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module RbProxy
22
module Helpers
3+
require 'resolv'
4+
35
def read_hosts_file
46
hosts_hash = Hash.new { |hash, key| hash[key] = [] }
57
File.readlines('/etc/hosts').each do |line|
@@ -13,7 +15,7 @@ def read_hosts_file
1315
end
1416

1517
def update_hosts_file
16-
manager_registration_ip = node['redborder']['manager_registration_ip'] if node['redborder'] && node['redborder']['manager_registration_ip']
18+
manager_registration_ip = managerToIp(node['redborder']['manager_registration_ip']) if node['redborder'] && node['redborder']['manager_registration_ip']
1719

1820
return unless manager_registration_ip
1921

@@ -53,5 +55,21 @@ def update_hosts_file
5355
end
5456
hosts_entries
5557
end
58+
59+
def managerToIp(str)
60+
ipv4_regex = /\A(\d{1,3}\.){3}\d{1,3}\z/
61+
ipv6_regex = /\A(?:[A-Fa-f0-9]{1,4}:){7}[A-Fa-f0-9]{1,4}\z/
62+
dns_regex = /\A[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\z/
63+
64+
return str if str =~ ipv4_regex || str =~ ipv6_regex
65+
66+
if str =~ dns_regex
67+
begin
68+
return Resolv.getaddress(str).to_s
69+
rescue Resolv::ResolvError
70+
return nil
71+
end
72+
end
73+
end
5674
end
5775
end

0 commit comments

Comments
 (0)