Skip to content

Commit

Permalink
Added get_item_property and use it for Win32::Registry and Get-ItemPr…
Browse files Browse the repository at this point in the history
…operty
  • Loading branch information
hsbt committed Feb 4, 2025
1 parent a487698 commit 333bc26
Showing 1 changed file with 19 additions and 57 deletions.
76 changes: 19 additions & 57 deletions ext/win32/lib/win32/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,47 +82,16 @@ def get_info
search = nil
nameserver = get_dns_server_list

slist = if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
reg.read_s('SearchList')
rescue Registry::Error
""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'SearchList' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty SearchList"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip
end
slist = get_item_property(TCPIP_NT, 'SearchList')
search = slist.split(/,\s*/) unless slist.empty?

if add_search = search.nil?
search = []
nvdom = if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
reg.read_s('NV Domain')
rescue Registry::Error
""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'NV Domain' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty NV domain"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip
end
nvdom = get_item_property(TCPIP_NT, 'NV Domain')

unless nvdom.empty?
@search = [ nvdom ]
udmnd = if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
reg.read_i('UseDomainNameDevolution')
rescue Registry::Error
0
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name 'UseDomainNameDevolution' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty UseDomainNameDevolution"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip.to_i
end

udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i
if udmnd != 0
if /^\w+\./ =~ nvdom
devo = $'
Expand All @@ -131,7 +100,6 @@ def get_info
end
end


ifs = if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
reg.keys
Expand All @@ -146,35 +114,15 @@ def get_info

ifs.each do |iface|
next unless ns = %w[NameServer DhcpNameServer].find do |key|
ns = if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
regif.read_s(key)
rescue Registry::Error
""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name '#{key}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{key}"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip
end
ns = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
break ns.split(/[,\s]\s*/) unless ns.empty?
end

next if (nameserver & ns).empty?

if add_search
[ 'Domain', 'DhcpDomain' ].each do |key|
dom = if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces' + "\\#{iface}" ) do |regif|
regif.read_s(key)
rescue Registry::Error
""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{TCPIP_NT}' -Name '#{key}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{key}"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip
end
dom = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
unless dom.empty?
search.concat(dom.split(/,\s*/))
break
Expand All @@ -185,6 +133,20 @@ def get_info
search << devo if add_search and devo
[ search.uniq, nameserver.uniq ]
end

def get_item_property(path, name)
if defined?(Win32::Registry)
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
reg.read_s(key)
rescue Registry::Error
""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{name}"
output, _ = Open3.capture2('powershell', '-Command', cmd)
output.strip
end
end
end
end
end

0 comments on commit 333bc26

Please sign in to comment.