What could you do with your code in 20 Lines or Less? That’s the question I like to ask for the DevCentral community, and every time I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.
This week the 20 Lines or Less is brought to you from the road. From Raleigh, North Carolina to be exact. I’m here today for another awesome F5 User Group, and am excited to add a dose of DevCentral and iRules goodness to the agenda. As such it’s even more appropriate than normal that I find myself crawling through the forums to see what the good people of DevCentral have been up to recently. As always, I’m far from let down. The community provides example after awesome example of just how iRules can be so powerful and useful with so little effort. This week I’ve got offerings of port based routing, binary scanning goodness, and an interesting take on cookie inspection. So let’s get started!
Route Traffic to specific port via URL
Chase asked about how to do some routing based on URL (host name, really). He wanted to look at the cname of a particular domain and, based on that, route to the same pool of servers on one of two different ports. While there are probably a few other, tricky ways to do this, Kevin came through with the simple, logical approach that I fully support. Two pools, same members, different ports, super simple iRule to select which one you want. Bing-bang-boom, you’ve got a port switcher based on host information. A simple solution that’ll get the job done with barely any code to speak of, and we here at the 20LoL writing desk approve of such things, not shockingly.
<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">HTTP_REQUEST</span> {
<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">switch</span> [<span style="color: #0000ff;">string</span> tolower [<span style="color: #0000ff;">HTTP::host</span>]] {
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #006080;">"appa.domain.com"</span> { <span style="color: #0000ff;">pool</span> appa_pool }
<span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #006080;">"appb.domain.com"</span> { <span style="color: #0000ff;">pool</span> appb_pool }
<span id="lnum5" style="color: #606060;"> 5:</span> }
<span id="lnum6" style="color: #606060;"> 6:</span> }
TCP Collection and binary scanning
There are approximately 10980128481490128438012985012832 different reasons that you might want to do some collection within an iRule. This is an example of one of those (no, I don’t know which number it is), in which rahtoll was looking to seek out a specific section of a payload and see if it matched a given value. There are a few ways to go about this, and after a little coaching from some of DC’s finest in the form of Michael Yates and the indisputably awesome Hoolio, it looks like a solution is now in place. If you’ve got inspection needs for specific chunks of a payload, this might be a good one to keep in your hip pocket.
<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">CLIENT_ACCEPTED</span> {
<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">TCP::collect</span> 65
<span id="lnum3" style="color: #606060;"> 3:</span> }
<span id="lnum4" style="color: #606060;"> 4:</span>
<span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">CLIENT_DATA</span> {
<span id="lnum6" style="color: #606060;"> 6:</span>
<span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #008000;"># use pool2 by default </span>
<span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #0000ff;">pool</span> test_pool2
<span id="lnum9" style="color: #606060;"> 9:</span>
<span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;">set</span> mero [<span style="color: #0000ff;">TCP::payload</span> 15]
<span id="lnum11" style="color: #606060;"> 11:</span> <span style="color: #008000;"># Start reading at the 5th byte and save 6 characters to $match </span>
<span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #0000ff;">if</span> {[<span style="color: #0000ff;">binary</span> <span style="color: #0000ff;">scan</span> $mero x5a6 match] == 1}{
<span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;">log</span> local0. <span style="color: #006080;">"Matched $match"</span>
<span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #0000ff;">if</span> { $match eq <span style="color: #006080;">"000001"</span> } {
<span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;">pool</span> test_pool1
<span id="lnum16" style="color: #606060;"> 16:</span> }
<span id="lnum17" style="color: #606060;"> 17:</span> }
<span id="lnum18" style="color: #606060;"> 18:</span> <span style="color: #0000ff;">TCP::release</span>
<span id="lnum19" style="color: #606060;"> 19:</span> }
Cookie Checking
We’ve covered cookies a dozen or more times over the years in the 20LoL (The fact that I can say that is a bit scary, and hawesome) but this is something I haven’t seen before. This is a way to check to see if cookies are enabled in a given browser, and a rather clever one at that. I’m sure there are some use cases for this in production, but there are definitely some in different test environments, and it may be the start to some cool deployment logic. A neat idea at the very least, and something that caught my eye in my perusal of all things iRules.
<span id="lnum1" style="color: #606060;"> 1:</span> <span style="color: #0000ff;">when</span> <span style="color: #cc6633;">HTTP_REQUEST</span> {
<span id="lnum2" style="color: #606060;"> 2:</span> <span style="color: #0000ff;">if</span> { ( [<span style="color: #0000ff;">HTTP::uri</span>] <span style="color: #0000ff;">starts_with</span> <span style="color: #006080;">"/cookietest"</span> ) } {
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #008000;">## test for cookie</span>
<span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;">if</span> { <span style="color: #0000ff;">not</span> ( [<span style="color: #0000ff;">HTTP::cookie</span> exists <span style="color: #006080;">"COOKIETEST"</span>] ) } {
<span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #008000;">## cookies are disabled</span>
<span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;">HTTP::respond</span> 200 content <span style="color: #006080;">"Sorry. This site requires cookies"</span>
<span id="lnum7" style="color: #606060;"> 7:</span> } <span style="color: #0000ff;">else</span> {
<span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #008000;">## cookies enabled - redirect to first URI</span>
<span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #0000ff;">HTTP::respond</span> 302 Location [<span style="color: #0000ff;">findstr</span> [<span style="color: #0000ff;">HTTP::uri</span>] <span style="color: #006080;">"/cookietest"</span> 11]
<span id="lnum10" style="color: #606060;"> 10:</span> }
<span id="lnum11" style="color: #606060;"> 11:</span> } <span style="color: #0000ff;">elseif</span> { [<span style="color: #0000ff;">HTTP::cookie</span> count] == <span style="color: #006080;">""</span> } {
<span id="lnum12" style="color: #606060;"> 12:</span> <span style="color: #008000;">## no cookies (could be first request) - redirect to cookie test URI with test cookie</span>
<span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #0000ff;">HTTP::respond</span> 302 Location <span style="color: #006080;">"/cookietest[HTTP::uri]"</span> <span style="color: #006080;">"Set-Cookie"</span> <span style="color: #006080;">"COOKIETEST=1"</span>
<span id="lnum14" style="color: #606060;"> 14:</span> }
<span id="lnum15" style="color: #606060;"> 15:</span> }
There you have it, another addition to the heap of killer iRules contributions that can get you a handy fix, solution or idea in less than 21 lines of code. See you in 2 weeks for more 20LoL goodness.