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.
From iRules that appeared in the illustrious Post of the Week, to users answering their own questions, this week’s 20 Lines or Less offers some goodies for everyone. Three different iRules supplied by three different community members go to show just a few more things that this awesome technology can do in a short amount of space and likely time. This week we’ve got browser controlling, port redirection and DMZ access concerns all being addressed in less than 21 lines of code. If you’re still not convinced that iRules can do some hawesome things in a small package, or if you are and are excited about learning more, read on.
Control Browser Version
During last week’s Post of the Week Joe and I chatted about a request from user Kemstar wherein he was looking for a way to control which browser versions were allowed to access a particular application. We responded in the affirmative and described a bit how to do this. Not shockingly Kemstar soon whipped up his very own iRule and, with a little help from Joe, now has a functional solution to the original problem. Not only is he famous in DCLand, but his problem got solved too, how about that?
<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;">not</span> ([<span style="color: #0000ff;">string</span> tolower [<span style="color: #0000ff;">HTTP::header</span> User-Agent]] <span style="color: #0000ff;">contains</span> <span style="color: #006080;">"msie 8.0"</span>)} {
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">HTTP::respond</span> 200 content { sorry, but the browser is <span style="color: #0000ff;">not</span> supported. }
<span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #0000ff;">log</span> local0. <span style="color: #006080;">"Unhandled User Agent: [HTTP::header User-Agent]"</span>
<span id="lnum5" style="color: #606060;"> 5:</span> }
<span id="lnum6" style="color: #606060;"> 6:</span> }
iRule to check Datagroup for allowed servers through a default VS
User Mike posed a question last week asking if it was possible to control which DMZ servers were allowed access to a set of internal servers, based on IP address of the inbound request. The answer is yes of course, but I needn’t post that to the thread, it would seem. Mike figured this out himself, and the very next day posted the solution for everyone else to benefit from as well. Thanks Mike, and way to be resourceful!
<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: #008000;"># Is client IP address defined in the FE datagroup? </span>
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">if</span> { [<span style="color: #0000ff;">class</span> match [<span style="color: #0000ff;">IP::client_addr</span>] <span style="color: #0000ff;">equals</span> sj01-fe-servers] }{
<span id="lnum4" style="color: #606060;"> 4:</span> <span style="color: #008000;"># Log the client IP address:port -> destination IP address:port </span>
<span id="lnum5" style="color: #606060;"> 5:</span> <span style="color: #0000ff;">log</span> local0. <span style="color: #006080;">"admin request accepted from client: \ </span>
<span id="lnum6" style="color: #606060;"> 6:</span> [IP::client_addr]:[TCP::client_port] -> [IP::local_addr]:[TCP::local_port]"
<span id="lnum7" style="color: #606060;"> 7:</span> } <span style="color: #0000ff;">elseif</span> { [<span style="color: #0000ff;">class</span> match [<span style="color: #0000ff;">IP::client_addr</span>] <span style="color: #0000ff;">equals</span> sj01-strongmail-servers] }{
<span id="lnum8" style="color: #606060;"> 8:</span> <span style="color: #008000;"># Client IP address is defined in the strongmail datagroup </span>
<span id="lnum9" style="color: #606060;"> 9:</span> <span style="color: #008000;"># Log the client IP address:port -> destination IP address:port </span>
<span id="lnum10" style="color: #606060;"> 10:</span> <span style="color: #0000ff;">log</span> local0. <span style="color: #006080;">"restricted client request accepted from client: \ </span>
<span id="lnum11" style="color: #606060;"> 11:</span> [IP::client_addr]:[TCP::client_port] -> [IP::local_addr]:[TCP::local_port]"
<span id="lnum12" style="color: #606060;"> 12:</span> } <span style="color: #0000ff;">else</span> {
<span id="lnum13" style="color: #606060;"> 13:</span> <span style="color: #008000;"># Request didn't match the conditions for allowing the request </span>
<span id="lnum14" style="color: #606060;"> 14:</span> <span style="color: #008000;"># Log the client IP address:port -> destination IP address:port </span>
<span id="lnum15" style="color: #606060;"> 15:</span> <span style="color: #0000ff;">log</span> local0. <span style="color: #006080;">"unknown request rejected from client: \ </span>
<span id="lnum16" style="color: #606060;"> 16:</span> [IP::client_addr]:[TCP::client_port] -> [IP::local_addr]:[TCP::local_port]"
<span id="lnum17" style="color: #606060;"> 17:</span> <span style="color: #0000ff;">drop</span>
<span id="lnum18" style="color: #606060;"> 18:</span> }
<span id="lnum19" style="color: #606060;"> 19:</span> }
Port Based Redirection
Regardless of what the original topic was, Nitass managed to deliver a mighty cool iRule that I figured I’d snatch up and spam out to the masses 20LoL style. This iRule acts once per connection and does a bit of magic that disables SSL for all connections, then for port 443 connections it re-enables it, and for any connection not on ports 80 or 443, it fires them off to a separate node at the originally specified port. Pretty tricky, and very cool. This one could be the basis for many a nifty functions methinks.
<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;">SSL::disable</span> <span style="color: #0000ff;">clientside</span>
<span id="lnum3" style="color: #606060;"> 3:</span> <span style="color: #0000ff;">switch</span> [<span style="color: #0000ff;">TCP::local_port</span>] {
<span id="lnum4" style="color: #606060;"> 4:</span> 80 { }
<span id="lnum5" style="color: #606060;"> 5:</span> 443 { <span style="color: #0000ff;">SSL::enable</span> <span style="color: #0000ff;">clientside</span> }
<span id="lnum6" style="color: #606060;"> 6:</span> <span style="color: #0000ff;">default</span> {
<span id="lnum7" style="color: #606060;"> 7:</span> <span style="color: #0000ff;">node</span> 200.200.200.102 [<span style="color: #0000ff;">TCP::local_port</span>]
<span id="lnum8" style="color: #606060;"> 8:</span> }
<span id="lnum9" style="color: #606060;"> 9:</span> }
<span id="lnum10" style="color: #606060;"> 10:</span> }