Skip to content

Instantly share code, notes, and snippets.

@leolovenet
Last active May 5, 2022 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leolovenet/71d5e5e7ee82c8fe2689db69cf046415 to your computer and use it in GitHub Desktop.
Save leolovenet/71d5e5e7ee82c8fe2689db69cf046415 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/luajit
local interface = "Wi-Fi"
local action = arg[1]
assert(action == "add" or action == "del" or action == "get")
local cmd = ""
if action == "get" then
cmd = "/usr/bin/sudo /usr/sbin/networksetup -getadditionalroutes " .. interface
print(cmd)
os.execute(cmd)
os.exit(0)
elseif action == "del" then
local gateway = assert(io.popen("/sbin/route -n get default"):read("*a"):match("gateway:%s+([%S]*)"))
cmd = "/usr/bin/sudo /usr/sbin/networksetup -setadditionalroutes " .. interface
print(cmd)
os.execute(cmd)
for i = 0, 50 do
if not io.popen("/usr/bin/sudo /sbin/route -q -n flush -inet"):read() then
break
end
end
cmd = "/usr/bin/sudo /sbin/route -q add -net 0.0.0.0 " .. gateway
print(cmd)
os.execute(cmd)
os.exit(0)
end
local gateway = assert(os.getenv("route_net_gateway"))
local max_line = arg[2] and assert(tonumber(arg[2]))
local mask_table = {
[0] = "0.0.0.0",
[1] = "128.0.0.0",
[2] = "192.0.0.0",
[3] = "224.0.0.0",
[4] = "240.0.0.0",
[5] = "248.0.0.0",
[6] = "252.0.0.0",
[7] = "254.0.0.0",
[8] = "255.0.0.0",
[9] = "255.128.0.0",
[10] = "255.192.0.0",
[11] = "255.224.0.0",
[12] = "255.240.0.0",
[13] = "255.248.0.0",
[14] = "255.252.0.0",
[15] = "255.254.0.0",
[16] = "255.255.0.0",
[17] = "255.255.128.0",
[18] = "255.255.192.0",
[19] = "255.255.224.0",
[20] = "255.255.240.0",
[21] = "255.255.248.0",
[22] = "255.255.252.0",
[23] = "255.255.254.0",
[24] = "255.255.255.0",
[25] = "255.255.255.128",
[26] = "255.255.255.192",
[27] = "255.255.255.224",
[28] = "255.255.255.240",
[29] = "255.255.255.248",
[30] = "255.255.255.252",
[31] = "255.255.255.254",
[32] = "255.255.255.255",
}
-- local txt_file = arg[0]:gsub("/[^/]*$", "/china_ip_list.txt")
local txt_file = "./china_ip_list.txt"
local fh = assert(io.open(txt_file))
local result = {}
local line_num = 0
for line in fh:lines() do
if line:len() > 0 then
local ip, mask = line:match("^(%d+%.%d+%.%d+%.%d+)/(%d+)")
mask = assert(tonumber(mask))
assert(mask >= 0 and mask <= 32)
mask = mask_table[mask]
local record = ip .. " " .. mask .. " " .. gateway
-- print(line, record)
table.insert(result, record)
line_num = line_num + 1
if max_line and line_num >= max_line then
break
end
end
end
os.execute("/usr/bin/sudo /usr/sbin/networksetup -setadditionalroutes " .. interface .. " " .. table.concat(result, " "))
fh:close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment