aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.gradle1
-rw-r--r--src/main/java/org/traccar/config/Keys.java7
-rw-r--r--src/main/java/org/traccar/web/ThrottlingFilter.java53
-rw-r--r--src/main/java/org/traccar/web/WebModule.java1
4 files changed, 62 insertions, 0 deletions
diff --git a/build.gradle b/build.gradle
index 8c196043a..8ce916432 100644
--- a/build.gradle
+++ b/build.gradle
@@ -54,6 +54,7 @@ dependencies {
implementation "org.glassfish:javax.json:1.1.4"
implementation "org.eclipse.jetty:jetty-server:$jettyVersion"
implementation "org.eclipse.jetty:jetty-servlet:$jettyVersion"
+ implementation "org.eclipse.jetty:jetty-servlets:$jettyVersion"
implementation "org.eclipse.jetty:jetty-webapp:$jettyVersion"
implementation "org.eclipse.jetty:jetty-jndi:$jettyVersion"
implementation "org.eclipse.jetty:jetty-proxy:$jettyVersion"
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java
index 3f52fbd96..8e11b4013 100644
--- a/src/main/java/org/traccar/config/Keys.java
+++ b/src/main/java/org/traccar/config/Keys.java
@@ -629,6 +629,13 @@ public final class Keys {
List.of(KeyType.CONFIG));
/**
+ * Maximum API requests per second. Above this limit requests and delayed and throttled.
+ */
+ public static final ConfigKey<Integer> WEB_MAX_REQUESTS_PER_SECOND = new IntegerConfigKey(
+ "web.maxRequestsPerSec",
+ List.of(KeyType.CONFIG));
+
+ /**
* Sanitize all strings returned via API. This is needed to fix XSS issues in the old web interface. New React-based
* interface doesn't require this.
*/
diff --git a/src/main/java/org/traccar/web/ThrottlingFilter.java b/src/main/java/org/traccar/web/ThrottlingFilter.java
new file mode 100644
index 000000000..054af652f
--- /dev/null
+++ b/src/main/java/org/traccar/web/ThrottlingFilter.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2022 Anton Tananaev (anton@traccar.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.web;
+
+import org.eclipse.jetty.servlets.DoSFilter;
+import org.traccar.config.Config;
+import org.traccar.config.Keys;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+@Singleton
+public class ThrottlingFilter extends DoSFilter {
+
+ @Inject
+ private Config config;
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+ super.init(filterConfig);
+ if (config.hasKey(Keys.WEB_MAX_REQUESTS_PER_SECOND)) {
+ setMaxRequestsPerSec(config.getInteger(Keys.WEB_MAX_REQUESTS_PER_SECOND));
+ }
+ }
+
+ @Override
+ protected String extractUserId(ServletRequest request) {
+ HttpSession session = ((HttpServletRequest) request).getSession(false);
+ if (session != null) {
+ var userId = session.getAttribute("userId");
+ return userId != null ? userId.toString() : null;
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/org/traccar/web/WebModule.java b/src/main/java/org/traccar/web/WebModule.java
index 6c133ff74..0722c5d1e 100644
--- a/src/main/java/org/traccar/web/WebModule.java
+++ b/src/main/java/org/traccar/web/WebModule.java
@@ -23,6 +23,7 @@ public class WebModule extends ServletModule {
@Override
protected void configureServlets() {
+ filter("/api/*").through(ThrottlingFilter.class);
filter("/api/media/*").through(MediaFilter.class);
serve("/api/socket").with(AsyncSocketServlet.class);
}