Skip to content

Commit 5de140c

Browse files
committed
Merge pull request #86 from GoogleCloudPlatform/fix-ip
Fix issue with truncating recorded ip addresses in datastore
2 parents 486352c + 5e5ffa6 commit 5de140c

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
5151
String userIp = req.getRemoteAddr();
5252
InetAddress address = InetAddress.getByName(userIp);
5353
if (address instanceof Inet6Address) {
54-
userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*";
54+
// nest indexOf calls to find the second occurrence of a character in a string
55+
// an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
56+
userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*";
5557
} else if (address instanceof Inet4Address) {
56-
userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*";
58+
userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*";
5759
}
5860

5961
Datastore datastore = DatastoreOptions.defaultInstance().service();

0 commit comments

Comments
 (0)