portconfig.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 = require("simple-uci").cursor()
  11. local sysconfig = require 'gluon.sysconfig'
  12. local util = require 'gluon.util'
  13. local wan = uci:get_all("network", "wan")
  14. local wan6 = uci:get_all("network", "wan6")
  15. local dns = uci:get_first("gluon-wan-dnsmasq", "static")
  16. local f = SimpleForm("portconfig", translate("WAN connection"))
  17. local s
  18. local o
  19. s = f:section(SimpleSection, nil, nil)
  20. o = s:option(ListValue, "ipv4", translate("IPv4"))
  21. o:value("dhcp", translate("Automatic (DHCP)"))
  22. o:value("static", translate("Static"))
  23. o:value("none", translate("Disabled"))
  24. o.default = wan.proto
  25. o = s:option(Value, "ipv4_addr", translate("IP address"))
  26. o:depends("ipv4", "static")
  27. o.value = wan.ipaddr
  28. o.datatype = "ip4addr"
  29. o.rmempty = false
  30. o = s:option(Value, "ipv4_netmask", translate("Netmask"))
  31. o:depends("ipv4", "static")
  32. o.value = wan.netmask or "255.255.255.0"
  33. o.datatype = "ip4addr"
  34. o.rmempty = false
  35. o = s:option(Value, "ipv4_gateway", translate("Gateway"))
  36. o:depends("ipv4", "static")
  37. o.value = wan.gateway
  38. o.datatype = "ip4addr"
  39. o.rmempty = false
  40. s = f:section(SimpleSection, nil, nil)
  41. o = s:option(ListValue, "ipv6", translate("IPv6"))
  42. o:value("dhcpv6", translate("Automatic (RA/DHCPv6)"))
  43. o:value("static", translate("Static"))
  44. o:value("none", translate("Disabled"))
  45. o.default = wan6.proto
  46. o = s:option(Value, "ipv6_addr", translate("IP address"))
  47. o:depends("ipv6", "static")
  48. o.value = wan6.ip6addr
  49. o.datatype = "ip6addr"
  50. o.rmempty = false
  51. o = s:option(Value, "ipv6_gateway", translate("Gateway"))
  52. o:depends("ipv6", "static")
  53. o.value = wan6.ip6gw
  54. o.datatype = "ip6addr"
  55. o.rmempty = false
  56. if dns then
  57. s = f:section(SimpleSection, nil, nil)
  58. o = s:option(DynamicList, "dns", translate("Static DNS servers"))
  59. o:write(nil, uci:get("gluon-wan-dnsmasq", dns, "server"))
  60. o.datatype = "ipaddr"
  61. end
  62. s = f:section(SimpleSection, nil, nil)
  63. o = s:option(Flag, "mesh_wan", translate("Enable meshing on the WAN interface"))
  64. o.default = uci:get_bool("network", "mesh_wan", "auto") and o.enabled or o.disabled
  65. o.rmempty = false
  66. if sysconfig.lan_ifname then
  67. o = s:option(Flag, "mesh_lan", translate("Enable meshing on the LAN interface"))
  68. o.default = uci:get_bool("network", "mesh_lan", "auto") and o.enabled or o.disabled
  69. o.rmempty = false
  70. end
  71. if uci:get('system', 'gpio_switch_poe_passthrough') then
  72. s = f:section(SimpleSection, nil, nil)
  73. o = s:option(Flag, "poe_passthrough", translate("Enable PoE passthrough"))
  74. o.default = uci:get_bool("system", "gpio_switch_poe_passthrough", "value") and o.enabled or o.disabled
  75. o.rmempty = false
  76. end
  77. function f.handle(self, state, data)
  78. if state == FORM_VALID then
  79. uci:set("network", "wan", "proto", data.ipv4)
  80. if data.ipv4 == "static" then
  81. uci:set("network", "wan", "ipaddr", data.ipv4_addr:trim())
  82. uci:set("network", "wan", "netmask", data.ipv4_netmask:trim())
  83. uci:set("network", "wan", "gateway", data.ipv4_gateway:trim())
  84. else
  85. uci:delete("network", "wan", "ipaddr")
  86. uci:delete("network", "wan", "netmask")
  87. uci:delete("network", "wan", "gateway")
  88. end
  89. uci:set("network", "wan6", "proto", data.ipv6)
  90. if data.ipv6 == "static" then
  91. uci:set("network", "wan6", "ip6addr", data.ipv6_addr:trim())
  92. uci:set("network", "wan6", "ip6gw", data.ipv6_gateway:trim())
  93. else
  94. uci:delete("network", "wan6", "ip6addr")
  95. uci:delete("network", "wan6", "ip6gw")
  96. end
  97. uci:set("network", "mesh_wan", "auto", data.mesh_wan)
  98. if sysconfig.lan_ifname then
  99. uci:set("network", "mesh_lan", "auto", data.mesh_lan)
  100. local interfaces = uci:get_list("network", "client", "ifname")
  101. for lanif in sysconfig.lan_ifname:gmatch('%S+') do
  102. if data.mesh_lan == '1' then
  103. util.remove_from_set(interfaces, lanif)
  104. else
  105. util.add_to_set(interfaces, lanif)
  106. end
  107. end
  108. uci:set_list("network", "client", "ifname", interfaces)
  109. end
  110. uci:save("network")
  111. uci:commit("network")
  112. if uci:get('system', 'gpio_switch_poe_passthrough') then
  113. uci:set('system', 'gpio_switch_poe_passthrough', 'value', data.poe_passthrough)
  114. uci:save('system')
  115. uci:commit('system')
  116. end
  117. if dns then
  118. if #data.dns > 0 then
  119. uci:set("gluon-wan-dnsmasq", dns, "server", data.dns)
  120. else
  121. uci:delete("gluon-wan-dnsmasq", dns, "server")
  122. end
  123. uci:save("gluon-wan-dnsmasq")
  124. uci:commit("gluon-wan-dnsmasq")
  125. end
  126. end
  127. return true
  128. end
  129. return f