Пример #1
0
 public PACDaemon(PACSettings pACSettings, string workingDirectory, string dlcPath)
 {
     _PACSettings    = pACSettings;
     _geositeUpdater = new GeositeUpdater(dlcPath);
     TouchPACFile();
     TouchUserRuleFile();
     WatchPacFile(workingDirectory);
     WatchUserRuleFile(workingDirectory);
 }
Пример #2
0
        public async Task UpdatePACFromGeosite(PACSettings pACSettings)
        {
            string geositeUrl          = GEOSITE_URL;
            string geositeSha256sumUrl = GEOSITE_SHA256SUM_URL;
            SHA256 mySHA256            = SHA256.Create();
            bool   blacklist           = pACSettings.PACDefaultToDirect;
            var    httpClient          = Locator.Current.GetService <HttpClient>();

            if (!string.IsNullOrWhiteSpace(pACSettings.CustomGeositeUrl))
            {
                this.Log().Info("Found custom Geosite URL in config file");
                geositeUrl = pACSettings.CustomGeositeUrl;
            }
            this.Log().Info($"Checking Geosite from {geositeUrl}");

            try
            {
                // download checksum first
                var geositeSha256sum = await httpClient.GetStringAsync(geositeSha256sumUrl);

                geositeSha256sum = geositeSha256sum.Substring(0, 64).ToUpper();
                this.Log().Info($"Got Sha256sum: {geositeSha256sum}");
                // compare downloaded checksum with local geositeDB
                byte[] localDBHashBytes = mySHA256.ComputeHash(geositeDB);
                string localDBHash      = BitConverter.ToString(localDBHashBytes).Replace("-", String.Empty);
                this.Log().Info($"Local Sha256sum: {localDBHash}");
                // if already latest
                if (geositeSha256sum == localDBHash)
                {
                    this.Log().Info("Local GeoSite DB is up to date.");
                    return;
                }

                // not latest. download new DB
                var downloadedBytes = await httpClient.GetByteArrayAsync(geositeUrl);

                // verify sha256sum
                byte[] downloadedDBHashBytes = mySHA256.ComputeHash(downloadedBytes);
                string downloadedDBHash      = BitConverter.ToString(downloadedDBHashBytes).Replace("-", String.Empty);
                this.Log().Info($"Actual Sha256sum: {downloadedDBHash}");
                if (geositeSha256sum != downloadedDBHash)
                {
                    this.Log().Info("Sha256sum Verification: FAILED. Downloaded GeoSite DB is corrupted. Aborting the update.");
                    throw new Exception("Sha256sum mismatch");
                }
                else
                {
                    this.Log().Info("Sha256sum Verification: PASSED. Applying to local GeoSite DB.");
                }

                // write to geosite file
                using (FileStream geositeFileStream = File.Create(DATABASE_PATH))
                    await geositeFileStream.WriteAsync(downloadedBytes, 0, downloadedBytes.Length);

                // update stuff
                geositeDB = downloadedBytes;
                LoadGeositeList();
                bool pacFileChanged = MergeAndWritePACFile(pACSettings.GeositeDirectGroups, pACSettings.GeositeProxiedGroups, blacklist);
                UpdateCompleted?.Invoke(null, new GeositeResultEventArgs(pacFileChanged));
            }
            catch (Exception e)
            {
                this.Log().Error(e, "An error occurred while updating PAC.");
            }
        }