public string Refresh() { m_lastRefreshTry = Utils.UnixTimeStamp(); Engine.Instance.Logs.Log(LogType.Verbose, Messages.ManifestUpdate); // TOOPTIMIZE: Stop at first error foreach (Provider provider in Providers) { if (provider.Enabled) { string result = provider.Refresh(); if (result != "") { return(result); } } } Engine.Instance.PostManifestUpdate(); Engine.Instance.Logs.Log(LogType.Verbose, Messages.ManifestDone); m_lastRefreshDone = Utils.UnixTimeStamp(); return(""); }
// Note: If cache is expired, but new query don't return IPs, old cache it's returned. public static IpAddresses ResolveDNS(string host, bool nocache) { if (nocache) { return(Platform.Instance.ResolveDNS(host)); } else { DnsManagerEntry entry = null; lock (m_cache) { if (m_cache.ContainsKey(host)) { entry = m_cache[host]; } else { entry = new DnsManagerEntry(); } } Int64 now = Utils.UnixTimeStamp(); Int64 delay = now - entry.TimeStamp; Int64 ttl = 3600; if ((Engine.Instance != null) && (Engine.Instance.Storage != null)) { ttl = Engine.Instance.Storage.GetInt("dns.cache.ttl"); } if (delay >= ttl) { IpAddresses result = Platform.Instance.ResolveDNS(host); if (result.Count != 0) { entry.Response = result; entry.TimeStamp = now; lock (m_cache) { m_cache[host] = entry; } } } return(entry.Response); } }
public bool NeedUpdate(bool recommended) { Int64 refreshInterval = Engine.Instance.Storage.GetInt("advanced.manifest.refresh"); if (refreshInterval == 0) { return(false); } if (refreshInterval < 0) { refreshInterval = 10; // Default } if (m_lastRefreshTry + 60 * refreshInterval < Utils.UnixTimeStamp()) { return(true); } return(false); }
public void DoRefresh(bool force) { bool postRefreshRecompute = force; foreach (Provider provider in Providers) { if (provider.Enabled) { if ((force) || (provider.GetNeedRefresh())) { postRefreshRecompute = true; string result = provider.OnRefresh(); if (result != "") { if (Engine.Instance.ConnectionActive == null) // Note: only if not connected, otherwise misunderstanding. { if (Engine.Instance.Storage.GetBool("ui.skip.provider.manifest.failed") == false) { Engine.Instance.OnProviderManifestFailed(provider); } } } } } } if (postRefreshRecompute) { Engine.Instance.PostManifestUpdate(); } m_lastRefreshDone = Utils.UnixTimeStamp(); if (InvalidateWithNextRefresh) { InvalidateWithNextRefresh = false; Engine.Instance.InvalidateConnections(); } }
public static string FormatTime(Int64 unix) { if (unix == 0) { return("-"); } string o = ""; Int64 now = Utils.UnixTimeStamp(); if (unix == now) { o = GetText("TimeJustNow"); } else if (unix < now) { o = FormatSeconds(now - unix) + " " + GetText("TimeAgo"); } else { o = FormatSeconds(unix - now) + " " + GetText("TimeRemain"); } return(o); }
public static IpAddresses GetGuardIps(bool force) { // This is called a lots of time. Int64 now = Utils.UnixTimeStamp(); if ((force == false) && ((now - m_lastGuardTime < 60))) { return(m_lastGuardIps); } IpAddresses ips = new IpAddresses(); try { string controlHost = Engine.Instance.Storage.Get("proxy.host").ToLowerInvariant().Trim(); if ((controlHost != "127.0.0.1") && (controlHost.ToLowerInvariant() != "localhost")) { // Guard IPS are used to avoid routing loop, that occur only if the Tor host is the same machine when OpenVPN run. return(ips); } List <string> ipsMessages = new List <string>(); using (TcpClient s = new TcpClient()) { Connect(s); Write(s, "getinfo circuit-status\n"); Flush(s); string circuits = Read(s); string[] circuitsLines = circuits.Split('\n'); foreach (string circuit in circuitsLines) { string id = circuit.ToLowerInvariant().RegExMatchOne("\\d+\\sbuilt\\s\\$([0-9a-f]+)"); if (id != "") { Write(s, "getinfo ns/id/" + id.ToUpperInvariant() + "\n"); string nodeInfo = Read(s); string[] nodeLines = nodeInfo.Split('\n'); foreach (string line in nodeLines) { string ip = line.RegExMatchOne("r\\s.+?\\s.+?\\s.+?\\s.+?\\s.+?\\s(.+?)\\s"); if ((IpAddress.IsIP(ip)) && (!ips.Contains(ip))) { ips.Add(ip); ipsMessages.Add(ip + " (circuit)"); } } } } Write(s, "getconf bridge\n"); Flush(s); string bridges = Read(s); if (bridges.IndexOf("meek") == -1) //Panic if we have meek enabled, don't yet know what to do :-( { string[] bridgeLines = bridges.Split('\n'); foreach (string bridge in bridgeLines) { List <string> matches = bridge.ToLowerInvariant().RegExMatchSingle("250.bridge=(.+?)\\s([0-9a-f\\.\\:]+?):\\d+\\s"); if ((matches != null) && (matches.Count == 2)) { string bridgeType = matches[0]; string ip = matches[1]; if ((IpAddress.IsIP(ip)) && (!ips.Contains(ip))) { ips.Add(matches[1]); ipsMessages.Add(matches[1] + " (" + bridgeType + ")"); } } } } else { Engine.Instance.Logs.Log(LogType.Warning, LanguageManager.GetText("TorControlMeekUnsupported")); } if (ips.Count == 0) { Engine.Instance.Logs.Log(LogType.Warning, LanguageManager.GetText("TorControlNoIps")); //throw new Exception(Messages.TorControlNoIps); } else { string list = String.Join("; ", ipsMessages.ToArray()); Engine.Instance.Logs.Log(LogType.Verbose, LanguageManager.GetText("TorControlGuardIps", list)); } } } catch (Exception e) { //throw new Exception(LanguageManager.GetText("TorControlException, e.Message)); Engine.Instance.Logs.Log(LogType.Warning, LanguageManager.GetText("TorControlException", e.Message)); } m_lastGuardIps = ips; m_lastGuardTime = now; return(ips); }
public virtual string OnRefresh() { m_lastTryRefresh = Utils.UnixTimeStamp(); return(""); }