cancel
Showing results for 
Search instead for 
Did you mean: 

Traffic script as function

SOLVED
Highlighted
Occasional Contributor

Traffic script as function

Dear,

 

Currently I am trying to make reverse proxying with stingray. It is working fine, but sometimes I need to make content rewritting. My question is: Is it possible to call a rule into another rule as a function?

 

Example:

 

I have the following rule ( request rules ):

 

$url = http.getpath();
if( string.startsWith($url,"/shinken/")){
http.setResponseCookie("Stingray","shinken");
$newUrl = string.replace($url,"/shinken/","/");
http.setPath($newUrl);
pool.use("linux_monitoring");
}

 

 

The Stingray returns the right result wihtout css, images... because path is wrong that is why I need to make rewriting. I am able to make rewriting and I make three rules: html, css, js rewriting

 

I have this rule ( response ):

 

$cookie = http.getcookie("Stingray");

if( !string.startsWith( http.getResponseHeader("Content-Type"),"text/html")){

break;

}else {

if( $cookie == "shinken" ){

$body = http.getResponseBody();

$line = string.regexsub($body,"href=['\"](+)['\"]","href=\"/shinken$1\"","g");

$line = string.regexsub($line,"src=['\"](+)['\"]","src=\"/shinken$1\"","g");

$line = string.regexsub($line,"action=['\"](+)['\"]","action=\"/shinken$1\"","g");

http.setResponseBody($line);

}

}

 


 

It is working fine but what I want is to use this rule for other entries which need the rewriting.

 

Do you have a solution?

 

Best regards,

Mxs57.

1 REPLY
Frequent Contributor

Re: Traffic script as function

Take a look at the 'Libraries' capability in TrafficScript.  This lets you centralize common code in a 'library' (just a standard TrafficScript rules) and then reference that code from other rules.

The TrafficScript manual describes Libraries in detail.  In brief:

Create a rule to be used as a library - the rule is named 'debug':



# per-connection debugging to the event log



sub on() {


  connection.data.set( "mydebug", 1 );


}



sub off() {


  connection.data.set( "mydebug", 0 );


}




# only logs the msg if debug is on for this connection



sub log( $msg ) {


  if( connection.data.get( "mydebug" ) ) {


      log.info( $msg );


  }


}


Now import and use that rule within another rule:



# For demo purposes (so that the map looks good), fake the source addres


# of this connection



# Here's where we pull in another rule ('library')



import debug;



# Comment out the following line to disable debugging


debug.on();



$countries = [ "US", "CA", "GB", "FR", "ES", "AU", "NZ", "DE", "CH", "IT", "GR", "RU" ];



while ( 1 ) {


  $a = random( 254 ) + 1;


  $b = random( 254 ) + 1;


  $c = random( 254 ) + 1;


  $d = random( 254 ) + 1;



  $ip = $a . '.' . $b . '.' . $c . '.' . $d;



  debug.log( "Trying IP address: " . $ip );



# now verify that the generated IP is valid


  if( array.contains( $countries, geo.getCountryCode( $ip ) ) ) break;


}



debug.log( "Picked " . $ip . " in " . geo.getCountry( $ip ) );



request.setRemoteIP( $ip );



# not strictly necessary...


debug.off();


Hopefully, this illustrates how to place functions in a separate rule ('library') and then invoke them from a rule that is associated with a virtual server.

regards

Owen