fixed regular expression in live nameserver parsing function
This commit is contained in:
+10
-7
@@ -162,29 +162,32 @@ function utils.get_live_interface_info(interface_name)
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Moved from original main script
|
|
||||||
function utils.get_live_dns_servers_from_resolv(logical_interface_name)
|
function utils.get_live_dns_servers_from_resolv(logical_interface_name)
|
||||||
local dns_servers = {}
|
local dns_servers = {}
|
||||||
local search_domains = {}
|
local search_domains = {}
|
||||||
-- Ensure logical_interface_name is somewhat safe before using in string matching,
|
|
||||||
-- though it's typically from internal config.
|
|
||||||
if not logical_interface_name or not logical_interface_name:match("^[a-zA-Z0-9_%-]+$") then
|
if not logical_interface_name or not logical_interface_name:match("^[a-zA-Z0-9_%-]+$") then
|
||||||
return dns_servers, search_domains -- Invalid interface name format
|
return dns_servers, search_domains
|
||||||
end
|
end
|
||||||
|
|
||||||
local file = io.open("/tmp/resolv.conf.auto", "r")
|
local file = io.open("/tmp/resolv.conf.auto", "r")
|
||||||
if not file then return dns_servers, search_domains end
|
if not file then return dns_servers, search_domains end
|
||||||
|
|
||||||
local in_target_section = false
|
local in_target_section = false
|
||||||
local section_start_pattern_str = "# Interface " .. logical_interface_name
|
-- It looks for the logical_interface_name followed by an optional underscore and digits (e.g., "_4"),
|
||||||
|
-- and then the end of the line. This prevents "wan" from incorrectly matching "wan_lte_".
|
||||||
|
local section_start_pattern = "^%s*# Interface%s+" .. logical_interface_name .. "(_%d+)?%s*$"
|
||||||
|
|
||||||
for line in file:lines() do
|
for line in file:lines() do
|
||||||
if not in_target_section then
|
if not in_target_section then
|
||||||
if line:match("^%s*" .. section_start_pattern_str:gsub("%%", "%%%%") .. "%s*$") then -- Escape % for string pattern
|
-- We now use the more precise pattern to find our starting point.
|
||||||
|
if line:match(section_start_pattern) then
|
||||||
in_target_section = true
|
in_target_section = true
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if line:match("^%s*# Interface ") and not line:match("^%s*" .. section_start_pattern_str:gsub("%%", "%%%%") .. "%s*$") then
|
-- The end-of-section logic is now also hardened. We break if we find a new
|
||||||
|
-- "# Interface" line that does NOT match our precise pattern.
|
||||||
|
if line:match("^%s*# Interface") and not line:match(section_start_pattern) then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user