示例#1
0
        public void AddTorrentFromFile(string fileName, byte[] fileContent, UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("action", "add-file");
            arguments.Add("path", string.Empty);

            var client = BuildClient(settings);

            // add-file should use POST unlike all other methods which are GET
            var request = new RestRequest(Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.Resource      = "/gui/";
            request.AddParameter("token", _authToken, ParameterType.QueryString);

            foreach (var argument in arguments)
            {
                request.AddParameter(argument.Key, argument.Value, ParameterType.QueryString);
            }

            request.AddFile("torrent_file", fileContent, fileName, @"application/octet-stream");

            ProcessRequest(request, client);
        }
示例#2
0
        public void MoveTorrentToTopInQueue(string hash, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", "queuetop")
                                 .AddQueryParam("hash", hash);

            ProcessRequest(requestBuilder, settings);
        }
示例#3
0
        public void SetState(string hash, UTorrentState state, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", state.ToString().ToLowerInvariant())
                                 .AddQueryParam("hash", hash);

            ProcessRequest(requestBuilder, settings);
        }
示例#4
0
        public void AddTorrentFromUrl(string torrentUrl, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", "add-url")
                                 .AddQueryParam("s", torrentUrl);

            ProcessRequest(requestBuilder, settings);
        }
示例#5
0
        public void RemoveTorrent(string hash, bool removeData, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", removeData ? "removedata" : "remove")
                                 .AddQueryParam("hash", hash);

            ProcessRequest(requestBuilder, settings);
        }
示例#6
0
        public void MoveTorrentToTopInQueue(string hash, UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("action", "queuetop");
            arguments.Add("hash", hash);

            ProcessRequest(arguments, settings);
        }
示例#7
0
        public List <UTorrentTorrent> GetTorrents(UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("list", 1);

            var result = ProcessRequest(requestBuilder, settings);

            return(result.Torrents);
        }
示例#8
0
        public void AddTorrentFromUrl(string torrentUrl, UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("action", "add-url");
            arguments.Add("s", torrentUrl);

            ProcessRequest(arguments, settings);
        }
示例#9
0
        public int GetVersion(UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", "getsettings");

            var result = ProcessRequest(requestBuilder, settings);

            return(result.Build);
        }
示例#10
0
        public int GetVersion(UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("action", "getsettings");

            var result = ProcessRequest(arguments, settings);

            return(result.Build);
        }
示例#11
0
        public List <UTorrentTorrent> GetTorrents(UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("list", 1);

            var result = ProcessRequest(arguments, settings);

            return(result.Torrents);
        }
示例#12
0
        public void AddTorrentFromFile(string fileName, byte[] fileContent, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .Post()
                                 .AddQueryParam("action", "add-file")
                                 .AddQueryParam("path", string.Empty)
                                 .AddFormUpload("torrent_file", fileName, fileContent, @"application/octet-stream");

            ProcessRequest(requestBuilder, settings);
        }
示例#13
0
        public void SetTorrentLabel(string hash, string label, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", "setprops")
                                 .AddQueryParam("hash", hash);

            requestBuilder.AddQueryParam("s", "label")
            .AddQueryParam("v", label);

            ProcessRequest(requestBuilder, settings);
        }
示例#14
0
        public void SetTorrentLabel(string hash, string label, UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("action", "setprops");
            arguments.Add("hash", hash);

            arguments.Add("s", "label");
            arguments.Add("v", label);

            ProcessRequest(arguments, settings);
        }
示例#15
0
        private HttpRequestBuilder BuildRequest(UTorrentSettings settings)
        {
            var requestBuilder = new HttpRequestBuilder(false, settings.Host, settings.Port)
                                 .Resource("/gui/")
                                 .KeepAlive()
                                 .SetHeader("Cache-Control", "no-cache")
                                 .Accept(HttpAccept.Json);

            requestBuilder.LogResponseContent = true;
            requestBuilder.NetworkCredential  = new NetworkCredential(settings.Username, settings.Password);

            return(requestBuilder);
        }
示例#16
0
        public UTorrentResponse GetTorrents(string cacheID, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("list", 1);

            if (cacheID.IsNotNullOrWhiteSpace())
            {
                requestBuilder.AddQueryParam("cid", cacheID);
            }

            var result = ProcessRequest(requestBuilder, settings);

            return(result);
        }
示例#17
0
        private void AuthenticateClient(HttpRequestBuilder requestBuilder, UTorrentSettings settings, bool reauthenticate = false)
        {
            var authKey = string.Format("{0}:{1}", requestBuilder.BaseUrl, settings.Password);

            var cookies   = _authCookieCache.Find(authKey);
            var authToken = _authTokenCache.Find(authKey);

            if (cookies == null || authToken == null || reauthenticate)
            {
                _authCookieCache.Remove(authKey);
                _authTokenCache.Remove(authKey);

                var authLoginRequest = BuildRequest(settings).Resource("/gui/token.html").Build();

                HttpResponse response;
                try
                {
                    response = _httpClient.Execute(authLoginRequest);
                    _logger.Debug("uTorrent authentication succeeded.");

                    var xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.LoadXml(response.Content);

                    authToken = xmlDoc.FirstChild.FirstChild.InnerText;
                }
                catch (HttpException ex)
                {
                    if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        _logger.Debug("uTorrent authentication failed.");
                        throw new DownloadClientAuthenticationException("Failed to authenticate with uTorrent.");
                    }

                    throw new DownloadClientException("Unable to connect to uTorrent, please check your settings", ex);
                }
                catch (WebException ex)
                {
                    throw new DownloadClientException("Unable to connect to uTorrent, please check your settings", ex);
                }

                cookies = response.GetCookies();

                _authCookieCache.Set(authKey, cookies);
                _authTokenCache.Set(authKey, authToken);
            }

            requestBuilder.SetCookies(cookies);
            requestBuilder.AddPrefixQueryParam("token", authToken, true);
        }
示例#18
0
        public Dictionary <string, string> GetConfig(UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", "getsettings");

            var result = ProcessRequest(requestBuilder, settings);

            var configuration = new Dictionary <string, string>();

            foreach (var configItem in result.Settings)
            {
                configuration.Add(configItem[0].ToString(), configItem[2].ToString());
            }

            return(configuration);
        }
示例#19
0
        public Dictionary <string, string> GetConfig(UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            arguments.Add("action", "getsettings");

            var result = ProcessRequest(arguments, settings);

            var configuration = new Dictionary <string, string>();

            foreach (var configItem in result.Settings)
            {
                configuration.Add(configItem[0].ToString(), configItem[2].ToString());
            }

            return(configuration);
        }
示例#20
0
        public void RemoveTorrent(string hash, bool removeData, UTorrentSettings settings)
        {
            var arguments = new Dictionary <string, object>();

            if (removeData)
            {
                arguments.Add("action", "removedata");
            }
            else
            {
                arguments.Add("action", "remove");
            }

            arguments.Add("hash", hash);

            ProcessRequest(arguments, settings);
        }
示例#21
0
        private IRestClient BuildClient(UTorrentSettings settings)
        {
            var url = string.Format(@"http://{0}:{1}",
                                    settings.Host,
                                    settings.Port);

            var restClient = RestClientFactory.BuildClient(url);

            restClient.Authenticator   = new HttpBasicAuthenticator(settings.Username, settings.Password);
            restClient.CookieContainer = _cookieContainer;

            if (_authToken.IsNullOrWhiteSpace())
            {
                // µTorrent requires a token and cookie for authentication. The cookie is set automatically when getting the token.
                _authToken = GetAuthToken(restClient);
            }

            return(restClient);
        }
示例#22
0
        public UTorrentResponse ProcessRequest(HttpRequestBuilder requestBuilder, UTorrentSettings settings)
        {
            AuthenticateClient(requestBuilder, settings);

            var request = requestBuilder.Build();

            HttpResponse response;

            try
            {
                response = _httpClient.Execute(request);
            }
            catch (HttpException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.BadRequest || ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _logger.Debug("Authentication required, logging in.");

                    AuthenticateClient(requestBuilder, settings, true);

                    request = requestBuilder.Build();

                    response = _httpClient.Execute(request);
                }
                else
                {
                    throw new DownloadClientException("Unable to connect to uTorrent, please check your settings", ex);
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.TrustFailure)
                {
                    throw new DownloadClientUnavailableException("Unable to connect to uTorrent, certificate validation failed.", ex);
                }

                throw new DownloadClientException("Unable to connect to uTorrent, please check your settings", ex);
            }

            return(Json.Deserialize <UTorrentResponse>(response.Content));
        }
示例#23
0
        public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, UTorrentSettings settings)
        {
            var arguments = new List <KeyValuePair <string, object> >();

            arguments.Add("action", "setprops");
            arguments.Add("hash", hash);

            arguments.Add("s", "seed_override");
            arguments.Add("v", 1);

            if (seedConfiguration.Ratio != null)
            {
                arguments.Add("s", "seed_ratio");
                arguments.Add("v", Convert.ToInt32(seedConfiguration.Ratio.Value * 1000));
            }

            if (seedConfiguration.SeedTime != null)
            {
                arguments.Add("s", "seed_time");
                arguments.Add("v", Convert.ToInt32(seedConfiguration.SeedTime.Value.TotalSeconds));
            }

            ProcessRequest(arguments, settings);
        }
示例#24
0
        public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, UTorrentSettings settings)
        {
            var requestBuilder = BuildRequest(settings)
                                 .AddQueryParam("action", "setprops")
                                 .AddQueryParam("hash", hash);

            requestBuilder.AddQueryParam("s", "seed_override")
            .AddQueryParam("v", 1);

            if (seedConfiguration.Ratio != null)
            {
                requestBuilder.AddQueryParam("s", "seed_ratio")
                .AddQueryParam("v", Convert.ToInt32(seedConfiguration.Ratio.Value * 1000));
            }

            if (seedConfiguration.SeedTime != null)
            {
                requestBuilder.AddQueryParam("s", "seed_time")
                .AddQueryParam("v", Convert.ToInt32(seedConfiguration.SeedTime.Value.TotalSeconds));
            }

            ProcessRequest(requestBuilder, settings);
        }
示例#25
0
        public UTorrentResponse ProcessRequest(IEnumerable <KeyValuePair <string, object> > arguments, UTorrentSettings settings)
        {
            var client = BuildClient(settings);

            var request = new RestRequest(Method.GET);

            request.RequestFormat = DataFormat.Json;
            request.Resource      = "/gui/";
            request.AddParameter("token", _authToken, ParameterType.QueryString);

            foreach (var argument in arguments)
            {
                request.AddParameter(argument.Key, argument.Value, ParameterType.QueryString);
            }

            return(ProcessRequest(request, client));
        }