Example #1
0
        private SaltAndNonce GetSaltAndNonce()
        {
            var httpOptions = new Duplicati.Library.Modules.Builtin.HttpOptions();

            httpOptions.Configure(m_options);

            using (httpOptions)
            {
                var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_baseUri + LOGIN_SCRIPT);
                req.Method    = "POST";
                req.UserAgent = "Duplicati TrayIcon Monitor, v" +
                                System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                req.Headers.Add(TRAYICONPASSWORDSOURCE_HEADER, m_TrayIconHeaderValue);
                req.ContentType = "application/x-www-form-urlencoded";

                Duplicati.Library.Utility.AsyncHttpRequest areq = new Library.Utility.AsyncHttpRequest(req);
                var body = System.Text.Encoding.ASCII.GetBytes("get-nonce=1");
                using (var f = areq.GetRequestStream(body.Length))
                    f.Write(body, 0, body.Length);

                using (var r = (System.Net.HttpWebResponse)areq.GetResponse())
                    using (var s = areq.GetResponseStream())
                        using (var sr = new System.IO.StreamReader(s, ENCODING, true))
                            return(Serializer.Deserialize <SaltAndNonce>(sr));
            }
        }
Example #2
0
        private string PerformLogin(string password, string nonce)
        {
            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_baseUri + LOGIN_SCRIPT);
            req.Method    = "POST";
            req.UserAgent = "Duplicati TrayIcon Monitor, v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            req.Headers.Add(TRAYICON_HEADER, "true");
            req.ContentType = "application/x-www-form-urlencoded";
            if (req.CookieContainer == null)
            {
                req.CookieContainer = new System.Net.CookieContainer();
            }
            req.CookieContainer.Add(new System.Net.Cookie("session-nonce", nonce, "/", req.RequestUri.Host));

            //Wrap it all in async stuff
            Duplicati.Library.Utility.AsyncHttpRequest areq = new Library.Utility.AsyncHttpRequest(req);
            var body = System.Text.Encoding.ASCII.GetBytes("password=" + Duplicati.Library.Utility.Uri.UrlEncode(password));

            using (var f = areq.GetRequestStream(body.Length))
                f.Write(body, 0, body.Length);

            using (var r = (System.Net.HttpWebResponse)areq.GetResponse())
                if (r.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return((r.Cookies[AUTH_COOKIE] ?? r.Cookies[Library.Utility.Uri.UrlEncode(AUTH_COOKIE)]).Value);
                }

            return(null);
        }
Example #3
0
        /// <summary>
        /// Runs the upload process
        /// </summary>
        /// <returns>A tuple with the completion task and the channel to use</returns>
        public static Tuple <Task, IWriteChannel <string> > Run()
        {
            var channel = ChannelManager.CreateChannel <string>(
                buffersize: MAX_PENDING_UPLOADS,
                pendingWritersOverflowStrategy: QueueOverflowStrategy.LIFO
                );

            var task = AutomationExtensions.RunTask(
                channel.AsRead(),

                async(chan) =>
            {
                while (true)
                {
                    var f = await chan.ReadAsync();

                    try
                    {
                        if (File.Exists(f))
                        {
                            var req         = (HttpWebRequest)WebRequest.Create(UPLOAD_URL);
                            req.Method      = "POST";
                            req.ContentType = "application/json; charset=utf-8";

                            int rc;
                            using (var fs = File.OpenRead(f))
                            {
                                if (fs.Length > 0)
                                {
                                    req.ContentLength = fs.Length;
                                    var areq          = new Library.Utility.AsyncHttpRequest(req);

                                    using (var rs = areq.GetRequestStream())
                                        Library.Utility.Utility.CopyStream(fs, rs);

                                    using (var resp = (HttpWebResponse)areq.GetResponse())
                                        rc = (int)resp.StatusCode;
                                }
                                else
                                {
                                    rc = 200;
                                }
                            }

                            if (rc >= 200 && rc <= 299)
                            {
                                File.Delete(f);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.Log.WriteErrorMessage(LOGTAG, "UploadFailed", ex, "UsageReporter failed");
                    }
                }
            }
                );

            return(new Tuple <Task, IWriteChannel <string> >(task, channel));
        }
        private T PerformRequestInternal <T>(Dictionary <string, string> queryparams)
        {
            queryparams["format"] = "json";

            string query = EncodeQueryString(queryparams);

            byte[] data = ENCODING.GetBytes(query);

            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(m_controlUri);
            req.Method        = "POST";
            req.ContentLength = data.Length;
            req.ContentType   = "application/x-www-form-urlencoded ; charset=" + ENCODING.BodyName;
            req.Headers.Add("Accept-Charset", ENCODING.BodyName);
            req.UserAgent = "Duplicati TrayIcon Monitor, v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            if (m_authtoken != null)
            {
                if (req.CookieContainer == null)
                {
                    req.CookieContainer = new System.Net.CookieContainer();
                }
                req.CookieContainer.Add(new System.Net.Cookie(AUTH_COOKIE, m_authtoken, "/", req.RequestUri.Host));
            }

            //Wrap it all in async stuff
            Duplicati.Library.Utility.AsyncHttpRequest areq = new Library.Utility.AsyncHttpRequest(req);

            using (System.IO.Stream s = areq.GetRequestStream())
                s.Write(data, 0, data.Length);

            //Assign the timeout, and add a little processing time as well
            if (queryparams["action"] == "get-current-state" && queryparams.ContainsKey("duration"))
            {
                areq.Timeout = (int)(Duplicati.Library.Utility.Timeparser.ParseTimeSpan(queryparams["duration"]) + TimeSpan.FromSeconds(5)).TotalMilliseconds;
            }

            using (System.Net.HttpWebResponse r = (System.Net.HttpWebResponse)areq.GetResponse())
                using (System.IO.Stream s = areq.GetResponseStream())
                    if (typeof(T) == typeof(string))
                    {
                        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                        {
                            s.CopyTo(ms);
                            return((T)(object)ENCODING.GetString(ms.ToArray()));
                        }
                    }
                    else
                    {
                        using (var sr = new System.IO.StreamReader(s, ENCODING, true))
                            return(Serializer.Deserialize <T>(sr));
                    }
        }
Example #5
0
        /// <summary>
        /// Run the processing of incomming requests
        /// </summary>
        private async Task Run()
        {
            while (true)
            {
                var f = await FilterChannel.ReadAsync();

                try
                {
                    if (File.Exists(f))
                    {
                        var req = (HttpWebRequest)WebRequest.Create(UPLOAD_URL);
                        req.Method      = "POST";
                        req.ContentType = "application/json; charset=utf-8";

                        int rc;
                        using (var fs = File.OpenRead(f))
                        {
                            if (fs.Length > 0)
                            {
                                req.ContentLength = fs.Length;
                                var areq = new Library.Utility.AsyncHttpRequest(req);

                                using (var rs = areq.GetRequestStream())
                                    Library.Utility.Utility.CopyStream(fs, rs);

                                using (var resp = (HttpWebResponse)areq.GetResponse())
                                    rc = (int)resp.StatusCode;
                            }
                            else
                            {
                                rc = 200;
                            }
                        }

                        if (rc >= 200 && rc <= 299)
                        {
                            File.Delete(f);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logging.Log.WriteMessage("UsageReporter failed", Duplicati.Library.Logging.LogMessageType.Error, ex);
                }
            }
        }
        /// <summary>
        /// Run the processing of incomming requests
        /// </summary>
        private async Task Run()
        {
            while (true)
            {
                var f = await FilterChannel.ReadAsync();

                try
                {
                    if (File.Exists(f))
                    {
                        var req = (HttpWebRequest)WebRequest.Create(UPLOAD_URL);
                        req.Method = "POST";
                        req.ContentType = "application/json; charset=utf-8";

                        int rc;
                        using(var fs = File.OpenRead(f))
                        {
                            req.ContentLength = fs.Length;
                            var areq = new Library.Utility.AsyncHttpRequest(req);

                            using(var rs =areq.GetRequestStream())
                                Library.Utility.Utility.CopyStream(fs, rs);

                            using(var resp = (HttpWebResponse)areq.GetResponse())
                                rc = (int)resp.StatusCode;
                        }

                        if (rc >= 200 && rc <= 299)
                            File.Delete(f);
                    }
                }
                catch (Exception ex)
                {
                    Logging.Log.WriteMessage("UsageReporter failed", Duplicati.Library.Logging.LogMessageType.Error, ex);
                }
            }        
        }