This time, only attempt to set the x-i2p-location header if the request comes in from a non-I2P domain. This should prevent it from unduly impacting the speed of those responses. If it never gets a request from a non-i2p domain, it never attempts to load the keys and therefore doesn't block. Style changes, camelcase fixes.

This commit is contained in:
idk
2021-06-19 23:40:27 -04:00
parent bfa17c53ec
commit cfdb0d411d
2 changed files with 24 additions and 103 deletions

View File

@ -26,22 +26,22 @@ import net.i2p.util.Log;
* @since 0.9.50
*/
public class XI2PLocationFilter extends HandlerWrapper {
private String X_I2P_LOCATION = null;
private String X_I2P_Location = null;
Log _log = I2PAppContext.getGlobalContext().logManager().getLog(XI2PLocationFilter.class);
private synchronized void setLocation(String xi2plocation) {
if (_log.shouldInfo())
_log.info("Checking X-I2P-LOCATION header prefix" + xi2plocation);
if (X_I2P_LOCATION != null)
_log.info("Checking X-I2P-Location header prefix" + xi2plocation);
if (X_I2P_Location != null)
return ;
if (xi2plocation == null)
return ;
if (xi2plocation.equals(""))
return ;
X_I2P_LOCATION = xi2plocation;
X_I2P_Location = xi2plocation;
if (_log.shouldInfo())
_log.info("Caching X-I2P-LOCATION header prefix" + X_I2P_LOCATION);
_log.info("Caching X-I2P-Location header prefix" + X_I2P_Location);
}
public synchronized String getXI2PLocation(String host, String port) {
@ -78,22 +78,22 @@ public class XI2PLocationFilter extends HandlerWrapper {
return rv.toBase32();
} catch (I2PException e) {
if (_log.shouldWarn())
_log.warn("I2PException Unable to set X-I2P-LOCATION, keys arent ready. This is probably safe to ignore and will go away after the first run." + e);
_log.warn("I2PException Unable to set X-I2P-Location, keys arent ready. This is probably safe to ignore and will go away after the first run." + e);
return null;
} catch (IOException e) {
if (_log.shouldWarn())
_log.warn("IOE Unable to set X-I2P-LOCATION, location is uninitialized due file not found. This probably means the keys aren't ready. This is probably safe to ignore." + e);
_log.warn("IOE Unable to set X-I2P-Location, location is uninitialized due file not found. This probably means the keys aren't ready. This is probably safe to ignore." + e);
return null;
}
}
}
if (_log.shouldWarn())
_log.warn("Unable to set X-I2P-LOCATION, location is target not found in any I2PTunnel config file. This should never happen.");
_log.warn("Unable to set X-I2P-Location, location is target not found in any I2PTunnel config file. This should never happen.");
return null;
}
} catch (IOException ioe) {
if (_log.shouldWarn())
_log.warn("IOE Unable to set X-I2P-LOCATION, location is uninitialized. This is probably safe to ignore. location='" + ioe + "'");
_log.warn("IOE Unable to set X-I2P-Location, location is uninitialized. This is probably safe to ignore. location='" + ioe + "'");
return null;
}
}
@ -107,27 +107,24 @@ public class XI2PLocationFilter extends HandlerWrapper {
public void handle(final String target, final Request request, final HttpServletRequest httpRequest, HttpServletResponse httpResponse)
throws IOException,
ServletException {
final String requestUri = httpRequest.getRequestURL().toString();
URL url = new URL(requestUri);
String domain = url.getHost();
if (domain != null) {
if (!domain.endsWith(".i2p")) {
// FIXME sync
if (X_I2P_LOCATION == null) {
String xi2plocation = getXI2PLocation(request.getLocalAddr(), String.valueOf(request.getLocalPort()));
if (_log.shouldInfo())
_log.info("Checking X-I2P-LOCATION header IP " + request.getLocalAddr() + " port " + request.getLocalPort() + " prefix " + xi2plocation);
setLocation(xi2plocation);
}
if (X_I2P_LOCATION != null) {
if (_log.shouldInfo())
_log.info("Checking X-I2P-LOCATION header prefix" + X_I2P_LOCATION);
final String requestUri = httpRequest.getRequestURL().toString();
URL url = new URL(requestUri);
String domain = url.getHost();
if (domain != null) {
if (!domain.endsWith(".i2p")) {
if (X_I2P_Location == null) {
String xi2plocation = getXI2PLocation(request.getLocalAddr(), String.valueOf(request.getLocalPort()));
if (_log.shouldInfo())
_log.info("Checking X-I2P-Location header IP " + request.getLocalAddr() + " port " + request.getLocalPort() + " prefix " + xi2plocation);
setLocation(xi2plocation);
} else if (X_I2P_Location != null) {
if (_log.shouldInfo())
_log.info("Checking X-I2P-Location header prefix" + X_I2P_Location);
String query = url.getQuery();
if (query == null)
query = "";
httpResponse.addHeader("X-I2P-LOCATION", X_I2P_LOCATION+url.getPath()+query);
query = "";
httpResponse.addHeader("X-I2P-Location", X_I2P_Location+url.getPath()+query);
}
}
}

View File

@ -1,76 +0,0 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<!--
Configure a custom context for the eepsite.
This context contains only a ServletContextHandler with a default servlet
to serve static html files and images.
-->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<New id="XI2PFilterHandler" class="org.eclipse.jetty.servlet.ServletContextHandler">
<Set name="contextPath">/</Set>
<Set name="resourceBase">./eepsite/docroot/</Set>
<!--<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.Default.cacheControl</Arg>
<Arg>max-age=3600,public</Arg>
</Call>-->
<!-- change to false to disallow directory listings -->
<!--<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.Default.dirAllowed</Arg>
<Arg>true</Arg>
</Call>
<Call name="setMimeTypes">
<Arg>
<New class="org.eclipse.jetty.http.MimeTypes">
<Call name="addMimeMapping">
<Arg>sud</Arg>
<Arg>application/zip</Arg>
</Call>
<Call name="addMimeMapping">
<Arg>su2</Arg>
<Arg>application/zip</Arg>
</Call>
<Call name="addMimeMapping">
<Arg>xpi2p</Arg>
<Arg>application/zip</Arg>
</Call>
</New>
</Arg>
</Call>-->
<Call name="addServlet">
<Arg>net.i2p.servlet.I2PDefaultServlet</Arg>
<Arg>/</Arg>
</Call>
<Call name="addFilter">
<Arg>
<New class="org.eclipse.jetty.servlet.FilterHolder" >
<Arg>
<New class="net.i2p.servlet.filters.XI2PLocationFilter" />
</Arg>
</New>
</Arg>
<Arg>/*</Arg>
<Arg>
<Call class="java.util.EnumSet" name="of" >
<Arg>
<Call class="javax.servlet.DispatcherType" name="valueOf" >
<Arg>REQUEST</Arg>
</Call>
</Arg>
</Call>
</Arg>
</Call>
</New>
<Ref id="Handlers">
<Call name="addHandler">
<Arg><Ref id="XI2PFilterHandler"></Ref></Arg>
</Call>
</Ref>
</Configure>