From 49c35236b2a46f36b02cedce838160b0a7b318fb Mon Sep 17 00:00:00 2001 From: DanB Date: Mon, 13 Sep 2021 17:24:01 +0200 Subject: [PATCH] Updating OpenSIPS tutorial configs --- .../osips/cgrates/etc/cgrates/cgrates.json | 34 +++--- .../osips/opensips/etc/opensips/opensips.cfg | 101 +++++++++++++++--- 2 files changed, 103 insertions(+), 32 deletions(-) diff --git a/data/tutorials/osips/cgrates/etc/cgrates/cgrates.json b/data/tutorials/osips/cgrates/etc/cgrates/cgrates.json index 447e22911..1a1b742b8 100644 --- a/data/tutorials/osips/cgrates/etc/cgrates/cgrates.json +++ b/data/tutorials/osips/cgrates/etc/cgrates/cgrates.json @@ -26,35 +26,35 @@ "enabled": true, "thresholds_conns": ["*localhost"], "stats_conns": ["*localhost"], - "attributes_conns": ["*internal"], + "attributes_conns": ["*localhost"], }, "cdrs": { "enabled": true, - "stats_conns": ["*internal"], - "chargers_conns": ["*internal"], + "stats_conns": ["*localhost"], + "chargers_conns": ["*localhost"], "sessions_cost_retries": 5, }, "chargers": { "enabled": true, - "attributes_conns": ["*internal"], + "attributes_conns": ["*localhost"], }, "sessions": { "enabled": true, "listen_bijson": "127.0.0.1:2014", // address where to listen for bidirectional JSON-RPC requests - "chargers_conns": ["*internal"], - "rals_conns": ["*internal"], - "cdrs_conns": ["*internal"], - "resources_conns": ["*internal"], - "routes_conns": ["*internal"], - "attributes_conns": ["*internal"], - "stats_conns": ["*internal"], - "thresholds_conns": ["*internal"], + "chargers_conns": ["*localhost"], + "rals_conns": ["*localhost"], + "cdrs_conns": ["*localhost"], + "resources_conns": ["*localhost"], + "routes_conns": ["*localhost"], + "attributes_conns": ["*localhost"], + "stats_conns": ["*localhost"], + "thresholds_conns": ["*localhost"], "debit_interval": "10s", "channel_sync_interval":"30s", }, @@ -69,7 +69,7 @@ "resources": { "enabled": true, - "thresholds_conns": ["*internal"], + "thresholds_conns": ["*localhost"], "string_indexed_fields": ["*req.Account"], "prefix_indexed_fields": ["*req.Destination"], }, @@ -77,7 +77,7 @@ "stats": { "enabled": true, - "thresholds_conns": ["*internal"], + "thresholds_conns": ["*localhost"], "string_indexed_fields": ["*req.Account"], }, @@ -90,8 +90,8 @@ "routes": { "enabled": true, - "resources_conns": ["*internal"], - "stats_conns": ["*internal"], + "resources_conns": ["*localhost"], + "stats_conns": ["*localhost"], "string_indexed_fields": ["*req.Account"], "prefix_indexed_fields": ["*req.Destination"], }, @@ -99,7 +99,7 @@ "apiers": { "enabled": true, - "scheduler_conns": ["*internal"], + "scheduler_conns": ["*localhost"], }, diff --git a/data/tutorials/osips/opensips/etc/opensips/opensips.cfg b/data/tutorials/osips/opensips/etc/opensips/opensips.cfg index 794255abe..010636c99 100644 --- a/data/tutorials/osips/opensips/etc/opensips/opensips.cfg +++ b/data/tutorials/osips/opensips/etc/opensips/opensips.cfg @@ -23,11 +23,7 @@ udp_workers=4 auto_aliases=no -listen=udp:enp0s3:5060 -listen=udp:127.0.0.1:5080 -listen=udp:127.0.0.1:5060 -listen=udp:enp0s3:5080 - +socket=udp:enp0s8:5060 ####### Modules Section ######## @@ -87,6 +83,14 @@ loadmodule "dialog.so" loadmodule "cgrates.so" modparam("cgrates", "cgrates_engine", "127.0.0.1:2014") +#### MySQL module +loadmodule "db_mysql.so" + +#### Drouting module +loadmodule "drouting.so" +modparam("drouting", "db_url", + "mysql://opensips:opensipsrw@localhost/opensips") + ####### Routing Logic ######## @@ -201,7 +205,7 @@ route{ # check auth with CGRateS $cgr_opt(GetMaxUsage) = 1; # also retrieve the max usage $cgr_opt(GetAttributes) = 1; - $cgr_opt(GetSuppliers) = 1; + $cgr_opt(GetRoutes) = 1; $cgr_opt(AuthorizeResources) = 1; $cgr_opt(AllocateResources) = 1; $cgr_opt(ReleaseResources) = 1; @@ -218,29 +222,96 @@ route{ } route [resume_cgr_auth] { - if ($rc != 1) { - xlog("Call not authorized: code=$cgrret!\n"); + $var(rc) = $rc; + # with GetMaxUsage == false, cgrates_auth() returns -2 on success + if ($var(rc) < 0 && ($cgr_ret(MaxUsage) != 0 || $var(rc) != -2)) { + xlog("L_NOTICE", "[$ci] CGRateS auth failed: rc=$var(rc), code=$cgr_ret\n"); send_reply(403, "Forbidden"); exit; } + + # Set the returned attributes from CGRateS as script pseudovariables $var(idx) = 0; while ($(cgr_ret(AttributesDigest){s.select,$var(idx),,}) != NULL) { $avp($(cgr_ret(AttributesDigest){s.select,$var(idx),,}{s.select,0,:})) = $(cgr_ret(AttributesDigest){s.select,$var(idx),,}{s.select,1,:}); $var(idx) = $var(idx) + 1; } - cgrates_acc("cdr|missed", "$fU", "$rU"); - xlog("### Have authorized the call and it should go through"); - route(relay); + + # Enable CDRs being sent to CGRateS + cgrates_acc("cdr|missed", "$fU", "$rU"); + + if ( $cgr_ret(RoutesDigest)==NULL ) { # no routing requested + route(relay); + } + + xlog("L_INFO", "[$ci] CGRateS auth OK, with routes: <$cgr_ret(RoutesDigest)>\n"); + $avp(carriers) := $cgr_ret(RoutesDigest); + $avp(carriers_idx) := 0; + + route( to_carriers ); } +route[to_carriers] { + + xlog("L_INFO", "[$ci] routing to carrier IDX $avp(carriers_idx)\n"); + + $var(carrier) = $(avp(carriers){s.select,$avp(carriers_idx),,}); + if ( $var(carrier) == NULL) { + send_reply( 404 , "No route available to route"); + exit; + } + $cgr(RouteID) = $var(carrier); + + xlog("L_INFO", "[$ci] routing to carrier <$var(carrier)>\n"); + $avp(carriers_idx) := $avp(carriers_idx) + 1; + + if ( !route_to_carrier("$var(carrier)") ) { + xlog("L_INFO", "[$ci] failed to route to carrier, skipping>\n"); + route( to_carriers ); + exit; + } + + t_on_failure("gw_failure"); + t_relay(); + + #end; + +} + +failure_route[gw_failure] { + xlog("L_INFO", "[$ci] GW call failed to established with $T_reply_code code\n"); + + if (t_was_cancelled()) { + exit; + } + + # failure indication about GWs? + if ( t_check_status("(408)|([56][0-9][0-9])") ) { + xlog("L_INFO", "[$ci] GW $rd failed with $T_reply_code -> retry\n"); + if ( use_next_gw() ) { + xlog("L_INFO", "[$ci] using new GW <$rd>\n"); + t_on_failure("gw_failure"); + t_relay(); + #end + } + + # no more GWs for the carrier, try the next carrier + route( to_carriers ); + } + +} + + + + route[relay] { # for INVITEs enable some additional helper routes if (is_method("INVITE")) { t_on_branch("per_branch_ops"); - t_on_reply("handle_nat"); - t_on_failure("missed_call"); + t_on_reply("handle_reply"); + t_on_failure("handle_failure"); } if (!t_relay()) { @@ -257,12 +328,12 @@ branch_route[per_branch_ops] { } -onreply_route[handle_nat] { +onreply_route[handle_reply] { xlog("incoming reply\n"); } -failure_route[missed_call] { +failure_route[handle_failure] { if (t_was_cancelled()) { exit; }