From e475c161cb9d822518d90cce5a7dc4350d55aded Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Fri, 24 Apr 2015 21:16:45 +0000
Subject: [PATCH] i2ptunnel: Don't register port mapper in clients overriding
 startRunning() if super.startRunning() failed Register as HTTPS proxy also in
 HTTP client

---
 .../net/i2p/i2ptunnel/I2PTunnelClientBase.java  |  5 ++++-
 .../i2p/i2ptunnel/I2PTunnelConnectClient.java   |  3 ++-
 .../net/i2p/i2ptunnel/I2PTunnelHTTPClient.java  | 17 +++++++++++++----
 .../net/i2p/i2ptunnel/I2PTunnelIRCClient.java   |  3 ++-
 4 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java
index 3f10ff7bd0..335833276a 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelClientBase.java
@@ -53,7 +53,7 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna
     protected boolean _ownDest;
 
     protected Destination dest;
-    private int localPort;
+    private volatile int localPort;
     private final String _handlerName;
 
     /**
@@ -477,6 +477,9 @@ public abstract class I2PTunnelClientBase extends I2PTunnelTask implements Runna
      * This will be fast if i2cp.delayOpen is true, but could take
      * a LONG TIME if it is false, as it connects to the router and builds tunnels.
      *
+     * Extending classes must check the value of boolean open after calling
+     * super.startRunning(), if false then something went wrong.
+     *
      */
     public void startRunning() {
         boolean openNow = !Boolean.parseBoolean(getTunnel().getClientOptions().getProperty("i2cp.delayOpen"));
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java
index 8763218662..29fca69692 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelConnectClient.java
@@ -120,7 +120,8 @@ public class I2PTunnelConnectClient extends I2PTunnelHTTPClientBase implements R
     @Override
     public void startRunning() {
         super.startRunning();
-        _context.portMapper().register(PortMapper.SVC_HTTPS_PROXY, getLocalPort());
+        if (open)
+            _context.portMapper().register(PortMapper.SVC_HTTPS_PROXY, getLocalPort());
     }
 
     @Override
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java
index 372648b4ee..8c865ea856 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java
@@ -293,9 +293,13 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
         _context.statManager().createRateStat("i2ptunnel.httpCompressed", "compressed size transferred", "I2PTunnel", new long[] { 60*60*1000 });
         _context.statManager().createRateStat("i2ptunnel.httpExpanded", "size transferred after expansion", "I2PTunnel", new long[] { 60*60*1000 });
         super.startRunning();
-        this.isr = new InternalSocketRunner(this);
-        this.isr.start();
-        _context.portMapper().register(PortMapper.SVC_HTTP_PROXY, getLocalPort());
+        if (open) {
+            this.isr = new InternalSocketRunner(this);
+            this.isr.start();
+            int port = getLocalPort();
+            _context.portMapper().register(PortMapper.SVC_HTTP_PROXY, port);
+            _context.portMapper().register(PortMapper.SVC_HTTPS_PROXY, port);
+        }
     }
 
     /**
@@ -303,10 +307,15 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
      */
     @Override
     public boolean close(boolean forced) {
+        int port = getLocalPort();
         int reg = _context.portMapper().getPort(PortMapper.SVC_HTTP_PROXY);
-        if(reg == getLocalPort()) {
+        if (reg == port) {
             _context.portMapper().unregister(PortMapper.SVC_HTTP_PROXY);
         }
+        reg = _context.portMapper().getPort(PortMapper.SVC_HTTPS_PROXY);
+        if (reg == port) {
+            _context.portMapper().unregister(PortMapper.SVC_HTTPS_PROXY);
+        }
         boolean rv = super.close(forced);
         if(this.isr != null) {
             this.isr.stopRunning();
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java
index e8fac6812b..7f4c2c22ac 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelIRCClient.java
@@ -198,7 +198,8 @@ public class I2PTunnelIRCClient extends I2PTunnelClientBase {
     @Override
     public void startRunning() {
         super.startRunning();
-        _context.portMapper().register(PortMapper.SVC_IRC, getLocalPort());
+        if (open)
+            _context.portMapper().register(PortMapper.SVC_IRC, getLocalPort());
     }
 
     @Override
-- 
GitLab