From 35b5eb74f6df3883cf4e988809151491bc6e8fd2 Mon Sep 17 00:00:00 2001 From: zzz Date: Wed, 29 Apr 2009 03:12:19 +0000 Subject: [PATCH] start of new address configuration --- .../net/i2p/router/web/ConfigNetHelper.java | 24 ++++++ apps/routerconsole/jsp/config.jsp | 43 ++++++++-- .../net/i2p/router/transport/Addresses.java | 80 +++++++++++++++++++ 3 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 router/java/src/net/i2p/router/transport/Addresses.java diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java index 0814df38e..2c90b805c 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigNetHelper.java @@ -5,6 +5,7 @@ import net.i2p.router.CommSystemFacade; import net.i2p.router.LoadTestManager; import net.i2p.router.Router; import net.i2p.router.RouterContext; +import net.i2p.router.transport.Addresses; import net.i2p.router.transport.TransportManager; import net.i2p.router.transport.udp.UDPAddress; import net.i2p.router.transport.udp.UDPTransport; @@ -20,6 +21,13 @@ public class ConfigNetHelper extends HelperBase { public final static String PROP_I2NP_NTCP_AUTO_IP = "i2np.ntcp.autoip"; private final static String CHECKED = " checked=\"true\" "; private final static String DISABLED = " disabled=\"true\" "; + + public String getUdphostname() { + String hostname = _context.getProperty(UDPTransport.PROP_EXTERNAL_HOST); + if (hostname == null) return ""; + return hostname; + } + public String getNtcphostname() { if (!TransportManager.enableNTCP(_context)) return "\" disabled=\"true"; @@ -116,6 +124,18 @@ public class ConfigNetHelper extends HelperBase { return ""; } +//////////////// FIXME + public String getUdpAutoIPChecked(int mode) { + String hostname = _context.getProperty(PROP_I2NP_NTCP_HOSTNAME); + boolean specified = hostname != null && hostname.length() > 0; + boolean auto = Boolean.valueOf(_context.getProperty(PROP_I2NP_NTCP_AUTO_IP)).booleanValue(); + if ((mode == 0 && (!specified) && !auto) || + (mode == 1 && specified && !auto) || + (mode == 2 && auto)) + return CHECKED; + return ""; + } + public String getUpnpChecked() { return getChecked(TransportManager.PROP_ENABLE_UPNP); } @@ -133,6 +153,10 @@ public class ConfigNetHelper extends HelperBase { } } + public String[] getAddresses() { + return Addresses.getAddresses(); + } + public String getInboundRate() { return "" + _context.bandwidthLimiter().getInboundKBytesPerSecond(); } diff --git a/apps/routerconsole/jsp/config.jsp b/apps/routerconsole/jsp/config.jsp index 4bcf6880c..10232ec73 100644 --- a/apps/routerconsole/jsp/config.jsp +++ b/apps/routerconsole/jsp/config.jsp @@ -68,17 +68,46 @@
--> UDP Configuration:
+ Externally reachable hostname or IP address:
+ /> + Use SSU detection only
+ /> + Use local public address if available, then UPnP detection, then SSU detection
+ /> + Use local public address if available, then SSU detection
+ /> + Use UPnP detection if available, then SSU detection
+ /> + Specify hostname or IP: + " /> + <% String[] ips = nethelper.getAddresses(); + if (ips.length > 0) { + out.print(" or \n"); + } + %> +
+ /> + Hidden mode - do not publish IP(not recommended)
Internal UDP port: " />
- External UDP address:
- Require SSU introductions? - />
+ /> + Require SSU introductions + (Enable if you cannot open your firewall)
+ Current External UDP address:

If you can, please poke a hole in your NAT or firewall to allow unsolicited UDP packets to reach you on your external UDP address. If you can't, I2P now includes supports UDP hole punching with "SSU introductions" - peers who will relay a request from someone you don't know to your router for your router so that you can make an outbound connection to them. I2P will use these introductions automatically if it detects that the port is not forwarded (as shown by - the Status: Firewalled line), or you can manually require them here. + the Reachability: Firewalled line), or you can manually require them here. Users behind symmetric NATs, such as OpenBSD's pf, are not currently supported.

@@ -88,18 +117,18 @@ Disable
/> Use IP address detected by SSU - (currently )
+ (currently )
/> Specify hostname or IP: " /> - (dyndns and the like are fine)
+ (dyndns and the like are fine)

Externally reachable TCP port:
/> Disable
/> Use the same port configured for SSU - (currently )?
+ (currently )
/> Specify Port: " />
diff --git a/router/java/src/net/i2p/router/transport/Addresses.java b/router/java/src/net/i2p/router/transport/Addresses.java new file mode 100644 index 000000000..cd82a9086 --- /dev/null +++ b/router/java/src/net/i2p/router/transport/Addresses.java @@ -0,0 +1,80 @@ +package net.i2p.router.transport; + +/* + * public domain + */ + +import java.net.InetAddress; +import java.net.Inet4Address; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; + + +/** + * Get the local addresses + * + * @author zzz + */ +public class Addresses { + + /** return the first non-local address it finds, or null */ + public static String getAnyAddress() { + String[] a = getAddresses(); + if (a.length > 0) + return a[0]; + return null; + } + + /** + * Return an array of all addresses, excluding + * IPv6, local, broadcast, multicast, etc. + */ + public static String[] getAddresses() { + Set rv = new HashSet(4); + try { + InetAddress localhost = InetAddress.getLocalHost(); + InetAddress[] allMyIps = InetAddress.getAllByName(localhost.getCanonicalHostName()); + if (allMyIps != null) { + for (int i = 0; i < allMyIps.length; i++) + add(rv, allMyIps[i]); + } + } catch (UnknownHostException e) {} + + try { + for(Enumeration ifcs = NetworkInterface.getNetworkInterfaces(); ifcs.hasMoreElements();) { + NetworkInterface ifc = ifcs.nextElement(); + for(Enumeration addrs = ifc.getInetAddresses(); addrs.hasMoreElements();) { + InetAddress addr = addrs.nextElement(); + add(rv, addr); + } + } + } catch (SocketException e) {} + + return rv.toArray(new String[rv.size()]); + } + + private static void add(Set set, InetAddress ia) { + if (ia.isAnyLocalAddress() || + ia.isLinkLocalAddress() || + ia.isLoopbackAddress() || + ia.isMulticastAddress() || + ia.isSiteLocalAddress() || + !(ia instanceof Inet4Address)) { +///////// testing +// System.err.println("Skipping: " + ia.getHostAddress()); +// return; + } + String ip = ia.getHostAddress(); + set.add(ip); + } + + public static void main(String[] args) { + String[] a = getAddresses(); + for (String s : a) + System.err.println("Address: " + s); + } +}