/** * Use the Date header as a backup time source */ public void headerReceived(String url, int attemptNum, String key, String val) { // We do this more than once, because // the first SSL handshake may take a while, and it may take the server // a while to render the index page. if (_gotDate < MAX_DATE_SETS && "date".equalsIgnoreCase(key) && _attemptStarted > 0) { long timeRcvd = System.currentTimeMillis(); long serverTime = RFC822Date.parse822Date(val); if (serverTime > 0) { // add 500ms since it's 1-sec resolution, and add half the RTT long now = serverTime + 500 + ((timeRcvd - _attemptStarted) / 2); long offset = now - _context.clock().now(); if (_context.clock().getUpdatedSuccessfully()) { // 2nd time better than the first if (_gotDate > 0) _context.clock().setNow(now, RouterClock.DEFAULT_STRATUM - 2); else _context.clock().setNow(now, RouterClock.DEFAULT_STRATUM - 1); if (_log.shouldLog(Log.WARN)) _log.warn("Reseed adjusting clock by " + DataHelper.formatDuration(Math.abs(offset))); } else { // No peers or NTP yet, this is probably better than the peer average will be for a while // default stratum - 1, so the peer average is a worse stratum _context.clock().setNow(now, RouterClock.DEFAULT_STRATUM - 1); _log.logAlways(Log.WARN, "NTP failure, Reseed adjusting clock by " + DataHelper.formatDuration(Math.abs(offset))); } _gotDate++; } } }
public NewsFetcher(RouterContext ctx, ConsoleUpdateManager mgr, List<URI> uris) { super(ctx, mgr, NEWS, uris); _newsFile = new File(ctx.getRouterDir(), NewsHelper.NEWS_FILE); _tempFile = new File(ctx.getTempDir(), "tmp-" + ctx.random().nextLong() + TEMP_NEWS_FILE); long lastMod = NewsHelper.lastChecked(ctx); if (lastMod > 0) _lastModified = RFC822Date.to822Date(lastMod); }
public void transferComplete(long alreadyTransferred, long bytesTransferred, long bytesRemaining, String url, String outputFile, boolean notModified) { if (_isPartial) { // Compare version with what we have now String newVersion = TrustedUpdate.getVersionString(new ByteArrayInputStream(_baos.toByteArray())); boolean newer = (new VersionComparator()).compare(newVersion, RouterVersion.VERSION) > 0; if (!newer) { updateStatus("<b>" + _("No new version found at {0}", linkify(url)) + "</b>"); if (_log.shouldLog(Log.WARN)) _log.warn("Found old version \"" + newVersion + "\" at " + url); } _isNewer = newer; return; } // Process the .sud/.su2 file updateStatus("<b>" + _("Update downloaded") + "</b>"); TrustedUpdate up = new TrustedUpdate(_context); File f = new File(_updateFile); File to = new File(_context.getRouterDir(), Router.UPDATE_FILE); String err = up.migrateVerified(RouterVersion.VERSION, f, to); f.delete(); if (err == null) { String policy = _context.getProperty(ConfigUpdateHandler.PROP_UPDATE_POLICY); this.done = true; // So unsigned update handler doesn't overwrite unless newer. String lastmod = _get.getLastModified(); long modtime = 0; if (lastmod != null) modtime = RFC822Date.parse822Date(lastmod); if (modtime <= 0) modtime = _context.clock().now(); _context.router().setConfigSetting(PROP_LAST_UPDATE_TIME, "" + modtime); _context.router().saveConfig(); if ("install".equals(policy)) { _log.log(Log.CRIT, "Update was VERIFIED, restarting to install it"); updateStatus("<b>" + _("Update verified") + "</b><br>" + _("Restarting")); restart(); } else { _log.log(Log.CRIT, "Update was VERIFIED, will be installed at next restart"); StringBuilder buf = new StringBuilder(64); buf.append("<b>").append(_("Update downloaded")).append("<br>"); if (_context.hasWrapper()) buf.append(_("Click Restart to install")); else buf.append(_("Click Shutdown and restart to install")); if (up.newVersion() != null) buf.append(' ').append(_("Version {0}", up.newVersion())); buf.append("</b>"); updateStatus(buf.toString()); } } else { _log.log(Log.CRIT, err + " from " + url); updateStatus("<b>" + err + ' ' + _("from {0}", linkify(url)) + " </b>"); } }
/** * HEAD the update url, and if the last-mod time is newer than the last update we downloaded, as * stored in the properties, then we download it using eepget. */ private boolean fetchUnsignedHead() { if (_urls.isEmpty()) return false; _currentURI = _urls.get(0); String url = _currentURI.toString(); // assume always proxied for now // boolean shouldProxy = // Boolean.valueOf(_context.getProperty(ConfigUpdateHandler.PROP_SHOULD_PROXY, // ConfigUpdateHandler.DEFAULT_SHOULD_PROXY)).booleanValue(); String proxyHost = _context.getProperty( ConfigUpdateHandler.PROP_PROXY_HOST, ConfigUpdateHandler.DEFAULT_PROXY_HOST); int proxyPort = _context.getProperty( ConfigUpdateHandler.PROP_PROXY_PORT, ConfigUpdateHandler.DEFAULT_PROXY_PORT_INT); try { EepHead get = new EepHead(_context, proxyHost, proxyPort, 0, url); if (get.fetch()) { String lastmod = get.getLastModified(); if (lastmod != null) { long modtime = RFC822Date.parse822Date(lastmod); if (modtime <= 0) return false; if (_ms <= 0) return false; if (modtime > _ms) { _unsignedUpdateAvailable = true; _mgr.notifyVersionAvailable( this, _urls.get(0), getType(), "", getMethod(), _urls, Long.toString(modtime), ""); } } return true; } } catch (Throwable t) { _log.error("Error fetching the unsigned update", t); } return false; }