Console: Properly register listen hosts with PortMapper

I2PTunnel: Fixup console links in error pages if console is
           on a non-standard host or port, or on https
PortMapper: Add method to convert wildcard host to actual host
This commit is contained in:
zzz
2016-01-06 17:50:06 +00:00
parent 46af643ca8
commit 144f54eb8c
3 changed files with 122 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import net.i2p.I2PAppContext;
@@ -110,6 +111,68 @@ public class PortMapper {
return ia.getHostName();
}
/**
* Get the actual host for a service.
* Will return "127.0.0.1" if the service was registered without a host.
* If the service was registered with the host "0.0.0.0", "::", or "0:0:0:0:0:0:0:0",
* it will return a public IP if we have one,
* else a local IP if we have one, else def.
* If it was not registered with a wildcard address, it will return the registered host.
*
* @param def default
* @return def if not registered
* @since 0.9.24
*/
public String getActualHost(String service, String def) {
InetSocketAddress ia = _dir.get(service);
if (ia == null)
return def;
return convertWildcard(ia.getHostName(), def);
}
/*
* See above
* @param def default
* @return def if no ips
* @since 0.9.24
*/
private static String convertWildcard(String ip, String def) {
String rv = ip;
if (rv.equals("0.0.0.0")) {
// public
rv = Addresses.getAnyAddress();
if (rv == null) {
rv = def;
// local
Set<String> addrs = Addresses.getAddresses(true, false);
for (String addr : addrs) {
if (!addr.startsWith("127.") && !addr.equals("0.0.0.0")) {
rv = addr;
break;
}
}
}
} else if (rv.equals("::") || rv.equals("0:0:0:0:0:0:0:0")) {
rv = def;
// public
Set<String> addrs = Addresses.getAddresses(false, true);
for (String addr : addrs) {
if (!addr.contains(".")) {
return rv;
}
}
// local
addrs = Addresses.getAddresses(true, true);
for (String addr : addrs) {
if (!addr.contains(".") && !addr.equals("::") && !addr.equals("0:0:0:0:0:0:0:0")) {
rv = addr;
break;
}
}
}
return rv;
}
/**
* For debugging only
* @since 0.9.20
@@ -122,7 +185,7 @@ public class PortMapper {
InetSocketAddress ia = _dir.get(s);
if (ia == null)
continue;
out.write("<tr><td>" + s + "<td>" + ia.getHostName() + "<td>" + ia.getPort() + '\n');
out.write("<tr><td>" + s + "<td>" + convertWildcard(ia.getHostName(), "127.0.0.1") + "<td>" + ia.getPort() + '\n');
}
out.write("</table>\n");
}