From d86ccded53a5b878c9f76f5c307bba85da82c029 Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Sun, 20 Dec 2020 13:50:54 -0500
Subject: [PATCH] Proxy: Move error page resources to jar

---
 apps/i2ptunnel/java/build.xml                 |  7 ++-
 .../i2ptunnel/I2PTunnelHTTPClientBase.java    | 16 ++++--
 .../localServer/LocalHTTPServer.java          | 56 +++++++++++--------
 .../i2ptunnel}/resources/proxy/README.txt     |  0
 .../proxy/ahelper-conflict-header.ht          |  0
 .../resources/proxy/ahelper-new-header.ht     |  0
 .../proxy/ahelper-notfound-header.ht          |  0
 .../i2ptunnel}/resources/proxy/auth-header.ht |  0
 .../resources/proxy/b32-auth-header.ht        |  0
 .../i2ptunnel}/resources/proxy/b32-header.ht  |  0
 .../resources/proxy/baduri-header.ht          |  0
 .../resources/proxy/denied-header.ht          |  0
 .../i2ptunnel}/resources/proxy/dnf-header.ht  |  0
 .../i2ptunnel}/resources/proxy/dnfb-header.ht |  0
 .../i2ptunnel}/resources/proxy/dnfh-header.ht |  0
 .../i2ptunnel}/resources/proxy/dnfp-header.ht |  0
 .../i2ptunnel}/resources/proxy/enc-header.ht  |  0
 .../i2ptunnel}/resources/proxy/encp-header.ht |  0
 .../resources/proxy/localhost-header.ht       |  0
 .../i2ptunnel}/resources/proxy/nols-header.ht |  0
 .../resources/proxy/nolsp-header.ht           |  0
 .../resources/proxy/noproxy-header.ht         |  0
 .../resources/proxy/protocol-header.ht        |  0
 .../resources/proxy/reset-header.ht           |  0
 .../resources/proxy/resetp-header.ht          |  0
 build.xml                                     |  6 +-
 26 files changed, 50 insertions(+), 35 deletions(-)
 rename {installer => apps/i2ptunnel}/resources/proxy/README.txt (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/ahelper-conflict-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/ahelper-new-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/ahelper-notfound-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/auth-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/b32-auth-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/b32-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/baduri-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/denied-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/dnf-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/dnfb-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/dnfh-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/dnfp-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/enc-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/encp-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/localhost-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/nols-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/nolsp-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/noproxy-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/protocol-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/reset-header.ht (100%)
 rename {installer => apps/i2ptunnel}/resources/proxy/resetp-header.ht (100%)

diff --git a/apps/i2ptunnel/java/build.xml b/apps/i2ptunnel/java/build.xml
index 09bb837f9e..d3e54fef19 100644
--- a/apps/i2ptunnel/java/build.xml
+++ b/apps/i2ptunnel/java/build.xml
@@ -86,7 +86,12 @@
     <target name="jar" depends="builddep, compile, bundle-proxy, jarUpToDate, listChangedFiles" unless="jar.uptodate" >
         <!-- set if unset -->
         <property name="workspace.changes.j.tr" value="" />
-        <jar destfile="./build/i2ptunnel.jar" basedir="./build/obj" includes="**/*.class" excludes="**/ui/*.class **/web/*.class" >
+        <mkdir dir="./build/obj/net/i2p/i2ptunnel/resources"/>
+        <copy todir="./build/obj/net/i2p/i2ptunnel/resources">
+            <fileset dir="../../routerconsole/jsp/" includes="themes/console/*/*.css themes/console/images/i2plogo.png themes/console/images/favicon.ico themes/console/images/itoopie_sm.png" />
+            <fileset dir="../resources" includes="**/*.ht" />
+        </copy>
+        <jar destfile="./build/i2ptunnel.jar" basedir="./build/obj" excludes="**/ui/*.class **/web/*.class" >
             <manifest>
                 <attribute name="Main-Class" value="net.i2p.i2ptunnel.I2PTunnel" />
                 <attribute name="${manifest.classpath.name}" value="i2p.jar mstreaming.jar" />
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java
index 20409b7ce6..adf1799cc2 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPClientBase.java
@@ -7,6 +7,7 @@ import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
@@ -40,6 +41,7 @@ import net.i2p.data.Base64;
 import net.i2p.data.DataHelper;
 import net.i2p.data.Destination;
 import net.i2p.data.i2cp.MessageStatusMessage;
+import net.i2p.i2ptunnel.localServer.LocalHTTPServer;
 import net.i2p.util.EepGet;
 import net.i2p.util.EventDispatcher;
 import net.i2p.util.InternalSocket;
@@ -670,8 +672,7 @@ public abstract class I2PTunnelHTTPClientBase extends I2PTunnelClientBase implem
      *  @since 0.9.4 moved from I2PTunnelHTTPClient
      */
     protected static String getErrorPage(I2PAppContext ctx, String base, String backup) {
-        File errorDir = new File(ctx.getBaseDir(), "docs");
-        File file = new File(errorDir, base + "-header.ht");
+        String file = "proxy/" + base + "-header.ht";
         try {
             return readFile(ctx, file);
         } catch(IOException ioe) {
@@ -683,20 +684,24 @@ public abstract class I2PTunnelHTTPClientBase extends I2PTunnelClientBase implem
     private static final String BUNDLE_NAME = "net.i2p.i2ptunnel.proxy.messages";
 
     /**
+     *  As of 0.9.49, loads the error pages from the jar, not the file system.
      *  @since 0.9.4 moved from I2PTunnelHTTPClient
      */
-    private static String readFile(I2PAppContext ctx, File file) throws IOException {
+    private static String readFile(I2PAppContext ctx, String file) throws IOException {
         Reader reader = null;
         char[] buf = new char[512];
         StringBuilder out = new StringBuilder(2048);
+        InputStream in = LocalHTTPServer.getResource(file);
+        if (in == null)
+            throw new IOException();
         try {
             boolean hasSusiDNS = ctx.portMapper().isRegistered(PortMapper.SVC_SUSIDNS);
             boolean hasI2PTunnel = ctx.portMapper().isRegistered(PortMapper.SVC_I2PTUNNEL);
             if (hasSusiDNS && hasI2PTunnel) {
-                reader = new TranslateReader(ctx, BUNDLE_NAME, new FileInputStream(file));
+                reader = new TranslateReader(ctx, BUNDLE_NAME, in);
             } else {
                 // strip out the addressbook links
-                reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
+                reader = new InputStreamReader(in, "UTF-8");
                 int len;
                 while((len = reader.read(buf)) > 0) {
                     out.append(buf, 0, len);
@@ -734,6 +739,7 @@ public abstract class I2PTunnelHTTPClientBase extends I2PTunnelClientBase implem
             String rv = out.toString();
             return rv;
         } finally {
+            try { in.close(); } catch (IOException ioe) {}
             try {
                 if(reader != null)
                     reader.close();
diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/localServer/LocalHTTPServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/localServer/LocalHTTPServer.java
index 26ae12ccd8..ab989c1015 100644
--- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/localServer/LocalHTTPServer.java
+++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/localServer/LocalHTTPServer.java
@@ -5,6 +5,7 @@ package net.i2p.i2ptunnel.localServer;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.Map;
@@ -124,32 +125,30 @@ public abstract class LocalHTTPServer {
         if ((method.equals("GET") || method.equals("HEAD")) &&
             targetRequest.startsWith("/themes/") &&
             !targetRequest.contains("..")) {
-            String filename = null;
-            try {
-                filename = targetRequest.substring(8); // "/themes/".length
-            } catch (IndexOutOfBoundsException ioobe) {
-                 return;
-            }
+            String filename = targetRequest.substring(1);
             // theme hack
-            if (filename.startsWith("console/default/"))
+            if (filename.startsWith("themes/console/default/"))
                 filename = filename.replaceFirst("default", context.getProperty("routerconsole.theme", "light"));
-            File themesDir = new File(context.getBaseDir(), "docs/themes");
-            File file = new File(themesDir, filename);
-            if (file.exists() && !file.isDirectory()) {
-                String type;
-                if (filename.endsWith(".css"))
-                    type = "text/css";
-                else if (filename.endsWith(".ico"))
-                    type = "image/x-icon";
-                else if (filename.endsWith(".png"))
-                    type = "image/png";
-                else if (filename.endsWith(".jpg"))
-                    type = "image/jpeg";
-                else type = "text/html";
-                out.write("HTTP/1.1 200 OK\r\nContent-Type: ".getBytes("UTF-8"));
-                out.write(type.getBytes("UTF-8"));
-                out.write("\r\nCache-Control: max-age=86400\r\nConnection: close\r\nProxy-Connection: close\r\n\r\n".getBytes("UTF-8"));
-                FileUtil.readFile(filename, themesDir.getAbsolutePath(), out);
+            InputStream in = getResource(filename);
+            if (in != null) {
+                try {
+                    String type;
+                    if (filename.endsWith(".css"))
+                        type = "text/css";
+                    else if (filename.endsWith(".ico"))
+                        type = "image/x-icon";
+                    else if (filename.endsWith(".png"))
+                        type = "image/png";
+                    else if (filename.endsWith(".jpg"))
+                        type = "image/jpeg";
+                    else type = "text/html";
+                    out.write("HTTP/1.1 200 OK\r\nContent-Type: ".getBytes("UTF-8"));
+                    out.write(type.getBytes("UTF-8"));
+                    out.write("\r\nCache-Control: max-age=86400\r\nConnection: close\r\nProxy-Connection: close\r\n\r\n".getBytes("UTF-8"));
+                    DataHelper.copy(in, out);
+                } finally {
+                    try { in.close(); } catch (IOException ioe) {}
+                }
                 return;
             }
         }
@@ -483,6 +482,15 @@ public abstract class LocalHTTPServer {
         return buf.toString();
     }
 
+    /**
+     *  @param resource relative path
+     *  @return stream or null if not found
+     *  @since 0.9.49
+     */
+    public static InputStream getResource(String resource) {
+            return LocalHTTPServer.class.getResourceAsStream("/net/i2p/i2ptunnel/resources/" + resource);
+    }
+
     /** these strings go in the jar, not the war */
     private static final String BUNDLE_NAME = "net.i2p.i2ptunnel.proxy.messages";
 
diff --git a/installer/resources/proxy/README.txt b/apps/i2ptunnel/resources/proxy/README.txt
similarity index 100%
rename from installer/resources/proxy/README.txt
rename to apps/i2ptunnel/resources/proxy/README.txt
diff --git a/installer/resources/proxy/ahelper-conflict-header.ht b/apps/i2ptunnel/resources/proxy/ahelper-conflict-header.ht
similarity index 100%
rename from installer/resources/proxy/ahelper-conflict-header.ht
rename to apps/i2ptunnel/resources/proxy/ahelper-conflict-header.ht
diff --git a/installer/resources/proxy/ahelper-new-header.ht b/apps/i2ptunnel/resources/proxy/ahelper-new-header.ht
similarity index 100%
rename from installer/resources/proxy/ahelper-new-header.ht
rename to apps/i2ptunnel/resources/proxy/ahelper-new-header.ht
diff --git a/installer/resources/proxy/ahelper-notfound-header.ht b/apps/i2ptunnel/resources/proxy/ahelper-notfound-header.ht
similarity index 100%
rename from installer/resources/proxy/ahelper-notfound-header.ht
rename to apps/i2ptunnel/resources/proxy/ahelper-notfound-header.ht
diff --git a/installer/resources/proxy/auth-header.ht b/apps/i2ptunnel/resources/proxy/auth-header.ht
similarity index 100%
rename from installer/resources/proxy/auth-header.ht
rename to apps/i2ptunnel/resources/proxy/auth-header.ht
diff --git a/installer/resources/proxy/b32-auth-header.ht b/apps/i2ptunnel/resources/proxy/b32-auth-header.ht
similarity index 100%
rename from installer/resources/proxy/b32-auth-header.ht
rename to apps/i2ptunnel/resources/proxy/b32-auth-header.ht
diff --git a/installer/resources/proxy/b32-header.ht b/apps/i2ptunnel/resources/proxy/b32-header.ht
similarity index 100%
rename from installer/resources/proxy/b32-header.ht
rename to apps/i2ptunnel/resources/proxy/b32-header.ht
diff --git a/installer/resources/proxy/baduri-header.ht b/apps/i2ptunnel/resources/proxy/baduri-header.ht
similarity index 100%
rename from installer/resources/proxy/baduri-header.ht
rename to apps/i2ptunnel/resources/proxy/baduri-header.ht
diff --git a/installer/resources/proxy/denied-header.ht b/apps/i2ptunnel/resources/proxy/denied-header.ht
similarity index 100%
rename from installer/resources/proxy/denied-header.ht
rename to apps/i2ptunnel/resources/proxy/denied-header.ht
diff --git a/installer/resources/proxy/dnf-header.ht b/apps/i2ptunnel/resources/proxy/dnf-header.ht
similarity index 100%
rename from installer/resources/proxy/dnf-header.ht
rename to apps/i2ptunnel/resources/proxy/dnf-header.ht
diff --git a/installer/resources/proxy/dnfb-header.ht b/apps/i2ptunnel/resources/proxy/dnfb-header.ht
similarity index 100%
rename from installer/resources/proxy/dnfb-header.ht
rename to apps/i2ptunnel/resources/proxy/dnfb-header.ht
diff --git a/installer/resources/proxy/dnfh-header.ht b/apps/i2ptunnel/resources/proxy/dnfh-header.ht
similarity index 100%
rename from installer/resources/proxy/dnfh-header.ht
rename to apps/i2ptunnel/resources/proxy/dnfh-header.ht
diff --git a/installer/resources/proxy/dnfp-header.ht b/apps/i2ptunnel/resources/proxy/dnfp-header.ht
similarity index 100%
rename from installer/resources/proxy/dnfp-header.ht
rename to apps/i2ptunnel/resources/proxy/dnfp-header.ht
diff --git a/installer/resources/proxy/enc-header.ht b/apps/i2ptunnel/resources/proxy/enc-header.ht
similarity index 100%
rename from installer/resources/proxy/enc-header.ht
rename to apps/i2ptunnel/resources/proxy/enc-header.ht
diff --git a/installer/resources/proxy/encp-header.ht b/apps/i2ptunnel/resources/proxy/encp-header.ht
similarity index 100%
rename from installer/resources/proxy/encp-header.ht
rename to apps/i2ptunnel/resources/proxy/encp-header.ht
diff --git a/installer/resources/proxy/localhost-header.ht b/apps/i2ptunnel/resources/proxy/localhost-header.ht
similarity index 100%
rename from installer/resources/proxy/localhost-header.ht
rename to apps/i2ptunnel/resources/proxy/localhost-header.ht
diff --git a/installer/resources/proxy/nols-header.ht b/apps/i2ptunnel/resources/proxy/nols-header.ht
similarity index 100%
rename from installer/resources/proxy/nols-header.ht
rename to apps/i2ptunnel/resources/proxy/nols-header.ht
diff --git a/installer/resources/proxy/nolsp-header.ht b/apps/i2ptunnel/resources/proxy/nolsp-header.ht
similarity index 100%
rename from installer/resources/proxy/nolsp-header.ht
rename to apps/i2ptunnel/resources/proxy/nolsp-header.ht
diff --git a/installer/resources/proxy/noproxy-header.ht b/apps/i2ptunnel/resources/proxy/noproxy-header.ht
similarity index 100%
rename from installer/resources/proxy/noproxy-header.ht
rename to apps/i2ptunnel/resources/proxy/noproxy-header.ht
diff --git a/installer/resources/proxy/protocol-header.ht b/apps/i2ptunnel/resources/proxy/protocol-header.ht
similarity index 100%
rename from installer/resources/proxy/protocol-header.ht
rename to apps/i2ptunnel/resources/proxy/protocol-header.ht
diff --git a/installer/resources/proxy/reset-header.ht b/apps/i2ptunnel/resources/proxy/reset-header.ht
similarity index 100%
rename from installer/resources/proxy/reset-header.ht
rename to apps/i2ptunnel/resources/proxy/reset-header.ht
diff --git a/installer/resources/proxy/resetp-header.ht b/apps/i2ptunnel/resources/proxy/resetp-header.ht
similarity index 100%
rename from installer/resources/proxy/resetp-header.ht
rename to apps/i2ptunnel/resources/proxy/resetp-header.ht
diff --git a/build.xml b/build.xml
index dd24907b97..86d19bfb14 100644
--- a/build.xml
+++ b/build.xml
@@ -1659,11 +1659,10 @@
         <fixcrlf srcdir="pkg-temp/docs" includes="*.ht" encoding="utf8" eol="crlf" />
     </target>
 
-    <!-- readme and proxy error page files -->
+    <!-- readme files -->
     <target name="prepConsoleDocUpdates">
         <copy todir="pkg-temp/docs/" >
           <fileset dir="installer/resources/readme/" includes="readme*.html" />
-          <fileset dir="installer/resources/proxy/" includes="*.ht" />
           <!--
                As of 0.9.36:
                All new and changed flags must go in the flags16x11/ dir,
@@ -3101,9 +3100,6 @@
 			<copy file="installer/resources/console.ico" todir="pkg-temp/docs/" />
 			<!-- HTTP Header files, english only,
 				if you need a different lang do it in a seperate target -->
-			<copy todir="pkg-temp/docs/" >
-				<fileset dir="installer/resources/proxy/"  includes="**-header.ht" />
-			</copy>
 			<!-- Theme light only -->
 			<copy todir="pkg-temp/docs/themes/console/light/" overwrite="true" >
 				<fileset dir="installer/resources/themes/console/light/" includes="**.css" />
-- 
GitLab