1

I want to configure the tomcat internalProxies remote IP valve to trust the Google GFE source addresses Which is

  • 35.191.0.0/16
  • 130.211.0.0/22

Tomcat only accepts a regular expression for these values.

  • What is the formula for converting an ip address range to a java.util.Regex?
  • are there any tools to convert ip address ranges to regexes?
ams
  • 60,316
  • 68
  • 200
  • 288
  • Refer this answer - https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address – codeogeek Jun 24 '20 at 01:56
  • Found this python script which you would have to convert to Java. https://gist.github.com/tom-knight/1b5e0dcf39062af8910e – dannyw Jan 23 '23 at 01:13

1 Answers1

0

I found this tool here. If you remove the backslash \ before . in provided regular expression seems to be compatible with java.util.regex.Pattern.

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class IPMatchTester {

     public static void main(String []args){
         // For 35.191.0.0/16
         Pattern p = Pattern.compile("^(35.191.(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])).(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$");
         int matchCount = 0;
         for( int j = 0; j <= 255; j++){
             for( int i = 0; i <= 255; i++){
                 if(!p.matcher("35.191."+ j +"." +i ).matches()){
                     System.out.println("35.191." + j + "."+ i + " no matched ");
                 } else{
                     matchCount++;
                 }
             }
         }
         System.out.println("Number of ip matched: " + matchCount);
         
         
         // For 130.211.0.0/22
         Pattern p2 = Pattern.compile("^(130.211.(?:[0-3]).(?:[0-9]|[1-9][0-9]|1(?:[0-9][0-9])|2(?:[0-4][0-9]|5[0-5])))$");
         int matchCount2 = 0;
         for( int j = 0; j <= 3; j++){
             for( int i = 0; i <=255; i++){
                 if(!p2.matcher("130.211."+ j +"." +i ).matches()){
                     System.out.println("130.211." + j + "."+ i + " no matched ");
                 } else{
                     matchCount2++;
                 }
             }
         }
         System.out.println("Number of ip matched: " + matchCount2);
     }
}
tinker
  • 855
  • 1
  • 6
  • 13