From 6d2270a1edb4e6fa9e23c09f075a7e0dc1f643bd Mon Sep 17 00:00:00 2001
From: zab2 <zab2@mail.i2p>
Date: Thu, 28 Mar 2019 15:32:49 +0000
Subject: [PATCH] Cancel timers if tunnel is stopped

---
 .../java/src/net/i2p/i2ptunnel/I2PTunnelServer.java    |  2 +-
 .../src/net/i2p/i2ptunnel/access/AccessFilter.java     | 10 +++++++++-
 .../src/net/i2p/i2ptunnel/access/FilterFactory.java    |  7 +++++--
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java
index 6a7d9b86c7..ecbc9af606 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelServer.java
@@ -228,7 +228,7 @@ public class I2PTunnelServer extends I2PTunnelTask implements Runnable {
             File filterDefinition = new File(getTunnel().filterDefinition);
             I2PAppContext context = getTunnel().getContext();
             try {
-                filter = FilterFactory.createFilter(context, filterDefinition);
+                filter = FilterFactory.createFilter(context, filterDefinition, this);
             } catch (IOException | InvalidDefinitionException bad) {
                 throw new IllegalArgumentException("Can't create socket manager", bad);
             }
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/AccessFilter.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/AccessFilter.java
index f44e81713b..72e1e3be78 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/AccessFilter.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/AccessFilter.java
@@ -17,6 +17,7 @@ import net.i2p.I2PAppContext;
 import net.i2p.util.SimpleTimer2;
 import net.i2p.util.Log;
 import net.i2p.data.Destination;
+import net.i2p.i2ptunnel.I2PTunnelTask;
 import net.i2p.client.streaming.IncomingConnectionFilter;
 
 class AccessFilter implements IncomingConnectionFilter {
@@ -26,6 +27,7 @@ class AccessFilter implements IncomingConnectionFilter {
 
     private final FilterDefinition definition;
     private final I2PAppContext context;
+    private final I2PTunnelTask task;
 
     /**
      * Trackers for known destinations defined in access lists
@@ -36,9 +38,11 @@ class AccessFilter implements IncomingConnectionFilter {
      */
     private final Map<String, DestTracker> unknownDests = new HashMap<String, DestTracker>();
 
-    AccessFilter(I2PAppContext context, FilterDefinition definition) throws IOException {
+    AccessFilter(I2PAppContext context, FilterDefinition definition, I2PTunnelTask task) 
+            throws IOException {
         this.context = context;
         this.definition = definition;
+        this.task = task;
 
         reload();
 
@@ -139,6 +143,8 @@ class AccessFilter implements IncomingConnectionFilter {
             super(context.simpleTimer2(), PURGE_INTERVAL);
         }
         public void timeReached() {
+            if (!task.isOpen())
+                return;
             purge();
             schedule(PURGE_INTERVAL);
         }
@@ -149,6 +155,8 @@ class AccessFilter implements IncomingConnectionFilter {
             super(context.simpleTimer2(), SYNC_INTERVAL);
         }
         public void timeReached() {
+            if (!task.isOpen())
+                return;
             try {
                 record();
                 reload();
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/FilterFactory.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/FilterFactory.java
index 0571e96d05..4333a529a9 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/FilterFactory.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/access/FilterFactory.java
@@ -9,10 +9,13 @@ import java.util.List;
 import java.util.ArrayList;
 
 import net.i2p.I2PAppContext;
+import net.i2p.i2ptunnel.I2PTunnelTask;
 import net.i2p.client.streaming.IncomingConnectionFilter;
 
 public class FilterFactory {
-    public static IncomingConnectionFilter createFilter(I2PAppContext context, File definition) 
+    public static IncomingConnectionFilter createFilter(I2PAppContext context, 
+                                                        File definition,
+                                                        I2PTunnelTask task)
         throws IOException, InvalidDefinitionException {
         List<String> linesList = new ArrayList<String>();
 
@@ -33,6 +36,6 @@ public class FilterFactory {
         }
 
         FilterDefinition parsedDefinition = DefinitionParser.parse(linesList.toArray(new String[0]));
-        return new AccessFilter(context, parsedDefinition);
+        return new AccessFilter(context, parsedDefinition, task);
     }
 }
-- 
GitLab