/// <summary>Retrieve key certificate from this authority.</summary> /// <param name="forceDownload"></param> /// <returns></returns> internal KeyCertificate GetKeyCertificate(RetrievalOptions options) { string cachedCertificateFilePath = CacheManager.GetKeyCertificateFilePath(this); string certificateText = null; if ((0 == (RetrievalOptions.ForceDownload & options)) && (0 != (RetrievalOptions.UseCache & options))) { if (File.Exists(cachedCertificateFilePath)) { certificateText = File.ReadAllText(cachedCertificateFilePath); } } if (null == certificateText) { certificateText = Encoding.ASCII.GetString( WellKnownUrlRetriever.Retrieve( WellKnownUrlRetriever.Document.KeyCertificate, this, true)); if (0 == (RetrievalOptions.DoNotUseCache & options)) { File.WriteAllText(cachedCertificateFilePath, certificateText); } } // Should the key certificate not be found, we expect the above code to throw // an exception. TODO : verify this expectation. KeyCertificate result = new KeyCertificateParser().Parse(certificateText); result.Verify(this); return(result); }
internal string get_router_consensus(string identityFingerprint, bool compressed = true) { Globals.LogInfo("consensus::get_router_consensus() [identity_fingerprint: {0}]", identityFingerprint); return(Encoding.ASCII.GetString( Authority.DownloadFromRandomAuthority( WellKnownUrlRetriever.GetMostRecentServerDescriptorPath(identityFingerprint, compressed), compressed))); }
/// <summary>Retrieve and parse a valid consensus, depending on flags value, /// either grab it from cache and/or download it from a random authority.</summary> /// <param name="options"></param> internal static Consensus Fetch(RetrievalOptions options) { // TODO : Factorize RetrievalOptions behavior with Authority.GetKeyCertificate string consensusContent = null; if ((0 == (RetrievalOptions.ForceDownload & options)) && (0 != (RetrievalOptions.UseCache & options)) && File.Exists(CacheManager.CachedConsensusFilePath)) { consensusContent = File.ReadAllText(CacheManager.CachedConsensusFilePath); } else if ((0 != (options & RetrievalOptions.ForceDownload)) || (0 != (options & RetrievalOptions.DoNotUseCache))) { consensusContent = Encoding.ASCII.GetString( WellKnownUrlRetriever.Retrieve( WellKnownUrlRetriever.Document.MostRecentV3Consensus)); if (0 == (RetrievalOptions.DoNotUseCache & options)) { File.WriteAllText(CacheManager.CachedConsensusFilePath, consensusContent); } } Consensus result = null; if (null != consensusContent) { result = Parser.ParseAndValidate(consensusContent); } // if the consensus is invalid, we have to download it anyway // TODO : Don't download if options do not allow to do so. if ((null == result) || (result.ValidUntilUTC < DateTime.UtcNow)) { consensusContent = Encoding.ASCII.GetString( WellKnownUrlRetriever.Retrieve( WellKnownUrlRetriever.Document.MostRecentV3Consensus)); if (0 == (options & RetrievalOptions.DoNotUseCache)) { File.WriteAllText(CacheManager.CachedConsensusFilePath, consensusContent); } result = Parser.ParseAndValidate(consensusContent); } return(result); }