pbx-advanced.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. --[[
  2. Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
  3. This file is part of luci-pbx.
  4. luci-pbx is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. luci-pbx is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with luci-pbx. If not, see <http://www.gnu.org/licenses/>.
  14. ]]--
  15. if nixio.fs.access("/etc/init.d/asterisk") then
  16. server = "asterisk"
  17. elseif nixio.fs.access("/etc/init.d/freeswitch") then
  18. server = "freeswitch"
  19. else
  20. server = ""
  21. end
  22. appname = "PBX"
  23. modulename = "pbx-advanced"
  24. defaultbindport = 5060
  25. defaultrtpstart = 19850
  26. defaultrtpend = 19900
  27. -- Returns all the network related settings, including a constructed RTP range
  28. function get_network_info()
  29. externhost = m.uci:get(modulename, "advanced", "externhost")
  30. ipaddr = m.uci:get("network", "lan", "ipaddr")
  31. bindport = m.uci:get(modulename, "advanced", "bindport")
  32. rtpstart = m.uci:get(modulename, "advanced", "rtpstart")
  33. rtpend = m.uci:get(modulename, "advanced", "rtpend")
  34. if bindport == nil then bindport = defaultbindport end
  35. if rtpstart == nil then rtpstart = defaultrtpstart end
  36. if rtpend == nil then rtpend = defaultrtpend end
  37. if rtpstart == nil or rtpend == nil then
  38. rtprange = nil
  39. else
  40. rtprange = rtpstart .. "-" .. rtpend
  41. end
  42. return bindport, rtprange, ipaddr, externhost
  43. end
  44. -- If not present, insert empty rules in the given config & section named PBX-SIP and PBX-RTP
  45. function insert_empty_sip_rtp_rules(config, section)
  46. -- Add rules named PBX-SIP and PBX-RTP if not existing
  47. found_sip_rule = false
  48. found_rtp_rule = false
  49. m.uci:foreach(config, section,
  50. function(s1)
  51. if s1._name == 'PBX-SIP' then
  52. found_sip_rule = true
  53. elseif s1._name == 'PBX-RTP' then
  54. found_rtp_rule = true
  55. end
  56. end)
  57. if found_sip_rule ~= true then
  58. newrule=m.uci:add(config, section)
  59. m.uci:set(config, newrule, '_name', 'PBX-SIP')
  60. end
  61. if found_rtp_rule ~= true then
  62. newrule=m.uci:add(config, section)
  63. m.uci:set(config, newrule, '_name', 'PBX-RTP')
  64. end
  65. end
  66. -- Delete rules in the given config & section named PBX-SIP and PBX-RTP
  67. function delete_sip_rtp_rules(config, section)
  68. -- Remove rules named PBX-SIP and PBX-RTP
  69. commit = false
  70. m.uci:foreach(config, section,
  71. function(s1)
  72. if s1._name == 'PBX-SIP' or s1._name == 'PBX-RTP' then
  73. m.uci:delete(config, s1['.name'])
  74. commit = true
  75. end
  76. end)
  77. -- If something changed, then we commit the config.
  78. if commit == true then m.uci:commit(config) end
  79. end
  80. -- Deletes QoS rules associated with this PBX.
  81. function delete_qos_rules()
  82. delete_sip_rtp_rules ("qos", "classify")
  83. end
  84. function insert_qos_rules()
  85. -- Insert empty PBX-SIP and PBX-RTP rules if not present.
  86. insert_empty_sip_rtp_rules ("qos", "classify")
  87. -- Get the network information
  88. bindport, rtprange, ipaddr, externhost = get_network_info()
  89. -- Iterate through the QoS rules, and if there is no other rule with the same port
  90. -- range at the priority service level, insert this rule.
  91. commit = false
  92. m.uci:foreach("qos", "classify",
  93. function(s1)
  94. if s1._name == 'PBX-SIP' then
  95. if s1.ports ~= bindport or s1.target ~= "Priority" or s1.proto ~= "udp" then
  96. m.uci:set("qos", s1['.name'], "ports", bindport)
  97. m.uci:set("qos", s1['.name'], "proto", "udp")
  98. m.uci:set("qos", s1['.name'], "target", "Priority")
  99. commit = true
  100. end
  101. elseif s1._name == 'PBX-RTP' then
  102. if s1.ports ~= rtprange or s1.target ~= "Priority" or s1.proto ~= "udp" then
  103. m.uci:set("qos", s1['.name'], "ports", rtprange)
  104. m.uci:set("qos", s1['.name'], "proto", "udp")
  105. m.uci:set("qos", s1['.name'], "target", "Priority")
  106. commit = true
  107. end
  108. end
  109. end)
  110. -- If something changed, then we commit the qos config.
  111. if commit == true then m.uci:commit("qos") end
  112. end
  113. -- This function is a (so far) unsuccessful attempt to manipulate the firewall rules from here
  114. -- Need to do more testing and eventually move to this mode.
  115. function maintain_firewall_rules()
  116. -- Get the network information
  117. bindport, rtprange, ipaddr, externhost = get_network_info()
  118. commit = false
  119. -- Only if externhost is set, do we control firewall rules.
  120. if externhost ~= nil and bindport ~= nil and rtprange ~= nil then
  121. -- Insert empty PBX-SIP and PBX-RTP rules if not present.
  122. insert_empty_sip_rtp_rules ("firewall", "rule")
  123. -- Iterate through the firewall rules, and if the dest_port and dest_ip setting of the\
  124. -- SIP and RTP rule do not match what we want configured, set all the entries in the rule\
  125. -- appropriately.
  126. m.uci:foreach("firewall", "rule",
  127. function(s1)
  128. if s1._name == 'PBX-SIP' then
  129. if s1.dest_port ~= bindport then
  130. m.uci:set("firewall", s1['.name'], "dest_port", bindport)
  131. m.uci:set("firewall", s1['.name'], "src", "wan")
  132. m.uci:set("firewall", s1['.name'], "proto", "udp")
  133. m.uci:set("firewall", s1['.name'], "target", "ACCEPT")
  134. commit = true
  135. end
  136. elseif s1._name == 'PBX-RTP' then
  137. if s1.dest_port ~= rtprange then
  138. m.uci:set("firewall", s1['.name'], "dest_port", rtprange)
  139. m.uci:set("firewall", s1['.name'], "src", "wan")
  140. m.uci:set("firewall", s1['.name'], "proto", "udp")
  141. m.uci:set("firewall", s1['.name'], "target", "ACCEPT")
  142. commit = true
  143. end
  144. end
  145. end)
  146. else
  147. -- We delete the firewall rules if one or more of the necessary parameters are not set.
  148. sip_rule_name=nil
  149. rtp_rule_name=nil
  150. -- First discover the configuration names of the rules.
  151. m.uci:foreach("firewall", "rule",
  152. function(s1)
  153. if s1._name == 'PBX-SIP' then
  154. sip_rule_name = s1['.name']
  155. elseif s1._name == 'PBX-RTP' then
  156. rtp_rule_name = s1['.name']
  157. end
  158. end)
  159. -- Then, using the names, actually delete the rules.
  160. if sip_rule_name ~= nil then
  161. m.uci:delete("firewall", sip_rule_name)
  162. commit = true
  163. end
  164. if rtp_rule_name ~= nil then
  165. m.uci:delete("firewall", rtp_rule_name)
  166. commit = true
  167. end
  168. end
  169. -- If something changed, then we commit the firewall config.
  170. if commit == true then m.uci:commit("firewall") end
  171. end
  172. m = Map (modulename, translate("Advanced Settings"),
  173. translate("This section contains settings that do not need to be changed under \
  174. normal circumstances. In addition, here you can configure your system \
  175. for use with remote SIP devices, and resolve call quality issues by enabling \
  176. the insertion of QoS rules."))
  177. -- Recreate the voip server config, and restart necessary services after changes are commited
  178. -- to the advanced configuration. The firewall must restart because of "Remote Usage".
  179. function m.on_after_commit(self)
  180. -- Make sure firewall rules are in place
  181. maintain_firewall_rules()
  182. -- If insertion of QoS rules is enabled
  183. if m.uci:get(modulename, "advanced", "qos_enabled") == "yes" then
  184. insert_qos_rules()
  185. else
  186. delete_qos_rules()
  187. end
  188. luci.sys.call("/etc/init.d/pbx-" .. server .. " restart 1\>/dev/null 2\>/dev/null")
  189. luci.sys.call("/etc/init.d/" .. server .. " restart 1\>/dev/null 2\>/dev/null")
  190. luci.sys.call("/etc/init.d/firewall restart 1\>/dev/null 2\>/dev/null")
  191. end
  192. -----------------------------------------------------------------------------
  193. s = m:section(NamedSection, "advanced", "settings", translate("Advanced Settings"))
  194. s.anonymous = true
  195. s:tab("general", translate("General Settings"))
  196. s:tab("remote_usage", translate("Remote Usage"),
  197. translatef("You can use your SIP devices/softphones with this system from a remote location \
  198. as well, as long as your Internet Service Provider gives you a public IP. \
  199. You will be able to call other local users for free (e.g. other Analog Telephone Adapters (ATAs)) \
  200. and use your VoIP providers to make calls as if you were local to the PBX. \
  201. After configuring this tab, go back to where users are configured and see the new \
  202. Server and Port setting you need to configure the remote SIP devices with. Please note that if this \
  203. PBX is not running on your router/gateway, you will need to configure port forwarding (NAT) on your \
  204. router/gateway. Please forward the ports below (SIP port and RTP range) to the IP address of the \
  205. device running this PBX."))
  206. s:tab("qos", translate("QoS Settings"),
  207. translate("If you experience jittery or high latency audio during heavy downloads, you may want \
  208. to enable QoS. QoS prioritizes traffic to and from your network for specified ports and IP \
  209. addresses, resulting in better latency and throughput for sound in our case. If enabled below, \
  210. a QoS rule for this service will be configured by the PBX automatically, but you must visit the \
  211. QoS configuration page (Network->QoS) to configure other critical QoS settings like Download \
  212. and Upload speed."))
  213. ringtime = s:taboption("general", Value, "ringtime", translate("Number of Seconds to Ring"),
  214. translate("Set the number of seconds to ring users upon incoming calls before hanging up \
  215. or going to voicemail, if the voicemail is installed and enabled."))
  216. ringtime.datatype = "port"
  217. ringtime.default = 30
  218. ua = s:taboption("general", Value, "useragent", translate("User Agent String"),
  219. translate("This is the name that the VoIP server will use to identify itself when \
  220. registering to VoIP (SIP) providers. Some providers require this to a specific \
  221. string matching a hardware SIP device."))
  222. ua.default = appname
  223. h = s:taboption("remote_usage", Value, "externhost", translate("Domain/IP Address/Dynamic Domain"),
  224. translate("You can enter your domain name, external IP address, or dynamic domain name here. \
  225. The best thing to input is a static IP address. If your IP address is dynamic and it changes, \
  226. your configuration will become invalid. Hence, it's recommended to set up Dynamic DNS in this case. \
  227. and enter your Dynamic DNS hostname here. You can configure Dynamic DNS with the luci-app-ddns package."))
  228. h.datatype = "host"
  229. p = s:taboption("remote_usage", Value, "bindport", translate("External SIP Port"),
  230. translate("Pick a random port number between 6500 and 9500 for the service to listen on. \
  231. Do not pick the standard 5060, because it is often subject to brute-force attacks. \
  232. When finished, (1) click \"Save and Apply\", and (2) look in the \
  233. \"SIP Device/Softphone Accounts\" section for updated Server and Port settings \
  234. for your SIP Devices/Softphones."))
  235. p.datatype = "port"
  236. p = s:taboption("remote_usage", Value, "rtpstart", translate("RTP Port Range Start"),
  237. translate("RTP traffic carries actual voice packets. This is the start of the port range \
  238. that will be used for setting up RTP communication. It's usually OK to leave this \
  239. at the default value."))
  240. p.datatype = "port"
  241. p.default = defaultrtpstart
  242. p = s:taboption("remote_usage", Value, "rtpend", translate("RTP Port Range End"))
  243. p.datatype = "port"
  244. p.default = defaultrtpend
  245. p = s:taboption("qos", ListValue, "qos_enabled", translate("Insert QoS Rules"))
  246. p:value("yes", translate("Yes"))
  247. p:value("no", translate("No"))
  248. p.default = "yes"
  249. return m