Forum Discussion
IP Filtering using Xff-clientip in iRule
- Jul 25, 2019
You are right Stan! Thanks for the contribution.
, here is the new code:
when HTTP_REQUEST { if { ([HTTP::uri] starts_with "/site") } { if {[HTTP::header exists "X-Forwarded-For"]} { set clientIP [string trim [getfield [HTTP::header value "X-Forwarded-For"] "," 1] " "] if { ([class match $clientIP equals management_IP]) } { pool pSite } } else { pool pSite } } }
KR,
Dario.
Hello Alan.
Try with this code:
when HTTP_REQUEST {
if { ([HTTP::uri] starts_with "/site") } {
if {[HTTP::header exists "X-Forwarded-For"]} {
set clientIPList [split [HTTP::header value "X-Forwarded-For"] ","]
set clientIP [lindex $ipList 0]
if { ([class match $clientIP equals management_IP]) } {
pool pSite
}
} else {
pool pSite
}
}
}
KR,
Dario.
2 optimizations to your code:
- use getfield command instead of split / lindex
- use string trim command on the result to make sure there is no space before the comma
- Jul 25, 2019
You are right Stan! Thanks for the contribution.
, here is the new code:
when HTTP_REQUEST { if { ([HTTP::uri] starts_with "/site") } { if {[HTTP::header exists "X-Forwarded-For"]} { set clientIP [string trim [getfield [HTTP::header value "X-Forwarded-For"] "," 1] " "] if { ([class match $clientIP equals management_IP]) } { pool pSite } } else { pool pSite } } }
KR,
Dario.
- Stanislas_Piro2Jul 25, 2019Cumulonimbus
From tcl string wiki
string trim string ?chars?
Returns a value equal to string except that any leading or trailing characters present in the string given by chars are removed. If chars is not specified then white space is removed (spaces, tabs, newlines, and carriage returns).
So no need to define the space in string trim command...
- Alan_MillarJul 25, 2019Nimbostratus
This is working properly, thank you for the help!
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com