Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }