I’m looking to create an iRule whereby visitors to a specific website are redirected if they do not come from particular ranges of source IP addresses. The range of IP addresses will be large (IP by country); is there a way to refer the source to a file? Anybody created such a rule previously? Advice much appreciated.
A colleague of mine subscribes to a service where he downloads the list of IP addresses per country. In his iRule he created a datagroup listing all the ranges in each country and then he wrote up an Irule using Matchclasses Click here and IP::client_addr Click here. It can be quite time consuming because of the data entry. A file can be used as the source within an iRule. Click here to take a look at an example where a source file is used to read the entries into the iRule.
The problems is that the irule is redirecting ALL traffic and seems to be ignoring the data group list.
We’ve tried the data group external file as a string - example:
“138.198.0.0/16”,
“141.97.0.0/16”,
“145.229.0.0/16”,
“145.233.0.0/16”,
and as an address - example
network 62.23.0.0/16,
network 62.24.32.0/19,
network 62.24.128.0/17,
network 62.30.0.0/16,
network 62.31.0.0/16,
Neither of these are being picked up. Could it be that the irule is looking for IP addresses rather than networks, so the list isn’t matching the query?
Can you use the second format, trim out all but a handful of entries, reload your configuration using ‘b load’, and then test the iRule further? I believe that if you modify the external class file, the change isn’t read into memory until the config is reloaded.
Try adding a log statement of the client IP address, the datagroup contents and the value of the matchclass command. This should give you a good indication of all the relevant data.
Also, once you have the class check working, you could move it to CLLIENT_ACCEPTED and set a variable if the client needs to be redirected. That way you do the check once per TCP connection instead of for every HTTP request. You could also forcibly close the TCP connection with TCP::close after the HTTP redirect to ensure the client doesn’t retry the request on the same TCP connection if they’ll never be allowed to reach the pool.