-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GRPC getClientIP - Strip and keep only ip-address (#85)
Fix GRPC getClientIP Issue --------- Co-authored-by: Kinshuk Bairagi <[email protected]> Co-authored-by: Kinshuk Bairagi <[email protected]>
- Loading branch information
1 parent
98abf95
commit f0b42e5
Showing
7 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
core/src/main/java/com/flipkart/gjex/core/util/NetworkUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.flipkart.gjex.core.util; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class NetworkUtils { | ||
|
||
private static final Pattern[] ipAddressPattern = { | ||
Pattern.compile( "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})"), | ||
Pattern.compile("((([0-9a-fA-F]){1,4})\\:){7}([0-9a-fA-F]){1,4}") | ||
}; | ||
|
||
|
||
/** | ||
* Extracts the IP address from a given string. | ||
* | ||
* @param str The input string from which the IP address is to be extracted. | ||
* @return The IP address extracted from the input string. | ||
*/ | ||
public static String extractIPAddress(String str) { | ||
if (str != null) { | ||
for (Pattern pattern : ipAddressPattern) { | ||
Matcher matcher = pattern.matcher(str); | ||
if (matcher.find()) { | ||
return matcher.group(); | ||
} | ||
} | ||
} | ||
return "0.0.0.0"; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/src/test/java/com/flipkart/gjex/core/util/NetworkUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.flipkart.gjex.core.util; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class NetworkUtilsTest { | ||
|
||
|
||
@Test | ||
public void extractIPAddressReturnsIPv4Address() { | ||
String input = "User IP is 192.168.1.1 and should be extracted"; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("192.168.1.1", result); | ||
} | ||
|
||
@Test | ||
public void extractIPAddressReturnsIPv6Address() { | ||
String input = "User IP is 2001:0db8:85a3:0000:0000:8a2e:0370:7334 and should be extracted"; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("2001:0db8:85a3:0000:0000:8a2e:0370:7334", result); | ||
} | ||
|
||
@Test | ||
public void extractIPAddressReturnsFirstIPv4AddressWhenMultiplePresent() { | ||
String input = "User IPs are 192.168.1.1 and 10.0.0.1, first one should be extracted"; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("192.168.1.1", result); | ||
} | ||
|
||
@Test | ||
public void extractIPAddressReturnsWithPrefix() { | ||
String input = "/192.168.1.1:1234"; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("192.168.1.1", result); | ||
} | ||
|
||
@Test | ||
public void extractIPAddressReturnsFirstIPv6AddressWhenMultiplePresent() { | ||
String input = "User IPs are 2001:0db8:85a3:0000:0000:8a2e:0370:7334 and fe80::1ff:fe23:4567:890a, first one should be extracted"; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("2001:0db8:85a3:0000:0000:8a2e:0370:7334", result); | ||
} | ||
|
||
@Test | ||
public void extractIPFromStringReturnsDefaultWhenNoIPPresent() { | ||
String input = "No IP address in this string"; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("0.0.0.0", result); | ||
} | ||
|
||
@Test | ||
public void extractIPAddress() { | ||
String input = ""; | ||
String result = NetworkUtils.extractIPAddress(input); | ||
assertEquals("0.0.0.0", result); | ||
} | ||
|
||
@Test | ||
public void extractIPAddressHandlesNullInput() { | ||
String result = NetworkUtils.extractIPAddress(null); | ||
assertEquals("0.0.0.0", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
guice/src/test/java/com/flipkart/gjex/grpc/interceptor/FilterInterceptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.flipkart.gjex.grpc.interceptor; | ||
|
||
import org.junit.Test; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class FilterInterceptorTest { | ||
|
||
@Test | ||
public void getClientIpReturnsHostNameForInetSocketAddress() { | ||
InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 1234); | ||
String result = FilterInterceptor.getClientIp(inetSocketAddress); | ||
assertEquals("localhost", result); | ||
} | ||
|
||
@Test | ||
public void getClientIpReturnsExtractedIpForNonInetSocketAddress() { | ||
SocketAddress socketAddress = new SocketAddress() { | ||
@Override | ||
public String toString() { | ||
return "192.168.1.1:1234"; | ||
} | ||
}; | ||
String result = FilterInterceptor.getClientIp(socketAddress); | ||
assertEquals("192.168.1.1", result); | ||
} | ||
|
||
@Test | ||
public void getClientIpReturnsDefaultIpForNullSocketAddress() { | ||
String result = FilterInterceptor.getClientIp(null); | ||
assertEquals("0.0.0.0", result); | ||
} | ||
|
||
@Test | ||
public void getClientIpReturnsExtractedIpForGRPCSocketAddress() { | ||
SocketAddress socketAddress = new SocketAddress() { | ||
@Override | ||
public String toString() { | ||
return "/192.168.1.1:1234"; | ||
} | ||
}; | ||
String result = FilterInterceptor.getClientIp(socketAddress); | ||
assertEquals("192.168.1.1", result); | ||
} | ||
|
||
@Test | ||
public void getClientIpReturnsExtractedFirstIpForGRPCSocketAddress() { | ||
SocketAddress socketAddress = new SocketAddress() { | ||
@Override | ||
public String toString() { | ||
return "192.168.1.1/192.168.1.2:1234"; | ||
} | ||
}; | ||
String result = FilterInterceptor.getClientIp(socketAddress); | ||
assertEquals("192.168.1.1", result); | ||
} | ||
|
||
|
||
} |