|
4 | 4 |
|
5 | 5 | =end
|
6 | 6 |
|
7 |
| -require 'win32/registry' |
8 |
| - |
9 | 7 | module Win32
|
10 | 8 | module Resolv
|
11 |
| - API = Registry::API |
12 |
| - Error = Registry::Error |
13 |
| - |
14 | 9 | def self.get_hosts_path
|
15 | 10 | path = get_hosts_dir
|
16 | 11 | path = File.expand_path('hosts', path)
|
@@ -47,89 +42,103 @@ module Win32
|
47 | 42 | # Windows NT
|
48 | 43 | #====================================================================
|
49 | 44 | module Resolv
|
50 |
| - module SZ |
51 |
| - refine Registry do |
52 |
| - # ad hoc workaround for broken registry |
53 |
| - def read_s(key) |
54 |
| - type, str = read(key) |
55 |
| - unless type == Registry::REG_SZ |
56 |
| - warn "Broken registry, #{name}\\#{key} was #{Registry.type2name(type)}, ignored" |
57 |
| - return String.new |
| 45 | + begin |
| 46 | + require 'win32/registry' |
| 47 | + module SZ |
| 48 | + refine Registry do |
| 49 | + # ad hoc workaround for broken registry |
| 50 | + def read_s(key) |
| 51 | + type, str = read(key) |
| 52 | + unless type == Registry::REG_SZ |
| 53 | + warn "Broken registry, #{name}\\#{key} was #{Registry.type2name(type)}, ignored" |
| 54 | + return String.new |
| 55 | + end |
| 56 | + str |
58 | 57 | end
|
59 |
| - str |
60 | 58 | end
|
61 | 59 | end
|
| 60 | + using SZ |
| 61 | + rescue LoadError |
| 62 | + require "open3" |
62 | 63 | end
|
63 |
| - using SZ |
64 | 64 |
|
65 | 65 | TCPIP_NT = 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters'
|
66 | 66 |
|
67 | 67 | class << self
|
68 | 68 | private
|
69 | 69 | def get_hosts_dir
|
70 |
| - Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg| |
71 |
| - reg.read_s_expand('DataBasePath') |
72 |
| - end |
| 70 | + get_item_property(TCPIP_NT, 'DataBasePath', expand: true) |
73 | 71 | end
|
74 | 72 |
|
75 | 73 | def get_info
|
76 | 74 | search = nil
|
77 | 75 | nameserver = get_dns_server_list
|
78 |
| - Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg| |
79 |
| - begin |
80 |
| - slist = reg.read_s('SearchList') |
81 |
| - search = slist.split(/,\s*/) unless slist.empty? |
82 |
| - rescue Registry::Error |
83 |
| - end |
84 | 76 |
|
85 |
| - if add_search = search.nil? |
86 |
| - search = [] |
87 |
| - begin |
88 |
| - nvdom = reg.read_s('NV Domain') |
89 |
| - unless nvdom.empty? |
90 |
| - @search = [ nvdom ] |
91 |
| - if reg.read_i('UseDomainNameDevolution') != 0 |
92 |
| - if /^\w+\./ =~ nvdom |
93 |
| - devo = $' |
94 |
| - end |
95 |
| - end |
| 77 | + slist = get_item_property(TCPIP_NT, 'SearchList') |
| 78 | + search = slist.split(/,\s*/) unless slist.empty? |
| 79 | + |
| 80 | + if add_search = search.nil? |
| 81 | + search = [] |
| 82 | + nvdom = get_item_property(TCPIP_NT, 'NV Domain') |
| 83 | + |
| 84 | + unless nvdom.empty? |
| 85 | + @search = [ nvdom ] |
| 86 | + udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i |
| 87 | + if udmnd != 0 |
| 88 | + if /^\w+\./ =~ nvdom |
| 89 | + devo = $' |
96 | 90 | end
|
97 |
| - rescue Registry::Error |
98 | 91 | end
|
99 | 92 | end
|
| 93 | + end |
100 | 94 |
|
101 |
| - reg.open('Interfaces') do |h| |
102 |
| - h.each_key do |iface, | |
103 |
| - h.open(iface) do |regif| |
104 |
| - next unless ns = %w[NameServer DhcpNameServer].find do |key| |
105 |
| - begin |
106 |
| - ns = regif.read_s(key) |
107 |
| - rescue Registry::Error |
108 |
| - else |
109 |
| - break ns.split(/[,\s]\s*/) unless ns.empty? |
110 |
| - end |
111 |
| - end |
112 |
| - next if (nameserver & ns).empty? |
113 |
| - |
114 |
| - if add_search |
115 |
| - begin |
116 |
| - [ 'Domain', 'DhcpDomain' ].each do |key| |
117 |
| - dom = regif.read_s(key) |
118 |
| - unless dom.empty? |
119 |
| - search.concat(dom.split(/,\s*/)) |
120 |
| - break |
121 |
| - end |
122 |
| - end |
123 |
| - rescue Registry::Error |
124 |
| - end |
| 95 | + ifs = if defined?(Win32::Registry) |
| 96 | + Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg| |
| 97 | + reg.keys |
| 98 | + rescue Registry::Error |
| 99 | + [] |
125 | 100 | end
|
| 101 | + else |
| 102 | + cmd = "Get-ChildItem 'HKLM:\\#{TCPIP_NT}\\Interfaces' | ForEach-Object { $_.PSChildName }" |
| 103 | + output, _ = Open3.capture2('powershell', '-Command', cmd) |
| 104 | + output.split(/\n+/) |
| 105 | + end |
| 106 | + |
| 107 | + ifs.each do |iface| |
| 108 | + next unless ns = %w[NameServer DhcpNameServer].find do |key| |
| 109 | + ns = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key) |
| 110 | + break ns.split(/[,\s]\s*/) unless ns.empty? |
| 111 | + end |
| 112 | + |
| 113 | + next if (nameserver & ns).empty? |
| 114 | + |
| 115 | + if add_search |
| 116 | + [ 'Domain', 'DhcpDomain' ].each do |key| |
| 117 | + dom = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key) |
| 118 | + unless dom.empty? |
| 119 | + search.concat(dom.split(/,\s*/)) |
| 120 | + break |
126 | 121 | end
|
127 | 122 | end
|
128 | 123 | end
|
129 |
| - search << devo if add_search and devo |
130 | 124 | end
|
| 125 | + search << devo if add_search and devo |
131 | 126 | [ search.uniq, nameserver.uniq ]
|
132 | 127 | end
|
| 128 | + |
| 129 | + def get_item_property(path, name, expand: false) |
| 130 | + if defined?(Win32::Registry) |
| 131 | + Registry::HKEY_LOCAL_MACHINE.open(path) do |reg| |
| 132 | + expand ? reg.read_s_expand(name) : reg.read_s(name) |
| 133 | + rescue Registry::Error |
| 134 | + "" |
| 135 | + end |
| 136 | + else |
| 137 | + cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{name}" |
| 138 | + output, _ = Open3.capture2('powershell', '-Command', cmd) |
| 139 | + output.strip |
| 140 | + end |
| 141 | + end |
133 | 142 | end
|
134 | 143 | end
|
135 | 144 | end
|
0 commit comments