From acec9b5275d1a0a64a19423ea3cc64237af28476 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sun, 15 Jan 2023 10:37:55 -0500
Subject: [PATCH] i2ptunnel: Return 503 from HTTP client on failure to build
 tunnels

previously just closed the socket
---
 .../i2p/i2ptunnel/I2PTunnelHTTPClient.java    | 20 ++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java
index 7f77c77cac..6e0fe36cda 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClient.java
@@ -270,6 +270,8 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
      * Warning, this does not make a copy of I2PTunnel's client options,
      * it modifies them directly.
      * unused?
+     *
+     * This will throw IAE on tunnel build failure
      */
     @Override
     protected I2PSocketOptions getDefaultOptions() {
@@ -293,6 +295,8 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
      * Warning, this does not make a copy of I2PTunnel's client options,
      * it modifies them directly.
      * Do not use overrides for per-socket options.
+     *
+     * This will throw IAE on tunnel build failure
      */
     @Override
     protected I2PSocketOptions getDefaultOptions(Properties overrides) {
@@ -1379,7 +1383,21 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
             // 1 == disconnect.  see ConnectionOptions in the new streaming lib, which i
             // dont want to hard link to here
             //opts.setProperty("i2p.streaming.inactivityTimeoutAction", ""+1);
-            I2PSocketOptions sktOpts = getDefaultOptions(opts);
+            I2PSocketOptions sktOpts;
+            try {
+                sktOpts = getDefaultOptions(opts);
+            } catch (RuntimeException re) {
+                // tunnel build failure
+                StringBuilder buf = new StringBuilder(128);
+                buf.append("HTTP/1.1 503 Service Unavailable");
+                if (re.getMessage() != null)
+                    buf.append(" - ").append(re.getMessage());
+                buf.append("\r\n\r\n");
+                try {
+                    out.write(buf.toString().getBytes("UTF-8"));
+                } catch (IOException ioe) {}
+                throw re;
+            }
             if (remotePort > 0)
                 sktOpts.setPort(remotePort);
             i2ps = createI2PSocket(clientDest, sktOpts);
-- 
GitLab