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 static IpAddresses GetGuardIps() { 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); } TcpClient s = Connect(); Write(s, "getinfo circuit-status\n"); Flush(s); string circuits = Read(s); string[] circuitsLines = circuits.Split('\n'); foreach (string circuit in circuitsLines) { string[] circuitItems = circuit.Split(' '); if (circuitItems.Length < 3) { continue; } if (circuitItems[1] != "BUILT") { continue; } string id = circuitItems[2]; id = id.Substring(1, id.IndexOf('~') - 1); Write(s, "getinfo ns/id/" + id + "\n"); string nodeInfo = Read(s); string[] nodeLines = nodeInfo.Split('\n'); foreach (string line in nodeLines) { string[] lineItems = line.Split(' '); if (lineItems.Length < 7) { continue; } if (lineItems[0] != "r") { continue; } string ip = lineItems[6]; if (ips.Contains(ip) == false) { Engine.Instance.Logs.Log(LogType.Verbose, MessagesFormatter.Format(Messages.TorControlGuardIp, ip, id)); ips.Add(ip); } } } s.Close(); if (ips.Count == 0) { Engine.Instance.Logs.Log(LogType.Warning, Messages.TorControlNoIps); //throw new Exception(Messages.TorControlNoIps); } } catch (Exception e) { //throw new Exception(MessagesFormatter.Format(Messages.TorControlException, e.Message)); Engine.Instance.Logs.Log(LogType.Warning, MessagesFormatter.Format(Messages.TorControlException, e.Message)); } return(ips); }