public override IpAddresses ResolveDNS(string host) { IpAddresses result = new IpAddresses(); string getentPath = LocateExecutable("getent"); if (getentPath != "") { // Note: CNAME record are automatically followed. SystemShell s = new SystemShell(); s.Path = getentPath; s.Arguments.Add("ahosts"); s.Arguments.Add(SystemShell.EscapeHost(host)); s.NoDebugLog = true; if (s.Run()) { string o = s.Output; o = o.CleanSpace(); foreach (string line in o.Split('\n')) { string[] fields = line.Split(' '); if (fields.Length < 2) { continue; } if (fields[1].Trim() != "STREAM") { continue; } result.Add(fields[0].Trim()); } } } return(result); }
public override void ResolveWithoutAnswer(string host) { // Base method with Dns.GetHostEntry have cache issue, for example on Fedora. if (Platform.Instance.FileExists("/usr/bin/host")) { ShellCmd("host -W 5 -t A " + SystemShell.EscapeHost(host)); } else { base.ResolveWithoutAnswer(host); } }
// Encounter Mono issue about the .Net method on OS X, similar to Mono issue under Linux. Use shell instead, like Linux public override long Ping(string host, int timeoutSec) { // Note: Linux timeout is -w, OS X timeout is -t string cmd = "ping -c 1 -t " + timeoutSec + " -q -n " + SystemShell.EscapeHost(host); string result = ShellCmd(cmd); // Note: Linux have mdev, OS X have stddev string sMS = Utils.ExtractBetween(result, "min/avg/max/stddev = ", "/"); float iMS; if (float.TryParse(sMS, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out iMS) == false) { iMS = -1; } return((long)iMS); }
public override long Ping(string host, int timeoutSec) { string cmd = "ping -c 1 -w " + timeoutSec.ToString() + " -q -n " + SystemShell.EscapeHost(host); string result = ShellCmd(cmd, true); string sMS = Utils.ExtractBetween(result.ToLowerInvariant(), "min/avg/max/mdev = ", "/"); float iMS; /* * if (float.TryParse(sMS, out iMS)) * return (Int64)iMS; * else * return -1; */ if (float.TryParse(sMS, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out iMS) == false) { iMS = -1; } return((long)iMS); }
public override IpAddresses ResolveDNS(string host) { // Base method with Dns.GetHostEntry have cache issue, for example on Fedora. OS X it's based on Mono. // Also, base methods with Dns.GetHostEntry sometime don't fetch AAAA IPv6 addresses. IpAddresses result = new IpAddresses(); string hostPath = LocateExecutable("host"); if (hostPath != "") { // Note: CNAME record are automatically followed. SystemShell s = new SystemShell(); s.Path = "/usr/bin/host"; s.Arguments.Add("-W 5"); s.Arguments.Add(SystemShell.EscapeHost(host)); s.NoDebugLog = true; if (s.Run()) { string hostout = s.Output; foreach (string line in hostout.Split('\n')) { string ipv4 = Utils.RegExMatchOne(line, "^.*? has address (.*?)$"); if (ipv4 != "") { result.Add(ipv4.Trim()); } string ipv6 = Utils.RegExMatchOne(line, "^.*? has IPv6 address (.*?)$"); if (ipv6 != "") { result.Add(ipv6.Trim()); } } } } return(result); }
public HttpResponse Fetch(HttpRequest request) { HttpResponse response = new HttpResponse(); ExceptionIfRequired(); ProgramScope programScope = new ProgramScope(this.GetPath(), "curl"); // Don't use proxy if connected to the VPN, or in special cases (checking) during connection. bool bypassProxy = request.BypassProxy; if (bypassProxy == false) { bypassProxy = Engine.Instance.IsConnected(); } string dataParameters = ""; if (request.Parameters.Count > 0) { foreach (string k in request.Parameters.Keys) { if (dataParameters != "") { dataParameters += "&"; } dataParameters += SystemShell.EscapeAlphaNumeric(k) + "=" + Uri.EscapeUriString(request.Parameters[k]); } } string args = ""; if (bypassProxy == false) { string proxyMode = Engine.Instance.Storage.GetLower("proxy.mode"); string proxyWhen = Engine.Instance.Storage.GetLower("proxy.when"); string proxyHost = Engine.Instance.Storage.Get("proxy.host"); int proxyPort = Engine.Instance.Storage.GetInt("proxy.port"); string proxyAuth = Engine.Instance.Storage.Get("proxy.auth").ToLowerInvariant(); string proxyLogin = Engine.Instance.Storage.Get("proxy.login"); string proxyPassword = Engine.Instance.Storage.Get("proxy.password"); if ((proxyWhen == "none") || (proxyWhen == "openvpn")) { proxyMode = "none"; } if (proxyMode == "detect") { throw new Exception(Messages.ProxyDetectDeprecated); } if (proxyMode == "tor") { proxyMode = "socks"; proxyAuth = "none"; proxyLogin = ""; proxyPassword = ""; } if (proxyMode == "http") { args += " --proxy http://" + SystemShell.EscapeHost(proxyHost) + ":" + proxyPort.ToString(); } else if (proxyMode == "socks") { // curl support different types of proxy. OpenVPN not, only socks5. So, it's useless to support other kind of proxy here. args += " --proxy socks5://" + SystemShell.EscapeHost(proxyHost) + ":" + proxyPort.ToString(); } if ((proxyMode != "none") && (proxyAuth != "none")) { if (proxyAuth == "basic") { args += " --proxy-basic"; } else if (proxyAuth == "ntlm") { args += " --proxy-ntlm"; } if (SystemShell.EscapeInsideQuoteAcceptable(proxyLogin) == false) { throw new Exception(MessagesFormatter.Format(Messages.UnacceptableCharacters, "Proxy Login")); } if (SystemShell.EscapeInsideQuoteAcceptable(proxyPassword) == false) { throw new Exception(MessagesFormatter.Format(Messages.UnacceptableCharacters, "Proxy Password")); } if ((proxyLogin != "") && (proxyPassword != "")) { args += " --proxy-user \"" + SystemShell.EscapeInsideQuote(proxyLogin) + "\":\"" + SystemShell.EscapeInsideQuote(proxyPassword) + "\""; } } } args += " \"" + SystemShell.EscapeUrl(request.Url) + "\""; args += " -sS"; // -s Silent mode, -S with errors args += " --max-time " + Engine.Instance.Storage.GetInt("tools.curl.max-time").ToString(); string pathCacert = Engine.Instance.LocateResource("cacert.pem"); if (pathCacert != "") { args += " --cacert \"" + SystemShell.EscapePath(pathCacert) + "\""; } if (request.ForceResolve != "") { args += " --resolve " + request.ForceResolve; } if (dataParameters != "") { args += " --data \"" + dataParameters + "\""; } if (request.IpLayer == "4") { args += " -4"; } if (request.IpLayer == "6") { args += " -6"; } args += " -i"; string error = ""; try { using (Process p = new Process()) { p.StartInfo.FileName = SystemShell.EscapePath(this.GetPath()); p.StartInfo.Arguments = args; p.StartInfo.WorkingDirectory = ""; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); { using (System.IO.MemoryStream Stream = new System.IO.MemoryStream()) { using (System.IO.MemoryStream StreamHeader = new System.IO.MemoryStream()) { using (System.IO.MemoryStream StreamBody = new System.IO.MemoryStream()) { byte[] buffer = new byte[4096]; int read; while ((read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length)) > 0) { Stream.Write(buffer, 0, read); } if (Stream.Length >= 4) { byte[] buffer2 = Stream.ToArray(); int i = 0; for (; i < Stream.Length - 4; i++) { if ((buffer2[i] == 13) && (buffer2[i + 1] == 10) && (buffer2[i + 2] == 13) && (buffer2[i + 3] == 10)) { StreamHeader.Write(buffer2, 0, i); StreamBody.Write(buffer2, i + 4, (int)Stream.Length - i - 4); break; } } if (StreamHeader.Length == 0) { StreamHeader.Write(buffer2, 0, (int)Stream.Length); } } else { StreamHeader.Write(Stream.ToArray(), 0, (int)Stream.Length); } response.BufferHeader = StreamHeader.ToArray(); response.BufferData = StreamBody.ToArray(); } } string headers = System.Text.Encoding.ASCII.GetString(response.BufferHeader); string[] headersLines = headers.Split('\n'); for (int l = 0; l < headersLines.Length; l++) { string line = headersLines[l]; if (l == 0) { response.StatusLine = line; } int posSep = line.IndexOf(":"); if (posSep != -1) { string k = line.Substring(0, posSep); string v = line.Substring(posSep + 1); response.Headers.Add(new KeyValuePair <string, string>(k.ToLowerInvariant().Trim(), v.Trim())); } } } } error = p.StandardError.ReadToEnd(); p.WaitForExit(); response.ExitCode = p.ExitCode; } } catch (Exception e) { error = e.Message; } programScope.End(); if (error != "") { throw new Exception(error.Trim()); } return(response); }
public byte[] FetchUrlEx(string url, System.Collections.Specialized.NameValueCollection parameters, string title, bool forceBypassProxy, string resolve) { if (Available() == false) { throw new Exception(Messages.ToolsCurlRequired); } if (Utils.CompareVersions(Version, minVersionRequired) == -1) { throw new Exception(GetRequiredVersionMessage()); } ProgramScope programScope = new ProgramScope(this.GetPath(), "curl"); // Don't use proxy if connected to the VPN, or in special cases (checking) during connection. bool bypassProxy = forceBypassProxy; if (bypassProxy == false) { bypassProxy = Engine.Instance.IsConnected(); } string dataParameters = ""; if (parameters != null) { foreach (string k in parameters.Keys) { if (dataParameters != "") { dataParameters += "&"; } dataParameters += SystemShell.EscapeAlphaNumeric(k) + "=" + Uri.EscapeUriString(parameters[k]); } } string args = ""; if (bypassProxy == false) { string proxyMode = Engine.Instance.Storage.Get("proxy.mode").ToLowerInvariant(); string proxyHost = Engine.Instance.Storage.Get("proxy.host"); int proxyPort = Engine.Instance.Storage.GetInt("proxy.port"); string proxyAuth = Engine.Instance.Storage.Get("proxy.auth").ToLowerInvariant(); string proxyLogin = Engine.Instance.Storage.Get("proxy.login"); string proxyPassword = Engine.Instance.Storage.Get("proxy.password"); if (proxyMode == "detect") { throw new Exception(Messages.ProxyDetectDeprecated); } if (proxyMode == "tor") { proxyMode = "socks"; proxyAuth = "none"; proxyLogin = ""; proxyPassword = ""; } if (proxyMode == "http") { args += " --proxy http://" + SystemShell.EscapeHost(proxyHost) + ":" + proxyPort.ToString(); } else if (proxyMode == "socks") { // curl support different types of proxy. OpenVPN not, only socks5. So, it's useless to support other kind of proxy here. args += " --proxy socks5://" + SystemShell.EscapeHost(proxyHost) + ":" + proxyPort.ToString(); } if ((proxyMode != "none") && (proxyAuth != "none")) { if (proxyAuth == "basic") { args += " --proxy-basic"; } else if (proxyAuth == "ntlm") { args += " --proxy-ntlm"; } if (SystemShell.EscapeInsideQuoteAcceptable(proxyLogin) == false) { throw new Exception(MessagesFormatter.Format(Messages.UnacceptableCharacters, "Proxy Login")); } if (SystemShell.EscapeInsideQuoteAcceptable(proxyPassword) == false) { throw new Exception(MessagesFormatter.Format(Messages.UnacceptableCharacters, "Proxy Password")); } if ((proxyLogin != "") && (proxyPassword != "")) { args += " --proxy-user \"" + SystemShell.EscapeInsideQuote(proxyLogin) + "\":\"" + SystemShell.EscapeInsideQuote(proxyPassword) + "\""; } } } args += " \"" + SystemShell.EscapeUrl(url) + "\""; args += " -sS"; // -s Silent mode, -S with errors args += " --max-time " + Engine.Instance.Storage.GetInt("tools.curl.max-time").ToString(); Tool cacertTool = Software.GetTool("cacert.pem"); if (cacertTool.Available()) { args += " --cacert \"" + SystemShell.EscapePath(cacertTool.Path) + "\""; } if (resolve != "") { args += " --resolve " + resolve; } if (dataParameters != "") { args += " --data \"" + dataParameters + "\""; } string error = ""; byte[] output = default(byte[]); int exitcode = -1; try { /* * if ((Engine.Instance != null) && (Engine.Instance.Storage != null) && (Engine.Instance.Storage.GetBool("log.level.debug"))) * { * string message = "curl " + this.GetPath() + " " + args; * message = Utils.RegExReplace(message, "[a-zA-Z0-9+/]{30,}=", "{base64-omissis}"); * Engine.Instance.Logs.Log(LogType.Verbose, message); * } */ Process p = new Process(); p.StartInfo.FileName = SystemShell.EscapePath(this.GetPath()); p.StartInfo.Arguments = args; p.StartInfo.WorkingDirectory = ""; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); using (var memoryStream = new System.IO.MemoryStream()) { //p.StandardOutput.BaseStream.CopyTo(memstream); // .Net 4 only Utils.CopyStream(p.StandardOutput.BaseStream, memoryStream); output = memoryStream.ToArray(); } error = p.StandardError.ReadToEnd(); p.WaitForExit(); exitcode = p.ExitCode; } catch (Exception e) { error = e.Message; output = default(byte[]); } programScope.End(); if (error != "") { throw new Exception(error.Trim()); } return(output); }