2
0

zrtp_proxy_media.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- Copyright (c) 2011-2012, Travis Cross.
  2. --
  3. -- The contents of this file are subject to the Mozilla Public License
  4. -- Version 1.1 (the "License"); you may not use this file except in
  5. -- compliance with the License. You may obtain a copy of the License
  6. -- at http://www.mozilla.org/MPL/
  7. --
  8. -- Software distributed under the License is distributed on an "AS IS"
  9. -- basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  10. -- the License for the specific language governing rights and
  11. -- limitations under the License.
  12. --
  13. -- zrtp_proxy_media.lua
  14. --
  15. -- The logic in this script enables ZRTP sessions to negotiate
  16. -- end-to-end security associations, which is desirable whether or not
  17. -- the switch natively supports ZRTP itself.
  18. --
  19. -- To enable this logic, call the script from the top of your dialplan
  20. -- as so:
  21. --
  22. -- <extension name="global" continue="true">
  23. -- <condition break="never">
  24. -- <action application="lua" data="lua/zrtp_proxy_media.lua"/>
  25. -- </condition>
  26. -- </extension>
  27. --
  28. -- If any particular call flow should never have proxy_media enabled,
  29. -- such as for connecting to voicemail systems or conferences, make
  30. -- sure this is called before the bridge:
  31. --
  32. -- <action application="lua" data="lua/zrtp_proxy_media.lua disable"/>
  33. api=freeswitch.API()
  34. function sappend(s1,s2) if s1 and #s1>0 then return s1..s2 else return s2 end end
  35. function log(level,msg) return freeswitch.consoleLog(level,msg.."\n") end
  36. function ready() return session:ready() end
  37. function getvar(var) return session:getVariable(var) end
  38. function getvarp(var) return getvar(var)=="true" end
  39. function setvar_a(k,v) return session:setVariable(k,v) end
  40. function append_var(k,v) return setvar_a(k,sappend(getvar(k),v)) end
  41. function export(k) return append_var("export_vars",","..k) end
  42. function setvar_ab(k,v) if v then setvar_a(k,v) end return export(k) end
  43. function setvar_b(k,v) return setvar_ab("nolocal:"..k,v) end
  44. function enable_zd(msg)
  45. log("info",msg)
  46. setvar_ab("zrtp_set","true")
  47. setvar_ab("proxy_media","true")
  48. setvar_ab("zrtp_secure_media","false")
  49. end
  50. function disable_zd(msg)
  51. log("info",msg)
  52. setvar_ab("zrtp_set","true")
  53. setvar_ab("proxy_media","false")
  54. setvar_ab("zrtp_secure_media","true")
  55. end
  56. function xfer(x)
  57. return session:transfer(x,getvar("dialplan"),getvar("context"))
  58. end
  59. function main()
  60. if ready() then
  61. session:setAutoHangup(false)
  62. local dst=getvar("destination_number")
  63. if argv[1]=="disable" then
  64. return disable_zd("zrtp-direct disabled on this call flow")
  65. elseif getvarp("zrtp_set") then
  66. return log("notice","zrtp already decided; doing nothing") end
  67. local x=dst:match("^%*%*%*(.*)$")
  68. if x then
  69. enable_zd("going zrtp-direct based on star code")
  70. return xfer(x) end
  71. local x=dst:match("^%*%*(.*)$")
  72. if x then
  73. disable_zd("going zrtp-indirect based on star code")
  74. return xfer(x) end
  75. if getvar("switch_r_sdp"):match("a=zrtp%-hash:") then
  76. return enable_zd("going zrtp-direct based on a=zrtp-hash") end
  77. return disable_zd("not going zrtp-direct")
  78. end
  79. end
  80. main()