Console: Use registered host/port for eepsite link (ticket #1604)

Jetty starter: Register host/port when started
PortMapper: Add hostname support
This commit is contained in:
zzz
2015-06-25 17:00:52 +00:00
parent 25268e7cb2
commit dd47389ad1
6 changed files with 89 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package net.i2p.util;
import java.io.IOException;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -16,7 +17,7 @@ import net.i2p.I2PAppContext;
* @since 0.8.12
*/
public class PortMapper {
private final ConcurrentHashMap<String, Integer> _dir;
private final ConcurrentHashMap<String, InetSocketAddress> _dir;
public static final String SVC_CONSOLE = "console";
public static final String SVC_HTTPS_CONSOLE = "https_console";
@ -37,7 +38,7 @@ public class PortMapper {
* @param context unused for now
*/
public PortMapper(I2PAppContext context) {
_dir = new ConcurrentHashMap<String, Integer>(8);
_dir = new ConcurrentHashMap<String, InetSocketAddress>(8);
}
/**
@ -46,9 +47,19 @@ public class PortMapper {
* @return success, false if already registered
*/
public boolean register(String service, int port) {
if (port <= 0)
return register(service, "127.0.0.1", port);
}
/**
* Add the service
* @param port > 0
* @return success, false if already registered
* @since 0.9.21
*/
public boolean register(String service, String host, int port) {
if (port <= 0 || port > 65535)
return false;
return _dir.putIfAbsent(service, Integer.valueOf(port)) == null;
return _dir.putIfAbsent(service, InetSocketAddress.createUnresolved(host, port)) == null;
}
/**
@ -73,10 +84,24 @@ public class PortMapper {
* @return def if not registered
*/
public int getPort(String service, int def) {
Integer port = _dir.get(service);
if (port == null)
InetSocketAddress ia = _dir.get(service);
if (ia == null)
return def;
return port.intValue();
return ia.getPort();
}
/**
* Get the registered host for a service.
* Will return "127.0.0.1" if the service was registered without a host.
* @param def default
* @return def if not registered
* @since 0.9.21
*/
public String getHost(String service, String def) {
InetSocketAddress ia = _dir.get(service);
if (ia == null)
return def;
return ia.getHostName();
}
/**
@ -85,10 +110,13 @@ public class PortMapper {
*/
public void renderStatusHTML(Writer out) throws IOException {
List<String> services = new ArrayList(_dir.keySet());
out.write("<h2>Port Mapper</h2><table><tr><th>Service<th>Port\n");
out.write("<h2>Port Mapper</h2><table><tr><th>Service<th>Host<th>Port\n");
Collections.sort(services);
for (String s : services) {
out.write("<tr><td>" + s + "<td>" + _dir.get(s) + '\n');
InetSocketAddress ia = _dir.get(s);
if (ia == null)
continue;
out.write("<tr><td>" + s + "<td>" + ia.getHostName() + "<td>" + ia.getPort() + '\n');
}
out.write("</table>\n");
}