2018-03-28 08:57:02 +00:00
|
|
|
local tools = require "luci.tools.status"
|
|
|
|
local sys = require "luci.sys"
|
|
|
|
local json = require("luci.json")
|
2018-06-12 17:22:55 +00:00
|
|
|
local fs = require("nixio.fs")
|
2018-09-01 19:59:02 +00:00
|
|
|
local net = require "luci.model.network".init()
|
2018-05-09 14:08:52 +00:00
|
|
|
local ucic = luci.model.uci.cursor()
|
2018-03-23 19:23:27 +00:00
|
|
|
module("luci.controller.openmptcprouter", package.seeall)
|
|
|
|
|
|
|
|
function index()
|
|
|
|
-- entry({"admin", "openmptcprouter"}, firstchild(), _("OpenMPTCProuter"), 19).index = true
|
|
|
|
-- entry({"admin", "openmptcprouter", "wizard"}, template("openmptcprouter/wizard"), _("Wizard"), 1).leaf = true
|
|
|
|
-- entry({"admin", "openmptcprouter", "wizard_add"}, post("wizard_add")).leaf = true
|
2018-03-28 08:59:15 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter"}, alias("admin", "system", "openmptcprouter", "wizard"), _("OpenMPTCProuter"), 1)
|
2018-03-28 08:57:02 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter", "wizard"}, template("openmptcprouter/wizard"), _("Settings Wizard"), 1)
|
2018-03-23 19:23:27 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter", "wizard_add"}, post("wizard_add"))
|
2018-03-28 08:57:02 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter", "status"}, template("openmptcprouter/wanstatus"), _("Status"), 2).leaf = true
|
|
|
|
entry({"admin", "system", "openmptcprouter", "interfaces_status"}, call("interfaces_status")).leaf = true
|
2018-04-12 13:18:56 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter", "settings"}, template("openmptcprouter/settings"), _("Advanced Settings"), 3).leaf = true
|
|
|
|
entry({"admin", "system", "openmptcprouter", "settings_add"}, post("settings_add")).leaf = true
|
2018-12-06 12:50:56 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter", "update_vps"}, post("update_vps")).leaf = true
|
2019-02-14 20:20:04 +00:00
|
|
|
entry({"admin", "system", "openmptcprouter", "debug"}, template("openmptcprouter/debug"), _("Show all settings"), 4).leaf = true
|
2018-03-23 19:23:27 +00:00
|
|
|
end
|
|
|
|
|
2018-09-01 19:59:02 +00:00
|
|
|
function interface_from_device(dev)
|
|
|
|
for _, iface in ipairs(net:get_networks()) do
|
|
|
|
local ifacen = iface:name()
|
|
|
|
local ifacename = ucic:get("network",ifacen,"ifname")
|
|
|
|
if ifacename == dev then
|
|
|
|
return ifacen
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2018-03-23 19:23:27 +00:00
|
|
|
function wizard_add()
|
2018-12-12 20:30:15 +00:00
|
|
|
local gostatus = true
|
|
|
|
-- Add new server
|
|
|
|
local add_server = luci.http.formvalue("add_server") or ""
|
|
|
|
local add_server_name = luci.http.formvalue("add_server_name") or ""
|
|
|
|
if add_server ~= "" and add_server_name ~= "" then
|
2018-12-13 12:54:06 +00:00
|
|
|
ucic:set("openmptcprouter",add_server_name:gsub("[^%w_]+","_"),"server")
|
2018-12-12 20:30:15 +00:00
|
|
|
gostatus = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Remove existing server
|
2018-12-17 19:46:53 +00:00
|
|
|
local delete_server = luci.http.formvaluetable("deleteserver") or ""
|
2018-12-12 20:30:15 +00:00
|
|
|
if delete_server ~= "" then
|
2018-12-17 19:46:53 +00:00
|
|
|
for serverdel, _ in pairs(delete_server) do
|
2018-12-21 15:20:20 +00:00
|
|
|
ucic:foreach("network", "interface", function(s)
|
|
|
|
local sectionname = s[".name"]
|
|
|
|
ucic:delete("network","server_" .. serverdel .. "_" .. sectionname .. "_route")
|
|
|
|
end)
|
|
|
|
ucic:delete("network","server_" .. serverdel .. "_default_route")
|
|
|
|
ucic:delete("openmptcprouter",serverdel)
|
|
|
|
ucic:save("openmptcprouter")
|
|
|
|
ucic:commit("openmptcprouter")
|
|
|
|
ucic:save("network")
|
|
|
|
ucic:commit("network")
|
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin/system/openmptcprouter/wizard"))
|
|
|
|
return
|
2018-12-12 20:30:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add new interface
|
2018-04-20 12:56:19 +00:00
|
|
|
local add_interface = luci.http.formvalue("add_interface") or ""
|
2018-09-01 19:59:02 +00:00
|
|
|
local add_interface_ifname = luci.http.formvalue("add_interface_ifname") or ""
|
2018-04-20 12:56:19 +00:00
|
|
|
if add_interface ~= "" then
|
|
|
|
local i = 1
|
2018-06-22 08:00:46 +00:00
|
|
|
local multipath_master = false
|
2018-04-20 12:56:19 +00:00
|
|
|
ucic:foreach("network", "interface", function(s)
|
|
|
|
local sectionname = s[".name"]
|
|
|
|
if sectionname:match("^wan(%d+)$") then
|
|
|
|
i = i + 1
|
|
|
|
end
|
2018-07-01 08:22:02 +00:00
|
|
|
if ucic:get("network",sectionname,"multipath") == "master" then
|
2018-06-22 08:00:46 +00:00
|
|
|
multipath_master = true
|
|
|
|
end
|
2018-04-20 12:56:19 +00:00
|
|
|
end)
|
2018-09-01 19:59:02 +00:00
|
|
|
local defif = "eth0"
|
|
|
|
if add_interface_ifname == "" then
|
|
|
|
local defif1 = ucic:get("network","wan1_dev","ifname") or ""
|
|
|
|
if defif1 ~= "" then
|
|
|
|
defif = defif1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
defif = add_interface_ifname
|
|
|
|
end
|
|
|
|
|
|
|
|
local ointf = interface_from_device(defif) or ""
|
2019-02-19 18:46:08 +00:00
|
|
|
local wanif = defif
|
2018-09-01 19:59:02 +00:00
|
|
|
if ointf ~= "" then
|
|
|
|
if ucic:get("network",ointf,"type") == "" then
|
|
|
|
ucic:set("network",ointf,"type","macvlan")
|
|
|
|
end
|
2019-02-19 18:46:08 +00:00
|
|
|
wanif = "wan" .. i
|
2018-09-01 19:59:02 +00:00
|
|
|
end
|
|
|
|
|
2018-04-20 12:56:19 +00:00
|
|
|
ucic:set("network","wan" .. i,"interface")
|
|
|
|
ucic:set("network","wan" .. i,"ifname",defif)
|
|
|
|
ucic:set("network","wan" .. i,"proto","static")
|
2018-09-01 19:59:02 +00:00
|
|
|
if ointf ~= "" then
|
|
|
|
ucic:set("network","wan" .. i,"type","macvlan")
|
|
|
|
end
|
2018-04-20 12:56:19 +00:00
|
|
|
ucic:set("network","wan" .. i,"ip4table","wan")
|
2018-06-22 08:00:46 +00:00
|
|
|
if multipath_master then
|
|
|
|
ucic:set("network","wan" .. i,"multipath","on")
|
|
|
|
else
|
|
|
|
ucic:set("network","wan" .. i,"multipath","master")
|
|
|
|
end
|
2018-04-20 12:56:19 +00:00
|
|
|
ucic:set("network","wan" .. i,"defaultroute","0")
|
2018-12-11 12:24:22 +00:00
|
|
|
ucic:reorder("network","wan" .. i, i + 2)
|
2018-04-20 12:56:19 +00:00
|
|
|
ucic:save("network")
|
|
|
|
ucic:commit("network")
|
2018-10-01 18:46:24 +00:00
|
|
|
|
|
|
|
ucic:set("qos","wan" .. i,"interface")
|
|
|
|
ucic:set("qos","wan" .. i,"classgroup","Default")
|
|
|
|
ucic:set("qos","wan" .. i,"enabled","0")
|
|
|
|
ucic:set("qos","wan" .. i,"upload","4000")
|
|
|
|
ucic:set("qos","wan" .. i,"download","100000")
|
|
|
|
ucic:save("qos")
|
|
|
|
ucic:commit("qos")
|
|
|
|
|
|
|
|
ucic:set("sqm","wan" .. i,"queue")
|
2019-04-03 21:14:25 +00:00
|
|
|
if ointf ~= "" then
|
|
|
|
ucic:set("sqm","wan" .. i,"interface","wan" .. i)
|
|
|
|
else
|
|
|
|
ucic:set("sqm","wan" .. i,"interface",defif)
|
|
|
|
end
|
2018-10-01 18:46:24 +00:00
|
|
|
ucic:set("sqm","wan" .. i,"qdisc","fq_codel")
|
|
|
|
ucic:set("sqm","wan" .. i,"script","simple.qos")
|
|
|
|
ucic:set("sqm","wan" .. i,"qdisc_advanced","0")
|
|
|
|
ucic:set("sqm","wan" .. i,"linklayer","none")
|
|
|
|
ucic:set("sqm","wan" .. i,"enabled","0")
|
|
|
|
ucic:set("sqm","wan" .. i,"debug_logging","0")
|
|
|
|
ucic:set("sqm","wan" .. i,"verbosity","5")
|
|
|
|
ucic:set("sqm","wan" .. i,"download","0")
|
|
|
|
ucic:set("sqm","wan" .. i,"upload","0")
|
|
|
|
ucic:save("sqm")
|
|
|
|
ucic:commit("sqm")
|
2018-10-22 17:18:16 +00:00
|
|
|
|
2019-02-19 18:46:08 +00:00
|
|
|
luci.sys.call("uci -q add_list vnstat.@vnstat[-1].interface=" .. wanif)
|
2018-12-04 15:56:02 +00:00
|
|
|
luci.sys.call("uci -q commit vnstat")
|
2018-10-01 18:46:24 +00:00
|
|
|
|
2018-04-20 12:56:19 +00:00
|
|
|
-- Dirty way to add new interface to firewall...
|
|
|
|
luci.sys.call("uci -q add_list firewall.@zone[1].network=wan" .. i)
|
2018-07-05 08:44:47 +00:00
|
|
|
luci.sys.call("uci -q commit firewall")
|
2018-04-20 12:56:19 +00:00
|
|
|
|
|
|
|
luci.sys.call("/etc/init.d/macvlan restart >/dev/null 2>/dev/null")
|
|
|
|
gostatus = false
|
|
|
|
end
|
|
|
|
|
2018-12-12 20:30:15 +00:00
|
|
|
-- Remove existing interface
|
2018-06-15 06:29:35 +00:00
|
|
|
local delete_intf = luci.http.formvaluetable("delete") or ""
|
2018-04-20 12:56:19 +00:00
|
|
|
if delete_intf ~= "" then
|
|
|
|
for intf, _ in pairs(delete_intf) do
|
2018-12-17 19:46:53 +00:00
|
|
|
local defif = ucic:set("network",intf,"ifname")
|
2018-04-20 12:56:19 +00:00
|
|
|
ucic:delete("network",intf)
|
|
|
|
ucic:delete("network",intf .. "_dev")
|
|
|
|
ucic:save("network")
|
|
|
|
ucic:commit("network")
|
2018-10-01 18:46:24 +00:00
|
|
|
ucic:delete("sqm",intf)
|
|
|
|
ucic:save("sqm")
|
|
|
|
ucic:commit("sqm")
|
|
|
|
ucic:delete("qos",intf)
|
|
|
|
ucic:save("qos")
|
|
|
|
ucic:commit("qos")
|
2018-12-17 19:46:53 +00:00
|
|
|
luci.sys.call("uci -q del_list vnstat.@vnstat[-1].interface=" .. defif)
|
2019-03-20 19:09:28 +00:00
|
|
|
luci.sys.call("uci -q commit vnstat")
|
2019-03-21 06:16:41 +00:00
|
|
|
luci.sys.call("uci -q del_list firewall.@zone[1].network=" .. intf)
|
2019-03-20 19:09:28 +00:00
|
|
|
luci.sys.call("uci -q commit firewall")
|
2018-11-27 14:21:47 +00:00
|
|
|
gostatus = false
|
2018-04-20 12:56:19 +00:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 15:10:52 +00:00
|
|
|
|
|
|
|
-- Set interfaces settings
|
|
|
|
local interfaces = luci.http.formvaluetable("intf")
|
|
|
|
for intf, _ in pairs(interfaces) do
|
2018-11-27 14:21:47 +00:00
|
|
|
local proto = luci.http.formvalue("cbid.network.%s.proto" % intf) or "static"
|
2018-08-08 14:00:11 +00:00
|
|
|
local ipaddr = luci.http.formvalue("cbid.network.%s.ipaddr" % intf) or ""
|
|
|
|
local netmask = luci.http.formvalue("cbid.network.%s.netmask" % intf) or ""
|
|
|
|
local gateway = luci.http.formvalue("cbid.network.%s.gateway" % intf) or ""
|
2019-04-03 21:14:25 +00:00
|
|
|
local sqmenabled = luci.http.formvalue("cbid.sqm.%s.enabled" % intf) or "0"
|
2018-11-27 14:21:47 +00:00
|
|
|
if proto ~= "other" then
|
|
|
|
ucic:set("network",intf,"proto",proto)
|
|
|
|
end
|
2018-06-07 15:10:52 +00:00
|
|
|
ucic:set("network",intf,"ipaddr",ipaddr)
|
|
|
|
ucic:set("network",intf,"netmask",netmask)
|
|
|
|
ucic:set("network",intf,"gateway",gateway)
|
2018-08-02 07:39:43 +00:00
|
|
|
|
2018-10-26 14:51:30 +00:00
|
|
|
local downloadspeed = luci.http.formvalue("cbid.sqm.%s.download" % intf) or "0"
|
|
|
|
local uploadspeed = luci.http.formvalue("cbid.sqm.%s.upload" % intf) or "0"
|
2019-01-13 10:10:05 +00:00
|
|
|
|
2019-02-06 19:28:14 +00:00
|
|
|
if not ucic:get("qos",intf) ~= "" then
|
2019-01-13 10:10:05 +00:00
|
|
|
ucic:set("qos",intf,"interface")
|
|
|
|
ucic:set("qos",intf,"classgroup","Default")
|
|
|
|
ucic:set("qos",intf,"enabled","0")
|
|
|
|
ucic:set("qos",intf,"upload","4000")
|
|
|
|
ucic:set("qos",intf,"download","100000")
|
|
|
|
end
|
|
|
|
|
2019-02-06 19:28:14 +00:00
|
|
|
if not ucic:get("sqm",intf) ~= "" then
|
2019-04-03 21:14:25 +00:00
|
|
|
local defif = get_device(intf)
|
2019-01-13 10:10:05 +00:00
|
|
|
if defif == "" then
|
|
|
|
defif = ucic:get("network",intf,"ifname") or ""
|
|
|
|
end
|
|
|
|
ucic:set("sqm",intf,"queue")
|
|
|
|
ucic:set("sqm",intf,"interface",defif)
|
|
|
|
ucic:set("sqm",intf,"qdisc","fq_codel")
|
|
|
|
ucic:set("sqm",intf,"script","simple.qos")
|
|
|
|
ucic:set("sqm",intf,"qdisc_advanced","0")
|
|
|
|
ucic:set("sqm",intf,"linklayer","none")
|
|
|
|
ucic:set("sqm",intf,"enabled","0")
|
|
|
|
ucic:set("sqm",intf,"debug_logging","0")
|
|
|
|
ucic:set("sqm",intf,"verbosity","5")
|
|
|
|
ucic:set("sqm",intf,"download","0")
|
|
|
|
ucic:set("sqm",intf,"upload","0")
|
|
|
|
end
|
|
|
|
|
2018-10-26 14:51:30 +00:00
|
|
|
if downloadspeed ~= "0" and uploadspeed ~= "0" then
|
2019-03-22 19:27:13 +00:00
|
|
|
ucic:set("network",intf,"downloadspeed",downloadspeed)
|
|
|
|
ucic:set("network",intf,"uploadspeed",uploadspeed)
|
2019-04-03 21:14:25 +00:00
|
|
|
ucic:set("sqm",intf,"download",downloadspeed*95/100)
|
|
|
|
ucic:set("sqm",intf,"upload",uploadspeed*95/100)
|
|
|
|
if sqmenabled == "1" then
|
|
|
|
ucic:set("sqm",intf,"enabled","1")
|
|
|
|
else
|
|
|
|
ucic:set("sqm",intf,"enabled","0")
|
2019-03-22 19:27:13 +00:00
|
|
|
end
|
2019-04-03 21:14:25 +00:00
|
|
|
ucic:set("qos",intf,"download",downloadspeed*95/100)
|
|
|
|
ucic:set("qos",intf,"upload",uploadspeed*95/100)
|
|
|
|
if sqmenabled == "1" then
|
|
|
|
ucic:set("qos",intf,"enabled","1")
|
|
|
|
else
|
|
|
|
ucic:set("qos",intf,"enabled","0")
|
2019-03-22 19:27:13 +00:00
|
|
|
end
|
2019-04-03 21:14:25 +00:00
|
|
|
else
|
|
|
|
ucic:set("sqm",intf,"download","0")
|
|
|
|
ucic:set("sqm",intf,"upload","0")
|
|
|
|
ucic:set("sqm",intf,"enabled","0")
|
|
|
|
ucic:set("qos",intf,"download","0")
|
|
|
|
ucic:set("qos",intf,"upload","0")
|
|
|
|
ucic:set("qos",intf,"enabled","0")
|
2018-08-02 07:39:43 +00:00
|
|
|
end
|
2018-06-07 15:10:52 +00:00
|
|
|
end
|
2018-08-02 07:39:43 +00:00
|
|
|
ucic:save("sqm")
|
|
|
|
ucic:commit("sqm")
|
2018-08-20 12:02:56 +00:00
|
|
|
ucic:save("qos")
|
|
|
|
ucic:commit("qos")
|
2018-06-07 15:10:52 +00:00
|
|
|
ucic:save("network")
|
|
|
|
ucic:commit("network")
|
2018-04-12 13:18:56 +00:00
|
|
|
|
2018-08-16 14:57:34 +00:00
|
|
|
-- Enable/disable IPv6
|
2019-01-11 17:09:23 +00:00
|
|
|
local disable_ipv6 = luci.http.formvalue("enableipv6") or "1"
|
2018-08-16 14:57:34 +00:00
|
|
|
set_ipv6_state(disable_ipv6)
|
2019-03-06 17:24:22 +00:00
|
|
|
|
|
|
|
-- Enable/disable external check
|
|
|
|
local externalcheck = luci.http.formvalue("externalcheck") or "1"
|
|
|
|
ucic:set("openmptcprouter","settings","external_check",externalcheck)
|
2018-08-16 14:57:34 +00:00
|
|
|
|
2018-06-26 16:07:36 +00:00
|
|
|
-- Get VPN set by default
|
|
|
|
local default_vpn = luci.http.formvalue("default_vpn") or "glorytun_tcp"
|
2018-07-02 11:52:36 +00:00
|
|
|
local vpn_port = ""
|
|
|
|
local vpn_intf = ""
|
2018-06-26 16:07:36 +00:00
|
|
|
if default_vpn:match("^glorytun.*") then
|
|
|
|
vpn_port = 65001
|
2018-07-02 11:52:36 +00:00
|
|
|
vpn_intf = "tun0"
|
2019-02-04 19:08:49 +00:00
|
|
|
ucic:set("network","omrvpn","proto","dhcp")
|
2018-06-26 16:07:36 +00:00
|
|
|
elseif default_vpn == "mlvpn" then
|
|
|
|
vpn_port = 65201
|
2018-07-02 11:52:36 +00:00
|
|
|
vpn_intf = "mlvpn0"
|
2019-01-03 16:40:19 +00:00
|
|
|
ucic:set("network","omrvpn","proto","dhcp")
|
2018-06-26 16:07:36 +00:00
|
|
|
elseif default_vpn == "openvpn" then
|
|
|
|
vpn_port = 65301
|
2018-07-02 11:52:36 +00:00
|
|
|
vpn_intf = "tun0"
|
2019-01-03 16:40:19 +00:00
|
|
|
ucic:set("network","omrvpn","proto","dhcp")
|
2018-07-02 11:52:36 +00:00
|
|
|
end
|
|
|
|
if vpn_intf ~= "" then
|
|
|
|
ucic:set("network","omrvpn","ifname",vpn_intf)
|
|
|
|
ucic:save("network")
|
|
|
|
ucic:commit("network")
|
2018-06-26 16:07:36 +00:00
|
|
|
end
|
|
|
|
|
2018-12-12 20:30:15 +00:00
|
|
|
-- Retrieve all server settings
|
|
|
|
local serversnb = 0
|
|
|
|
local servers = luci.http.formvaluetable("server")
|
|
|
|
for server, _ in pairs(servers) do
|
|
|
|
local server_ip = luci.http.formvalue("%s.server_ip" % server) or ""
|
2019-01-14 17:13:04 +00:00
|
|
|
local master = luci.http.formvalue("master") or ""
|
2018-12-12 20:30:15 +00:00
|
|
|
|
|
|
|
-- OpenMPTCProuter VPS
|
|
|
|
local openmptcprouter_vps_key = luci.http.formvalue("%s.openmptcprouter_vps_key" % server) or ""
|
|
|
|
ucic:set("openmptcprouter",server,"server")
|
|
|
|
ucic:set("openmptcprouter",server,"username","openmptcprouter")
|
|
|
|
ucic:set("openmptcprouter",server,"password",openmptcprouter_vps_key)
|
2019-01-14 17:38:26 +00:00
|
|
|
if master == server or (master == "" and serversnb == 0) then
|
2018-12-14 12:19:58 +00:00
|
|
|
ucic:set("openmptcprouter",server,"get_config","1")
|
2019-01-14 17:13:04 +00:00
|
|
|
ucic:set("openmptcprouter",server,"master","1")
|
|
|
|
ucic:set("openmptcprouter",server,"backup","0")
|
2018-12-14 12:19:58 +00:00
|
|
|
else
|
|
|
|
ucic:set("openmptcprouter",server,"get_config","0")
|
2019-01-14 17:13:04 +00:00
|
|
|
ucic:set("openmptcprouter",server,"master","0")
|
|
|
|
ucic:set("openmptcprouter",server,"backup","1")
|
2018-12-14 12:19:58 +00:00
|
|
|
end
|
2018-12-12 20:30:15 +00:00
|
|
|
ucic:set("openmptcprouter",server,"ip",server_ip)
|
2018-12-13 15:46:40 +00:00
|
|
|
ucic:set("openmptcprouter",server,"port","65500")
|
2018-12-12 20:30:15 +00:00
|
|
|
ucic:save("openmptcprouter")
|
|
|
|
if server_ip ~= "" then
|
|
|
|
serversnb = serversnb + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local ss_servers_nginx = {}
|
|
|
|
local ss_servers_ha = {}
|
|
|
|
local vpn_servers = {}
|
|
|
|
local k = 0
|
|
|
|
local ss_ip
|
|
|
|
|
|
|
|
for server, _ in pairs(servers) do
|
2019-01-14 17:13:04 +00:00
|
|
|
local master = luci.http.formvalue("master") or ""
|
2019-01-07 06:54:18 +00:00
|
|
|
local server_ip = luci.http.formvalue("%s.server_ip" % server) or ""
|
2018-12-12 20:30:15 +00:00
|
|
|
-- We have an IP, so set it everywhere
|
|
|
|
if server_ip ~= "" then
|
|
|
|
-- Check if we have more than one IP, in this case use Nginx HA
|
|
|
|
if serversnb > 1 then
|
2019-01-14 17:13:04 +00:00
|
|
|
if master == server then
|
2018-12-12 20:30:15 +00:00
|
|
|
ss_ip=server_ip
|
2018-12-14 12:19:58 +00:00
|
|
|
table.insert(ss_servers_nginx,server_ip .. ":65101 max_fails=2 fail_timeout=20s")
|
2018-12-12 20:59:08 +00:00
|
|
|
table.insert(ss_servers_ha,server_ip .. ":65101 check")
|
2018-07-02 11:52:36 +00:00
|
|
|
if vpn_port ~= "" then
|
2018-12-14 12:19:58 +00:00
|
|
|
table.insert(vpn_servers,server_ip .. ":" .. vpn_port .. " max_fails=2 fail_timeout=20s")
|
2018-07-02 11:52:36 +00:00
|
|
|
end
|
2018-06-26 16:07:36 +00:00
|
|
|
else
|
2018-12-12 20:30:15 +00:00
|
|
|
table.insert(ss_servers_nginx,server_ip .. ":65101 backup")
|
2018-12-12 20:59:08 +00:00
|
|
|
table.insert(ss_servers_ha,server_ip .. ":65101 backup")
|
2018-07-02 11:52:36 +00:00
|
|
|
if vpn_port ~= "" then
|
2018-12-12 20:30:15 +00:00
|
|
|
table.insert(vpn_servers,server_ip .. ":" .. vpn_port .. " backup")
|
2018-07-02 11:52:36 +00:00
|
|
|
end
|
2018-06-26 16:07:36 +00:00
|
|
|
end
|
|
|
|
k = k + 1
|
2018-12-12 20:30:15 +00:00
|
|
|
ucic:set("nginx-ha","ShadowSocks","enable","1")
|
|
|
|
ucic:set("nginx-ha","VPN","enable","1")
|
|
|
|
ucic:set("nginx-ha","ShadowSocks","upstreams",ss_servers_nginx)
|
|
|
|
ucic:set("nginx-ha","VPN","upstreams",vpn_servers)
|
|
|
|
ucic:set("haproxy-tcp","general","enable","0")
|
|
|
|
ucic:set("haproxy-tcp","general","upstreams",ss_servers_ha)
|
|
|
|
server_ip = "127.0.0.1"
|
|
|
|
--ucic:set("shadowsocks-libev","sss0","server",ss_ip)
|
|
|
|
else
|
|
|
|
ucic:set("nginx-ha","ShadowSocks","enable","0")
|
|
|
|
ucic:set("nginx-ha","VPN","enable","0")
|
|
|
|
--ucic:set("shadowsocks-libev","sss0","server",server_ip)
|
|
|
|
--ucic:set("openmptcprouter","vps","ip",server_ip)
|
|
|
|
--ucic:save("openmptcprouter")
|
2018-06-26 16:07:36 +00:00
|
|
|
end
|
2018-12-12 20:30:15 +00:00
|
|
|
ucic:set("shadowsocks-libev","sss0","server",server_ip)
|
|
|
|
ucic:set("glorytun","vpn","host",server_ip)
|
|
|
|
ucic:set("mlvpn","general","host",server_ip)
|
|
|
|
luci.sys.call("uci -q del openvpn.omr.remote")
|
|
|
|
luci.sys.call("uci -q add_list openvpn.omr.remote=" .. server_ip)
|
2018-06-26 16:07:36 +00:00
|
|
|
ucic:set("qos","serverin","srchost",server_ip)
|
|
|
|
ucic:set("qos","serverout","dsthost",server_ip)
|
|
|
|
end
|
2018-06-07 14:51:37 +00:00
|
|
|
end
|
|
|
|
|
2018-12-12 20:30:15 +00:00
|
|
|
ucic:save("qos")
|
|
|
|
ucic:commit("qos")
|
|
|
|
ucic:save("nginx-ha")
|
|
|
|
ucic:commit("nginx-ha")
|
|
|
|
ucic:save("openvpn")
|
|
|
|
ucic:commit("openvpn")
|
|
|
|
ucic:save("mlvpn")
|
|
|
|
ucic:commit("mlvpn")
|
|
|
|
ucic:save("glorytun")
|
|
|
|
ucic:commit("glorytun")
|
|
|
|
ucic:save("shadowsocks-libev")
|
|
|
|
ucic:commit("shadowsocks-libev")
|
|
|
|
|
|
|
|
|
2018-04-12 13:18:56 +00:00
|
|
|
-- Set ShadowSocks settings
|
2018-03-23 19:23:27 +00:00
|
|
|
local shadowsocks_key = luci.http.formvalue("shadowsocks_key")
|
2018-11-27 14:21:47 +00:00
|
|
|
local shadowsocks_disable = luci.http.formvalue("disableshadowsocks") or "0"
|
2018-03-23 19:23:27 +00:00
|
|
|
if shadowsocks_key ~= "" then
|
|
|
|
ucic:set("shadowsocks-libev","sss0","key",shadowsocks_key)
|
2019-01-14 16:26:36 +00:00
|
|
|
--ucic:set("shadowsocks-libev","sss0","method","chacha20")
|
|
|
|
--ucic:set("shadowsocks-libev","sss0","server_port","65101")
|
2018-11-27 14:21:47 +00:00
|
|
|
ucic:set("shadowsocks-libev","sss0","disabled",shadowsocks_disable)
|
2018-03-23 19:23:27 +00:00
|
|
|
ucic:save("shadowsocks-libev")
|
|
|
|
ucic:commit("shadowsocks-libev")
|
2018-06-08 12:53:35 +00:00
|
|
|
else
|
|
|
|
ucic:set("shadowsocks-libev","sss0","key","")
|
2018-11-27 14:21:47 +00:00
|
|
|
ucic:set("shadowsocks-libev","sss0","disabled",shadowsocks_disable)
|
2018-06-08 12:53:35 +00:00
|
|
|
ucic:save("shadowsocks-libev")
|
|
|
|
ucic:commit("shadowsocks-libev")
|
2018-09-19 13:28:32 +00:00
|
|
|
luci.sys.call("/etc/init.d/shadowsocks rules_down >/dev/null 2>/dev/null")
|
2018-03-23 19:23:27 +00:00
|
|
|
end
|
2018-04-12 13:18:56 +00:00
|
|
|
|
2018-06-12 17:26:32 +00:00
|
|
|
-- Set Glorytun settings
|
|
|
|
if default_vpn:match("^glorytun.*") then
|
|
|
|
ucic:set("glorytun","vpn","enable",1)
|
|
|
|
else
|
|
|
|
ucic:set("glorytun","vpn","enable",0)
|
|
|
|
end
|
|
|
|
|
2018-04-12 13:18:56 +00:00
|
|
|
local glorytun_key = luci.http.formvalue("glorytun_key")
|
2018-03-23 19:23:27 +00:00
|
|
|
if glorytun_key ~= "" then
|
|
|
|
ucic:set("glorytun","vpn","port","65001")
|
|
|
|
ucic:set("glorytun","vpn","key",glorytun_key)
|
|
|
|
ucic:set("glorytun","vpn","mptcp",1)
|
|
|
|
ucic:set("glorytun","vpn","chacha20",1)
|
2018-06-11 08:21:48 +00:00
|
|
|
if default_vpn == "glorytun_udp" then
|
|
|
|
ucic:set("glorytun","vpn","proto","udp")
|
|
|
|
else
|
|
|
|
ucic:set("glorytun","vpn","proto","tcp")
|
|
|
|
end
|
2018-06-08 12:53:35 +00:00
|
|
|
else
|
|
|
|
ucic:set("glorytun","vpn","key","")
|
|
|
|
ucic:set("glorytun","vpn","enable",0)
|
|
|
|
ucic:set("glorytun","vpn","proto","tcp")
|
2018-03-23 19:23:27 +00:00
|
|
|
end
|
2018-12-04 15:56:02 +00:00
|
|
|
ucic:save("glorytun")
|
|
|
|
ucic:commit("glorytun")
|
2018-03-23 19:23:27 +00:00
|
|
|
|
2018-06-07 14:51:37 +00:00
|
|
|
-- Set MLVPN settings
|
2018-06-12 17:26:32 +00:00
|
|
|
if default_vpn == "mlvpn" then
|
|
|
|
ucic:set("mlvpn","general","enable",1)
|
|
|
|
else
|
|
|
|
ucic:set("mlvpn","general","enable",0)
|
|
|
|
end
|
|
|
|
|
2018-06-07 14:51:37 +00:00
|
|
|
local mlvpn_password = luci.http.formvalue("mlvpn_password")
|
|
|
|
if mlvpn_password ~= "" then
|
|
|
|
ucic:set("mlvpn","general","password",mlvpn_password)
|
|
|
|
ucic:set("mlvpn","general","firstport","65201")
|
|
|
|
ucic:set("mlvpn","general","interface_name","mlvpn0")
|
2018-06-08 12:53:35 +00:00
|
|
|
else
|
2018-11-27 14:21:47 +00:00
|
|
|
--ucic:set("mlvpn","general","enable",0)
|
2018-06-08 12:53:35 +00:00
|
|
|
ucic:set("mlvpn","general","password","")
|
2018-06-07 14:51:37 +00:00
|
|
|
end
|
2018-12-04 15:56:02 +00:00
|
|
|
ucic:save("mlvpn")
|
|
|
|
ucic:commit("mlvpn")
|
2018-06-07 14:51:37 +00:00
|
|
|
|
2018-11-02 19:20:53 +00:00
|
|
|
-- Set OpenVPN settings
|
2018-06-12 17:22:55 +00:00
|
|
|
local openvpn_key = luci.http.formvalue("openvpn_key")
|
|
|
|
if openvpn_key ~= "" then
|
|
|
|
local openvpn_key_path = "/etc/luci-uploads/openvpn.key"
|
|
|
|
local fp
|
|
|
|
luci.http.setfilehandler(
|
|
|
|
function(meta, chunk, eof)
|
|
|
|
if not fp and meta and meta.name == "openvpn_key" then
|
|
|
|
fp = io.open(openvpn_key_path, "w")
|
|
|
|
end
|
|
|
|
if fp and chunk then
|
|
|
|
fp:write(chunk)
|
|
|
|
end
|
|
|
|
if fp and eof then
|
|
|
|
fp:close()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
ucic:set("openvpn","omr","secret",openvpn_key_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
if default_vpn == "openvpn" then
|
|
|
|
ucic:set("openvpn","omr","enabled",1)
|
2018-06-12 17:26:32 +00:00
|
|
|
else
|
|
|
|
ucic:set("openvpn","omr","enabled",0)
|
2018-06-12 17:22:55 +00:00
|
|
|
end
|
2018-12-04 15:56:02 +00:00
|
|
|
ucic:save("openvpn")
|
|
|
|
ucic:commit("openvpn")
|
2018-06-12 17:22:55 +00:00
|
|
|
|
2018-11-27 14:21:47 +00:00
|
|
|
-- OpenMPTCProuter VPS
|
2018-12-12 20:30:15 +00:00
|
|
|
--local openmptcprouter_vps_key = luci.http.formvalue("openmptcprouter_vps_key") or ""
|
|
|
|
--ucic:set("openmptcprouter","vps","username","openmptcprouter")
|
|
|
|
--ucic:set("openmptcprouter","vps","password",openmptcprouter_vps_key)
|
|
|
|
--ucic:set("openmptcprouter","vps","get_config","1")
|
2018-11-27 14:21:47 +00:00
|
|
|
local shadowsocks_disable = luci.http.formvalue("disableshadowsocks") or "0"
|
|
|
|
ucic:set("openmptcprouter","settings","shadowsocks_disable",shadowsocks_disable)
|
|
|
|
ucic:set("openmptcprouter","settings","vpn",default_vpn)
|
2018-12-13 12:54:06 +00:00
|
|
|
ucic:delete("openmptcprouter","settings","master_lcintf")
|
2018-11-27 14:21:47 +00:00
|
|
|
ucic:save("openmptcprouter")
|
|
|
|
ucic:commit("openmptcprouter")
|
|
|
|
|
2018-11-02 19:20:53 +00:00
|
|
|
-- Restart all
|
2018-03-23 19:23:27 +00:00
|
|
|
luci.sys.call("(env -i /bin/ubus call network reload) >/dev/null 2>/dev/null")
|
2018-12-11 12:24:22 +00:00
|
|
|
luci.sys.call("/etc/init.d/mptcp restart >/dev/null 2>/dev/null")
|
2018-11-27 14:21:47 +00:00
|
|
|
if openmptcprouter_vps_key ~= "" then
|
|
|
|
luci.sys.call("/etc/init.d/openmptcprouter-vps restart >/dev/null 2>/dev/null")
|
|
|
|
os.execute("sleep 2")
|
|
|
|
end
|
2018-06-14 21:09:43 +00:00
|
|
|
luci.sys.call("/etc/init.d/shadowsocks restart >/dev/null 2>/dev/null")
|
|
|
|
luci.sys.call("/etc/init.d/glorytun restart >/dev/null 2>/dev/null")
|
|
|
|
luci.sys.call("/etc/init.d/glorytun-udp restart >/dev/null 2>/dev/null")
|
|
|
|
luci.sys.call("/etc/init.d/mlvpn restart >/dev/null 2>/dev/null")
|
|
|
|
luci.sys.call("/etc/init.d/openvpn restart >/dev/null 2>/dev/null")
|
2018-12-28 12:18:20 +00:00
|
|
|
luci.sys.call("/etc/init.d/omr-tracker restart >/dev/null 2>/dev/null")
|
2019-02-13 06:42:27 +00:00
|
|
|
luci.sys.call("/etc/init.d/omr-6in4 restart >/dev/null 2>/dev/null")
|
2018-06-07 14:51:37 +00:00
|
|
|
if gostatus == true then
|
2018-04-20 12:56:19 +00:00
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin/system/openmptcprouter/status"))
|
|
|
|
else
|
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin/system/openmptcprouter/wizard"))
|
|
|
|
end
|
2018-03-26 13:06:16 +00:00
|
|
|
return
|
2018-03-28 08:57:02 +00:00
|
|
|
end
|
|
|
|
|
2018-04-12 13:18:56 +00:00
|
|
|
function settings_add()
|
2018-11-27 14:21:47 +00:00
|
|
|
-- Redirects all ports from VPS to OpenMPTCProuter
|
2019-01-07 06:54:18 +00:00
|
|
|
local servers = luci.http.formvaluetable("server")
|
2018-12-21 15:20:20 +00:00
|
|
|
local redirect_ports = luci.http.formvaluetable("redirect_ports")
|
2019-01-07 06:54:18 +00:00
|
|
|
for server, _ in pairs(servers) do
|
|
|
|
local value = luci.http.formvalue("redirect_ports.%s" % server) or "0"
|
2018-12-21 15:20:20 +00:00
|
|
|
ucic:set("openmptcprouter",server,"redirect_ports",value)
|
|
|
|
end
|
2018-11-27 14:21:47 +00:00
|
|
|
|
2018-04-12 13:18:56 +00:00
|
|
|
-- Set tcp_keepalive_time
|
|
|
|
local tcp_keepalive_time = luci.http.formvalue("tcp_keepalive_time")
|
|
|
|
luci.sys.exec("sysctl -w net.ipv4.tcp_keepalive_time=%s" % tcp_keepalive_time)
|
2018-06-13 06:36:20 +00:00
|
|
|
luci.sys.exec("sed -i 's:^net.ipv4.tcp_keepalive_time=[0-9]*:net.ipv4.tcp_keepalive_time=%s:' /etc/sysctl.d/zzz_openmptcprouter.conf" % tcp_keepalive_time)
|
2018-07-22 13:23:02 +00:00
|
|
|
|
|
|
|
-- Set tcp_fin_timeout
|
|
|
|
local tcp_fin_timeout = luci.http.formvalue("tcp_fin_timeout")
|
2018-12-17 15:59:48 +00:00
|
|
|
luci.sys.exec("sysctl -w net.ipv4.tcp_fin_timeout=%s" % tcp_fin_timeout)
|
2018-07-22 13:23:02 +00:00
|
|
|
luci.sys.exec("sed -i 's:^net.ipv4.tcp_fin_timeout=[0-9]*:net.ipv4.tcp_fin_timeout=%s:' /etc/sysctl.d/zzz_openmptcprouter.conf" % tcp_fin_timeout)
|
2018-12-23 13:14:28 +00:00
|
|
|
|
|
|
|
-- Set tcp_syn_retries
|
|
|
|
local tcp_syn_retries = luci.http.formvalue("tcp_syn_retries")
|
|
|
|
luci.sys.exec("sysctl -w net.ipv4.tcp_syn_retries=%s" % tcp_syn_retries)
|
|
|
|
luci.sys.exec("sed -i 's:^net.ipv4.tcp_syn_retries=[0-9]*:net.ipv4.tcp_syn_retries=%s:' /etc/sysctl.d/zzz_openmptcprouter.conf" % tcp_syn_retries)
|
2018-04-12 13:18:56 +00:00
|
|
|
|
2019-01-17 19:43:53 +00:00
|
|
|
-- Set tcp_fastopen
|
|
|
|
local tcp_fastopen = luci.http.formvalue("tcp_fastopen")
|
|
|
|
luci.sys.exec("sysctl -w net.ipv4.tcp_fastopen=%s" % tcp_fastopen)
|
|
|
|
luci.sys.exec("sed -i 's:^net.ipv4.tcp_fastopen=[0-3]*:net.ipv4.tcp_fastopen=%s:' /etc/sysctl.d/zzz_openmptcprouter.conf" % tcp_fastopen)
|
|
|
|
|
2018-04-12 13:18:56 +00:00
|
|
|
-- Disable IPv6
|
2019-02-05 20:06:16 +00:00
|
|
|
local disable_ipv6 = luci.http.formvalue("enableipv6") or "1"
|
2018-08-16 14:57:34 +00:00
|
|
|
set_ipv6_state(disable_ipv6)
|
2018-07-09 12:55:42 +00:00
|
|
|
|
2018-11-02 19:20:53 +00:00
|
|
|
-- Enable/disable obfs
|
2019-02-19 18:46:08 +00:00
|
|
|
local obfs = luci.http.formvalue("obfs") or "0"
|
2019-01-26 12:51:53 +00:00
|
|
|
local obfs_plugin = luci.http.formvalue("obfs_plugin") or "v2ray"
|
2018-07-09 12:55:42 +00:00
|
|
|
ucic:foreach("shadowsocks-libev", "ss_redir", function (section)
|
|
|
|
ucic:set("shadowsocks-libev",section[".name"],"obfs",obfs)
|
2019-01-26 12:51:53 +00:00
|
|
|
ucic:set("shadowsocks-libev",section[".name"],"obfs_plugin",obfs_plugin)
|
2018-07-09 12:55:42 +00:00
|
|
|
end)
|
|
|
|
ucic:set("shadowsocks-libev","tracker","obfs",obfs)
|
2019-01-26 12:51:53 +00:00
|
|
|
ucic:set("shadowsocks-libev","tracker","obfs_plugin",obfs_plugin)
|
2018-07-09 12:55:42 +00:00
|
|
|
|
2018-04-25 10:27:07 +00:00
|
|
|
ucic:save("shadowsocks-libev")
|
|
|
|
ucic:commit("shadowsocks-libev")
|
2018-10-31 11:39:40 +00:00
|
|
|
|
|
|
|
-- Set master to dynamic or static
|
|
|
|
local master_type = luci.http.formvalue("master_type") or "static"
|
|
|
|
ucic:set("openmptcprouter","settings","master",master_type)
|
|
|
|
|
2018-04-17 07:27:15 +00:00
|
|
|
-- Set CPU scaling minimum frequency
|
|
|
|
local scaling_min_freq = luci.http.formvalue("scaling_min_freq") or ""
|
|
|
|
if scaling_min_freq ~= "" then
|
|
|
|
ucic:set("openmptcprouter","settings","scaling_min_freq",scaling_min_freq)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set CPU scaling maximum frequency
|
|
|
|
local scaling_max_freq = luci.http.formvalue("scaling_max_freq") or ""
|
|
|
|
if scaling_max_freq ~= "" then
|
|
|
|
ucic:set("openmptcprouter","settings","scaling_max_freq",scaling_max_freq)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Set CPU governor
|
|
|
|
local scaling_governor = luci.http.formvalue("scaling_governor") or ""
|
|
|
|
if scaling_governor ~= "" then
|
|
|
|
ucic:set("openmptcprouter","settings","scaling_governor",scaling_governor)
|
|
|
|
end
|
2018-04-17 12:48:55 +00:00
|
|
|
|
2018-10-31 11:39:40 +00:00
|
|
|
ucic:save("openmptcprouter")
|
|
|
|
ucic:commit("openmptcprouter")
|
|
|
|
|
2018-11-02 19:20:53 +00:00
|
|
|
-- Apply all settings
|
2018-04-17 12:48:55 +00:00
|
|
|
luci.sys.call("/etc/init.d/openmptcprouter restart >/dev/null 2>/dev/null")
|
|
|
|
|
2018-04-12 13:18:56 +00:00
|
|
|
-- Done, redirect
|
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin/system/openmptcprouter/settings"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2018-12-06 12:50:56 +00:00
|
|
|
function update_vps()
|
|
|
|
-- Update VPS
|
|
|
|
local update_vps = luci.http.formvalue("flash") or ""
|
|
|
|
if update_vps ~= "" then
|
2018-12-13 15:46:40 +00:00
|
|
|
ucic:foreach("openmptcprouter", "server", function(s)
|
2018-12-16 17:48:50 +00:00
|
|
|
local serverip = ucic:get("openmptcprouter",s[".name"],"ip")
|
|
|
|
local adminport = ucic:get("openmptcprouter",s[".name"],"port") or "65500"
|
2018-12-21 15:20:20 +00:00
|
|
|
local token = ucic:get("openmptcprouter",s[".name"],"token") or ""
|
2018-12-13 15:46:40 +00:00
|
|
|
if token ~= "" then
|
|
|
|
sys.exec('curl -4 --max-time 20 -s -k -H "Authorization: Bearer ' .. token .. '" https://' .. serverip .. ":" .. adminport .. "/update")
|
|
|
|
luci.sys.call("/etc/init.d/openmptcprouter-vps restart >/dev/null 2>/dev/null")
|
|
|
|
luci.http.redirect(luci.dispatcher.build_url("admin/system/openmptcprouter/status"))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end)
|
2018-12-06 12:50:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-03 13:06:58 +00:00
|
|
|
function get_ip(interface)
|
|
|
|
local dump = require("luci.util").ubus("network.interface.%s" % interface, "status", {})
|
2018-07-24 15:08:27 +00:00
|
|
|
local ip = ""
|
2018-05-03 13:06:58 +00:00
|
|
|
if dump and dump['ipv4-address'] then
|
|
|
|
local _, ipv4address
|
|
|
|
for _, ipv4address in ipairs(dump['ipv4-address']) do
|
|
|
|
ip = dump['ipv4-address'][_].address
|
|
|
|
end
|
|
|
|
end
|
2018-07-24 15:08:27 +00:00
|
|
|
if ip == "" then
|
|
|
|
local dump = require("luci.util").ubus("network.interface.%s_4" % interface, "status", {})
|
|
|
|
if dump and dump['ipv4-address'] then
|
|
|
|
local _, ipv4address
|
|
|
|
for _, ipv4address in ipairs(dump['ipv4-address']) do
|
|
|
|
ip = dump['ipv4-address'][_].address
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-03 13:06:58 +00:00
|
|
|
return ip
|
|
|
|
end
|
|
|
|
|
2018-06-15 20:40:10 +00:00
|
|
|
function get_device(interface)
|
|
|
|
local dump = require("luci.util").ubus("network.interface.%s" % interface, "status", {})
|
|
|
|
return dump['l3_device']
|
|
|
|
end
|
|
|
|
|
2018-06-08 08:52:13 +00:00
|
|
|
function get_gateway(interface)
|
2018-06-14 04:22:24 +00:00
|
|
|
local gateway = ""
|
2018-06-08 08:52:13 +00:00
|
|
|
local dump = nil
|
|
|
|
|
|
|
|
dump = require("luci.util").ubus("network.interface.%s" % interface, "status", {})
|
|
|
|
|
|
|
|
if dump and dump.route then
|
|
|
|
local _, route
|
|
|
|
for _, route in ipairs(dump.route) do
|
|
|
|
if dump.route[_].target == "0.0.0.0" then
|
|
|
|
gateway = dump.route[_].nexthop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 10:47:40 +00:00
|
|
|
if gateway == "" then
|
2019-01-05 09:10:48 +00:00
|
|
|
if dump and dump.inactive and dump.inactive.route then
|
2019-01-03 10:47:40 +00:00
|
|
|
local _, route
|
|
|
|
for _, route in ipairs(dump.inactive.route) do
|
|
|
|
if dump.inactive.route[_].target == "0.0.0.0" then
|
|
|
|
gateway = dump.inactive.route[_].nexthop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-24 15:08:27 +00:00
|
|
|
|
|
|
|
if gateway == "" then
|
|
|
|
dump = require("luci.util").ubus("network.interface.%s_4" % interface, "status", {})
|
|
|
|
|
|
|
|
if dump and dump.route then
|
|
|
|
local _, route
|
|
|
|
for _, route in ipairs(dump.route) do
|
|
|
|
if dump.route[_].target == "0.0.0.0" then
|
|
|
|
gateway = dump.route[_].nexthop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 10:47:40 +00:00
|
|
|
if gateway == "" then
|
2019-01-05 09:10:48 +00:00
|
|
|
if dump and dump.inactive and dump.inactive.route then
|
2019-01-03 10:47:40 +00:00
|
|
|
local _, route
|
|
|
|
for _, route in ipairs(dump.inactive.route) do
|
|
|
|
if dump.inactive.route[_].target == "0.0.0.0" then
|
|
|
|
gateway = dump.inactive.route[_].nexthop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-24 15:08:27 +00:00
|
|
|
end
|
2018-06-08 08:52:13 +00:00
|
|
|
return gateway
|
|
|
|
end
|
|
|
|
|
2018-05-18 07:40:50 +00:00
|
|
|
-- This function come from OverTheBox by OVH with some changes
|
|
|
|
-- Copyright 2015 OVH <OverTheBox@ovh.net>
|
|
|
|
-- Simon Lelievre (simon.lelievre@corp.ovh.com)
|
|
|
|
-- Sebastien Duponcheel <sebastien.duponcheel@ovh.net>
|
2018-11-02 19:20:53 +00:00
|
|
|
-- Modified by Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
|
2018-05-18 07:40:50 +00:00
|
|
|
-- Under GPL3+
|
2018-03-28 08:57:02 +00:00
|
|
|
function interfaces_status()
|
|
|
|
local ut = require "luci.util"
|
|
|
|
local ntm = require "luci.model.network".init()
|
|
|
|
local uci = require "luci.model.uci".cursor()
|
|
|
|
|
|
|
|
local mArray = {}
|
|
|
|
|
|
|
|
-- OpenMPTCProuter info
|
|
|
|
mArray.openmptcprouter = {}
|
2018-11-02 19:20:53 +00:00
|
|
|
--mArray.openmptcprouter["version"] = ut.trim(sys.exec("cat /etc/os-release | grep VERSION= | sed -e 's:VERSION=::'"))
|
|
|
|
mArray.openmptcprouter["version"] = uci:get("openmptcprouter", "settings", "version") or ut.trim(sys.exec("cat /etc/os-release | grep VERSION= | sed -e 's:VERSION=::' -e 's/^.//' -e 's/.$//'"))
|
2018-07-24 15:08:27 +00:00
|
|
|
|
2018-10-31 11:39:40 +00:00
|
|
|
mArray.openmptcprouter["latest_version_omr"] = uci:get("openmptcprouter", "latest_versions", "omr") or ""
|
|
|
|
mArray.openmptcprouter["latest_version_vps"] = uci:get("openmptcprouter", "latest_versions", "vps") or ""
|
|
|
|
|
2018-08-08 21:54:19 +00:00
|
|
|
mArray.openmptcprouter["service_addr"] = uci:get("shadowsocks-libev", "sss0", "server") or ""
|
2018-03-28 08:57:02 +00:00
|
|
|
mArray.openmptcprouter["local_addr"] = uci:get("network", "lan", "ipaddr")
|
2018-08-09 14:57:26 +00:00
|
|
|
mArray.openmptcprouter["server_mptcp"] = ""
|
2018-05-13 19:24:00 +00:00
|
|
|
-- dns
|
|
|
|
mArray.openmptcprouter["dns"] = false
|
2018-05-18 07:40:50 +00:00
|
|
|
local dns_test = sys.exec("dig openmptcprouter.com | grep 'ANSWER: 0'")
|
2018-05-13 19:24:00 +00:00
|
|
|
if dns_test == "" then
|
|
|
|
mArray.openmptcprouter["dns"] = true
|
|
|
|
end
|
2018-03-28 08:57:02 +00:00
|
|
|
|
2018-08-08 14:00:11 +00:00
|
|
|
mArray.openmptcprouter["ipv6"] = "disabled"
|
2019-01-11 19:31:28 +00:00
|
|
|
if uci:get("openmptcprouter","settings","disable_ipv6") ~= "1" then
|
2018-08-08 14:00:11 +00:00
|
|
|
mArray.openmptcprouter["ipv6"] = "enabled"
|
|
|
|
end
|
|
|
|
|
2018-07-31 15:54:25 +00:00
|
|
|
mArray.openmptcprouter["ss_addr"] = ""
|
|
|
|
--mArray.openmptcprouter["ss_addr6"] = ""
|
|
|
|
mArray.openmptcprouter["wan_addr"] = ""
|
|
|
|
mArray.openmptcprouter["wan_addr6"] = ""
|
|
|
|
local tracker_ip = ""
|
2018-06-18 21:58:02 +00:00
|
|
|
if mArray.openmptcprouter["dns"] == true then
|
2018-12-14 12:19:58 +00:00
|
|
|
-- wanaddr
|
2018-12-17 15:59:48 +00:00
|
|
|
--mArray.openmptcprouter["wan_addr"] = uci:get("openmptcprouter","omr","public_detected_ipv4") or ""
|
2019-02-25 19:06:54 +00:00
|
|
|
|
|
|
|
if uci:get("openmptcprouter","settings","external_check") ~= "0" then
|
|
|
|
mArray.openmptcprouter["wan_addr"] = ut.trim(sys.exec("wget -4 -qO- -T 2 http://ip.openmptcprouter.com"))
|
|
|
|
if mArray.openmptcprouter["wan_addr"] == "" then
|
|
|
|
mArray.openmptcprouter["wan_addr"] = ut.trim(sys.exec("dig TXT +timeout=2 +short o-o.myaddr.l.google.com | awk -F'\"' '{print $2}'"))
|
|
|
|
end
|
|
|
|
if mArray.openmptcprouter["ipv6"] == "enabled" then
|
|
|
|
mArray.openmptcprouter["wan_addr6"] = uci:get("openmptcprouter","omr","public_detected_ipv6") or ""
|
|
|
|
if mArray.openmptcprouter["wan_addr6"] == "" then
|
|
|
|
mArray.openmptcprouter["wan_addr6"] = ut.trim(sys.exec("wget -6 -qO- -T 2 http://ipv6.openmptcprouter.com"))
|
|
|
|
end
|
2018-12-14 12:19:58 +00:00
|
|
|
end
|
2019-02-25 19:06:54 +00:00
|
|
|
mArray.openmptcprouter["external_check"] = true
|
|
|
|
else
|
|
|
|
mArray.openmptcprouter["external_check"] = false
|
2018-12-14 12:19:58 +00:00
|
|
|
end
|
2018-06-18 21:58:02 +00:00
|
|
|
-- shadowsocksaddr
|
2018-12-17 15:59:48 +00:00
|
|
|
mArray.openmptcprouter["ss_addr"] = uci:get("openmptcprouter","omr","detected_ss_ipv4") or ""
|
2018-08-24 11:34:59 +00:00
|
|
|
if mArray.openmptcprouter["ss_addr"] == "" then
|
|
|
|
tracker_ip = uci:get("shadowsocks-libev","tracker","local_address") or ""
|
|
|
|
if tracker_ip ~= "" then
|
|
|
|
local tracker_port = uci:get("shadowsocks-libev","tracker","local_port")
|
2019-02-25 19:06:54 +00:00
|
|
|
if uci:get("openmptcprouter","settings","external_check") ~= "0" then
|
|
|
|
mArray.openmptcprouter["ss_addr"] = ut.trim(sys.exec("curl -s -4 --socks5 " .. tracker_ip .. ":" .. tracker_port .. " -m 2 http://ip.openmptcprouter.com"))
|
|
|
|
--mArray.openmptcprouter["ss_addr6"] = sys.exec("curl -s -6 --socks5 " .. tracker_ip .. ":" .. tracker_port .. " -m 3 http://ipv6.openmptcprouter.com")
|
|
|
|
end
|
2018-08-24 11:34:59 +00:00
|
|
|
end
|
2018-06-18 21:58:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-04 16:08:10 +00:00
|
|
|
mArray.openmptcprouter["remote_addr"] = luci.http.getenv("REMOTE_ADDR") or ""
|
|
|
|
mArray.openmptcprouter["remote_from_lease"] = false
|
2018-03-28 08:57:02 +00:00
|
|
|
local leases=tools.dhcp_leases()
|
|
|
|
for _, value in pairs(leases) do
|
|
|
|
if value["ipaddr"] == mArray.openmptcprouter["remote_addr"] then
|
|
|
|
mArray.openmptcprouter["remote_from_lease"] = true
|
|
|
|
mArray.openmptcprouter["remote_hostname"] = value["hostname"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-31 13:53:17 +00:00
|
|
|
if mArray.openmptcprouter["service_addr"] ~= "" and mArray.openmptcprouter["service_addr"] ~= "127.0.0.1" then
|
|
|
|
mArray.openmptcprouter["vps_status"] = "DOWN"
|
|
|
|
else
|
|
|
|
mArray.openmptcprouter["vps_status"] = "UP"
|
|
|
|
end
|
|
|
|
|
2019-01-07 17:02:53 +00:00
|
|
|
mArray.openmptcprouter["vps_admin"] = false
|
|
|
|
mArray.openmptcprouter["vps_admin_error_msg"] = "Not found"
|
2018-11-27 14:21:47 +00:00
|
|
|
-- Get VPS info
|
2018-12-12 20:30:15 +00:00
|
|
|
ucic:foreach("openmptcprouter", "server", function(s)
|
2019-02-22 18:47:08 +00:00
|
|
|
local serverip = uci:get("openmptcprouter",s[".name"],"ip") or ""
|
2019-01-03 14:54:10 +00:00
|
|
|
if serverip ~= "" then
|
2018-12-14 12:19:58 +00:00
|
|
|
mArray.openmptcprouter["vps_omr_version"] = uci:get("openmptcprouter", s[".name"], "omr_version") or ""
|
|
|
|
mArray.openmptcprouter["vps_kernel"] = uci:get("openmptcprouter",s[".name"],"kernel") or ""
|
|
|
|
mArray.openmptcprouter["vps_machine"] = uci:get("openmptcprouter",s[".name"],"machine") or ""
|
|
|
|
local adminport = uci:get("openmptcprouter",s[".name"],"port") or "65500"
|
2018-12-12 20:30:15 +00:00
|
|
|
local token = uci:get("openmptcprouter",s[".name"],"token") or ""
|
|
|
|
if token ~= "" then
|
2018-12-14 12:19:58 +00:00
|
|
|
local vpsinfo_json = sys.exec('curl -4 --max-time 2 -s -k -H "Authorization: Bearer ' .. token .. '" https://' .. serverip .. ':' .. adminport .. '/status')
|
2018-12-14 19:49:07 +00:00
|
|
|
if vpsinfo_json ~= "" and vpsinfo_json ~= nil then
|
|
|
|
local status, vpsinfo = pcall(function()
|
|
|
|
return json.decode(vpsinfo_json)
|
|
|
|
end)
|
|
|
|
if status and vpsinfo.vps ~= nil then
|
|
|
|
mArray.openmptcprouter["vps_loadavg"] = vpsinfo.vps.loadavg or ""
|
|
|
|
mArray.openmptcprouter["vps_uptime"] = vpsinfo.vps.uptime or ""
|
|
|
|
mArray.openmptcprouter["vps_mptcp"] = vpsinfo.vps.mptcp or ""
|
2018-12-17 15:59:48 +00:00
|
|
|
mArray.openmptcprouter["vps_admin"] = true
|
2018-12-31 13:53:17 +00:00
|
|
|
mArray.openmptcprouter["vps_status"] = "UP"
|
2018-12-14 19:49:07 +00:00
|
|
|
else
|
|
|
|
uci:set("openmptcprouter",s[".name"],"admin_error","1")
|
|
|
|
uci:delete("openmptcprouter",s[".name"],"token")
|
|
|
|
uci:save("openmptcprouter",s[".name"])
|
|
|
|
uci:commit("openmptcprouter",s[".name"])
|
2018-12-17 15:59:48 +00:00
|
|
|
mArray.openmptcprouter["vps_admin"] = false
|
2019-01-07 17:02:53 +00:00
|
|
|
mArray.openmptcprouter["vps_admin_error_msg"] = "Answer error"
|
2018-12-14 19:49:07 +00:00
|
|
|
end
|
2018-12-17 15:59:48 +00:00
|
|
|
else
|
|
|
|
mArray.openmptcprouter["vps_admin"] = false
|
2019-01-07 17:02:53 +00:00
|
|
|
mArray.openmptcprouter["vps_admin_error_msg"] = "No answer"
|
2018-12-12 20:30:15 +00:00
|
|
|
end
|
2018-12-17 15:59:48 +00:00
|
|
|
else
|
|
|
|
mArray.openmptcprouter["vps_admin"] = false
|
2019-01-07 17:02:53 +00:00
|
|
|
mArray.openmptcprouter["vps_admin_error_msg"] = "No token"
|
2018-11-29 13:47:53 +00:00
|
|
|
end
|
|
|
|
end
|
2018-12-12 20:30:15 +00:00
|
|
|
end)
|
2018-11-27 14:21:47 +00:00
|
|
|
|
2018-03-28 08:57:02 +00:00
|
|
|
-- Check openmptcprouter service are running
|
|
|
|
mArray.openmptcprouter["tun_service"] = false
|
2018-08-10 13:09:05 +00:00
|
|
|
mArray.openmptcprouter["tun_state"] = ""
|
|
|
|
mArray.openmptcprouter["tun6_state"] = ""
|
2018-06-12 17:22:55 +00:00
|
|
|
if string.find(sys.exec("/usr/bin/pgrep '^(/usr/sbin/)?glorytun(-udp)?$'"), "%d+") or string.find(sys.exec("/usr/bin/pgrep '^(/usr/sbin/)?mlvpn?$'"), "%d+") or string.find(sys.exec("/usr/bin/pgrep '^(/usr/sbin/)?openvpn?$'"), "%d+") then
|
2018-03-28 08:57:02 +00:00
|
|
|
mArray.openmptcprouter["tun_service"] = true
|
2018-06-06 15:56:07 +00:00
|
|
|
mArray.openmptcprouter["tun_ip"] = get_ip("omrvpn")
|
|
|
|
local tun_dev = uci:get("network","omrvpn","ifname")
|
2018-06-15 20:40:10 +00:00
|
|
|
if tun_dev == "" then
|
|
|
|
tun_dev = get_device("omrvpn")
|
|
|
|
end
|
2018-06-08 08:52:13 +00:00
|
|
|
if tun_dev ~= "" then
|
|
|
|
local peer = get_gateway("omrvpn")
|
2018-06-09 19:47:40 +00:00
|
|
|
if peer == "" then
|
2018-06-08 14:48:15 +00:00
|
|
|
peer = ut.trim(sys.exec("ip -4 r list dev " .. tun_dev .. " | grep kernel | awk '/proto kernel/ {print $1}' | grep -v / | tr -d '\n'"))
|
2018-06-08 08:52:13 +00:00
|
|
|
end
|
2018-06-06 15:56:07 +00:00
|
|
|
if peer ~= "" then
|
2018-08-23 12:52:09 +00:00
|
|
|
local tunnel_ping_test = ut.trim(sys.exec("ping -w 1 -c 1 -I " .. tun_dev .. " " .. peer .. " | grep '100% packet loss'"))
|
2018-06-06 15:56:07 +00:00
|
|
|
if tunnel_ping_test == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
mArray.openmptcprouter["tun_state"] = "UP"
|
2018-06-06 15:56:07 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
mArray.openmptcprouter["tun_state"] = "DOWN"
|
2018-06-06 15:56:07 +00:00
|
|
|
end
|
2018-08-10 13:09:05 +00:00
|
|
|
if mArray.openmptcprouter["ipv6"] == "enabled" then
|
2018-08-23 12:52:09 +00:00
|
|
|
local tunnel_ping6_test = ut.trim(sys.exec("ping6 -w 1 -c 1 -I 6in4-omr6in4 fe80::a00:1 | grep '100% packet loss'"))
|
2018-08-08 14:00:11 +00:00
|
|
|
if tunnel_ping6_test == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
mArray.openmptcprouter["tun6_state"] = "UP"
|
2018-08-08 14:00:11 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
mArray.openmptcprouter["tun6_state"] = "DOWN"
|
2018-08-08 14:00:11 +00:00
|
|
|
end
|
2018-07-10 14:19:18 +00:00
|
|
|
end
|
2018-06-06 15:56:07 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
mArray.openmptcprouter["tun_state"] = "DOWN"
|
|
|
|
mArray.openmptcprouter["tun6_state"] = "DOWN"
|
2018-06-06 15:56:07 +00:00
|
|
|
end
|
2018-05-03 13:06:58 +00:00
|
|
|
end
|
2018-03-28 08:57:02 +00:00
|
|
|
end
|
2018-05-03 13:06:58 +00:00
|
|
|
|
2018-06-06 15:56:07 +00:00
|
|
|
-- check Shadowsocks is running
|
2018-03-28 08:57:02 +00:00
|
|
|
mArray.openmptcprouter["socks_service"] = false
|
|
|
|
if string.find(sys.exec("/usr/bin/pgrep ss-redir"), "%d+") then
|
|
|
|
mArray.openmptcprouter["socks_service"] = true
|
|
|
|
end
|
|
|
|
|
2018-06-14 04:22:24 +00:00
|
|
|
mArray.openmptcprouter["socks_service_enabled"] = true
|
|
|
|
local ss_server = uci:get("shadowsocks-libev","sss0","disabled") or "0"
|
|
|
|
if ss_server == "1" then
|
|
|
|
mArray.openmptcprouter["socks_service_enabled"] = false
|
|
|
|
end
|
2019-01-08 15:57:06 +00:00
|
|
|
local ss_key = uci:get("shadowsocks-libev","sss0","key") or ""
|
|
|
|
mArray.openmptcprouter["socks_service_method"] = uci:get("shadowsocks-libev","sss0","method")
|
|
|
|
if ss_key == "" then
|
|
|
|
mArray.openmptcprouter["socks_service_key"] = false
|
|
|
|
else
|
|
|
|
mArray.openmptcprouter["socks_service_key"] = true
|
|
|
|
end
|
2018-06-14 04:22:24 +00:00
|
|
|
|
2018-03-28 08:57:02 +00:00
|
|
|
-- Add DHCP infos by parsing dnsmasq config file
|
|
|
|
mArray.openmptcprouter.dhcpd = {}
|
|
|
|
dnsmasq = ut.trim(sys.exec("cat /var/etc/dnsmasq.conf*"))
|
|
|
|
for itf, range_start, range_end, mask, leasetime in dnsmasq:gmatch("range=[%w,!:-]*set:(%w+),(%d+\.%d+\.%d+\.%d+),(%d+\.%d+\.%d+\.%d+),(%d+\.%d+\.%d+\.%d+),(%w+)") do
|
|
|
|
mArray.openmptcprouter.dhcpd[itf] = {}
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].interface = itf
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].range_start = range_start
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].range_end = range_end
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].netmask = mask
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].leasetime = leasetime
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].router = mArray.openmptcprouter["local_addr"]
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].dns = mArray.openmptcprouter["local_addr"]
|
|
|
|
end
|
|
|
|
for itf, option, value in dnsmasq:gmatch("option=(%w+),([%w:-]+),(%d+\.%d+\.%d+\.%d+)") do
|
|
|
|
if mArray.openmptcprouter.dhcpd[itf] then
|
|
|
|
if option == "option:router" or option == "6" then
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].router = value
|
|
|
|
end
|
|
|
|
if option == "option:dns-server" or option == "" then
|
|
|
|
mArray.openmptcprouter.dhcpd[itf].dns = value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-06 15:56:07 +00:00
|
|
|
|
2018-03-28 08:57:02 +00:00
|
|
|
-- Parse mptcp kernel info
|
|
|
|
local mptcp = {}
|
|
|
|
local fullmesh = ut.trim(sys.exec("cat /proc/net/mptcp_fullmesh"))
|
|
|
|
for ind, addressId, backup, ipaddr in fullmesh:gmatch("(%d+), (%d+), (%d+), (%d+\.%d+\.%d+\.%d+)") do
|
|
|
|
mptcp[ipaddr] = {}
|
|
|
|
mptcp[ipaddr].index = ind
|
|
|
|
mptcp[ipaddr].id = addressId
|
|
|
|
mptcp[ipaddr].backup= backup
|
|
|
|
mptcp[ipaddr].ipaddr= ipaddr
|
|
|
|
end
|
|
|
|
|
|
|
|
-- retrieve core temperature
|
|
|
|
--mArray.openmptcprouter["core_temp"] = sys.exec("cat /sys/devices/platform/coretemp.0/hwmon/hwmon0/temp2_input 2>/dev/null"):match("%d+")
|
|
|
|
mArray.openmptcprouter["loadavg"] = sys.exec("cat /proc/loadavg 2>/dev/null"):match("[%d%.]+ [%d%.]+ [%d%.]+")
|
|
|
|
mArray.openmptcprouter["uptime"] = sys.exec("cat /proc/uptime 2>/dev/null"):match("[%d%.]+")
|
|
|
|
|
|
|
|
-- overview status
|
|
|
|
mArray.wans = {}
|
|
|
|
mArray.tunnels = {}
|
2018-10-12 08:49:56 +00:00
|
|
|
allintf = {}
|
2018-03-28 08:57:02 +00:00
|
|
|
|
|
|
|
uci:foreach("network", "interface", function (section)
|
|
|
|
local interface = section[".name"]
|
|
|
|
local net = ntm:get_network(interface)
|
2018-08-20 12:02:56 +00:00
|
|
|
local ipaddr = net:ipaddr() or ""
|
2018-08-10 13:09:05 +00:00
|
|
|
local gateway = section["gateway"] or ""
|
|
|
|
local multipath = section["multipath"]
|
|
|
|
local enabled = section["auto"]
|
2018-03-28 08:57:02 +00:00
|
|
|
|
|
|
|
--if not ipaddr or not gateway then return end
|
|
|
|
-- Don't show if0 in the overview
|
|
|
|
--if interface == "lo" then return end
|
|
|
|
|
2019-03-28 20:33:46 +00:00
|
|
|
local ifname = get_device(interface)
|
2018-06-15 20:40:10 +00:00
|
|
|
if ifname == "" then
|
2019-03-28 20:33:46 +00:00
|
|
|
ifname = section["ifname"] or ""
|
2018-06-15 20:40:10 +00:00
|
|
|
end
|
2018-10-12 08:49:56 +00:00
|
|
|
duplicateif = false
|
|
|
|
if ifname ~= "" and ifname ~= nil then
|
|
|
|
if allintf[ifname] then
|
|
|
|
connectivity = "ERROR"
|
|
|
|
duplicateif = true
|
|
|
|
else
|
|
|
|
allintf[ifname] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-03 13:06:58 +00:00
|
|
|
--if multipath == "off" and not ifname:match("^tun.*") then return end
|
|
|
|
if multipath == "off" then return end
|
2018-07-24 15:08:27 +00:00
|
|
|
|
|
|
|
if enabled == "0" then return end
|
2018-03-28 08:57:02 +00:00
|
|
|
|
|
|
|
local connectivity
|
2018-07-03 12:04:01 +00:00
|
|
|
|
|
|
|
if ifname ~= "" and ifname ~= nil then
|
2018-08-02 07:39:43 +00:00
|
|
|
if fs.access("/sys/class/net/" .. ifname) then
|
|
|
|
local multipath_state = ut.trim(sys.exec("multipath " .. ifname .. " | grep deactivated"))
|
|
|
|
if multipath_state == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "OK"
|
2018-08-02 07:39:43 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "ERROR"
|
2018-08-02 07:39:43 +00:00
|
|
|
end
|
2018-07-03 12:04:01 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "ERROR"
|
2018-07-03 12:04:01 +00:00
|
|
|
end
|
2018-08-03 20:41:02 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "ERROR"
|
2018-05-18 07:40:50 +00:00
|
|
|
end
|
|
|
|
|
2018-12-10 13:59:01 +00:00
|
|
|
if ipaddr == "" and ifname ~= nil then
|
|
|
|
ipaddr = ut.trim(sys.exec("ip -4 -br addr ls dev " .. ifname .. " | awk -F'[ /]+' '{print $3}' | tr -d '\n'"))
|
|
|
|
end
|
|
|
|
if ipaddr == "" and ifname ~= nil then
|
|
|
|
ipaddr = ut.trim(sys.exec("ip -4 addr show dev " .. ifname .. " | grep -m 1 inet | awk '{print $2}' | cut -d'/' -s -f1 | tr -d '\n'"))
|
|
|
|
end
|
2018-05-18 07:40:50 +00:00
|
|
|
if ipaddr == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "ERROR"
|
2018-03-28 08:57:02 +00:00
|
|
|
end
|
|
|
|
|
2018-05-18 07:40:50 +00:00
|
|
|
-- Detect WAN gateway status
|
2018-08-10 13:09:05 +00:00
|
|
|
local gw_ping = "UP"
|
2018-06-08 14:48:15 +00:00
|
|
|
if gateway == "" then
|
|
|
|
gateway = get_gateway(interface)
|
|
|
|
end
|
2019-01-03 14:54:10 +00:00
|
|
|
if gateway == "" and ifname ~= nil then
|
2018-08-03 20:41:02 +00:00
|
|
|
if fs.access("/sys/class/net/" .. ifname) then
|
|
|
|
gateway = ut.trim(sys.exec("ip -4 r list dev " .. ifname .. " | grep kernel | awk '/proto kernel/ {print $1}' | grep -v / | tr -d '\n'"))
|
2018-10-16 06:21:32 +00:00
|
|
|
if gateway == "" then
|
|
|
|
gateway = ut.trim(sys.exec("ip -4 r list dev " .. ifname .. " | grep default | awk '{print $3}' | tr -d '\n'"))
|
|
|
|
end
|
2018-08-03 20:41:02 +00:00
|
|
|
end
|
2018-06-08 14:48:15 +00:00
|
|
|
end
|
2019-01-03 14:54:10 +00:00
|
|
|
if gateway ~= "" then
|
2018-08-23 12:52:09 +00:00
|
|
|
local gw_ping_test = ut.trim(sys.exec("ping -w 1 -c 1 " .. gateway .. " | grep '100% packet loss'"))
|
2018-05-18 07:40:50 +00:00
|
|
|
if gw_ping_test ~= "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
gw_ping = "DOWN"
|
2018-05-11 12:14:18 +00:00
|
|
|
if connectivity == "OK" then
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "WARNING"
|
2018-05-18 07:40:50 +00:00
|
|
|
end
|
|
|
|
end
|
2018-08-22 09:32:25 +00:00
|
|
|
elseif gateway == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
gw_ping = "DOWN"
|
|
|
|
connectivity = "ERROR"
|
2018-05-18 07:40:50 +00:00
|
|
|
end
|
|
|
|
|
2018-07-27 13:36:10 +00:00
|
|
|
local latency = ""
|
2018-08-10 13:09:05 +00:00
|
|
|
local server_ping = ""
|
2018-12-13 15:46:40 +00:00
|
|
|
if connectivity ~= "ERROR" and ifname ~= "" and gateway ~= "" and gw_ping ~= "DOWN" and ifname ~= nil and mArray.openmptcprouter["service_addr"] ~= "" then
|
|
|
|
local serverip = mArray.openmptcprouter["service_addr"]
|
|
|
|
if serverip == "127.0.0.1" then
|
|
|
|
serverip = mArray.openmptcprouter["wan_addr"]
|
|
|
|
end
|
|
|
|
if serverip ~= "" then
|
2018-12-14 12:19:58 +00:00
|
|
|
local server_ping_test = sys.exec("ping -w 1 -c 1 -I " .. ifname .. " " .. serverip)
|
2018-12-13 15:46:40 +00:00
|
|
|
local server_ping_result = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | grep '100% packet loss'"))
|
|
|
|
if server_ping_result ~= "" then
|
|
|
|
server_ping = "DOWN"
|
|
|
|
if connectivity == "OK" then
|
|
|
|
connectivity = "WARNING"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
mArray.openmptcprouter["vps_status"] = "UP"
|
|
|
|
server_ping = "UP"
|
|
|
|
latency = ut.trim(sys.exec("echo '" .. server_ping_test .. "' | cut -d '/' -s -f5 | cut -d '.' -f1"))
|
2018-07-27 13:36:10 +00:00
|
|
|
end
|
|
|
|
end
|
2018-05-30 15:39:59 +00:00
|
|
|
end
|
2018-06-28 13:44:55 +00:00
|
|
|
|
2018-07-27 15:02:07 +00:00
|
|
|
local multipath_available
|
2018-08-03 20:41:02 +00:00
|
|
|
if connectivity ~= "ERROR" and mArray.openmptcprouter["dns"] == true and ifname ~= nil and ifname ~= "" and gateway ~= "" and gw_ping == "UP" then
|
2018-06-28 13:44:55 +00:00
|
|
|
-- Test if multipath can work on the connection
|
2018-08-24 11:34:59 +00:00
|
|
|
local multipath_available_state = uci:get("openmptcprouter",interface,"mptcp_status") or ""
|
2018-08-23 12:52:09 +00:00
|
|
|
if multipath_available_state == "" then
|
|
|
|
--if mArray.openmptcprouter["service_addr"] ~= "" then
|
2018-08-24 11:34:59 +00:00
|
|
|
-- multipath_available_state = ut.trim(sys.exec("omr-tracebox-mptcp " .. mArray.openmptcprouter["service_addr"] .. " " .. ifname .. " | grep 'MPTCP disabled'"))
|
2018-08-23 12:52:09 +00:00
|
|
|
--else
|
|
|
|
multipath_available_state = ut.trim(sys.exec("omr-mptcp-intf " .. ifname .. " | grep 'Nay, Nay, Nay'"))
|
|
|
|
--end
|
2018-08-09 14:57:26 +00:00
|
|
|
else
|
2018-08-24 11:34:59 +00:00
|
|
|
multipath_available_state = ut.trim(sys.exec("echo '" .. multipath_available_state .. "' | grep 'MPTCP disabled'"))
|
2018-08-09 14:57:26 +00:00
|
|
|
end
|
2018-08-23 12:52:09 +00:00
|
|
|
if multipath_available_state == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
multipath_available = "OK"
|
2018-06-28 13:44:55 +00:00
|
|
|
else
|
2018-09-18 20:48:57 +00:00
|
|
|
multipath_available_state_wan = ut.trim(sys.exec("omr-mptcp-intf " .. ifname .. " | grep 'Nay, Nay, Nay'"))
|
2018-08-09 14:57:26 +00:00
|
|
|
if multipath_available_state_wan == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
multipath_available = "OK"
|
2018-12-12 20:30:15 +00:00
|
|
|
if mArray.openmptcprouter["service_addr"] ~= "" and mArray.openmptcprouter["service_addr"] ~= "127.0.0.1" then
|
2018-12-14 12:19:58 +00:00
|
|
|
mArray.openmptcprouter["server_mptcp"] = "disabled"
|
2018-09-18 20:48:57 +00:00
|
|
|
end
|
2018-08-09 14:57:26 +00:00
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
multipath_available = "ERROR"
|
2018-08-24 19:56:46 +00:00
|
|
|
connectivity = "WARNING"
|
2018-07-31 15:54:25 +00:00
|
|
|
end
|
2018-06-28 13:44:55 +00:00
|
|
|
end
|
|
|
|
else
|
2018-08-10 13:09:05 +00:00
|
|
|
multipath_available = "NO CHECK"
|
2018-06-28 13:44:55 +00:00
|
|
|
end
|
|
|
|
|
2018-05-30 15:39:59 +00:00
|
|
|
|
2018-05-18 07:40:50 +00:00
|
|
|
-- Detect if WAN get an IPv6
|
2018-08-10 13:09:05 +00:00
|
|
|
local ipv6_discover = "NONE"
|
2018-08-16 14:57:34 +00:00
|
|
|
if ifname ~= nil and mArray.openmptcprouter["ipv6"] == "enabled" then
|
2018-05-18 07:40:50 +00:00
|
|
|
local ipv6_result = _ipv6_discover(ifname)
|
|
|
|
if type(ipv6_result) == "table" and #ipv6_result > 0 then
|
|
|
|
local ipv6_addr_test
|
|
|
|
for k,v in ipairs(ipv6_result) do
|
2018-08-03 13:00:39 +00:00
|
|
|
if v.RecursiveDnsServer then
|
|
|
|
if type(v.RecursiveDnsServer) ~= "table" then
|
2018-08-10 13:09:05 +00:00
|
|
|
ipv6_addr_test = sys.exec("ip -6 addr | grep " .. v.RecursiveDnsServer)
|
2018-08-03 13:00:39 +00:00
|
|
|
if ipv6_addr_test == "" then
|
2018-08-10 13:09:05 +00:00
|
|
|
ipv6_discover = "DETECTED"
|
2018-08-03 13:00:39 +00:00
|
|
|
if connectivity == "OK" then
|
2018-08-10 13:09:05 +00:00
|
|
|
connectivity = "WARNING"
|
2018-08-03 13:00:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-05-18 07:40:50 +00:00
|
|
|
end
|
2018-05-11 12:14:18 +00:00
|
|
|
end
|
2018-05-03 08:51:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-23 12:52:09 +00:00
|
|
|
local publicIP = uci:get("openmptcprouter",interface,"publicip") or ""
|
2019-02-25 19:06:54 +00:00
|
|
|
if ifname ~= nil and publicIP == "" and uci:get("openmptcprouter","settings","external_check") ~= "0" then
|
2018-08-23 12:52:09 +00:00
|
|
|
publicIP = ut.trim(sys.exec("omr-ip-intf " .. ifname))
|
|
|
|
end
|
2018-07-24 17:42:44 +00:00
|
|
|
local whois = ""
|
|
|
|
if publicIP ~= "" then
|
2018-08-23 12:52:09 +00:00
|
|
|
whois = uci:get("openmptcprouter",interface,"asn") or ""
|
2019-02-25 19:06:54 +00:00
|
|
|
if whois == "" and uci:get("openmptcprouter","settings","external_check") ~= "0" then
|
2018-08-23 12:52:09 +00:00
|
|
|
--whois = ut.trim(sys.exec("whois " .. publicIP .. " | grep -i 'netname' | awk '{print $2}'"))
|
2018-12-14 19:49:07 +00:00
|
|
|
whois = ut.trim(sys.exec("wget -4 -qO- -T 1 'http://api.iptoasn.com/v1/as/ip/" .. publicIP .. "' | jsonfilter -q -e '@.as_description'"))
|
2018-08-23 12:52:09 +00:00
|
|
|
end
|
2018-07-24 17:42:44 +00:00
|
|
|
end
|
2018-08-24 11:34:59 +00:00
|
|
|
|
|
|
|
local mtu = uci:get("openmptcprouter",interface,"mtu") or ""
|
2018-11-05 07:30:39 +00:00
|
|
|
if mtu == "" and ifname ~= nil then
|
2018-08-28 21:01:25 +00:00
|
|
|
mtu = ut.trim(sys.exec("cat /sys/class/net/" .. ifname .. "/mtu | tr -d '\n'"))
|
|
|
|
end
|
2018-03-28 08:57:02 +00:00
|
|
|
|
|
|
|
local data = {
|
2018-08-10 13:09:05 +00:00
|
|
|
label = section["label"] or interface,
|
2018-05-18 07:40:50 +00:00
|
|
|
name = interface,
|
|
|
|
link = net:adminlink(),
|
|
|
|
ifname = ifname,
|
|
|
|
ipaddr = ipaddr,
|
|
|
|
gateway = gateway,
|
2018-08-10 13:09:05 +00:00
|
|
|
multipath = section["multipath"],
|
2018-05-18 07:40:50 +00:00
|
|
|
status = connectivity,
|
|
|
|
wanip = publicIP,
|
|
|
|
latency = latency,
|
2018-08-24 11:34:59 +00:00
|
|
|
mtu = mtu,
|
2018-07-24 15:08:27 +00:00
|
|
|
whois = whois or "unknown",
|
2018-08-10 13:09:05 +00:00
|
|
|
qos = section["trafficcontrol"],
|
|
|
|
download = section["download"],
|
|
|
|
upload = section["upload"],
|
2018-05-18 07:40:50 +00:00
|
|
|
gw_ping = gw_ping,
|
2018-07-24 15:08:27 +00:00
|
|
|
server_ping = server_ping,
|
2018-05-18 07:40:50 +00:00
|
|
|
ipv6_discover = ipv6_discover,
|
2018-06-18 08:56:32 +00:00
|
|
|
multipath_available = multipath_available,
|
2018-10-12 08:49:56 +00:00
|
|
|
duplicateif = duplicateif,
|
2018-03-28 08:57:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 11:05:21 +00:00
|
|
|
if ifname ~= nil and ifname:match("^tun.*") then
|
2018-03-28 08:57:02 +00:00
|
|
|
table.insert(mArray.tunnels, data);
|
2018-07-31 15:54:25 +00:00
|
|
|
elseif ifname ~= nil and ifname:match("^mlvpn.*") then
|
|
|
|
table.insert(mArray.tunnels, data);
|
2018-03-28 08:57:02 +00:00
|
|
|
else
|
|
|
|
table.insert(mArray.wans, data);
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
luci.http.prepare_content("application/json")
|
|
|
|
luci.http.write_json(mArray)
|
2018-05-18 07:40:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- This come from OverTheBox by OVH
|
|
|
|
-- Copyright 2015 OVH <OverTheBox@ovh.net>
|
|
|
|
-- Simon Lelievre (simon.lelievre@corp.ovh.com)
|
|
|
|
-- Sebastien Duponcheel <sebastien.duponcheel@ovh.net>
|
|
|
|
-- Under GPL3+
|
|
|
|
function _ipv6_discover(interface)
|
|
|
|
local result = {}
|
|
|
|
|
|
|
|
--local ra6_list = (sys.exec("rdisc6 -nm " .. interface))
|
2018-05-21 05:58:37 +00:00
|
|
|
local ra6_list = (sys.exec("rdisc6 -n1 -r1 " .. interface))
|
2018-05-18 07:40:50 +00:00
|
|
|
-- dissect results
|
|
|
|
local lines = {}
|
|
|
|
local index = {}
|
|
|
|
ra6_list:gsub('[^\r\n]+', function(c)
|
|
|
|
table.insert(lines, c)
|
|
|
|
if c:match("Hop limit") then
|
|
|
|
table.insert(index, #lines)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
local ra6_result = {}
|
|
|
|
for k,v in ipairs(index) do
|
|
|
|
local istart = v
|
|
|
|
local iend = index[k+1] or #lines
|
|
|
|
|
|
|
|
local entry = {}
|
|
|
|
for i=istart,iend - 1 do
|
|
|
|
local level = lines[i]:find('%w')
|
|
|
|
local line = lines[i]:sub(level)
|
|
|
|
|
|
|
|
local param, value
|
|
|
|
if line:match('^from') then
|
|
|
|
param, value = line:match('(from)%s+(.*)$')
|
|
|
|
else
|
|
|
|
param, value = line:match('([^:]+):(.*)$')
|
|
|
|
-- Capitalize param name and remove spaces
|
|
|
|
param = param:gsub("(%a)([%w_']*)", function(first, rest) return first:upper()..rest:lower() end):gsub("[%s-]",'')
|
|
|
|
param = param:gsub("%.$", '')
|
|
|
|
-- Remove text between brackets, seconds and spaces
|
|
|
|
value = value:lower()
|
|
|
|
value = value:gsub("%(.*%)", '')
|
|
|
|
value = value:gsub("%s-seconds%s-", '')
|
|
|
|
value = value:gsub("^%s+", '')
|
|
|
|
value = value:gsub("%s+$", '')
|
|
|
|
end
|
|
|
|
|
|
|
|
if entry[param] == nil then
|
|
|
|
entry[param] = value
|
|
|
|
elseif type(entry[param]) == "table" then
|
|
|
|
table.insert(entry[param], value)
|
|
|
|
else
|
|
|
|
old = entry[param]
|
|
|
|
entry[param] = {}
|
|
|
|
table.insert(entry[param], old)
|
|
|
|
table.insert(entry[param], value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(ra6_result, entry)
|
|
|
|
end
|
|
|
|
return ra6_result
|
|
|
|
end
|
2018-08-10 15:24:23 +00:00
|
|
|
|
2018-08-16 14:57:34 +00:00
|
|
|
function set_ipv6_state(disable_ipv6)
|
2018-10-01 18:46:24 +00:00
|
|
|
-- Disable/Enable IPv6 support
|
2019-01-11 17:09:23 +00:00
|
|
|
--luci.sys.exec("sysctl -qw net.ipv6.conf.all.disable_ipv6=%s" % disable_ipv6)
|
|
|
|
--luci.sys.exec("sed -i 's:^net.ipv6.conf.all.disable_ipv6=[0-9]*:net.ipv6.conf.all.disable_ipv6=%s:' /etc/sysctl.d/zzz_openmptcprouter.conf" % disable_ipv6)
|
|
|
|
luci.sys.exec("sysctl -qw net.ipv6.conf.all.disable_ipv6=0")
|
|
|
|
luci.sys.exec("sed -i 's:^net.ipv6.conf.all.disable_ipv6=[0-9]*:net.ipv6.conf.all.disable_ipv6=0:' /etc/sysctl.d/zzz_openmptcprouter.conf" % disable_ipv6)
|
2018-10-01 18:46:24 +00:00
|
|
|
|
|
|
|
-- Disable/Enable IPv6 for firewall
|
2018-08-16 14:57:34 +00:00
|
|
|
ucic:set("firewall",ucic:get_first("firewall","defaults"),"disable_ipv6",disable_ipv6)
|
|
|
|
ucic:save("firewall")
|
|
|
|
ucic:commit("firewall")
|
2018-10-01 18:46:24 +00:00
|
|
|
|
|
|
|
-- Disable/Enable IPv6 in OpenMPTCProuter settings
|
|
|
|
ucic:set("openmptcprouter","settings","disable_ipv6",disable_ipv6)
|
|
|
|
ucic:commit("openmptcprouter")
|
|
|
|
|
|
|
|
-- Disable/Enable route announce of IPv6
|
2018-08-30 17:26:08 +00:00
|
|
|
if disable_ipv6 == "1" then
|
2018-08-18 17:26:44 +00:00
|
|
|
ucic:set("dhcp","lan","ra_default","0")
|
2019-02-03 18:46:21 +00:00
|
|
|
ucic:set("network","lan","ipv6","0")
|
2019-03-11 18:35:49 +00:00
|
|
|
else
|
2018-10-01 18:46:24 +00:00
|
|
|
-- ucic:set("dhcp","lan","ra_default","1")
|
2019-03-11 18:35:49 +00:00
|
|
|
ucic:set("network","lan","ipv6","1")
|
|
|
|
ucic:set("network","lan","delegate","0")
|
2018-08-18 17:26:44 +00:00
|
|
|
end
|
2018-10-01 18:46:24 +00:00
|
|
|
|
|
|
|
-- Disable/Enable IPv6 DHCP and change Shadowsocks listen address
|
2018-08-30 17:26:08 +00:00
|
|
|
if disable_ipv6 == "1" then
|
2018-08-16 14:57:34 +00:00
|
|
|
luci.sys.call("uci -q del dhcp.lan.dhcpv6")
|
|
|
|
luci.sys.call("uci -q del dhcp.lan.ra")
|
2018-08-30 20:23:04 +00:00
|
|
|
luci.sys.call("uci -q del dhcp.lan.ra_default")
|
2019-02-12 18:46:52 +00:00
|
|
|
ucic:set("shadowsocks-libev","hi","local_address","0.0.0.0")
|
2018-08-16 14:57:34 +00:00
|
|
|
else
|
2019-03-28 20:33:46 +00:00
|
|
|
ucic:set("dhcp","lan","dhcpv6","server")
|
|
|
|
ucic:set("dhcp","lan","ra","server")
|
|
|
|
ucic:set("dhcp","lan","ra_default","1")
|
2019-02-12 18:46:52 +00:00
|
|
|
ucic:set("shadowsocks-libev","hi","local_address","::")
|
2018-08-16 14:57:34 +00:00
|
|
|
end
|
|
|
|
ucic:save("dhcp")
|
|
|
|
ucic:commit("dhcp")
|
2018-11-27 14:21:47 +00:00
|
|
|
--if disable_ipv6 == "1" then
|
|
|
|
-- luci.sys.exec("/etc/init.d/odhcpd stop >/dev/null 2>&1")
|
|
|
|
-- luci.sys.exec("/etc/init.d/odhcpd disable >/dev/null 2>&1")
|
2018-10-01 18:46:24 +00:00
|
|
|
--else
|
|
|
|
-- luci.sys.exec("/etc/init.d/odhcpd start >/dev/null 2>&1")
|
|
|
|
-- luci.sys.exec("/etc/init.d/odhcpd enable >/dev/null 2>&1")
|
2018-11-27 14:21:47 +00:00
|
|
|
--end
|
2018-08-10 15:24:23 +00:00
|
|
|
end
|