diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java index e38a59100d6163c220054963863e2788d6f37172..2bddc3982c222d2e284a2a8af97446b2e9344f88 100644 --- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java +++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java @@ -404,6 +404,8 @@ public class CommSystemFacadeImpl extends CommSystemFacade { /** * Uses the transport IP first because that lookup is fast, * then the SSU IP from the netDb. + * + * @return two-letter lower-case country code or null */ public String getCountry(Hash peer) { byte[] ip = TransportImpl.getIP(peer); diff --git a/router/java/src/net/i2p/router/transport/GeoIP.java b/router/java/src/net/i2p/router/transport/GeoIP.java index 20261e968bce239e9c681df7d514a20a25d9d34e..a7da9fad8617366f3de79763be416abac5efb737 100644 --- a/router/java/src/net/i2p/router/transport/GeoIP.java +++ b/router/java/src/net/i2p/router/transport/GeoIP.java @@ -15,8 +15,8 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; -import net.i2p.I2PAppContext; import net.i2p.data.DataHelper; +import net.i2p.router.RouterContext; import net.i2p.util.ConcurrentHashSet; import net.i2p.util.Log; @@ -37,14 +37,14 @@ import net.i2p.util.Log; */ public class GeoIP { private Log _log; - private I2PAppContext _context; + private RouterContext _context; private final Map<String, String> _codeToName; private final Map<Long, String> _IPToCountry; private final Set<Long> _pendingSearch; private final Set<Long> _notFound; private final AtomicBoolean _lock; - public GeoIP(I2PAppContext context) { + public GeoIP(RouterContext context) { _context = context; _log = context.logManager().getLog(GeoIP.class); _codeToName = new ConcurrentHashMap(); @@ -59,6 +59,7 @@ public class GeoIP { static final String GEOIP_DIR_DEFAULT = "geoip"; static final String GEOIP_FILE_DEFAULT = "geoip.txt"; static final String COUNTRY_FILE_DEFAULT = "countries.txt"; + public static final String PROP_IP_COUNTRY = "i2np.lastCountry"; /** * Fire off a thread to lookup all pending IPs. @@ -87,6 +88,7 @@ public class GeoIP { } LookupJob j = new LookupJob(); j.run(); + updateOurCountry(); } private class LookupJob implements Runnable { @@ -232,6 +234,19 @@ public class GeoIP { return rv; } + /** + * Put our country code in the config, where others (such as Timestamper) can get it, + * and it will be there next time at startup. + */ + private void updateOurCountry() { + String oldCountry = _context.router().getConfigSetting(PROP_IP_COUNTRY); + String country = _context.commSystem().getCountry(_context.routerHash()); + if (country != null && !country.equals(oldCountry)) { + _context.router().setConfigSetting(PROP_IP_COUNTRY, country); + _context.router().saveConfig(); + } + } + /** * Add to the list needing lookup */ @@ -296,6 +311,7 @@ public class GeoIP { return _codeToName.get(code); } +/*** doesn't work since switched to RouterContext above public static void main(String args[]) { GeoIP g = new GeoIP(new I2PAppContext()); String tests[] = {"0.0.0.0", "0.0.0.1", "0.0.0.2", "0.0.0.255", "1.0.0.0", @@ -309,4 +325,5 @@ public class GeoIP { System.out.println(tests[i] + " : " + g.get(tests[i])); } +***/ } diff --git a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java index 7cd94f05397ad5f0331ad3d43a3abb435b6b0127..b438d115ff0b0796278236e206fc7002c6027a0c 100644 --- a/router/java/src/net/i2p/router/transport/udp/UDPTransport.java +++ b/router/java/src/net/i2p/router/transport/udp/UDPTransport.java @@ -474,6 +474,8 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority _context.statManager().addRateData("udp.addressUpdated", 1, 0); if (!fixedPort) _context.router().setConfigSetting(PROP_EXTERNAL_PORT, ourPort+""); + // queue a country code lookup of the new IP + _context.commSystem().queueLookup(ourIP); // store these for laptop-mode (change ident on restart... or every time... when IP changes) _context.router().setConfigSetting(PROP_IP, _externalListenHost.getHostAddress()); _context.router().setConfigSetting(PROP_IP_CHANGE, "" + _context.clock().now());