Skip to content

Commit 6e2cc56

Browse files
author
Shun Fan
committed
Fix issue with truncating recorded ip addresses
1 parent ba9555d commit 6e2cc56

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

managed_vms/cloudsql/src/main/java/com/example/managedvms/cloudsql/CloudSqlServlet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
4747
String userIp = req.getRemoteAddr();
4848
InetAddress address = InetAddress.getByName(userIp);
4949
if (address instanceof Inet6Address) {
50-
userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*";
50+
// nest indexOf calls to find the second occurrence of a character in a string
51+
// an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
52+
userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*";
5153
} else if (address instanceof Inet4Address) {
52-
userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*";
54+
userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*";
5355
}
5456

5557
final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL "

0 commit comments

Comments
 (0)