eliminate startup race condition on reading router.config file, prevent router state exception while waiting for initialization

pull/13/head v1.6
knaccc 5 years ago
parent acae006728
commit 3d436d8a8c

@ -103,9 +103,15 @@ public class RouterWrapper {
if(router.isAlive()) {
try {
File routerConfigFile = new File(i2PBaseDir, "router.config");
if(!routerConfigFile.exists()) continue;
if(!(routerConfigFile.exists() && routerConfigFile.canRead())) {
Thread.sleep(100);
continue;
}
Optional<String> portString = Files.lines(routerConfigFile.toPath()).filter(s -> s.startsWith("i2np.udp.port=")).findFirst();
if(portString.isEmpty()) continue;
if(portString.isEmpty()) {
Thread.sleep(100);
continue;
}
routerExternalPort = Integer.parseInt(portString.get().split("=")[1]);
break;
}
@ -323,85 +329,90 @@ public class RouterWrapper {
final static String PROP_I2NP_NTCP_PORT = "i2np.ntcp.port";
public NetworkStateMessage getReachability() {
RouterContext _context = router.getContext();
if (_context.commSystem().isDummy())
return new NetworkStateMessage(NetworkState.VMCOMM, "VM Comm System");
if (_context.router().getUptime() > 60*1000 && (!_context.router().gracefulShutdownInProgress()) &&
!_context.clientManager().isAlive())
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Client Manager I2CP Error - check logs")); // not a router problem but the user should know
// Warn based on actual skew from peers, not update status, so if we successfully offset
// the clock, we don't complain.
//if (!_context.clock().getUpdatedSuccessfully())
long skew = _context.commSystem().getFramedAveragePeerClockSkew(33);
// Display the actual skew, not the offset
if (Math.abs(skew) > 30*1000)
return new NetworkStateMessage(NetworkState.CLOCKSKEW, _t("ERR-Clock Skew"));
if (_context.router().isHidden())
return new NetworkStateMessage(NetworkState.HIDDEN, _t("Hidden"));
RouterInfo routerInfo = _context.router().getRouterInfo();
if (routerInfo == null)
return new NetworkStateMessage(NetworkState.TESTING, _t("Testing"));
CommSystemFacade.Status status = _context.commSystem().getStatus();
NetworkState state = NetworkState.RUNNING;
switch (status) {
case OK:
case IPV4_OK_IPV6_UNKNOWN:
case IPV4_OK_IPV6_FIREWALLED:
case IPV4_UNKNOWN_IPV6_OK:
case IPV4_DISABLED_IPV6_OK:
case IPV4_SNAT_IPV6_OK:
RouterAddress ra = routerInfo.getTargetAddress("NTCP");
if (ra == null)
return new NetworkStateMessage(NetworkState.RUNNING, _t(status.toStatusString()));
byte[] ip = ra.getIP();
if (ip == null)
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Unresolved TCP Address"));
// TODO set IPv6 arg based on configuration?
if (TransportUtil.isPubliclyRoutable(ip, true))
return new NetworkStateMessage(NetworkState.RUNNING, _t(status.toStatusString()));
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Private TCP Address"));
case IPV4_SNAT_IPV6_UNKNOWN:
case DIFFERENT:
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-SymmetricNAT"));
case REJECT_UNSOLICITED:
state = NetworkState.FIREWALLED;
case IPV4_DISABLED_IPV6_FIREWALLED:
if (routerInfo.getTargetAddress("NTCP") != null)
return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled with Inbound TCP Enabled"));
// fall through...
case IPV4_FIREWALLED_IPV6_OK:
case IPV4_FIREWALLED_IPV6_UNKNOWN:
if (((FloodfillNetworkDatabaseFacade)_context.netDb()).floodfillEnabled())
return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled and Floodfill"));
//if (_context.router().getRouterInfo().getCapabilities().indexOf('O') >= 0)
// return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled and Fast"));
return new NetworkStateMessage(state, _t(status.toStatusString()));
case DISCONNECTED:
return new NetworkStateMessage(NetworkState.TESTING, _t("Disconnected - check network connection"));
case HOSED:
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-UDP Port In Use - Set i2np.udp.internalPort=xxxx in advanced config and restart"));
case UNKNOWN:
state = NetworkState.TESTING;
case IPV4_UNKNOWN_IPV6_FIREWALLED:
case IPV4_DISABLED_IPV6_UNKNOWN:
default:
ra = routerInfo.getTargetAddress("SSU");
if (ra == null && _context.router().getUptime() > 5*60*1000) {
if (_context.commSystem().countActivePeers() <= 0)
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-No Active Peers, Check Network Connection and Firewall"));
else if (_context.getProperty(PROP_I2NP_NTCP_HOSTNAME) == null ||
_context.getProperty(PROP_I2NP_NTCP_PORT) == null)
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-UDP Disabled and Inbound TCP host/port not set"));
else
return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled with UDP Disabled"));
}
return new NetworkStateMessage(state, _t(status.toStatusString()));
try {
RouterContext _context = router.getContext();
if (_context.commSystem().isDummy())
return new NetworkStateMessage(NetworkState.VMCOMM, "VM Comm System");
if (_context.router().getUptime() > 60*1000 && (!_context.router().gracefulShutdownInProgress()) &&
!_context.clientManager().isAlive())
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Client Manager I2CP Error - check logs")); // not a router problem but the user should know
// Warn based on actual skew from peers, not update status, so if we successfully offset
// the clock, we don't complain.
//if (!_context.clock().getUpdatedSuccessfully())
long skew = _context.commSystem().getFramedAveragePeerClockSkew(33);
// Display the actual skew, not the offset
if (Math.abs(skew) > 30*1000)
return new NetworkStateMessage(NetworkState.CLOCKSKEW, _t("ERR-Clock Skew"));
if (_context.router().isHidden())
return new NetworkStateMessage(NetworkState.HIDDEN, _t("Hidden"));
RouterInfo routerInfo = _context.router().getRouterInfo();
if (routerInfo == null)
return new NetworkStateMessage(NetworkState.TESTING, _t("Testing"));
CommSystemFacade.Status status = _context.commSystem().getStatus();
NetworkState state = NetworkState.RUNNING;
switch (status) {
case OK:
case IPV4_OK_IPV6_UNKNOWN:
case IPV4_OK_IPV6_FIREWALLED:
case IPV4_UNKNOWN_IPV6_OK:
case IPV4_DISABLED_IPV6_OK:
case IPV4_SNAT_IPV6_OK:
RouterAddress ra = routerInfo.getTargetAddress("NTCP");
if (ra == null)
return new NetworkStateMessage(NetworkState.RUNNING, _t(status.toStatusString()));
byte[] ip = ra.getIP();
if (ip == null)
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Unresolved TCP Address"));
// TODO set IPv6 arg based on configuration?
if (TransportUtil.isPubliclyRoutable(ip, true))
return new NetworkStateMessage(NetworkState.RUNNING, _t(status.toStatusString()));
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-Private TCP Address"));
case IPV4_SNAT_IPV6_UNKNOWN:
case DIFFERENT:
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-SymmetricNAT"));
case REJECT_UNSOLICITED:
state = NetworkState.FIREWALLED;
case IPV4_DISABLED_IPV6_FIREWALLED:
if (routerInfo.getTargetAddress("NTCP") != null)
return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled with Inbound TCP Enabled"));
// fall through...
case IPV4_FIREWALLED_IPV6_OK:
case IPV4_FIREWALLED_IPV6_UNKNOWN:
if (((FloodfillNetworkDatabaseFacade)_context.netDb()).floodfillEnabled())
return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled and Floodfill"));
//if (_context.router().getRouterInfo().getCapabilities().indexOf('O') >= 0)
// return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled and Fast"));
return new NetworkStateMessage(state, _t(status.toStatusString()));
case DISCONNECTED:
return new NetworkStateMessage(NetworkState.TESTING, _t("Disconnected - check network connection"));
case HOSED:
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-UDP Port In Use - Set i2np.udp.internalPort=xxxx in advanced config and restart"));
case UNKNOWN:
state = NetworkState.TESTING;
case IPV4_UNKNOWN_IPV6_FIREWALLED:
case IPV4_DISABLED_IPV6_UNKNOWN:
default:
ra = routerInfo.getTargetAddress("SSU");
if (ra == null && _context.router().getUptime() > 5*60*1000) {
if (_context.commSystem().countActivePeers() <= 0)
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-No Active Peers, Check Network Connection and Firewall"));
else if (_context.getProperty(PROP_I2NP_NTCP_HOSTNAME) == null ||
_context.getProperty(PROP_I2NP_NTCP_PORT) == null)
return new NetworkStateMessage(NetworkState.ERROR, _t("ERR-UDP Disabled and Inbound TCP host/port not set"));
else
return new NetworkStateMessage(NetworkState.WARN, _t("WARN-Firewalled with UDP Disabled"));
}
return new NetworkStateMessage(state, _t(status.toStatusString()));
}
}
catch(Exception e) {
return new NetworkStateMessage(NetworkState.WARN, "Starting...");
}
}

Loading…
Cancel
Save