Advertisement
Guest User

Untitled

a guest
Jan 30th, 2018
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.41 KB | None | 0 0
  1. --pretranslated: do not change this file
  2.  
  3. -- Localization
  4. -- NG-70914 modified displayed name for UPnP line 720
  5. -- NG-92666 TI-GUI : Query : New Features added
  6. gettext.textdomain('webui-core')
  7.  
  8. local ngx = ngx
  9. local content_helper = require("web.content_helper")
  10. local proxy = require("datamodel")
  11. local ui_helper = require("web.ui_helper")
  12. local message_helper = require("web.uimessage_helper")
  13. local post_helper = require("web.post_helper")
  14. local portslist = require("portslist_helper")
  15. local uinetwork = require("web.uinetwork_helper")
  16. local hosts_ac, hosts_ac_v6 = uinetwork.getAutocompleteHostsList()
  17. local io, pairs, string = io, pairs, string
  18. local table, ipairs, tonumber, format, match = table, ipairs, tonumber, string.format, string.match
  19. local session = ngx.ctx.session
  20.  
  21. local tech = false
  22. if session:getrole() == "engineer" then
  23.     tech = true
  24. end
  25.  
  26. local ddns_supported_services , valid_services = {}, {}
  27.  
  28. local function generate_ddns_supported_services()
  29.     -- open the supported services file that come with the ddns package
  30.     local f = io.open("/etc/ddns/services", "r")
  31.     if f then
  32.         for line in f:lines() do
  33.             --a service in this file is  indicated as a url between quotes, we want a list with urls and name of service in capitals
  34.             local service = line:match('^%b""')
  35.             if service then
  36.                 service = service:gsub('"','')
  37.                 local serviceline = { service , service:upper() }
  38.                 ddns_supported_services[#ddns_supported_services + 1] = serviceline
  39.             end
  40.         end
  41.         f:close()
  42.     end
  43. end
  44.  
  45. generate_ddns_supported_services()
  46.  
  47. -- Retrieve GW IP + netmask for use by validation function
  48. local ipdetails = {
  49.     gw = "uci.network.interface.@lan.ipaddr",
  50.     netmask = "uci.network.interface.@lan.netmask"
  51. }
  52. content_helper.getExactContent(ipdetails)
  53.  
  54. local ddns_status_data = {
  55.     ddns_status = "rpc.ddns.status",
  56. }
  57. content_helper.getExactContent(ddns_status_data)
  58.  
  59. -- DMZ / DynDNS / UPnP switches Only one handleQuery in a page
  60. local qry_params = {
  61.     DMZ_enable = "rpc.network.firewall.dmz.enable",
  62.     DMZ_destinationip = "rpc.network.firewall.dmz.redirect.dest_ip",
  63.     ddns_enabled = "uci.ddns.service.@myddns_ipv4.enabled",
  64.     ddns_service_name = "uci.ddns.service.@myddns_ipv4.service_name",
  65.     ddns_domain = "uci.ddns.service.@myddns_ipv4.domain",
  66.     ddns_lookup_host = "uci.ddns.service.@myddns_ipv4.lookup_host",
  67.     ddns_username = "uci.ddns.service.@myddns_ipv4.username",
  68.     ddns_password = "uci.ddns.service.@myddns_ipv4.password",
  69.     ddns_usehttps = "uci.ddns.service.@myddns_ipv4.use_https",
  70.     ddns_cacert = "uci.ddns.service.@myddns_ipv4.cacert",
  71.     upnp_status = "uci.upnpd.config.enable_upnp",
  72.     upnp_natpmp = "uci.upnpd.config.enable_natpmp",
  73.     upnp_secure_mode = "uci.upnpd.config.secure_mode",
  74. }
  75.  
  76. -- Shortcuts to validation helpers to make lines shorter
  77. local gVIPIL = post_helper.getValidationIfPropInList
  78. local gVIES = post_helper.getValidateInEnumSelect
  79. local vB = post_helper.validateBoolean
  80. local vNES = post_helper.validateNonEmptyString
  81. local vSIP = post_helper.validateStringIsPort
  82. local vSIPR = post_helper.validateStringIsPortRange
  83. local gVP = post_helper.getValidationPassword
  84. local gVSIDIP = post_helper.getValidateStringIsDeviceIPv4
  85. local vSIIP6 = post_helper.validateStringIsIPv6
  86. local vSIDIP = gVSIDIP(ipdetails.gw, ipdetails.netmask)
  87. local gAV = post_helper.getAndValidation
  88. local gOrV = post_helper.getOrValidation
  89. local vQTN = post_helper.validateQTN
  90. local gOV = post_helper.getOptionalValidation
  91. local vSIDN = post_helper.validateStringIsDomainName
  92. local vSIIP = post_helper.validateStringIsIP
  93. local vSIM = post_helper.validateStringIsMAC                                           
  94.  
  95. local function set_lookup_host(value, object, key)
  96.     object["ddns_lookup_host"] = object["ddns_domain"]
  97.     return true
  98. end
  99.  
  100. local function set_cacert(_, object)
  101.     object["ddns_cacert"] = "IGNORE"
  102.     return true
  103. end
  104.  
  105. local qry_valid = {
  106.     DMZ_enable = vB,
  107.     DMZ_destinationip = gAV(gVIPIL(vSIDIP,"DMZ_enable",{"1"}),gOV(vQTN)),
  108.     ddns_enabled = vB,
  109.     ddns_service_name = gAV(gVIPIL(gVIES(ddns_supported_services), "ddns_enabled", {"1"})),
  110.     ddns_username = gVIPIL(vNES, "ddns_enabled", {"1"}),
  111.     ddns_password = gVP(gVIPIL(vNES, "ddns_enabled", {"1"})),
  112.     ddns_domain = gAV(gOrV(vSIDN, vSIIP), gVIPIL(vNES, "ddns_enabled", {"1"})),
  113.     ddns_lookup_host = set_lookup_host,
  114.     ddns_usehttps = vB,
  115.     ddns_cacert = set_cacert,
  116.     upnp_status = vB,
  117.     upnp_natpmp = vB,
  118.     upnp_secure_mode = vB,
  119. }
  120.  
  121. local ddns_state_map = {
  122.   disabled = T"disabled",
  123.   updating = T"updating",
  124.   updated = T"updated",
  125.   error = T"error",
  126. }
  127.  
  128. local ddns_light_map = {
  129.   disabled = "off",
  130.   updating = "orange",
  131.   updated = "green",
  132.   error = "red",
  133. }
  134.  
  135. local del_pfw_index = nil
  136.  
  137. local wol = io.open("/lib/functions/firewall-wol.sh", "r") and proxy.get("uci.wol.config.")
  138. if wol then
  139.   qry_params.WOL_enabled = "uci.wol.config.enabled"
  140.   qry_params.WOL_port = "uci.wol.config.src_dport"
  141.   qry_valid.WOL_enabled = vB
  142.   qry_valid.WOL_port = vSIP
  143. end
  144.  
  145. local qry_data, qry_helpmsg = post_helper.handleQuery(qry_params, qry_valid)
  146.  
  147. local action
  148. if ngx.var.request_method == "POST" then
  149.   action = ngx.req.get_post_args().action
  150.  
  151.   local content = ngx.req.get_post_args()
  152.  
  153.   if content.action == "TABLE-DELETE" and content.tableid == "portforwarding" then
  154.     del_pfw_index = tonumber(content.index)
  155.   end
  156. end
  157.  
  158. local ddns_status = "error"
  159. local ddns_update_info = "No error received from server"
  160.  
  161. if qry_data.ddns_enabled ~= "1" then
  162.   ddns_status = "disabled"
  163. else
  164.   if action == "SAVE" then
  165.     ddns_status = "updating"
  166.   elseif ddns_status_data.ddns_status then
  167.     if ddns_status_data.ddns_status == "Domain's IP updated" then
  168.       ddns_status = "updated"
  169.     elseif ddns_status_data.ddns_status == "No error received from server" then
  170.       ddns_status = "updating"
  171.     else
  172.       ddns_status = "error"
  173.     end
  174.  
  175.     ddns_update_info = format("%s", ddns_status_data.ddns_status)
  176.   end
  177. end
  178.  
  179. -- In UCI
  180. --config 'userredirect'
  181. --        option 'enabled'  '1'
  182. --        option 'name' 'ssh'
  183. --        option 'src' 'wan'
  184. --        option 'proto' 'tcp'
  185. --        option 'src_dport' '5555'
  186. --        option 'dest_ip' '192.168.1.100'
  187. --        option 'dest_mac' '9c:97:26:c5:9b:28'
  188. --        option 'dest_port' '22'
  189. --        option 'target' 'DNAT'
  190. --        option 'dest' 'lan'
  191.  
  192. -- In Transformer
  193. -- rpc.network.firewall.portforward.{i}.enabled
  194. -- rpc.network.firewall.portforward.{i}.name
  195. -- rpc.network.firewall.portforward.{i}.src
  196. -- rpc.network.firewall.portforward.{i}.src_dport
  197. -- rpc.network.firewall.portforward.{i}.dest_ip
  198. -- rpc.network.firewall.portforward.{i}.dest_mac
  199. -- rpc.network.firewall.portforward.{i}.dest_port
  200. -- rpc.network.firewall.portforward.{i}.target
  201. -- rpc.network.firewall.portforward.{i}.dest
  202. -- rpc.network.firewall.portforward.{i}.proto.@1.value
  203.  
  204. -- Templates for pre-defined rules
  205. local function table_removekey(table, key)
  206.    local element = table[key]
  207.    table[key] = nil
  208.    return element
  209. end
  210.  
  211. local wan_app = {}
  212. -- Retrieve all wan-services sections in system config
  213. local servicesTable = proxy.getPN("uci.system.wan-service.", true)
  214. if servicesTable then
  215.   for _,service in ipairs(servicesTable) do
  216.     local port = match(service.path, "uci%.system%.wan%-service%.@([^%.]+)%.")
  217.     if port then
  218.       wan_app[#wan_app + 1 ] = port
  219.     end
  220.   end
  221. end
  222.  
  223. local wan_ports ={}
  224. -- Retrieve the list of ports in all wan-services section
  225. for _,app in ipairs(wan_app) do
  226.   local wan_port_path = "uci.system.wan-service.@".. app .. ".ports"
  227.   wan_ports[#wan_ports + 1 ] = proxy.get(wan_port_path)
  228.  end
  229.  
  230. local reserved_ports = {}
  231. for _,wanPort in ipairs(wan_ports) do
  232.     for port in wanPort[1].value:gmatch("%w+") do
  233.        reserved_ports [#reserved_ports +1] = port
  234.     end
  235. end
  236.  
  237. --Remove the ports configured in system.wan-service in allowed_portlist
  238. local allowed_portlist = portslist
  239. for _,wanPort in ipairs(reserved_ports) do
  240.   for i,j in pairs(allowed_portlist) do
  241.     if wanPort and (tonumber(wanPort) == tonumber(j)) then
  242.        table_removekey(allowed_portlist,i)
  243.     end
  244.   end
  245. end
  246.  
  247. -- Function to check the port are reserved or not while adding the port mapping rule.
  248. local function allowed_ports()
  249.   return function(ports, postdata, key)
  250.     for _,wanPort in ipairs(reserved_ports) do
  251.         if wanPort and (wanPort == ports) then
  252.         return nil, T"Ports already Reserved"
  253.       end
  254.     end
  255.     return true
  256.   end
  257. end
  258.  
  259. local knownapps = require("pfwd_helper")
  260.  
  261. local pfw_helper_map = {}
  262.  
  263. pfw_helper_map["FTP server"] = {pfw = "FTP server", helper = "ftp", wanport = "21"}
  264. pfw_helper_map["TFTP server"] = {pfw = "TFTP server", helper = "tftp", wanport = "69"}
  265. pfw_helper_map["PPTP"] = {pfw = "PPTP", helper = "pptp", wanport = "1723"}
  266.  
  267.  
  268. local portrange_pattern = "^(%d+)%:(%d+)$"
  269. local function compare_startport(a,b)
  270.   return a.start < b.start
  271. end
  272.  
  273. local function validPorts(ports)
  274.     local curend = -1
  275.  
  276.     for _,v in ipairs(ports) do
  277.         if v.start <= curend then
  278.             return nil, { wanport = T"An existing mapping overlaps with the ports range" }
  279.         else
  280.             curend = v["end"]
  281.         end
  282.     end
  283.     return true
  284. end
  285.  
  286. -- Firewall forwarding rules
  287. local pfw_columns = {
  288.   {
  289.     header = "",
  290.     name = "enabled",
  291.     param = "enabled",
  292.     type = "switch",
  293.     default = "1",
  294.     attr = { switch = { ["data-placement"] = "right" }}
  295.   },
  296.   {
  297.     header = T"Name",
  298.     name = "name",
  299.     param = "name",
  300.     type = "text",
  301.     unique = true,
  302.     attr = { input = { class="span2" } },
  303.   },
  304.   {
  305.     header = T"Protocol",
  306.     name = "protocol",
  307.     param = "proto.@1.value",
  308.     default = "tcp",
  309.     type = "select",
  310.     values = {
  311.       { "tcp", "TCP"},
  312.       { "udp", "UDP"},
  313.       { "tcpudp", "TCP/UDP"}
  314.     },
  315.     attr = { select = { class="span2" } },
  316.   },
  317.   {
  318.     header = T"WAN port",
  319.     name = "wanport",
  320.     param = "src_dport",
  321.     type = "text",
  322.     attr = { input = { class="span1", maxlength="11" }, autocomplete=allowed_portlist },
  323.   },
  324.   {
  325.     header = T"LAN port",
  326.     name = "lanport",
  327.     param = "dest_port",
  328.     type = "text",
  329.     attr = { input = { class="span1", maxlength="11" }, autocomplete=portslist },
  330.   },
  331.     {
  332.     header = T"Destination IP",
  333.     name = "destinationip",
  334.     param = "dest_ip",
  335.     type = "text",
  336.     attr = { input = { class="span2", maxlength="17" }, autocomplete=iplist },
  337.   },
  338. }
  339.  
  340.  
  341. local function globalValid(data)
  342.     local tcp = {}
  343.     local udp = {}
  344.     local p1,p2
  345.     local err, msg
  346.  
  347.     local allowedIndexes
  348.     if del_pfw_index ~= nil then
  349.         del_pfw_data, allowedIndexes = content_helper.loadTableData("rpc.network.firewall.portforward.", pfw_columns, nil, "name")
  350.     end
  351.  
  352.     for i,v in ipairs(data) do
  353.     if v[3] and v[4] then
  354.         local chunks = { v[4]:match(portrange_pattern) }
  355.         if #chunks == 2 then
  356.             p1 = tonumber(chunks[1])
  357.             p2 = tonumber(chunks[2])
  358.         else
  359.             p1 = tonumber(v[4])
  360.             p2 = p1
  361.         end
  362.  
  363.         local proto = v[3]
  364.         if proto == "tcp" or proto == "tcpudp" then
  365.             tcp[#tcp+1] = { start = p1, ["end"] = p2, index = i }
  366.         end
  367.         if proto == "udp" or proto == "tcpudp" then
  368.             udp[#udp+1] = { start = p1, ["end"] = p2, index = i }
  369.         end
  370.    end
  371.     end
  372.  
  373.     table.sort(tcp, compare_startport)
  374.     table.sort(udp, compare_startport)
  375.  
  376.     err, msg = validPorts(tcp)
  377.     if not err then
  378.         return err, msg
  379.     end
  380.     err, msg = validPorts(udp)
  381.     return err, msg
  382. end
  383.  
  384. local function getValidateName(value)
  385.   if #value == 0 or #value > 63 then
  386.     return nil, T"A name must be between 1 and 63 characters"
  387.   end
  388.   -- "DMZ rule" is the name reserved for DMZ portmap rule
  389.   if value == "DMZ rule" then
  390.     return nil, T"Reserved name cannot be given as rule name"
  391.   end
  392.   if match(value, "[^%w%-%s]") then
  393.     return nil, T"A name must contain only alphanumeric characters and dash"
  394.   end
  395.   return true
  396. end
  397.  
  398. local protocolList = {
  399.     { "tcp", "TCP"},
  400.     { "udp", "UDP"},
  401.     { "tcpudp", "TCP+UDP"}
  402. }
  403.  
  404. local pfw_valid = {
  405.     enabled = vB,
  406.     name = getValidateName,
  407.     lanport = vSIPR,
  408.     wanport = gAV(vSIPR,allowed_ports()),
  409.     destinationip = gAV(vSIDIP,vQTN),
  410.     protocol = gVIES(protocolList),
  411.     destinationmac = vSIM,
  412. }
  413.  
  414. -- ip handleTableQuery parameter filter callback to only show ipv4 port forwardings ...
  415. -- and only user created rules
  416. -- return true if entry should be displayed
  417. local function pfw_filter(data)
  418.     if (data.target == "DNAT" and
  419.         data.src == "wan" and
  420.         data.dest == "lan" and
  421.         data.family == "ipv4" and data.name ~= "DMZ rule") then
  422.         return true
  423.     end
  424.  
  425.     return false
  426. end
  427.  
  428. -- Warning, this uses transformer paths. So use correct naming
  429. local pfw_defaultObject = {
  430.     src = "wan",
  431.     dest = "lan",
  432.     family = "ipv4",
  433.     target = "DNAT",
  434. }
  435.  
  436. local function set_helper_port(helper, port)
  437.     local path = "uci.firewall.helper."
  438.     local data = proxy.get(path)
  439.  
  440.     if data == nil then
  441.         return
  442.     end
  443.  
  444.     for _,v in ipairs(data) do
  445.         if v.param == "helper" and v.value == helper then
  446.             proxy.set(v.path .. "dest_port", port)
  447.             return
  448.         end
  449.     end
  450.  
  451. end
  452.  
  453. local function get_firewall_helper(userredir_name)
  454.     local tmp
  455.     for _,tmp in pairs(pfw_helper_map) do
  456.         if userredir_name == tmp.pfw then
  457.             return tmp
  458.         end
  459.     end
  460.  
  461.     return nil
  462. end
  463.  
  464. local function get_firewall_helper_name(userredir_name)
  465.     local tmp = get_firewall_helper(userredir_name)
  466.  
  467.     if tmp ~= nil then
  468.         return tmp["helper"]
  469.     else
  470.         return nil
  471.     end
  472. end
  473.  
  474. local function update_firewall_helper(index, content)
  475.     if index == nil then
  476.         return
  477.     end
  478.  
  479.     local helper = get_firewall_helper_name(content.name)
  480.  
  481.     -- the corresponding helper in firewall should be updated also
  482.     if helper ~= nil then
  483.         if content.enabled == "1" then
  484.             set_helper_port(helper, string.untaint(content.wanport))
  485.         else
  486.             set_helper_port(helper, pfw_helper_map[string.untaint(content.name)].wanport)
  487.         end
  488.     end
  489. end
  490.  
  491.  
  492. local function onDelete(index)
  493.     if del_pfw_index == nil then
  494.         return
  495.     end
  496.  
  497.     local helper = get_firewall_helper(del_pfw_data[del_pfw_index][2])
  498.     del_pfw_index = nil
  499.  
  500.     if helper ~= nil and helper["wanport"] ~= nil then
  501.         set_helper_port(helper.helper, helper.wanport)
  502.     end
  503. end
  504.  
  505. local pfw_options = {
  506.     tableid = "portforwarding",
  507.     basepath = "rpc.network.firewall.portforward.",
  508.     createMsg = T"Add new IPv4 port mapping",
  509.     newList = knownapps,
  510.     valid = globalValid,
  511.     sorted = "name",
  512.     onModify = update_firewall_helper,
  513.     onAdd = update_firewall_helper,
  514.     onDelete = onDelete,
  515. }
  516.  
  517. local pfw_data, pfw_helpmsg = post_helper.handleTableQuery(pfw_columns, pfw_options, pfw_filter, pfw_defaultObject, pfw_valid)
  518.  
  519. -- Ipv6 data retrieval - Start
  520.  
  521. -- Useful pieces of transformer data for ipv6
  522. local ipv6Data = {
  523.     -- is IPv6 enabled on the LAN
  524.     lanIpv6Enabled = "uci.network.interface.@lan.ipv6",
  525.     pinholeEnabled = "uci.firewall.rulesgroup.@pinholerules.enabled",
  526. }
  527. content_helper.getExactContent(ipv6Data)
  528.  
  529. -- for drop down selector and validation
  530. local protocolList_v6 = {
  531.     { "tcp", T"TCP"},
  532.     { "udp", T"UDP"},
  533.     { "tcpudp", T"TCP/UDP"},
  534. --  { "udplite", T"UDPLite"}, -- doesn't exist
  535.     { "icmpv6", T"ICMPv6"},
  536. --  { "esp", T"ESP"},  -- fails
  537. --  { "ah", T"AH"},  -- fails
  538. --  { "sctp", T"SCTP"}, -- fails
  539.     { "all", T"All"},
  540. }
  541.  
  542. -- ipv6 handleTableQuery parameter to match columns to rpc table data
  543. -- ipv6 Firewall forwarding rules
  544. local pfw_v6_columns = {
  545.   {
  546.     header = "",
  547.     name = "enabled_v6",
  548.     param = "enabled",
  549.     type = "switch",
  550.     default = "1",
  551.     attr = { switch = { ["data-placement"] = "right" }}
  552.   },
  553.   {
  554.     header = T"Name",
  555.     name = "name",
  556.     param = "name",
  557.     type = "text",
  558.     unique = true,
  559.     attr = { input = { class="span2" } },
  560.   },
  561.   {
  562.     header = T"Protocol",
  563.     name = "protocol",
  564.     param = "proto.@1.value",
  565.     default = "tcp",
  566.     type = "select",
  567.     values = protocolList_v6,
  568.     attr = { select = { class="span2" } },
  569.   },
  570.   {
  571.     header = T"Destination port",
  572.     name = "wanport",
  573.     param = "dest_port",
  574.     type = "text",
  575.     attr = { input = { class="span1", maxlength="11" }, autocomplete=portslist },
  576.   },
  577.   {
  578.     header = T"Destination IP",
  579.     name = "dest_ip_v6",
  580.     param = "dest_ip",
  581.     type = "text",
  582.     attr = { input = { class="span2", maxlength="39" }, autocomplete=hosts_ac_v6 },
  583.   },
  584.   {
  585.     header = T"Destination MAC",
  586.     name = "destinationmac_v6",
  587.     param = "dest_mac",
  588.     type = "text",
  589.     readonly = true,
  590.     attr = {  },
  591.   },
  592. }
  593.  
  594. -- ipv6 handleTableQuery parameter to specify transformer table to use
  595. local pfw_v6_options = {
  596.     tableid = "fwrules_v6",
  597.     basepath = "rpc.network.firewall.pinholerule.",
  598.     createMsg = T"Add new IPv6 forwarding rule",
  599.     sorted = "name",
  600.     newList = knownapps,
  601. }
  602.  
  603. -- ipv6 handleTableQuery parameter filter callback to only show ipv6 rules ...
  604. -- and only user created rules
  605. -- return true if entry should be displayed
  606. local function pfw_v6_filter(data)
  607.     if (data.target == "ACCEPT" and
  608.         data.src == "wan" and
  609.         data.dest == "lan" and
  610.         data.family == "ipv6") then
  611.         return true
  612.     end
  613.  
  614.     return false
  615. end
  616.  
  617. -- ipv6 handleTableQuery parameter for default values when adding entry
  618. local pfw_v6_defaultObject = {
  619.     target = "ACCEPT",
  620.     src = "wan",
  621.     dest = "lan",
  622.     family = "ipv6",
  623. }
  624.  
  625. -- ipv6 handleTableQuery parameter to check validity of new entry values
  626. -- every editable column needs a validity check function
  627. local pfw_v6_valid = {
  628.     enabled_v6 = vB,
  629.     name = getValidateName,
  630.     protocol = gVIES(protocolList_v6),
  631.     wanport = vSIPR,
  632.     dest_ip_v6 = vSIIP6,
  633. }
  634.  
  635. -- ipv6 pull in all the data for the display
  636. local pfw_v6_data, pfw_v6_helpmsg = post_helper.handleTableQuery(pfw_v6_columns, pfw_v6_options, pfw_v6_filter, pfw_v6_defaultObject, pfw_v6_valid)
  637.  
  638. -- Ipv6 data retrieval - End
  639.  
  640. -- DNS rules
  641. local policy_select = {{"any", "any"}}
  642. local outpolicy_select = {{"copy", "copy"}, {"default", "default"}}
  643.  
  644. local policy_pn = proxy.getPN("uci.mwan.policy.",true)
  645. if policy_pn then
  646.     for _,v in ipairs(policy_pn) do
  647.         local path = v["path"]
  648.         local policy_name = match(path,"@([^@%.]-)%.")
  649.         if policy_name then
  650.             policy_select[#policy_select + 1] = {policy_name, policy_name}
  651.             outpolicy_select[#outpolicy_select + 1] = {policy_name, policy_name}
  652.         end
  653.     end
  654. end
  655.  
  656. local intfs_select = {{"default", "default"}}
  657.  
  658. local intfs_pn = proxy.getPN("uci.network.interface.",true)
  659. if intfs_pn then
  660.     for _,v in ipairs(intfs_pn) do
  661.         local path = v["path"]
  662.         local intf_name = match(path,"@([^@%.]-)%.")
  663.         if intf_name and intf_name ~= "loopback" and intf_name ~= "lan" then
  664.             intfs_select[#intfs_select + 1] = {intf_name,intf_name}
  665.         end
  666.     end
  667. end
  668.  
  669. local dnsrule_columns = {
  670.   {
  671.     header = "",
  672.     name = "enable",
  673.     param = "enable",
  674.     type = "switch",
  675.     default = "1",
  676.     attr = { switch = { ["data-placement"] = "right" }}
  677.   },
  678.   {
  679.     header = T"Domain",
  680.     name = "domain",
  681.     param = "domain",
  682.     type = "text",
  683.     attr = { input = { class="span2", maxlength="30"} },
  684.   },
  685.   {
  686.     header = T"DNS Set",
  687.     name = "dnsset",
  688.     param = "dnsset",
  689.     type = "select",
  690.     values = intfs_select,
  691.     attr = { select = { class="span1" } },
  692.   },
  693.   {
  694.     header = T"Policy",
  695.     name = "policy",
  696.     param = "policy",
  697.     type = "select",
  698.     values = policy_select,
  699.     attr = { select = { class="span1" } },
  700.   },
  701.   {
  702.     header = T"Out Policy",
  703.     name = "outpolicy",
  704.     param = "outpolicy",
  705.     type = "select",
  706.     values = outpolicy_select,
  707.     attr = { select = { class="span1" } },
  708.   },
  709. }
  710.  
  711. local function dns_sort(rule1, rule2)
  712.   return rule1.paramindex < rule2.paramindex
  713. end
  714.  
  715. local domain_valid = {
  716.    domain = gOrV(vSIDN, vSIIP),
  717. }
  718. local dnsrule_options = {
  719.     tableid = "dnsrules",
  720.     basepath = "uci.dhcp.dnsrule.@.",
  721.     createMsg = T"Add new dns rule",
  722.     sorted = dns_sort,
  723.     objectName  = post_helper.getRandomKey(),
  724.     addNamedObject = true
  725. }
  726.  
  727. local dnsrule_data, dnsrule_helpmsg = post_helper.handleTableQuery(dnsrule_columns, dnsrule_options, nil, nil, domain_valid)
  728.  
  729. for k, v in pairs(dnsrule_data) do
  730.     if(v[1] == nil or v[1] == "") then
  731.         v[1] = "1"
  732.     end
  733.     if(v[4] == nil or v[4] == "") then
  734.         v[4] = "any"
  735.     end
  736.     if(v[5] == nil or v[5] == "") then
  737.         v[5] = "copy"
  738.     end
  739. end
  740.  
  741. -- UPnP forwarding rules
  742. local upnp_columns = {
  743.   {
  744.     header = T"Protocol",
  745.     name = "protocol",
  746.     param = "proto",
  747.     default = "tcp",
  748.     type = "select",
  749.     values = {
  750.       { "tcp", "TCP"},
  751.       { "udp", "UDP"},
  752.       { "tcpudp", "TCP+UDP"}
  753.     },
  754.     attr = { select = { class="span2" } },
  755.   },
  756.   {
  757.     header = T"WAN port",
  758.     name = "wanport",
  759.     param = "src_dport",
  760.     type = "text",
  761.     attr = { input = { class="span1", maxlength="5" } },
  762.   },
  763.   {
  764.     header = T"LAN port",
  765.     name = "lanport",
  766.     param = "dest_port",
  767.     type = "text",
  768.     attr = { input = { class="span1", maxlength="5" } },
  769.   },
  770.   {
  771.     header = T"Destination",
  772.     name = "destinationip",
  773.     param = "dest_ip",
  774.     type = "text",
  775.     attr = { input = { class="span2", maxlength="15"} },
  776.   },
  777.   {
  778.     header = T"Description",
  779.     name = "description",
  780.     param = "description",
  781.     type = "text",
  782.     attr = { input = { class="span2", maxlength="15"} },
  783.   },
  784. }
  785.  
  786. local upnp_options = {
  787.     canEdit = false,
  788.     canAdd = false,
  789.     canDelete = false,
  790.     tableid = "upnpportforwarding",
  791.     basepath = "sys.upnp.redirect.",
  792. }
  793.  
  794. local upnp_data, upnp_helpmsg = post_helper.handleTableQuery(upnp_columns, upnp_options, nil, nil, nil)
  795.  
  796.   ngx.print('\
  797. \
  798. ');  ngx.print(ui_helper.createHeader(T"WAN services", true, true))   ngx.print('\
  799. \
  800. <div class="modal-body update">\
  801.  <form class="form-horizontal" method="post" action="modals/wanservices-modal.lp">\
  802.    ');  
  803.         ngx.print(ui_helper.createMessages(message_helper.popMessages()))
  804.       ngx.print('\
  805. \
  806.    <fieldset class="advanced hide">\
  807.      <legend>');  ngx.print( T"DMZ" ); ngx.print('</legend>\
  808.      ');  
  809.         -- Switch for toggling DMZ state
  810.         local dmzipattr = {
  811.             autocomplete = hosts_ac
  812.         }
  813.         local DMZ_destinationmac = {
  814.           "rpc.network.firewall.dmz.redirect.dest_mac",
  815.         }
  816.         content_helper.getExactContent(DMZ_destinationmac)
  817.         ngx.print(ui_helper.createSwitch(T"Enabled", "DMZ_enable", qry_data["DMZ_enable"], nil, qry_helpmsg["DMZ_enable"]),
  818.                   ui_helper.createInputText(T"Destination IP", "DMZ_destinationip", qry_data["DMZ_destinationip"], dmzipattr, qry_helpmsg["DMZ_destinationip"]),
  819.                   ui_helper.createLabel(T"Destination MAC", DMZ_destinationmac[1]))
  820.         ngx.print('\
  821.    </fieldset>\
  822. \
  823.    <fieldset>\
  824.      <legend>');  ngx.print( T"IPv4 Port forwarding table" ); ngx.print('</legend>\
  825.      <div class="alert alert-info">');
  826.         ngx.print(T"To disable Port Forward Rules, edit the rule on the right and then switch the button on the left.");
  827.         ngx.print('</div>');             
  828.         ngx.print(ui_helper.createTable(pfw_columns, pfw_data, pfw_options, nil, pfw_helpmsg))
  829.         ngx.print('\
  830.    </fieldset>\
  831. \
  832.    ');  
  833. --NG-93922 Unable to create IPv6 Portforwarding entry in WAN services card, section removed
  834. --    if ipv6Data.lanIpv6Enabled ~= "0" and ipv6Data.pinholeEnabled == "1" then
  835. --      ngx.print('\
  836. --        <fieldset>\
  837. --          <legend>');  ngx.print( T"IPv6 forwarding table" ); ngx.print('</legend>\
  838. --          ');  
  839. --            -- magic
  840. --            -- display/edit all the retrieved ipv6 firewall rule data
  841. --            ngx.print(ui_helper.createTable(pfw_v6_columns, pfw_v6_data, pfw_v6_options, nil, pfw_v6_helpmsg))
  842. --            ngx.print('\
  843. --        </fieldset>\
  844. --    ');  
  845. --    end
  846.     if tech then
  847.       ngx.print('\
  848. \
  849.    <fieldset class="advanced hide">\
  850.      <legend>');  ngx.print( T"DNS rules" ); ngx.print('</legend>\
  851.      ');  
  852.         ngx.print(ui_helper.createTable(dnsrule_columns, dnsrule_data, dnsrule_options, nil, dnsrule_helpmsg))
  853.         ngx.print('\
  854.    </fieldset>')
  855.     end
  856.         ngx.print('\
  857.    <fieldset>\
  858.      <legend>');  ngx.print( T"UPnP" ); ngx.print('</legend>\
  859.      ');  
  860.         local advanced = { group = { class = "advanced hide" }}
  861.         ngx.print(
  862.           ui_helper.createSwitch(T"UPnP IGD Enabled", "upnp_status", qry_data["upnp_status"], advanced, qry_helpmsg["upnp_status"]),
  863.           ui_helper.createSwitch(T"NAT-PMP Enabled", "upnp_natpmp", qry_data["upnp_natpmp"], advanced, qry_helpmsg["upnp_natpmp"]),
  864.           ui_helper.createSwitch(T"Secure Mode Enabled", "upnp_secure_mode", qry_data["upnp_secure_mode"], advanced, qry_helpmsg["upnp_secure_mode"]),
  865.           ui_helper.createTable(upnp_columns, upnp_data, upnp_options, nil, upnp_helpmsg)
  866.         )
  867.         ngx.print('\
  868.    </fieldset>\
  869. \
  870.    <fieldset>\
  871.      <legend>');  ngx.print( T"DynDNS" ); ngx.print('</legend>\
  872.      ');  
  873.          ngx.print(
  874.             ui_helper.createLight(T"Status", nil, ddns_state_map[ddns_status], { light = { class = ddns_light_map[ddns_status] } }),
  875.             ui_helper.createSwitch(T"Enabled", "ddns_enabled", qry_data["ddns_enabled"], nil, qry_helpmsg["ddns_enabled"]),
  876.             ui_helper.createInputSelect(T"Service Name", "ddns_service_name", ddns_supported_services, qry_data["ddns_service_name"], nil, qry_helpmsg["ddns_service_name"]),
  877.             ui_helper.createSwitch(T"HTTPS", "ddns_usehttps", qry_data["ddns_usehttps"], nil, qry_helpmsg["ddns_usehttps"]),
  878.             ui_helper.createLabel(T"", T"Note: HTTPS mode will enable encryption but not certificate-based authentication of DynDNS service", { span = {class = "span7"},}),
  879.             ui_helper.createInputText(T"Domain", "ddns_domain", qry_data["ddns_domain"], nil, qry_helpmsg["ddns_domain"]),
  880.             ui_helper.createInputText(T"User Name", "ddns_username", qry_data["ddns_username"], nil, qry_helpmsg["ddns_username"]),
  881.             ui_helper.createInputPassword(T"Password", "ddns_password", qry_data["ddns_password"], nil, qry_helpmsg["ddns_password"])
  882.           )
  883.         if qry_data["ddns_enabled"] == "1" then
  884.           local basic = {
  885.             span = {
  886.             class = "span4"
  887.             },
  888.           }
  889.           ngx.print(
  890.             ui_helper.createLabel(T"DynDNS Information", ddns_update_info, basic)
  891.           )
  892.         end
  893.         ngx.print('\
  894.    </fieldset>\
  895. \
  896.    ');  if wol then  ngx.print('\
  897.      <fieldset class="advanced hide">\
  898.        <legend>');  ngx.print( T"Wake on LAN over the Internet" ); ngx.print('</legend>\
  899.        ');  
  900.           ngx.print(ui_helper.createSwitch(T"Enabled", "WOL_enabled", qry_data["WOL_enabled"], nil, qry_helpmsg["WOL_enabled"]),
  901.                     ui_helper.createInputText(T"WAN port", "WOL_port", qry_data["WOL_port"], nil, qry_helpmsg["WOL_port"]))
  902.           ngx.print('\
  903.      </fieldset>\
  904.    ');  end  ngx.print('\
  905. \
  906.  </form>\
  907. </div>\
  908. ');  ngx.print( ui_helper.createFooter() ); ngx.print('\
  909. ');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement