forwards.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local ds = require "luci.dispatcher"
  5. local ft = require "luci.tools.firewall"
  6. m = Map("firewall", translate("Firewall - Port Forwards"),
  7. translate("Port forwarding allows remote computers on the Internet to \
  8. connect to a specific computer or service within the \
  9. private LAN."))
  10. --
  11. -- Port Forwards
  12. --
  13. s = m:section(TypedSection, "redirect", translate("Port Forwards"))
  14. s.template = "cbi/tblsection"
  15. s.addremove = true
  16. s.anonymous = true
  17. s.sortable = true
  18. s.extedit = ds.build_url("admin/network/firewall/forwards/%s")
  19. s.template_addremove = "firewall/cbi_addforward"
  20. function s.create(self, section)
  21. local n = m:formvalue("_newfwd.name")
  22. local p = m:formvalue("_newfwd.proto")
  23. local E = m:formvalue("_newfwd.extzone")
  24. local e = m:formvalue("_newfwd.extport")
  25. local I = m:formvalue("_newfwd.intzone")
  26. local a = m:formvalue("_newfwd.intaddr")
  27. local i = m:formvalue("_newfwd.intport")
  28. if p == "other" or (p and a) then
  29. created = TypedSection.create(self, section)
  30. self.map:set(created, "target", "DNAT")
  31. self.map:set(created, "src", E or "wan")
  32. self.map:set(created, "dest", I or "lan")
  33. self.map:set(created, "proto", (p ~= "other") and p or "all")
  34. self.map:set(created, "src_dport", e)
  35. self.map:set(created, "dest_ip", a)
  36. self.map:set(created, "dest_port", i)
  37. self.map:set(created, "name", n)
  38. end
  39. if p ~= "other" then
  40. created = nil
  41. end
  42. end
  43. function s.parse(self, ...)
  44. TypedSection.parse(self, ...)
  45. if created then
  46. m.uci:save("firewall")
  47. luci.http.redirect(ds.build_url(
  48. "admin/network/firewall/redirect", created
  49. ))
  50. end
  51. end
  52. function s.filter(self, sid)
  53. return (self.map:get(sid, "target") ~= "SNAT")
  54. end
  55. ft.opt_name(s, DummyValue, translate("Name"))
  56. local function forward_proto_txt(self, s)
  57. return "%s-%s" %{
  58. translate("IPv4"),
  59. ft.fmt_proto(self.map:get(s, "proto"),
  60. self.map:get(s, "icmp_type")) or "TCP+UDP"
  61. }
  62. end
  63. local function forward_src_txt(self, s)
  64. local z = ft.fmt_zone(self.map:get(s, "src"), translate("any zone"))
  65. local a = ft.fmt_ip(self.map:get(s, "src_ip"), translate("any host"))
  66. local p = ft.fmt_port(self.map:get(s, "src_port"))
  67. local m = ft.fmt_mac(self.map:get(s, "src_mac"))
  68. if p and m then
  69. return translatef("From %s in %s with source %s and %s", a, z, p, m)
  70. elseif p or m then
  71. return translatef("From %s in %s with source %s", a, z, p or m)
  72. else
  73. return translatef("From %s in %s", a, z)
  74. end
  75. end
  76. local function forward_via_txt(self, s)
  77. local a = ft.fmt_ip(self.map:get(s, "src_dip"), translate("any router IP"))
  78. local p = ft.fmt_port(self.map:get(s, "src_dport"))
  79. if p then
  80. return translatef("Via %s at %s", a, p)
  81. else
  82. return translatef("Via %s", a)
  83. end
  84. end
  85. match = s:option(DummyValue, "match", translate("Match"))
  86. match.rawhtml = true
  87. match.width = "50%"
  88. function match.cfgvalue(self, s)
  89. return "<small>%s<br />%s<br />%s</small>" % {
  90. forward_proto_txt(self, s),
  91. forward_src_txt(self, s),
  92. forward_via_txt(self, s)
  93. }
  94. end
  95. dest = s:option(DummyValue, "dest", translate("Forward to"))
  96. dest.rawhtml = true
  97. dest.width = "40%"
  98. function dest.cfgvalue(self, s)
  99. local z = ft.fmt_zone(self.map:get(s, "dest"), translate("any zone"))
  100. local a = ft.fmt_ip(self.map:get(s, "dest_ip"), translate("any host"))
  101. local p = ft.fmt_port(self.map:get(s, "dest_port")) or
  102. ft.fmt_port(self.map:get(s, "src_dport"))
  103. if p then
  104. return translatef("%s, %s in %s", a, p, z)
  105. else
  106. return translatef("%s in %s", a, z)
  107. end
  108. end
  109. ft.opt_enabled(s, Flag, translate("Enable")).width = "1%"
  110. return m