i2ptunnel: Check port mapper for webapp presence (ticket #2161)

This commit is contained in:
zzz
2018-03-14 14:09:41 +00:00
parent 66ee7b563a
commit 6eb09bd0c8
5 changed files with 82 additions and 12 deletions

View File

@ -773,19 +773,23 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
out.write(("</a></th>\n<th align=\"center\">" + out.write(("</a></th>\n<th align=\"center\">" +
"<a href=\"" + conflictURL + "\">").getBytes("UTF-8")); "<a href=\"" + conflictURL + "\">").getBytes("UTF-8"));
out.write(_t("Conflicting address helper destination").getBytes("UTF-8")); out.write(_t("Conflicting address helper destination").getBytes("UTF-8"));
out.write(("</a></th></tr>\n<tr><td align=\"center\">" + out.write(("</a></th></tr>\n").getBytes("UTF-8"));
if (_context.portMapper().getPort(PortMapper.SVC_IMAGEGEN) > 0) {
out.write(("<tr><td align=\"center\">" +
"<a href=\"" + trustedURL + "\">" + "<a href=\"" + trustedURL + "\">" +
"<img src=\"" + "<img src=\"" +
conURL + "imagegen/id?s=160&amp;c=" + conURL + "imagegen/id?s=160&amp;c=" +
h1.toBase64().replace("=", "%3d") + h1.toBase64().replace("=", "%3d") +
"\" width=\"160\" height=\"160\"></a>\n").getBytes("UTF-8")); "\" width=\"160\" height=\"160\"></a>\n" +
out.write(("</td>\n<td align=\"center\">" + "</td>\n<td align=\"center\">" +
"<a href=\"" + conflictURL + "\">" + "<a href=\"" + conflictURL + "\">" +
"<img src=\"" + "<img src=\"" +
conURL + "imagegen/id?s=160&amp;c=" + conURL + "imagegen/id?s=160&amp;c=" +
h2.toBase64().replace("=", "%3d") + h2.toBase64().replace("=", "%3d") +
"\" width=\"160\" height=\"160\"></a>\n").getBytes("UTF-8")); "\" width=\"160\" height=\"160\"></a>\n" +
out.write("</td></tr></table>".getBytes("UTF-8")); "</td></tr>").getBytes("UTF-8"));
}
out.write("</table>".getBytes("UTF-8"));
} }
out.write("</div>".getBytes("UTF-8")); out.write("</div>".getBytes("UTF-8"));
writeFooter(out); writeFooter(out);

View File

@ -7,9 +7,11 @@ import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.io.Writer; import java.io.Writer;
import java.net.Socket; import java.net.Socket;
@ -542,18 +544,48 @@ public abstract class I2PTunnelHTTPClientBase extends I2PTunnelClientBase implem
char[] buf = new char[512]; char[] buf = new char[512];
StringBuilder out = new StringBuilder(2048); StringBuilder out = new StringBuilder(2048);
try { try {
boolean hasSusiDNS = ctx.portMapper().getPort(PortMapper.SVC_SUSIDNS) > 0;
boolean hasI2PTunnel = ctx.portMapper().getPort(PortMapper.SVC_I2PTUNNEL) > 0;
if (hasSusiDNS && hasI2PTunnel) {
reader = new TranslateReader(ctx, BUNDLE_NAME, new FileInputStream(file));
} else {
// strip out the addressbook links
reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
int len;
while((len = reader.read(buf)) > 0) {
out.append(buf, 0, len);
}
reader.close();
if (!hasSusiDNS) {
DataHelper.replace(out, "<a href=\"http://127.0.0.1:7657/susidns/index\">_(\"Addressbook\")</a>", "");
}
if (!hasI2PTunnel) {
// there are also a couple in auth-header.ht that aren't worth stripping, for auth only
DataHelper.replace(out,
"<span class=\"script\">_(\"You may want to {0}retry{1} as this will randomly reselect an outproxy from the pool you have defined {2}here{3} (if you have more than one configured).\", \"<a href=\\\"javascript:parent.window.location.reload()\\\">\", \"</a>\", \"<a href=\\\"http://127.0.0.1:7657/i2ptunnel/index.jsp\\\">\", \"</a>\")</span>",
"");
DataHelper.replace(out,
"<noscript>_(\"You may want to retry as this will randomly reselect an outproxy from the pool you have defined {0}here{1} (if you have more than one configured).\", \"<a href=\\\"http://127.0.0.1:7657/i2ptunnel/index.jsp\\\">\", \"</a>\")</noscript>",
"");
DataHelper.replace(out,
"_(\"If you continue to have trouble you may want to edit your outproxy list {0}here{1}.\", \"<a href=\\\"http://127.0.0.1:7657/i2ptunnel/edit.jsp?tunnel=0\\\">\", \"</a>\")",
"");
}
String s = out.toString();
out.setLength(0);
reader = new TranslateReader(ctx, BUNDLE_NAME, new StringReader(s));
}
int len; int len;
reader = new TranslateReader(ctx, BUNDLE_NAME, new FileInputStream(file));
while((len = reader.read(buf)) > 0) { while((len = reader.read(buf)) > 0) {
out.append(buf, 0, len); out.append(buf, 0, len);
} }
String rv = out.toString();
// Do we need to replace http://127.0.0.1:7657 console links in the error page? // Do we need to replace http://127.0.0.1:7657 console links in the error page?
// Get the registered host and port from the PortMapper. // Get the registered host and port from the PortMapper.
String url = ctx.portMapper().getConsoleURL(); String url = ctx.portMapper().getConsoleURL();
if (!url.equals("http://127.0.0.1:7657/")) { if (!url.equals("http://127.0.0.1:7657/")) {
rv = rv.replace("http://127.0.0.1:7657/", url); DataHelper.replace(out, "http://127.0.0.1:7657/", url);
} }
String rv = out.toString();
return rv; return rv;
} finally { } finally {
try { try {

View File

@ -18,6 +18,7 @@ import net.i2p.data.DataHelper;
import net.i2p.data.Destination; import net.i2p.data.Destination;
import net.i2p.i2ptunnel.I2PTunnelHTTPClient; import net.i2p.i2ptunnel.I2PTunnelHTTPClient;
import net.i2p.util.FileUtil; import net.i2p.util.FileUtil;
import net.i2p.util.PortMapper;
import net.i2p.util.Translate; import net.i2p.util.Translate;
/** /**
@ -199,7 +200,8 @@ public abstract class LocalHTTPServer {
else else
tbook = book; tbook = book;
String conURL = I2PAppContext.getGlobalContext().portMapper().getConsoleURL(); PortMapper pm = I2PAppContext.getGlobalContext().portMapper();
String conURL = pm.getConsoleURL();
out.write(("HTTP/1.1 200 OK\r\n"+ out.write(("HTTP/1.1 200 OK\r\n"+
"Content-Type: text/html; charset=UTF-8\r\n"+ "Content-Type: text/html; charset=UTF-8\r\n"+
"Referrer-Policy: no-referrer\r\n"+ "Referrer-Policy: no-referrer\r\n"+
@ -214,8 +216,10 @@ public abstract class LocalHTTPServer {
"</head><body>\n" + "</head><body>\n" +
"<div class=logo>\n" + "<div class=logo>\n" +
"<a href=\"" + conURL + "\" title=\"" + _t("Router Console") + "\"><img src=\"http://proxy.i2p/themes/console/images/i2plogo.png\" alt=\"I2P Router Console\" border=\"0\"></a><hr>\n" + "<a href=\"" + conURL + "\" title=\"" + _t("Router Console") + "\"><img src=\"http://proxy.i2p/themes/console/images/i2plogo.png\" alt=\"I2P Router Console\" border=\"0\"></a><hr>\n" +
"<a href=\"" + conURL + "config\">" + _t("Configuration") + "</a> <a href=\"" + conURL + "help.jsp\">" + _t("Help") + "</a> <a href=\"" + conURL + "susidns/index\">" + _t("Addressbook") + "</a>\n" + "<a href=\"" + conURL + "config\">" + _t("Configuration") + "</a> <a href=\"" + conURL + "help.jsp\">" + _t("Help") + "</a>").getBytes("UTF-8"));
"</div>" + if (pm.getPort(PortMapper.SVC_SUSIDNS) > 0)
out.write((" <a href=\"" + conURL + "susidns/index\">" + _t("Addressbook") + "</a>\n").getBytes("UTF-8"));
out.write(("</div>" +
"<div class=warning id=warning>\n" + "<div class=warning id=warning>\n" +
"<h3>" + "<h3>" +
(success ? (success ?

View File

@ -217,6 +217,9 @@
****/ ****/
String b64 = editBean.getDestinationBase64(curTunnel); String b64 = editBean.getDestinationBase64(curTunnel);
net.i2p.util.PortMapper pm = net.i2p.I2PAppContext.getGlobalContext().portMapper();
boolean hasImagegen = pm.getPort(net.i2p.util.PortMapper.SVC_IMAGEGEN) > 0;
boolean hasSusiDNS = pm.getPort(net.i2p.util.PortMapper.SVC_SUSIDNS) > 0;
if (!"".equals(b64)) { if (!"".equals(b64)) {
%> %>
<tr> <tr>
@ -231,8 +234,18 @@
%> %>
<td class="buttons" colspan="2"> <td class="buttons" colspan="2">
<%
if (hasImagegen) {
%>
<a class="control" title="<%=intl._t("Generate a QR Code for this domain")%>" href="/imagegen/qr?s=320&amp;t=<%=name%>&amp;c=http%3a%2f%2f<%=name%>%2f%3fi2paddresshelper%3d<%=b64%>" target="_top"><%=intl._t("Generate QR Code")%></a> <a class="control" title="<%=intl._t("Generate a QR Code for this domain")%>" href="/imagegen/qr?s=320&amp;t=<%=name%>&amp;c=http%3a%2f%2f<%=name%>%2f%3fi2paddresshelper%3d<%=b64%>" target="_top"><%=intl._t("Generate QR Code")%></a>
<%
}
if (hasSusiDNS) {
%>
<a class="control" title="<%=intl._t("Add to Private addressbook")%>" href="/susidns/addressbook.jsp?book=private&amp;hostname=<%=name%>&amp;destination=<%=b64%>#add"><%=intl._t("Add to local addressbook")%></a> <a class="control" title="<%=intl._t("Add to Private addressbook")%>" href="/susidns/addressbook.jsp?book=private&amp;hostname=<%=name%>&amp;destination=<%=b64%>#add"><%=intl._t("Add to local addressbook")%></a>
<%
}
%>
<a class="control" title="<%=intl._t("Register, unregister or change details for hostname")%>" href="register?tunnel=<%=curTunnel%>"><%=intl._t("Registration Authentication")%></a> <a class="control" title="<%=intl._t("Register, unregister or change details for hostname")%>" href="register?tunnel=<%=curTunnel%>"><%=intl._t("Registration Authentication")%></a>
</td> </td>
<% <%
@ -887,9 +900,17 @@
<tr> <tr>
<td class="buttons" colspan="2"> <td class="buttons" colspan="2">
<%
if (hasImagegen) {
%>
<a class="control" title="<%=intl._t("Generate QR Code")%>" href="/imagegen/qr?s=320&amp;t=<%=name%>&amp;c=http%3a%2f%2f<%=name%>%2f%3fi2paddresshelper%3d<%=ab64%>" target="_top"><%=intl._t("Generate QR Code")%></a> <a class="control" title="<%=intl._t("Generate QR Code")%>" href="/imagegen/qr?s=320&amp;t=<%=name%>&amp;c=http%3a%2f%2f<%=name%>%2f%3fi2paddresshelper%3d<%=ab64%>" target="_top"><%=intl._t("Generate QR Code")%></a>
<%
}
if (hasSusiDNS) {
%>
<a class="control" title="<%=intl._t("Add to Private addressbook")%>" href="/susidns/addressbook.jsp?book=private&amp;hostname=<%=name%>&amp;destination=<%=ab64%>#add"><%=intl._t("Add to local addressbook")%></a> <a class="control" title="<%=intl._t("Add to Private addressbook")%>" href="/susidns/addressbook.jsp?book=private&amp;hostname=<%=name%>&amp;destination=<%=ab64%>#add"><%=intl._t("Add to local addressbook")%></a>
<% <%
}
} else { } else {
%> %>

View File

@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Reader;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -68,7 +69,15 @@ public class TranslateReader extends FilterReader {
* @param in UTF-8 * @param in UTF-8
*/ */
public TranslateReader(I2PAppContext ctx, String bundle, InputStream in) throws IOException { public TranslateReader(I2PAppContext ctx, String bundle, InputStream in) throws IOException {
super(new BufferedReader(new InputStreamReader(in, "UTF-8"))); this(ctx, bundle, new BufferedReader(new InputStreamReader(in, "UTF-8")));
}
/**
* @param bundle may be null for tagging only
* @since 0.9.34
*/
public TranslateReader(I2PAppContext ctx, String bundle, Reader in) throws IOException {
super(in);
_ctx = ctx; _ctx = ctx;
_bundle = bundle; _bundle = bundle;
_args = new ArrayList<String>(4); _args = new ArrayList<String>(4);