Forum Discussion

knight207_63919's avatar
knight207_63919
Icon for Nimbostratus rankNimbostratus
Sep 19, 2011

Proxy outbound HTTP base on User Agent

I am somewhat new to iRules so this is my first go.

 

 

I want to send outbound HTTP requests from certain browers to proxy servers using the user agent. Anything not matching should just forward out to default gateway.

 

 

Any comments or suggestions appreciated.

 

 

when HTTP_REQUEST {

 

if { [HTTP::header "User-Agent"] contains "MSIE" } {

 

Match user agent MSIE and sent to Pool with persist profile

 

persist PER-HTTP-PROFILE-A

 

pool PROXY-POOL-HTTP

 

}

 

else if { [HTTP::header "User-Agent"] contains "Firefox" }{

 

Match user agent Firefox and sent to Pool with persist profile

 

persist PER-HTTP-PROFILE-B

 

pool PROXY-POOL-HTTP

 

}

 

else if { [HTTP::header "User-Agent"] contains "Chrome" }{

 

Match user agent Chrome and sent to Pool with persist profile

 

persist PER-HTTP-PROFILE-B

 

pool PROXY-POOL-HTTP

 

}

 

else {

 

do nothing and send connection to default gateway using IP_Forwarding_virtual_server

 

forward

 

}

 

  • I would suggest using a Switch Statement rather than nested If Statements for efficiency and inside of your default behavior put a logging statement that you can enable periodically to see what other types of browsers are hitting your Virtual Server (or if you want to identify one to add it to your switch statement). When not in use you can disable it.

     
    when HTTP_REQUEST {
    switch -glob [HTTP::header User-Agent] {
    "*MSIE*" { do something }
    "*Firefox*" { do something }
    "*Chrome*" { do something }
    default {
    log local0. "Unhandled User Agent: [HTTP::header User-Agent]"
    }
    }
    }
    
  • Nice one Michael. You could also set the User-Agent header to lower case to avoid case issues:

    
    when HTTP_REQUEST {
        switch -glob [string tolower [HTTP::header User-Agent]] {
            "*msie*" { do something }
            "*firefox*" { do something }
            "*chrome*" { do something }
            default {
                log local0. "Unhandled User Agent: [HTTP::header User-Agent]"
            }
        }
    }
    

    Aaron