Jetty: Register HTTPS eepsite with port mapper (ticket #2159)

This commit is contained in:
zzz
2018-03-14 15:25:05 +00:00
parent 6eb09bd0c8
commit 948bbae661
2 changed files with 35 additions and 13 deletions

View File

@ -34,8 +34,8 @@ import static net.i2p.app.ClientAppState.*;
import net.i2p.util.I2PAppThread;
import net.i2p.util.PortMapper;
import org.eclipse.jetty.server.AbstractNetworkConnector;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.xml.XmlConfiguration;
@ -58,7 +58,7 @@ public class JettyStart implements ClientApp {
// warning, may be null if called from main
private final I2PAppContext _context;
private volatile ClientAppState _state;
private volatile int _port;
private volatile int _port, _sslPort;
/**
* All args must be XML file names.
@ -136,21 +136,34 @@ public class JettyStart implements ClientApp {
if (!lc.isRunning()) {
try {
lc.start();
if (_context != null && _context.portMapper().getPort(PortMapper.SVC_EEPSITE) <= 0) {
if (_context != null) {
PortMapper pm = _context.portMapper();
if (lc instanceof Server) {
Server server = (Server) lc;
Connector[] connectors = server.getConnectors();
if (connectors.length > 0) {
Connector conn = connectors[0];
if (conn instanceof NetworkConnector) {
NetworkConnector nconn = (NetworkConnector) conn;
for (int i = 0; i < connectors.length; i++) {
Connector conn = connectors[i];
if (conn instanceof AbstractNetworkConnector) {
AbstractNetworkConnector nconn = (AbstractNetworkConnector) conn;
int port = nconn.getPort();
if (port > 0) {
_port = port;
String host = nconn.getHost();
if (host.equals("0.0.0.0") || host.equals("::"))
if (host.equals("0.0.0.0"))
host = "127.0.0.1";
_context.portMapper().register(PortMapper.SVC_EEPSITE, host, port);
else if (host.equals("::"))
host = "::1";
// see ConnectionFactory javadoc, but from testing, it ends with /1.1
boolean isSSL = nconn.getConnectionFactory("SSL-http/1.1") != null;
String svc;
if (isSSL) {
_sslPort = port;
svc = PortMapper.SVC_HTTPS_EEPSITE;
} else {
_port = port;
svc = PortMapper.SVC_EEPSITE;
}
if (pm.getPort(svc) <= 0)
pm.register(svc, host, port);
}
}
}
@ -194,9 +207,16 @@ public class JettyStart implements ClientApp {
}
}
}
if (_context != null && _port > 0 && _context.portMapper().getPort(PortMapper.SVC_EEPSITE) == _port) {
if (_context != null) {
PortMapper pm = _context.portMapper();
if (_port > 0 && pm.getPort(PortMapper.SVC_EEPSITE) == _port) {
_port = 0;
_context.portMapper().unregister(PortMapper.SVC_EEPSITE);
pm.unregister(PortMapper.SVC_EEPSITE);
}
if (_sslPort > 0 && pm.getPort(PortMapper.SVC_HTTPS_EEPSITE) == _sslPort) {
_sslPort = 0;
pm.unregister(PortMapper.SVC_HTTPS_EEPSITE);
}
}
changeState(STOPPED);
}

View File

@ -26,6 +26,8 @@ public class PortMapper {
public static final String SVC_HTTP_PROXY = "HTTP";
public static final String SVC_HTTPS_PROXY = "HTTPS";
public static final String SVC_EEPSITE = "eepsite";
/** @since 0.9.34 */
public static final String SVC_HTTPS_EEPSITE = "https_eepsite";
public static final String SVC_IRC = "irc";
public static final String SVC_SOCKS = "socks";
public static final String SVC_TAHOE = "tahoe-lafs";