Skip to content

OpenSIPS Example

Lorenzo Mangani edited this page May 13, 2017 · 4 revisions

OpenSIPS

First we check $si (source ip) to check if the destination is blocked using cacheep/REST API query. If so, we respond with SIP 403 and update statistics. If destination is not blocked, we optionally check $ua and if it’s friendly scanner we block it using caceep/REST API

Modules:

  • loadmodule "rest_client.so"
  • loadmodule "enum.so" #In case ENUM block
  • loadmodule "json.so"

Generic Examples

REST Lookup

if (rest_get("http://127.0.0.1/api/get/$rU", "$var(reason)", "$var(ct)", "$var(rcode)")) {
if ($var(reason) != $null) {
        xlog("Call blocked with reason: $var(reason)\n");
        send_reply("403", "Forbidden");
        exit;
}
}

ENUM Lookup

if( enum_query() ) {
         # Blacklist Entry found!
         xlog("ENUM - blacklist entry found: $rU" ) ;
         route(kill) ;
}

REST Example

 if (is_method("REGISTER | INVITE")) {
       if (rest_get("http://blacklist.xx.com:3001/api/get/$si", "$var(response)")) {
 	$json(result) := $var(response);
$var(req) = "blacklist.blocked.completed,service=opensips,region=us-west value=1 "+$Ts+"000000000";
if (!rest_post("http://blacklist.xx.com:8186/write", "$var(req)", ,"$var(body)", "$var(ct)", "$var(rcode)")) {
xlog("Error code $var(rcode) in HTTP POST!\n"); 
}
if($json(result/blocked) == "true")  {
       send_reply("403","You have been banned");
       $var(req) = "blacklist.blocked.completed,service=opensips,region=us-west value=1 "+$Ts+"000000000";
       if (!rest_post("http://blacklist.xx.com:8186/write", "$var(req)", ,"$var(body)", "$var(ct)", "$var(rcode)")) {
xlog("Error code $var(rcode) in HTTP POST!\n");
       } exit; 
} else {
       $var(req) = "blacklist.ublocked.request,service=opensips,region=us-west value=1 "+$Ts+"000000000";
       if (!rest_post("http://blacklist.xx.com:8186/write", "$var(req)", ,"$var(body)", "$var(ct)", "$var(rcode)")) {
xlog("Error code $var(rcode) in HTTP POST!\n");
}
if($ua =~ "friendly-scanner|sipcli|VoIP SIP") {
$var(req) = "blacklist.block.request,service=opensips,region=us-west value=1 "+$Ts+"000000000";
if (!rest_post("http://blacklist.xx.com:8186/write", "$var(req)", ,"$var(body)", "$var(ct)", "$var(rcode)")) {
                                                xlog("Error code $var(rcode) in HTTP POST!\n"); 
}
                                }
                        }
                };
                 xlog("L_ERR", "DO LOOKUP: $si, $var(response)");
}
Clone this wiki locally