portconfig.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2014 Nils Schneider <nils@nilsschneider.net>
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. $Id$
  9. ]]--
  10. local uci = luci.model.uci.cursor()
  11. local lutil = require 'luci.util'
  12. local sysconfig = require 'gluon.sysconfig'
  13. local util = require 'gluon.util'
  14. local wan = uci:get_all("network", "wan")
  15. local wan6 = uci:get_all("network", "wan6")
  16. local dns = uci:get_first("gluon-wan-dnsmasq", "static")
  17. local f = SimpleForm("portconfig", translate("WAN connection"))
  18. local s
  19. local o
  20. s = f:section(SimpleSection, nil, nil)
  21. o = s:option(ListValue, "ipv4", translate("IPv4"))
  22. o:value("dhcp", translate("Automatic (DHCP)"))
  23. o:value("static", translate("Static"))
  24. o:value("none", translate("Disabled"))
  25. o.default = wan.proto
  26. o = s:option(Value, "ipv4_addr", translate("IP address"))
  27. o:depends("ipv4", "static")
  28. o.value = wan.ipaddr
  29. o.datatype = "ip4addr"
  30. o.rmempty = false
  31. o = s:option(Value, "ipv4_netmask", translate("Netmask"))
  32. o:depends("ipv4", "static")
  33. o.value = wan.netmask or "255.255.255.0"
  34. o.datatype = "ip4addr"
  35. o.rmempty = false
  36. o = s:option(Value, "ipv4_gateway", translate("Gateway"))
  37. o:depends("ipv4", "static")
  38. o.value = wan.gateway
  39. o.datatype = "ip4addr"
  40. o.rmempty = false
  41. s = f:section(SimpleSection, nil, nil)
  42. o = s:option(ListValue, "ipv6", translate("IPv6"))
  43. o:value("dhcpv6", translate("Automatic (RA/DHCPv6)"))
  44. o:value("static", translate("Static"))
  45. o:value("none", translate("Disabled"))
  46. o.default = wan6.proto
  47. o = s:option(Value, "ipv6_addr", translate("IP address"))
  48. o:depends("ipv6", "static")
  49. o.value = wan6.ip6addr
  50. o.datatype = "ip6addr"
  51. o.rmempty = false
  52. o = s:option(Value, "ipv6_gateway", translate("Gateway"))
  53. o:depends("ipv6", "static")
  54. o.value = wan6.ip6gw
  55. o.datatype = "ip6addr"
  56. o.rmempty = false
  57. if dns then
  58. s = f:section(SimpleSection, nil, nil)
  59. o = s:option(DynamicList, "dns", translate("Static DNS servers"))
  60. o:write(nil, uci:get("gluon-wan-dnsmasq", dns, "server"))
  61. o.datatype = "ipaddr"
  62. end
  63. s = f:section(SimpleSection, nil, nil)
  64. o = s:option(Flag, "mesh_wan", translate("Enable meshing on the WAN interface"))
  65. o.default = uci:get_bool("network", "mesh_wan", "auto") and o.enabled or o.disabled
  66. o.rmempty = false
  67. if sysconfig.lan_ifname then
  68. o = s:option(Flag, "mesh_lan", translate("Enable meshing on the LAN interface"))
  69. o.default = uci:get_bool("network", "mesh_lan", "auto") and o.enabled or o.disabled
  70. o.rmempty = false
  71. end
  72. if uci:get('system', 'gpio_switch_poe_passthrough') then
  73. s = f:section(SimpleSection, nil, nil)
  74. o = s:option(Flag, "poe_passthrough", translate("Enable PoE passthrough"))
  75. o.default = uci:get_bool("system", "gpio_switch_poe_passthrough", "value") and o.enabled or o.disabled
  76. o.rmempty = false
  77. end
  78. function f.handle(self, state, data)
  79. if state == FORM_VALID then
  80. uci:set("network", "wan", "proto", data.ipv4)
  81. if data.ipv4 == "static" then
  82. uci:set("network", "wan", "ipaddr", data.ipv4_addr:trim())
  83. uci:set("network", "wan", "netmask", data.ipv4_netmask:trim())
  84. uci:set("network", "wan", "gateway", data.ipv4_gateway:trim())
  85. else
  86. uci:delete("network", "wan", "ipaddr")
  87. uci:delete("network", "wan", "netmask")
  88. uci:delete("network", "wan", "gateway")
  89. end
  90. uci:set("network", "wan6", "proto", data.ipv6)
  91. if data.ipv6 == "static" then
  92. uci:set("network", "wan6", "ip6addr", data.ipv6_addr:trim())
  93. uci:set("network", "wan6", "ip6gw", data.ipv6_gateway:trim())
  94. else
  95. uci:delete("network", "wan6", "ip6addr")
  96. uci:delete("network", "wan6", "ip6gw")
  97. end
  98. uci:set("network", "mesh_wan", "auto", data.mesh_wan)
  99. if sysconfig.lan_ifname then
  100. uci:set("network", "mesh_lan", "auto", data.mesh_lan)
  101. local interfaces = uci:get_list("network", "client", "ifname")
  102. for _, lanif in ipairs(lutil.split(sysconfig.lan_ifname, ' ')) do
  103. if data.mesh_lan == '1' then
  104. util.remove_from_set(interfaces, lanif)
  105. else
  106. util.add_to_set(interfaces, lanif)
  107. end
  108. end
  109. uci:set_list("network", "client", "ifname", interfaces)
  110. end
  111. uci:save("network")
  112. uci:commit("network")
  113. if uci:get('system', 'gpio_switch_poe_passthrough') then
  114. uci:set('system', 'gpio_switch_poe_passthrough', 'value', data.poe_passthrough)
  115. uci:save('system')
  116. uci:commit('system')
  117. end
  118. if dns then
  119. if #data.dns > 0 then
  120. uci:set("gluon-wan-dnsmasq", dns, "server", data.dns)
  121. else
  122. uci:delete("gluon-wan-dnsmasq", dns, "server")
  123. end
  124. uci:save("gluon-wan-dnsmasq")
  125. uci:commit("gluon-wan-dnsmasq")
  126. end
  127. end
  128. return true
  129. end
  130. return f