openvpn-basic.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. local fs = require("nixio.fs")
  4. local basicParams = {
  5. --
  6. -- Widget, Name, Default(s), Description
  7. --
  8. { ListValue,
  9. "verb",
  10. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
  11. translate("Set output verbosity") },
  12. { Value,
  13. "nice",
  14. 0,
  15. translate("Change process priority") },
  16. { Value,
  17. "port",
  18. 1194,
  19. translate("TCP/UDP port # for both local and remote") },
  20. { ListValue,
  21. "dev_type",
  22. { "tun", "tap" },
  23. translate("Type of used device") },
  24. { Value,
  25. "ifconfig",
  26. "10.200.200.3 10.200.200.1",
  27. translate("Set tun/tap adapter parameters") },
  28. { Value,
  29. "server",
  30. "10.200.200.0 255.255.255.0",
  31. translate("Configure server mode") },
  32. { Value,
  33. "server_bridge",
  34. "192.168.1.1 255.255.255.0 192.168.1.128 192.168.1.254",
  35. translate("Configure server bridge") },
  36. { Flag,
  37. "nobind",
  38. 0,
  39. translate("Do not bind to local address and port") },
  40. { ListValue,
  41. "comp_lzo",
  42. {"yes","no","adaptive"},
  43. translate("Security recommendation: It is recommended to not enable compression and set this parameter to `no`")},
  44. { Value,
  45. "keepalive",
  46. "10 60",
  47. translate("Helper directive to simplify the expression of --ping and --ping-restart in server mode configurations") },
  48. { Flag,
  49. "client",
  50. 0,
  51. translate("Configure client mode") },
  52. { Flag,
  53. "client_to_client",
  54. 0,
  55. translate("Allow client-to-client traffic") },
  56. { DynamicList,
  57. "remote",
  58. "vpnserver.example.org",
  59. translate("Remote host name or IP address") },
  60. { FileUpload,
  61. "secret",
  62. "/etc/openvpn/secret.key",
  63. translate("Enable Static Key encryption mode (non-TLS)") },
  64. { ListValue,
  65. "key_direction",
  66. { 0, 1 },
  67. translate("The key direction for 'tls-auth' and 'secret' options") },
  68. { FileUpload,
  69. "pkcs12",
  70. "/etc/easy-rsa/keys/some-client.pk12",
  71. translate("PKCS#12 file containing keys") },
  72. { FileUpload,
  73. "ca",
  74. "/etc/easy-rsa/keys/ca.crt",
  75. translate("Certificate authority") },
  76. { FileUpload,
  77. "dh",
  78. "/etc/easy-rsa/keys/dh1024.pem",
  79. translate("Diffie-Hellman parameters") },
  80. { FileUpload,
  81. "cert",
  82. "/etc/easy-rsa/keys/some-client.crt",
  83. translate("Local certificate") },
  84. { FileUpload,
  85. "key",
  86. "/etc/easy-rsa/keys/some-client.key",
  87. translate("Local private key") },
  88. }
  89. local has_ipv6 = fs.access("/proc/net/ipv6_route")
  90. if has_ipv6 then
  91. table.insert( basicParams, { ListValue,
  92. "proto",
  93. { "udp", "tcp-client", "tcp-server", "udp6", "tcp6-client", "tcp6-server" },
  94. translate("Use protocol")
  95. })
  96. else
  97. table.insert( basicParams, { ListValue,
  98. "proto",
  99. { "udp", "tcp-client", "tcp-server" },
  100. translate("Use protocol")
  101. })
  102. end
  103. local m = Map("openvpn")
  104. m.redirect = luci.dispatcher.build_url("admin", "vpn", "openvpn")
  105. m.apply_on_parse = true
  106. local p = m:section( SimpleSection )
  107. p.template = "openvpn/pageswitch"
  108. p.mode = "basic"
  109. p.instance = arg[1]
  110. local s = m:section( NamedSection, arg[1], "openvpn" )
  111. for _, option in ipairs(basicParams) do
  112. local o = s:option(
  113. option[1], option[2],
  114. option[2], option[4]
  115. )
  116. o.optional = true
  117. if option[1] == DummyValue then
  118. o.value = option[3]
  119. elseif option[1] == FileUpload then
  120. o.initial_directory = "/etc/openvpn"
  121. function o.cfgvalue(self, section)
  122. local cfg_val = AbstractValue.cfgvalue(self, section)
  123. if cfg_val then
  124. return cfg_val
  125. end
  126. end
  127. function o.formvalue(self, section)
  128. local sel_val = AbstractValue.formvalue(self, section)
  129. local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
  130. if sel_val and sel_val ~= "" then
  131. return sel_val
  132. end
  133. if txt_val and txt_val ~= "" then
  134. return txt_val
  135. end
  136. end
  137. function o.remove(self, section)
  138. local cfg_val = AbstractValue.cfgvalue(self, section)
  139. local txt_val = luci.http.formvalue("cbid."..self.map.config.."."..section.."."..self.option..".textbox")
  140. if cfg_val and fs.access(cfg_val) and txt_val == "" then
  141. fs.unlink(cfg_val)
  142. end
  143. return AbstractValue.remove(self, section)
  144. end
  145. elseif option[1] == Flag then
  146. o.default = nil
  147. else
  148. if option[1] == DynamicList then
  149. function o.cfgvalue(...)
  150. local val = AbstractValue.cfgvalue(...)
  151. return ( val and type(val) ~= "table" ) and { val } or val
  152. end
  153. end
  154. if type(option[3]) == "table" then
  155. if o.optional then o:value("", "-- remove --") end
  156. for _, v in ipairs(option[3]) do
  157. v = tostring(v)
  158. o:value(v)
  159. end
  160. o.default = tostring(option[3][1])
  161. else
  162. o.default = tostring(option[3])
  163. end
  164. end
  165. for i=5,#option do
  166. if type(option[i]) == "table" then
  167. o:depends(option[i])
  168. end
  169. end
  170. end
  171. return m