Util: Store multiple eepsite ports in PortMapper

Jetty: Register all servers with PortMapper
Console: List all Jetty servers in sidebar
This commit is contained in:
zzz
2021-02-21 09:17:31 -05:00
parent 1a5fedbb6d
commit 6e3b483c03
3 changed files with 74 additions and 20 deletions

View File

@@ -206,8 +206,7 @@ public class JettyStart implements ClientApp {
_port = port;
svc = PortMapper.SVC_EEPSITE;
}
if (pm.getPort(svc) <= 0)
pm.register(svc, host, port);
pm.register(svc, host, port);
}
}
}
@@ -253,13 +252,13 @@ public class JettyStart implements ClientApp {
}
if (_context != null) {
PortMapper pm = _context.portMapper();
if (_port > 0 && pm.getPort(PortMapper.SVC_EEPSITE) == _port) {
if (_port > 0) {
pm.unregister(PortMapper.SVC_EEPSITE, _port);
_port = 0;
pm.unregister(PortMapper.SVC_EEPSITE);
}
if (_sslPort > 0 && pm.getPort(PortMapper.SVC_HTTPS_EEPSITE) == _sslPort) {
if (_sslPort > 0) {
pm.unregister(PortMapper.SVC_HTTPS_EEPSITE, _sslPort);
_sslPort = 0;
pm.unregister(PortMapper.SVC_HTTPS_EEPSITE);
}
}
changeState(STOPPED);

View File

@@ -261,19 +261,27 @@ class SummaryBarRenderer {
svcs.put(tx, rbuf.toString());
}
String url = getEepsiteURL(pm);
if (url != null) {
List<String> urls = pm.getEepsiteURLs();
if (urls != null) {
String tx = _t("Web Server");
rbuf.setLength(0);
rbuf.append("<tr><td><img src=\"/themes/console/images/server.png\" height=\"16\" width=\"16\" alt=\"\"></td><td align=\"left\">" +
"<a href=\"")
.append(url)
.append("\" target=\"_blank\" title=\"")
.append(_t("Local web server"))
. append("\">")
.append(nbsp(tx))
.append("</a></td></tr>\n");
svcs.put(tx, rbuf.toString());
String txtt = _t("Local web Server");
int sz = urls.size();
if (sz > 1)
Collections.sort(urls);
for (int i = 0; i < sz; i++) {
String url = urls.get(i);
String txp = sz > 1 ? tx + ' ' + (i + 1) : tx;
rbuf.setLength(0);
rbuf.append("<tr><td><img src=\"/themes/console/images/server.png\" height=\"16\" width=\"16\" alt=\"\"></td><td align=\"left\">" +
"<a href=\"")
.append(url)
.append("\" target=\"_blank\" title=\"")
.append(txtt)
.append("\">")
.append(nbsp(txp))
.append("</a></td></tr>\n");
svcs.put(txp, rbuf.toString());
}
}
Map<String, String> apps = NavHelper.getClientAppLinks();

View File

@@ -6,6 +6,7 @@ import java.net.InetSocketAddress;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -20,6 +21,8 @@ import net.i2p.I2PAppContext;
*/
public class PortMapper {
private final ConcurrentHashMap<String, InetSocketAddress> _dir;
private final Set<String> _eepsites;
public static final String PROP_PREFER_HTTPS = "routerconsole.preferHTTPS";
public static final String SVC_CONSOLE = "console";
@@ -98,6 +101,7 @@ public class PortMapper {
*/
public PortMapper(I2PAppContext context) {
_dir = new ConcurrentHashMap<String, InetSocketAddress>(8);
_eepsites = new ConcurrentHashSet<String>(4);
}
/**
@@ -110,7 +114,10 @@ public class PortMapper {
}
/**
* Add the service
* Add the service.
* If service is SVC_EEPSITE or SVC_HTTPS_EEPSITE,
* the URL will be included in getEepsites()
*
* @param port &gt; 0
* @return success, false if already registered
* @since 0.9.21
@@ -118,7 +125,10 @@ public class PortMapper {
public boolean register(String service, String host, int port) {
if (port <= 0 || port > 65535)
return false;
return _dir.putIfAbsent(service, InetSocketAddress.createUnresolved(host, port)) == null;
boolean rv = _dir.putIfAbsent(service, InetSocketAddress.createUnresolved(host, port)) == null;
if (service.equals(SVC_EEPSITE) || service.equals(SVC_HTTPS_EEPSITE))
rv = _eepsites.add(toURL(service, host, port));
return rv;
}
/**
@@ -140,10 +150,19 @@ public class PortMapper {
/**
* Remove the service,
* only if it is registered with the supplied port.
* If service is SVC_EEPSITE or SVC_HTTPS_EEPSITE,
* the URL will be removed from getEepsites()
*
* @since 0.9.34
*/
public void unregister(String service, int port) {
if (service.equals(SVC_EEPSITE) || service.equals(SVC_HTTPS_EEPSITE)) {
String end = ":" + port + '/';
for (Iterator<String> iter = _eepsites.iterator(); iter.hasNext(); ) {
if (iter.next().endsWith(end))
iter.remove();
}
}
// not synched
if (getPort(service) == port)
_dir.remove(service);
@@ -315,6 +334,34 @@ public class PortMapper {
return "https://" + httpsHost + ':' + httpsPort + '/';
}
/**
* @return unsorted, non-empty, or null if none
* @since 0.9.50
*/
public List<String> getEepsiteURLs() {
if (_eepsites.isEmpty())
return null;
return new ArrayList<String>(_eepsites);
}
/**
* @since 0.9.50 from SummaryBarRenderer
*/
private static String toURL(String svc, String host, int port) {
StringBuilder buf = new StringBuilder(64);
buf.append(svc.equals(SVC_HTTPS_EEPSITE) ? "https://" : "http://");
host = convertWildcard(host, "127.0.0.1");
if (host.contains(":"))
buf.append('[');
buf.append(host);
if (host.contains(":"))
buf.append(']');
buf.append(':')
.append(port)
.append('/');
return buf.toString();
}
/**
* For debugging only
* @since 0.9.20