Skip to content

Commit 6b1c230

Browse files
committed
added default-outbound-addr
1 parent 0c9ea52 commit 6b1c230

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

netns.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ local _options = {
99
subnet = "172.18.0.0/16",
1010
nameservers = { "1.1.1.1", "8.8.8.8" },
1111
subnet6 = "",
12-
remove = false
12+
remove = false,
13+
defaultOutboundADdr = false
1314
}
1415

1516
for _, v in ipairs(_args) do
@@ -43,6 +44,8 @@ for _, v in ipairs(_args) do
4344
if #_nameservers > 0 then
4445
_options.nameservers = _nameservers
4546
end
47+
elseif v.id == "default-outbound-addr" then
48+
_options.defaultOutboundADdr = true
4649
elseif v.id == "remove" then
4750
_options.remove = true
4851
elseif v.id == "force" then
@@ -132,7 +135,16 @@ elseif _options.remove then -- nothing to be removed
132135
os.exit(0)
133136
end
134137

135-
if type(_options.outboundAddr) ~= "string" or not _options.outboundAddr:match("[^%.]*%.[^%.]*%.[^%.]*%.[^/]*") then
138+
if type(_options.outboundAddr) ~= "string" and _options.defaultOutboundADdr then
139+
local _result = proc.exec("ip -o route get to 1.1.1.1", {
140+
stdout = "pipe",
141+
stderr = "pipe"
142+
})
143+
local _route = _result.stdoutStream:read("a")
144+
local _outboundAddr = _route:match("src ([^%.]*%.[^%.]*%.[^%.]*%.[^%s/])")
145+
_options.outboundAddr = _outboundAddr
146+
end
147+
if not _options.outboundAddr:match("[^%.]*%.[^%.]*%.[^%.]*%.[^/]*") then
136148
_error("Invalid netns outbound addr!")
137149
os.exit(2)
138150
end

0 commit comments

Comments
 (0)