コード例 #1
0
ファイル: QBitTorrent.cs プロジェクト: cdamodify/ipfilter
        public async Task <FilterUpdateResult> UpdateFilterAsync(FilterDownloadResult filter, CancellationToken cancellationToken, IProgress <ProgressModel> progress)
        {
            var localPath            = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.SpecialFolderOption.Create);
            var destinationPath      = Path.Combine(localPath, FolderName, "filter", "ipfilter.dat");
            var destinationDirectory = Path.GetDirectoryName(destinationPath);

            if (destinationDirectory != null && !Directory.Exists(destinationDirectory))
            {
                Directory.CreateDirectory(destinationDirectory);
            }

            Trace.TraceInformation("Writing filter to " + destinationPath);
            using (var destination = File.Open(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var writer = new BitTorrentWriter(destination))
                {
                    await writer.Write(filter.Entries, progress);
                }

            // Update qBittorrent config
            var qBittorrentIniPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), FolderName, "qBittorrent.ini");

            if (File.Exists(qBittorrentIniPath))
            {
                Trace.TraceInformation("Pointing qBittorrent to " + destinationPath);
                Trace.TraceInformation("Updating qBittorrent configuration: " + qBittorrentIniPath);

                try
                {
                    WriteIniSetting("Preferences", @"IPFilter\Enabled", "true", qBittorrentIniPath);
                    WriteIniSetting("Preferences", @"IPFilter\File", destinationPath.Replace("\\", "/"), qBittorrentIniPath);
                }
                catch (Exception ex)
                {
                    Trace.TraceWarning("Couldn't update qBittorrent configuration: " + ex);
                }
            }

            return(new FilterUpdateResult {
                FilterTimestamp = filter.FilterTimestamp
            });
        }
コード例 #2
0
        public async Task Write()
        {
            var entries = new List <FilterEntry>
            {
                new FilterEntry(0x01020304, 0x010203FF)
                {
                    Description = "Testing the description"
                }
            };

            using (var stream = new MemoryStream())
            {
                using (var writer = new BitTorrentWriter(stream))
                {
                    var progress = new Mock <IProgress <ProgressModel> >();
                    await writer.Write(entries, progress.Object);
                }

                Assert.AreEqual("001.002.003.004 - 001.002.003.255 , 000 , Testing the description\r\n", Encoding.ASCII.GetString(stream.ToArray()));
            }
        }
コード例 #3
0
        public async Task <FilterUpdateResult> UpdateFilterAsync(FilterDownloadResult filter, CancellationToken cancellationToken, IProgress <ProgressModel> progress)
        {
            var roamingPath     = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create);
            var destinationPath = Path.Combine(roamingPath, FolderName, "ipfilter.dat");

            Trace.TraceInformation("Writing filter to " + destinationPath);
            using (var destination = File.Open(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var writer = new BitTorrentWriter(destination))
                {
                    await writer.Write(filter.Entries, progress);
                }

            return(new FilterUpdateResult {
                FilterTimestamp = filter.FilterTimestamp
            });

            // TODO: Check if IP Filter is enabled in µTorrent
//            string settingsPath = Environment.ExpandEnvironmentVariables(@"%APPDATA%\uTorrent\settings.dat");
//            var settings = File.ReadAllText(settingsPath);
//            if (settings.Contains("15:ipfilter.enablei0e"))
//            {
//                MessageBox.Show("You haven't enabled IP Filtering in µTorrent! Go to http://ipfilter.codeplex.com/ for help.", "IP filtering not enabled", MessageBoxButton.OK);
//            }
        }