Esempio n. 1
0
        private static void UpdateAppImagesFile(bool large)
        {
            var filePath = large ? CachePaths.AppImagesLarge : CachePaths.AppImages;
            var fileName = Path.GetFileName(filePath);

            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }
            var fileDate = File.Exists(filePath) ? File.GetLastWriteTime(filePath) : DateTime.MinValue;

            foreach (var mirror in AppSupply.GetMirrors(AppSuppliers.Internal))
            {
                var link = PathEx.AltCombine(mirror, ".free", fileName);
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (!NetEx.FileIsAvailable(link, 30000, UserAgents.Internal))
                {
                    continue;
                }
                if (!((NetEx.GetFileDate(link, 30000, UserAgents.Internal) - fileDate).TotalSeconds > 0d))
                {
                    break;
                }
                NetEx.Transfer.DownloadFile(link, filePath, 60000, UserAgents.Internal, false);
                if (!File.Exists(filePath))
                {
                    continue;
                }
                File.SetLastWriteTime(filePath, DateTime.Now);
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: '{filePath}' has been updated.");
                }
                break;
            }
        }
Esempio n. 2
0
        private static void UpdateAppInfoFile()
        {
            ResetAppInfoFile();
            if (_appInfo?.Count > 430)
            {
                goto Shareware;
            }

            foreach (var mirror in AppSupply.GetMirrors(AppSuppliers.Internal))
            {
                var link = PathEx.AltCombine(mirror, ".free", "AppInfo.ini");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (NetEx.FileIsAvailable(link, 30000, UserAgents.Internal))
                {
                    NetEx.Transfer.DownloadFile(link, CachePaths.AppInfo, 60000, UserAgents.Internal, false);
                }
                if (!File.Exists(CachePaths.AppInfo))
                {
                    continue;
                }
                break;
            }

            var blacklist = Array.Empty <string>();

            if (File.Exists(CachePaths.AppInfo))
            {
                blacklist = Ini.GetSections(CachePaths.AppInfo).Where(x => Ini.Read(x, "Disabled", false, CachePaths.AppInfo)).ToArray();
                UpdateAppInfoData(CachePaths.AppInfo, blacklist);
            }

            var tmpDir = Path.Combine(CorePaths.TempDir, PathEx.GetTempDirName());

            if (!DirectoryEx.Create(tmpDir))
            {
                return;
            }
            var tmpZip = Path.Combine(tmpDir, "AppInfo.7z");

            foreach (var mirror in AppSupply.GetMirrors(AppSuppliers.Internal))
            {
                var link = PathEx.AltCombine(mirror, ".free", "AppInfo.7z");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (NetEx.FileIsAvailable(link, 30000, UserAgents.Internal))
                {
                    NetEx.Transfer.DownloadFile(link, tmpZip, 60000, UserAgents.Internal, false);
                }
                if (!File.Exists(tmpZip))
                {
                    continue;
                }
                break;
            }
            if (!File.Exists(tmpZip))
            {
                var link = PathEx.AltCombine(AppSupplierHosts.PortableApps, "updater", "update.7z");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (NetEx.FileIsAvailable(link, 60000, UserAgents.Empty))
                {
                    NetEx.Transfer.DownloadFile(link, tmpZip, 60000, UserAgents.Empty, false);
                }
            }
            if (File.Exists(tmpZip))
            {
                using (var process = Compaction.SevenZipHelper.Unzip(tmpZip, tmpDir))
                    if (process?.HasExited == false)
                    {
                        process.WaitForExit();
                    }
                FileEx.TryDelete(tmpZip);
            }
            var tmpIni = DirectoryEx.GetFiles(tmpDir, "*.ini").FirstOrDefault();

            if (!File.Exists(tmpIni))
            {
                DirectoryEx.TryDelete(tmpDir);
                return;
            }
            UpdateAppInfoData(tmpIni, blacklist);

            FileEx.Serialize(CachePaths.AppInfo, AppInfo, true);
            DirectoryEx.TryDelete(tmpDir);

Shareware:
            if (!Shareware.Enabled)
            {
                return;
            }

            foreach (var srv in Shareware.GetAddresses())
            {
                var key = Shareware.FindAddressKey(srv);
                var usr = Shareware.GetUser(srv);
                var pwd = Shareware.GetPassword(srv);
                var url = PathEx.AltCombine(srv, "AppInfo.ini");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Shareware: Looking for '{{{key.Encode()}}}/AppInfo.ini'.");
                }
                if (!NetEx.FileIsAvailable(url, usr, pwd, 60000, UserAgents.Default))
                {
                    continue;
                }
                var appInfo = NetEx.Transfer.DownloadString(url, usr, pwd, 60000, UserAgents.Default);
                if (string.IsNullOrWhiteSpace(appInfo))
                {
                    continue;
                }
                UpdateAppInfoData(appInfo, null, key.Decode(BinaryToTextEncodings.Base85));
            }
        }
Esempio n. 3
0
        public AppTransferor(AppData appData)
        {
            AppData  = appData ?? throw new ArgumentNullException(nameof(appData));
            DestPath = default;
            SrcData  = new List <Tuple <string, string, string, bool> >();
            UserData = Tuple.Create(default(string), default(string));

            var downloadCollection = AppData.DownloadCollection;
            var packageVersion     = default(string);

            if (ActionGuid.IsUpdateInstance && AppData?.UpdateCollection?.SelectMany(x => x.Value).All(x => x?.Item1?.StartsWithEx("http") == true) == true)
            {
                var appIniDir  = Path.Combine(appData.InstallDir, "App", "AppInfo");
                var appIniPath = Path.Combine(appIniDir, "appinfo.ini");
                if (!File.Exists(appIniPath))
                {
                    appIniPath = Path.Combine(appIniDir, "plugininstaller.ini");
                }
                packageVersion = Ini.Read("Version", nameof(appData.PackageVersion), default(string), appIniPath);
                if (!string.IsNullOrEmpty(packageVersion) && AppData?.UpdateCollection?.ContainsKey(packageVersion) == true)
                {
                    downloadCollection = AppData.UpdateCollection;
                }
            }

            foreach (var pair in downloadCollection)
            {
                if (!pair.Key.EqualsEx(AppData.Settings.ArchiveLang) && (string.IsNullOrEmpty(packageVersion) || !pair.Key.EqualsEx(packageVersion)))
                {
                    continue;
                }

                foreach (var(item1, item2) in pair.Value)
                {
                    var srcUrl = item1;
                    if (srcUrl.StartsWith("{", StringComparison.InvariantCulture) && srcUrl.EndsWith("}", StringComparison.InvariantCulture))
                    {
                        srcUrl = FindArchivePath(srcUrl).Item2;
                    }

                    if (DestPath == default)
                    {
                        if (!DirectoryEx.Create(Settings.TransferDir))
                        {
                            continue;
                        }
                        var fileName = Path.GetFileName(srcUrl);
                        if (string.IsNullOrEmpty(fileName))
                        {
                            continue;
                        }
                        DestPath = PathEx.Combine(Settings.TransferDir, fileName);
                    }

                    var           shortHost = NetEx.GetShortHost(srcUrl);
                    var           redirect  = Settings.ForceTransferRedirection || !NetEx.IPv4IsAvalaible && !string.IsNullOrWhiteSpace(shortHost) && !shortHost.EqualsEx(AppSupplierHosts.Internal);
                    string        userAgent;
                    List <string> mirrors;
                    switch (shortHost)
                    {
                    case AppSupplierHosts.Internal:
                        userAgent = UserAgents.Internal;
                        mirrors   = AppSupply.GetMirrors(AppSuppliers.Internal);
                        break;

                    case AppSupplierHosts.PortableApps:
                        userAgent = UserAgents.Empty;
                        mirrors   = AppSupply.GetMirrors(AppSuppliers.PortableApps);
                        break;

                    case AppSupplierHosts.SourceForge:
                        userAgent = UserAgents.Default;
                        mirrors   = AppSupply.GetMirrors(AppSuppliers.SourceForge);
                        break;

                    default:
                        userAgent = UserAgents.Default;
                        if (AppData.ServerKey != default)
                        {
                            var srv = Shareware.GetAddresses().FirstOrDefault(x => Shareware.FindAddressKey(x) == AppData.ServerKey.ToArray().Encode(BinaryToTextEncoding.Base85));
                            if (srv != default)
                            {
                                if (!srcUrl.StartsWithEx("http://", "https://"))
                                {
                                    srcUrl = PathEx.AltCombine(srv, srcUrl);
                                }
                                UserData = Tuple.Create(Shareware.GetUser(srv), Shareware.GetPassword(srv));
                            }
                        }
                        SrcData.Add(Tuple.Create(srcUrl, item2, userAgent, false));
                        continue;
                    }

                    var sHost = NetEx.GetShortHost(srcUrl);
                    var fhost = srcUrl.Substring(0, srcUrl.IndexOf(sHost, StringComparison.OrdinalIgnoreCase) + sHost.Length);
                    foreach (var mirror in mirrors)
                    {
                        if (!fhost.EqualsEx(mirror))
                        {
                            srcUrl = srcUrl.Replace(fhost, mirror);
                        }
                        if (SrcData.Any(x => x.Item1.EqualsEx(srcUrl)))
                        {
                            continue;
                        }
                        if (redirect)
                        {
                            userAgent = UserAgents.Internal;
                            srcUrl    = CorePaths.RedirectUrl + srcUrl.Encode();
                        }
                        SrcData.Add(Tuple.Create(srcUrl, item2, userAgent, false));
                        if (Log.DebugMode > 1)
                        {
                            Log.Write($"Transfer: '{srcUrl}' has been added.");
                        }
                    }
                }
                break;
            }
            Transfer = new WebTransferAsync();
        }