aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/helper/IpRetriever.java
diff options
context:
space:
mode:
authordeveloperKurt <mustafakurt.business@gmail.com>2020-01-01 13:42:05 +0300
committerdeveloperKurt <mustafakurt.business@gmail.com>2020-01-01 13:42:05 +0300
commitfcad646a7c912463c53de946b2586e4a49a6e1ff (patch)
tree665e5f82a531d19ed2f762bb451157326f3a0e9c /src/main/java/org/traccar/helper/IpRetriever.java
parent1ca6c8561f1aa21d5f73030bcd0ab3e9c578a9f1 (diff)
downloadtrackermap-server-fcad646a7c912463c53de946b2586e4a49a6e1ff.tar.gz
trackermap-server-fcad646a7c912463c53de946b2586e4a49a6e1ff.tar.bz2
trackermap-server-fcad646a7c912463c53de946b2586e4a49a6e1ff.zip
As requested in issue #4393, added logging IP address functionality when login attempts fail.
Diffstat (limited to 'src/main/java/org/traccar/helper/IpRetriever.java')
-rw-r--r--src/main/java/org/traccar/helper/IpRetriever.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/helper/IpRetriever.java b/src/main/java/org/traccar/helper/IpRetriever.java
new file mode 100644
index 000000000..37a1e8662
--- /dev/null
+++ b/src/main/java/org/traccar/helper/IpRetriever.java
@@ -0,0 +1,48 @@
+package org.traccar.helper;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * Gets the client's IP address regardless of whether the server is behind a proxy server or a load balancer.
+ *
+ */
+public final class IpRetriever
+ {
+
+
+ /**
+ * Retrieves the client's IP address.
+ * Handles the cases like whether the server is behind a proxy server or a load balancer
+ * also if the request is being made by using a reverse proxy.
+ *
+ * @param request {@link HttpServletRequest} instance
+ * @return client's IP address
+ */
+ public static String retrieveIP(HttpServletRequest request) {
+
+ if(request != null){
+ String ipAddress = request.getHeader("X-FORWARDED-FOR");
+
+ if (ipAddress != null && !ipAddress.isEmpty()) {
+ return removeUnwantedData(ipAddress);
+ }
+ else{
+ ipAddress = request.getRemoteAddr();
+ return ipAddress;
+ }
+
+ } else return null;
+
+ }
+
+ /**
+ * If the request uses a reverse proxy, the header value will also contain load balancer and reverse proxy server IPs
+ * This method gets rid of them.
+ *
+ * @param ipAddress IP address value from the header
+ * @return IP address of the client
+ */
+ private static String removeUnwantedData(String ipAddress){
+ return ipAddress.contains(",") ? ipAddress.split(",")[0] : ipAddress;
+ }
+}