mod_xml_curl实现动态管理

一、mod_xml_curl的安装

进入项目源码编辑文件modules.conf,取消掉xml_int/mod_xml_curl的注释

make mod_xml_curl-install

如果你要默认启动模块,可以尝试在/usr/local/freeswitch/conf/autoload_configs/modules.conf.xml设置默认启动,也可以在freeswitch服务器load mod_xml_curl

二、mod_xml_curl的使用

该模块分为4个不同的部分,它们是;

  • configuration - 配置项,理论上可以接管所有的配置
  • directory - 用户目录(用户身份验证)
  • dialplan - 拨号方案项目(呼叫路由)
  • phrases - SAY api的短语(语音短语管理

三、mod_xml_curl管理用户注册

<configuration name="xml_curl.conf" description="cURL XML Gateway">
  <bindings>
      <!-- 配置用户目录 -->
      <binding name="directory">
        <param name="gateway-url" value="http://183.66.37.178:7001/api/sipAccount/login?code=$${code}" bindings="directory"/>
      </binding>
  </bindings>
</configuration>

java代码:

if("id".equals(req.getParameter("key"))
                && StringUtils.isNotEmpty(req.getParameter("user"))
                && StringUtils.isNotEmpty(req.getParameter("FreeSWITCH-IPv4"))
                ){
            String code = req.getParameter("code");
            if(org.apache.commons.lang.StringUtils.isEmpty(code)){
                return "code必传";
            }
            return feignClientCallCenter.sipAccountLogin(req.getParameter("user"),req.getParameter("code"),req.getParameter("FreeSWITCH-IPv4"));

        }
@PostMapping("/login")
    public @ResponseBody
    Object ApiReqResult(@RequestParam("accountCode") String accountCode,@RequestParam("code") String code,@RequestParam("IPv4") String IPv4) {
        SipAccountEntity sipAccountEntity = sipAccountService.findByAccountCode(accountCode);
        if(sipAccountEntity == null){
            return "not found";
        }

        TbFreeswitchEntity tbFreeswitchEntity = tbFreeswitchService.findByCode(code);
        if(tbFreeswitchEntity == null){
            return "freeswitch not found";
        }

        int count = tbSipAccountRelationFreeswitchService.countBySipAccountIdAndFreeswitchId(sipAccountEntity.getId(),tbFreeswitchEntity.getId());
        if(count <= 0){
            return "sip freeswitch mismatch";
        }

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<document type=\"freeswitch/xml\">\n" +
                "  <section name=\"directory\">\n" +
                "    <domain name=\""+IPv4+"\">\n" +
                "      <user id=\""+sipAccountEntity.getAccountCode()+"\">\n" +
                "        <params>\n" +
                "          <param name=\"password\" value=\""+sipAccountEntity.getPassword()+"\"/>\n" +
                "          <param name=\"dial-string\" value=\"{sip_invite_domain=${dialed_domain},presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}\"/>\n" +
                "        </params>\n" +
                "        <variables>\n" +
                "          <variable name=\"user_context\" value=\""+sipAccountEntity.getUserContext()+"\"/>\n" +
                "          <variable name=\"toll_allow\" value=\"domestic,international,local\"/>\n" +
                "          <variable name=\"accountcode\" value=\""+sipAccountEntity.getAccountCode()+"\"/>\n" +
                "          <variable name=\"effective_caller_id_name\" value=\"Extension  "+sipAccountEntity.getAccountCode()+" \"/>\n" +
                "          <variable name=\"effective_caller_id_number\" value=\""+sipAccountEntity.getAccountCode()+"\"/>\n" +
                "          <variable name=\"outbound_caller_id_name\" value=\"\"/>\n" +
                "          <variable name=\"outbound_caller_id_number\" value=\"\"/>\n" +
                "          <variable name=\"callgroup\" value=\"techsupport\"/>\n" +
                "        </variables>\n" +
                "      </user>\n" +
                "    </domain>\n" +
                "  </section>\n" +
                "</document>";
        return xml;
    }

四、mod_xml_curl接管sip网关

<configuration name="xml_curl.conf" description="cURL XML Gateway">
  <bindings>
      <!-- 可接管所有参数,接口端可根据参数判断 -->
      <binding name="configuration">
          <param name="gateway-url" value="http://183.66.37.178:7001/api/sipGateway/get?code=$${code}" bindings="configuration"/>
      </binding>
  </bindings>
</configuration>

java代码

@RequestMapping(value = "get")
    public @ResponseBody
    Object get(HttpServletRequest req, HttpServletResponse res) {
        String code = req.getParameter("code");
        if(StringUtils.isEmpty(code)){
            return "code必传";
        }
        if("sofia.conf".equals(req.getParameter("key_value"))&&"external".equals(req.getParameter("profile"))){
            //线路
            return feignClientCallCenter.getSipGatewayByCode(code);
        }else if("acl.conf".equals(req.getParameter("key_value"))){
            //对接网关
            return feignClientCallCenter.getDockGatewayByCode(code);
        }
        return "conf not found";
    }
@PostMapping("/getByFreeswitchCode")
    public String getByFreeswitchCode(@RequestParam("code") String code){
        TbFreeswitchEntity tbFreeswitchEntity = tbFreeswitchService.findByCode(code);
        if(tbFreeswitchEntity == null){
            return "code不存在";
        }
        //线路
        SipGatewayEntity query = new SipGatewayEntity();
        query.setType(SipGatewayEntity.SipGatewayType.routine);
        query.setRefreshStatus(null);
        query.setRegister(null);
        List<SipGatewayEntity> sipGatewayEntityList = sipGatewayService.findByFreeswitchId(tbFreeswitchEntity.getId());
        String gateway = "";
        for (SipGatewayEntity sipGatewayEntity:
                sipGatewayEntityList) {
            gateway += "\t\t\t\t<gateway name=\""+sipGatewayEntity.getSign()+"\">\n" +
                    "\t\t\t\t\t<param name=\"realm\" value=\""+sipGatewayEntity.getRealm()+"\"/>\n" +
                    "\t\t\t\t\t<param name=\"username\" value=\""+sipGatewayEntity.getUsername()+"\"/>\n" +
                    "\t\t\t\t\t<param name=\"password\" value=\""+sipGatewayEntity.getPassword()+"\"/>\n" +
                    "\t\t\t\t\t<param name=\"register\" value=\"false\"/>\n" +
                    "\t\t\t\t</gateway>\n" ;
        }
        return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<document type=\"freeswitch/xml\">\n" +
                "  <section name=\"configuration\" description=\"Various Configuration\">\n" +
                "    <configuration name=\"sofia.conf\" description=\"sofia Endpoint\">\n" +
                "\t\t  <profiles>\n" +
                "\t\t\t<profile name=\"external\">\n" +
                "\t\t\t  <!-- http://wiki.freeswitch.org/wiki/Sofia_Configuration_Files -->\n" +
                "\t\t\t  <!-- This profile is only for outbound registrations to providers -->\n" +
                "\t\t\t  <gateways>\n" +
                gateway+
                "\t\t\t  </gateways>\n" +
                "\t\t\t</profile>\n" +
                "\n" +
                "\t\t  </profiles>\n" +
                "\t\t</configuration>\n" +
                "  </section>\n" +
                "</document>\n";

    }

每次要添加新的配置的时候,调用frees witch的一下命令,就会请求接口,把新的配置刷新到内存中:

sofia profile external rescan

五、mod_xml_curl接管acl配置

<configuration name="xml_curl.conf" description="cURL XML Gateway">
  <bindings>
      <!-- 可接管所有参数,接口端可根据参数判断 -->
      <binding name="configuration">
          <param name="gateway-url" value="http://183.66.37.178:7001/api/sipGateway/get?code=$${code}" bindings="configuration"/>
      </binding>
  </bindings>
</configuration>

以上配置不变

Java代码

@PostMapping("/getByFreeswitchCode")
    public String getByFreeswitchCode(@RequestParam("code") String code){
        TbFreeswitchEntity tbFreeswitchEntity = tbFreeswitchService.findByCode(code);
        if(tbFreeswitchEntity == null){
            return "code不存在";
        }
        List<TbDockGatewayEntity> tbDockGatewayEntityList = tbDockGatewayService.findByTbFreeswitchId(tbFreeswitchEntity.getId());
        String ip = "";
        for (TbDockGatewayEntity tbDockGatewayEntity:
                tbDockGatewayEntityList) {
            ip += "\t <node type=\"allow\" cidr=\""+tbDockGatewayEntity.getIp()+"/32\"/>\n";
        }
        String msg = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<document type=\"freeswitch/xml\">\n" +
                " <section name=\"configuration\" description=\"Various Configuration\">\n" +
                "<configuration name=\"acl.conf\" description=\"Network Lists\">\n" +
                "  <network-lists>\n" +
                "    <list name=\"lan\" default=\"allow\">\n" +
                "      <node type=\"deny\" cidr=\"192.168.42.0/24\"/>\n" +
                "      <node type=\"allow\" cidr=\"192.168.42.42/32\"/>\n" +
                "      <node type=\"allow\" cidr=\"127.0.0.1/32\"/>\n" +
                "    </list>\n" +
                "\n" +
                "    <list name=\"domains\" default=\"deny\">\n" +
                "      <node type=\"allow\" domain=\"$${domain}\"/>\n" +
                ip+
                "    </list>\n" +
                "  </network-lists>\n" +
                "</configuration>\n"+
                "  </section>\n" +
                "</document>\n";
        return msg;

    }

每次修改完配置之后,调用一下命令,frees witch就会把配置重新刷新到内存中

reloadacl

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值