What could you do with your code in 20 Lines or Less?

That's the question we like to ask from, for, and of (feel free to insert your favorite preposition here) the DevCentral community, and every time we do, we go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head. Thus was born the 20LoL (20 Lines or Less) series many moons ago. Over the years we've highlighted hundreds of iRules examples, all of which do downright cool things in less than 21 lines of code.

 

Logging Client IP in NAT Scenarios

BIG-IP 11.3.1 LTM connection session logs to syslog server

This is a common question, but typically, the problem is solved in-line with header replacement, or even fancier solutions like embedding IPs in tcp options and such. For this case, member Tabish is content to just have the data offline in log files as the app developers don't want to modify the header. This can be logged in several ways, and two were proposed as answers. One was to log to the local syslog instance (that can also be configured to log off box) and the other was to use high speed logging.

 

when HTTP_REQUEST { set host [HTTP::host]
  set username [HTTP::username]
  set client_IP [IP::client_addr]
  set request "\"[HTTP::method] [HTTP::uri] HTTP/[HTTP::version]\""
  set request_time [clock clicks -milliseconds]
  set referer [HTTP::header "Referer"]
  set ua [HTTP::header "User-Agent"]
  set xff [HTTP::header "X-Forwarded-For"]
}
when HTTP_RESPONSE {
  set response_time [expr [clock clicks -milliseconds] - $request_time]
  set now [clock format [clock seconds] -format "%d/%b/%Y:%H:%M:%S %z"]
  set contentlength [HTTP::header "Content-Length"]
  set status [HTTP::status]
  HSL::send [HSL::open -proto TCP -pool /CLI01_COPR/POOL_SYSLOG] "$host $client_IP $username $now $request $status $contentlength \"$referer\" \"$ua\" \"$xff\" $response_time" 
}

 

 

Reverse Proxy

iRule for a reverse Proxy

This iRule solution would qualify for 5 Lines or Less, if such a series existed! Member Nikhil is just looking for reverse proxy support, with the BIG-IP handling host and uri rewriting. Simple task, provided in two forms in the thread, which I've combined into a form you can use on all systems version 10.0+, though in reality, one or the other is sufficient for the version you have and eats less memory. I just wanted an opportunity to show off the infrequently used tcl_platform command! Hit the link above for the primitives for each version.

 

when HTTP_REQUEST {
 set ver tcl_platform(osVersion)
 if { ($ver starts_with "11.5") or ($ver starts_with "11.6") {
   HTTP::host "nik-int.test.com"
 } else {
   HTTP::header replace Host "nik-int.test.com"
 }
 HTTP::uri "/media[HTTP::uri]"
}

 

A Walk in the Park with the Stream Profile

STREAM::EXPRESSION and characters that may not allow a match

Member Steve was stuck on a stream expression not exactly matching properly.  Nitass shared an example escaping the question marks (in the static::search variable) and it worked a treat.

 

when RULE_INIT {
  set static::search "http://web.site.com/fd/qalogon.php\\?groupid=aaa_library&using sql35\\?="
  set static::replace "http://new.site.com:8080/share/page/site/directory/dashboard"
}
when HTTP_REQUEST {
  STREAM::disable
  HTTP::header remove "Accept-Encoding"
}
when HTTP_RESPONSE {
  if {[HTTP::header value Content-Type] contains "text"}{
    STREAM::expression @${static::search}@${static::replace}@
    STREAM::enable
  }
}