11package pl .panszelescik .proxy_protocol_support .shared .config ;
22
3- import java .io .BufferedReader ;
43import java .io .IOException ;
5- import java .io .InputStreamReader ;
64import java .net .URI ;
5+ import java .net .http .HttpClient ;
6+ import java .net .http .HttpRequest ;
7+ import java .net .http .HttpResponse ;
78import java .util .HashSet ;
89
910/**
@@ -16,23 +17,35 @@ public class TCPShieldIntegration {
1617 private static final URI IPV4 = URI .create ("https://tcpshield.com/v4/" );
1718 private static final URI IPV4_CF = URI .create ("https://tcpshield.com/v4-cf/" );
1819
19- public static HashSet <CIDRMatcher > getWhitelistedIPs () throws IOException {
20+ public static HashSet <CIDRMatcher > getWhitelistedIPs () throws IOException , InterruptedException {
2021 final HashSet <CIDRMatcher > matchers = new HashSet <>();
2122
22- readFromUrl (matchers , IPV4 );
23- readFromUrl (matchers , IPV4_CF );
23+ try (HttpClient client = HttpClient .newHttpClient ()) {
24+ readFromUrl (matchers , client , IPV4 );
25+ readFromUrl (matchers , client , IPV4_CF );
26+ }
2427
2528 return matchers ;
2629 }
2730
28- private static void readFromUrl (HashSet <CIDRMatcher > matchers , URI uri ) throws IOException {
29- try (final BufferedReader reader = new BufferedReader (new InputStreamReader (uri .toURL ().openStream ()))) {
30- while (reader .ready ()) {
31- final String line = reader .readLine ().trim ();
32- if (!line .isEmpty () && !line .startsWith ("#" ) && !line .startsWith ("127.0.0.1" )) {
33- matchers .add (new CIDRMatcher (line ));
34- }
35- }
31+ private static void readFromUrl (HashSet <CIDRMatcher > matchers , HttpClient client , URI uri ) throws IOException , InterruptedException {
32+ final HttpRequest request = HttpRequest
33+ .newBuilder (uri )
34+ .version (HttpClient .Version .HTTP_2 )
35+ .GET ()
36+ .build ();
37+
38+ final HttpResponse <String > response = client .send (request , HttpResponse .BodyHandlers .ofString ());
39+ final int statusCode = response .statusCode ();
40+ if (statusCode < 200 || statusCode >= 300 ) {
41+ throw new IOException ("Failed to fetch " + uri + ": HTTP " + statusCode );
3642 }
43+
44+ response .body ()
45+ .lines ()
46+ .map (String ::trim )
47+ .filter (l -> !l .isEmpty () && !l .startsWith ("#" ) && !l .startsWith ("127.0.0.1" ))
48+ .map (CIDRMatcher ::new )
49+ .forEach (matchers ::add );
3750 }
3851}
0 commit comments