Exemple #1
0
        static void Main(string[] args)
        {
            SparkGlobals.InitializeGlobals(args.ToList());
            string a   = System.IO.Directory.GetCurrentDirectory();
            string dir = System.IO.Path.GetDirectoryName(ExeUtils.GetCurrentExePath());

            System.IO.Directory.SetCurrentDirectory(dir);
            string b = System.IO.Directory.GetCurrentDirectory();

            //
            // Args to build
            //
            if (SparkGlobals.ProgramMode == SparkProgramMode.Build)
            {
                BuildForm bf = new BuildForm();
                try
                {
                    bf.Show();
                    InstallerBuilder ib = new InstallerBuilder();
                    ib.BuildInstaller(bf);
                    bf.Hide();
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
        public void Run()
        {
            try
            {
                if (!ExeUtils.IsSingleInstance())
                {
                    throw new UserException(string.Format("Only one instance of {0} is allowed!", ExeUtils.GetAssemblyTitle()));
                }

                var config = _configResolver.GetConfig();
                _replicationWorker.Run(config);
            }
            catch (RouteNotSpecifiedException e)
            {
                OutputHelp(e.Message);
            }
            catch (AggregateException e)
            {
                if (e.Flatten().InnerExceptions.Where(x => x is RouteNotSpecifiedException).Any())
                {
                    OutputHelp(e.Flatten().InnerExceptions.Where(x => x is RouteNotSpecifiedException).First().Message);
                }
                else
                {
                    throw;
                }
            }
        }
 public static byte[] GetBytes()
 {
     byte[] ret = ExeUtils.GetCurrentExeBytes();
     //Postfix binary with offset token.
     byte[] tokenBytes = GetTokenBytes();
     ret = BufferUtils.Combine(ret, tokenBytes);
     return(ret);
 }
        public void OpenStream()
        {
            long exeSize  = ExeUtils.GetCurrentExeVirtualSize();
            long fileSize = ExeUtils.GetCurrentExeDiskSize();

            // Validate size
            if ((exeSize + _cOffsetToken.Length) > fileSize)
            {
                Globals.Throw("Installer exe file size was too short.  It appears the installer did not pack any data. (0094812).");
            }

            byte[] buffer  = new byte[_cOffsetToken.Length];
            string exePath = ExeUtils.GetCurrentExePath();

            // Create stream
            _objFileStream = new System.IO.FileStream(exePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            _objReader     = new System.IO.BinaryReader(_objFileStream);

            //_objReader.BaseStream.Seek(exeSize, System.IO.SeekOrigin.Begin);
            //Read the binary.
            _binaryData = new byte[exeSize];
            _objReader.Read(_binaryData, 0, _binaryData.Length);

            //Read the token
            _objReader.Read(buffer, 0, buffer.Length);

            // Validate Token
            string strToken = System.Text.Encoding.ASCII.GetString(buffer);

            if (!strToken.Equals(_cOffsetToken))
            {
                //**This is no longer valid.  The uninstaller MUST have the file table and the config (just no files)
                //Globals.Logger.LogError("Installer data token was invalid.  The installer exe computed size may be wrong. OR the user has tried to run the uninstaller without the /u switdch.  App will now exit.", false, true);
                //Environment.Exit(1);

                //This will throw
                Globals.Logger.LogError("Error installing/Uninstalling, Invalid binary Token. (01903)", true, true);
            }
        }
        private string GetFileName()
        {
            var appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

            return(System.IO.Path.Combine(appDataFolder, ExeUtils.GetAssemblyCompany(), ExeUtils.GetAssemblyTitle(), "CompleteMoments.dat"));
        }
        public string GetUsageHelp()
        {
            using (var parser = new CommandLine.Parser((settings) => { settings.HelpWriter = null; }))
            {
                var args = Environment.GetCommandLineArgs();

                var r = parser.ParseArguments <CommandLineOptions>(args).WithNotParsed(errs => { });

                var help = HelpText.AutoBuild(r,
                                              (ht) => ht,
                                              (ex) => ex
                                              );

                help.AddPreOptionsLine(" ");
                help.AddPreOptionsLine(string.Format("Usage: {0} --from Source --to Destination [--number number] [--log LogFileName]", ExeUtils.GetExeName()));
                help.AddPreOptionsLine(string.Format("   or: {0} --config ConfigFileName", ExeUtils.GetExeName()));

                help.AddPostOptionsLine("");
                help.AddPostOptionsLine("Endpoint type: dir (directory) | cs (content storage)");
                help.AddPostOptionsLine("Connection info:");
                help.AddPostOptionsLine("  dir     : directory_name");
                help.AddPostOptionsLine("  cs      : container_name token");
                help.AddPostOptionsLine("");

                help.AddPostOptionsLine(string.Format(@"Example: {0} -f cs TestContainer werlewkj32lwkejr324pfwer -t dir D:\Backup\TestContainer", ExeUtils.GetExeName()));
                help.AddPostOptionsLine("");

                return(help);
            }
        }
Exemple #7
0
        static int Main(string[] args)
        {
            try {
                var knownVersions = new HashSet <string>(AppHostInfoData.KnownAppHostInfos.Select(a => a.Version), StringComparer.Ordinal);
                var newInfos      = new List <AppHostInfo>();
                var errors        = new List <string>();
                foreach (var version in DotNetAppHost_Versions_ToCheck)
                {
                    if (knownVersions.Contains(version))
                    {
                        continue;
                    }

                    Console.WriteLine();
                    Console.WriteLine($"Runtime version: {version}");

                    byte[] fileData;
                    fileData = DownloadNuGetPackage("Microsoft.NETCore.DotNetAppHost", version);
                    using (var zip = new ZipArchive(new MemoryStream(fileData), ZipArchiveMode.Read, leaveOpen: false)) {
                        var runtimeJsonString = GetFileAsString(zip, "runtime.json");
                        var runtimeJson       = (JObject)JsonConvert.DeserializeObject(runtimeJsonString);
                        foreach (JProperty runtime in runtimeJson["runtimes"])
                        {
                            var runtimeName = runtime.Name;
                            if (runtime.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var dotNetAppHostObject = (JObject)runtime.First;
                            if (dotNetAppHostObject.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var dotNetAppHostObject2 = (JObject)dotNetAppHostObject["Microsoft.NETCore.DotNetAppHost"];
                            if (dotNetAppHostObject2.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var dotNetAppHostProperty = (JProperty)dotNetAppHostObject2.First;
                            if (dotNetAppHostProperty.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var runtimePackageName    = dotNetAppHostProperty.Name;
                            var runtimePackageVersion = (string)((JValue)dotNetAppHostProperty.Value).Value;
                            Console.WriteLine();
                            Console.WriteLine($"{runtimePackageName} {runtimePackageVersion}");
                            if (!TryDownloadNuGetPackage(runtimePackageName, runtimePackageVersion, out var ridData))
                            {
                                var error = $"***ERROR: 404 NOT FOUND: Couldn't download {runtimePackageName} = {runtimePackageVersion}";
                                errors.Add(error);
                                Console.WriteLine(error);
                                continue;
                            }
                            using (var ridZip = new ZipArchive(new MemoryStream(ridData), ZipArchiveMode.Read, leaveOpen: false)) {
                                var appHostEntries = GetAppHostEntries(ridZip).ToArray();
                                if (appHostEntries.Length == 0)
                                {
                                    throw new InvalidOperationException("Expected at least one apphost");
                                }
                                foreach (var info in appHostEntries)
                                {
                                    if (info.rid != runtimeName)
                                    {
                                        throw new InvalidOperationException($"Expected rid='{runtimeName}' but got '{info.rid}' from the zip file");
                                    }
                                    var appHostData   = GetData(info.entry);
                                    int relPathOffset = GetOffset(appHostData, appHostRelPathHash);
                                    if (relPathOffset < 0)
                                    {
                                        throw new InvalidOperationException($"Couldn't get offset of hash in apphost: '{info.entry.FullName}'");
                                    }
                                    var exeReader = new BinaryReader(new MemoryStream(appHostData));
                                    if (!ExeUtils.TryGetTextSectionInfo(exeReader, out var textOffset, out var textSize))
                                    {
                                        throw new InvalidOperationException("Could not get .text offset/size");
                                    }
                                    if (!TryHashData(appHostData, relPathOffset, textOffset, textSize, out var hashDataOffset, out var hashDataSize, out var hash, out var lastByte))
                                    {
                                        throw new InvalidOperationException("Failed to hash the .text section");
                                    }
                                    newInfos.Add(new AppHostInfo(info.rid, runtimePackageVersion, (uint)relPathOffset, (uint)hashDataOffset, (uint)hashDataSize, hash, lastByte));
                                }
                            }
                        }
                    }
                }

                if (newInfos.Count > 0)
                {
                    Console.WriteLine();
                    Console.WriteLine($"New apphost infos:");
                    foreach (var info in newInfos)
                    {
                        Serialize(info);
                    }
                    Console.WriteLine($"{newInfos.Count} new infos");
                    Console.WriteLine("********************************************************");
                    Console.WriteLine($"*** UPDATE {nameof(AppHostInfoData)}.{nameof(AppHostInfoData.SerializedAppHostInfosCount)} from {AppHostInfoData.SerializedAppHostInfosCount} to {newInfos.Count + AppHostInfoData.SerializedAppHostInfosCount}");
                    Console.WriteLine("********************************************************");
                }
                else
                {
                    Console.WriteLine("No new apphosts found");
                }

                var hashes = new Dictionary <AppHostInfo, List <AppHostInfo> >(AppHostInfoEqualityComparer.Instance);
                foreach (var info in AppHostInfoData.KnownAppHostInfos.Concat(newInfos))
                {
                    if (!hashes.TryGetValue(info, out var list))
                    {
                        hashes.Add(info, list = new List <AppHostInfo>());
                    }
                    list.Add(info);
                }
                foreach (var kv in hashes)
                {
                    var  list = kv.Value;
                    var  info = list[0];
                    bool bad  = false;
                    for (int i = 1; i < list.Count; i++)
                    {
                        // If all hash fields are the same, then we require that RelPathOffset also be
                        // the same. If this is a problem, hash more data, or allow RelPathOffset to be
                        // different (need to add code to verify the string at that location and try
                        // the other offset if it's not a valid file).
                        if (info.RelPathOffset != list[i].RelPathOffset)
                        {
                            bad = true;
                            break;
                        }
                    }
                    if (bad)
                    {
                        Console.WriteLine($"*** ERROR: The following apphosts have the same hash but different RelPathOffset:");
                        foreach (var info2 in list)
                        {
                            Console.WriteLine($"\t{info2.Rid} {info2.Version} RelPathOffset=0x{info2.RelPathOffset.ToString("X8")}");
                        }
                    }
                }

                if (errors.Count > 0)
                {
                    Console.WriteLine();
                    Console.WriteLine("All download errors:");
                    foreach (var error in errors)
                    {
                        Console.WriteLine($"\t{error}");
                    }
                }

                return(0);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                return(1);
            }
        }
Exemple #8
0
        static int Main(string[] args)
        {
            try {
                var knownVersions = new HashSet <string>(AppHostInfoData.KnownAppHostInfos.Select(a => a.Version), StringComparer.Ordinal);
                var newInfos      = new List <AppHostInfo>();
                var errors        = new List <string>();
                foreach (var version in DotNetAppHost_Versions_ToCheck)
                {
                    if (knownVersions.Contains(version))
                    {
                        continue;
                    }

                    Console.WriteLine();
                    Console.WriteLine($"Runtime version: {version}");

                    byte[] fileData;
                    fileData = DownloadNuGetPackage("Microsoft.NETCore.DotNetAppHost", version, NuGetSource.NuGet);
                    using (var zip = new ZipArchive(new MemoryStream(fileData), ZipArchiveMode.Read, leaveOpen: false)) {
                        var runtimeJsonString = GetFileAsString(zip, "runtime.json");
                        var runtimeJson       = (JObject)JsonConvert.DeserializeObject(runtimeJsonString);
                        foreach (JProperty runtime in runtimeJson["runtimes"])
                        {
                            var runtimeName = runtime.Name;
                            if (runtime.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var dotNetAppHostObject = (JObject)runtime.First;
                            if (dotNetAppHostObject.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var dotNetAppHostObject2 = (JObject)dotNetAppHostObject["Microsoft.NETCore.DotNetAppHost"];
                            if (dotNetAppHostObject2.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var dotNetAppHostProperty = (JProperty)dotNetAppHostObject2.First;
                            if (dotNetAppHostProperty.Count != 1)
                            {
                                throw new InvalidOperationException("Expected 1 child");
                            }
                            var runtimePackageName    = dotNetAppHostProperty.Name;
                            var runtimePackageVersion = (string)((JValue)dotNetAppHostProperty.Value).Value;
                            Console.WriteLine();
                            Console.WriteLine($"{runtimePackageName} {runtimePackageVersion}");
                            NuGetSource[] nugetSources;
                            if (runtimeName.StartsWith("tizen.", StringComparison.Ordinal))
                            {
                                nugetSources = tizenNugetSources;
                            }
                            else
                            {
                                nugetSources = dotnetNugetSources;
                            }
                            bool couldDownload = false;
                            byte[]? ridData = null;
                            foreach (var nugetSource in nugetSources)
                            {
                                if (TryDownloadNuGetPackage(runtimePackageName, runtimePackageVersion, nugetSource, out ridData))
                                {
                                    couldDownload = true;
                                    break;
                                }
                            }
                            if (!couldDownload)
                            {
                                var error = $"***ERROR: 404 NOT FOUND: Couldn't download {runtimePackageName} = {runtimePackageVersion}";
                                errors.Add(error);
                                Console.WriteLine(error);
                                continue;
                            }
                            Debug.Assert(!(ridData is null));
                            using (var ridZip = new ZipArchive(new MemoryStream(ridData), ZipArchiveMode.Read, leaveOpen: false)) {
                                var appHostEntries = GetAppHostEntries(ridZip).ToArray();
                                if (appHostEntries.Length == 0)
                                {
                                    throw new InvalidOperationException("Expected at least one apphost");
                                }
                                foreach (var info in appHostEntries)
                                {
                                    if (info.rid != runtimeName)
                                    {
                                        throw new InvalidOperationException($"Expected rid='{runtimeName}' but got '{info.rid}' from the zip file");
                                    }
                                    var appHostData   = GetData(info.entry);
                                    int relPathOffset = GetOffset(appHostData, appHostRelPathHash);
                                    if (relPathOffset < 0)
                                    {
                                        throw new InvalidOperationException($"Couldn't get offset of hash in apphost: '{info.entry.FullName}'");
                                    }
                                    int sigOffset = GetOffset(appHostData, appHostSignature);
                                    if (sigOffset < 0)
                                    {
                                        throw new InvalidOperationException($"Couldn't get offset of sig in apphost: '{info.entry.FullName}'");
                                    }
                                    bool mustBeZero = false;
                                    for (int i = 0; i < AppHostInfo.MaxAppHostRelPathLength; i++)
                                    {
                                        byte b = appHostData[relPathOffset + i];
                                        if (mustBeZero)
                                        {
                                            if (b != 0)
                                            {
                                                throw new InvalidOperationException($"Not zero padded or the string data is smaller");
                                            }
                                        }
                                        else
                                        {
                                            mustBeZero = b == 0;
                                        }
                                    }
                                    var exeReader = new BinaryReader(new MemoryStream(appHostData));
                                    if (!ExeUtils.TryGetTextSectionInfo(exeReader, out var textOffset, out var textSize))
                                    {
                                        throw new InvalidOperationException("Could not get .text offset/size");
                                    }
                                    if (!TryHashData(appHostData, relPathOffset, textOffset, textSize, out var hashDataOffset, out var hashDataSize, out var hash, out var lastByte))
                                    {
                                        throw new InvalidOperationException("Failed to hash the .text section");
                                    }
                                    newInfos.Add(new AppHostInfo(info.rid, runtimePackageVersion, (uint)relPathOffset, (uint)hashDataOffset, (uint)hashDataSize, hash, lastByte));
                                }
                            }
                        }
                    }
                }

                if (newInfos.Count > 0)
                {
                    Console.WriteLine();
                    Console.WriteLine($"All apphost infos:");
                    var addedInfos = new HashSet <AppHostInfo>(AppHostInfoDupeEqualityComparer.Instance);

                    var allInfos = new List <AppHostInfo>(newInfos);
                    allInfos.AddRange(AppHostInfoData.KnownAppHostInfos);

                    var stringsTable   = new Dictionary <string, uint>(StringComparer.Ordinal);
                    var serializedData = AppHostInfoData.serializedAppHostInfos;
                    if (serializedData.Length > 0)
                    {
                        int  o          = 0;
                        uint numStrings = AppHostInfoData.DeserializeCompressedUInt32(serializedData, ref o);
                        for (uint i = 0; i < numStrings; i++)
                        {
                            stringsTable.Add(AppHostInfoData.DeserializeString(serializedData, ref o), i);
                        }
#if !APPHOSTINFO_STRINGS
#error APPHOSTINFO_STRINGS must be defined at the project level so strings can be restored
#endif
                        if (numStrings == 0)
                        {
                            throw new InvalidOperationException("No strings");
                        }
                    }

                    foreach (var info in allInfos)
                    {
                        if (!stringsTable.ContainsKey(info.Rid))
                        {
                            stringsTable.Add(info.Rid, (uint)stringsTable.Count);
                        }
                    }
                    foreach (var info in allInfos)
                    {
                        if (!stringsTable.ContainsKey(info.Version))
                        {
                            stringsTable.Add(info.Version, (uint)stringsTable.Count);
                        }
                    }

                    int expectedStringIndex = 0;
                    Console.WriteLine("#if APPHOSTINFO_STRINGS");
                    SerializeCompressedUInt32((uint)stringsTable.Count, "StringsTableCount");
                    foreach (var kv in stringsTable.OrderBy(a => a.Value))
                    {
                        if (kv.Value != expectedStringIndex)
                        {
                            throw new InvalidOperationException();
                        }
                        SerializeString(kv.Key, null);
                        expectedStringIndex++;
                    }
                    Console.WriteLine("#endif");
                    int numDupes = 0;
                    foreach (var info in allInfos)
                    {
                        if (info.Rid.Length == 0 || info.Version.Length == 0)
                        {
                            throw new InvalidOperationException();
                        }
                        bool dupe = !addedInfos.Add(info);
                        if (dupe)
                        {
                            numDupes++;
                        }
                        Serialize(info, stringsTable, dupe);
                    }
                    Console.WriteLine($"{newInfos.Count} new infos");
                    Console.WriteLine("********************************************************");
                    Console.WriteLine($"*** UPDATE {nameof(AppHostInfoData)}.{nameof(AppHostInfoData.SerializedAppHostInfosCount)} from {AppHostInfoData.SerializedAppHostInfosCount} to {newInfos.Count + AppHostInfoData.SerializedAppHostInfosCount} (dupes)");
                    Console.WriteLine($"*** UPDATE {nameof(AppHostInfoData)}.{nameof(AppHostInfoData.SerializedAppHostInfosCount)} from {AppHostInfoData.SerializedAppHostInfosCount} to {newInfos.Count + AppHostInfoData.SerializedAppHostInfosCount - numDupes} (no dupes)");
                    Console.WriteLine("********************************************************");
                }
                else
                {
                    Console.WriteLine("No new apphosts found");
                }

                var hashes = new Dictionary <AppHostInfo, List <AppHostInfo> >(AppHostInfoEqualityComparer.Instance);
                foreach (var info in AppHostInfoData.KnownAppHostInfos.Concat(newInfos))
                {
                    if (!hashes.TryGetValue(info, out var list))
                    {
                        hashes.Add(info, list = new List <AppHostInfo>());
                    }
                    list.Add(info);
                }
                foreach (var kv in hashes)
                {
                    var  list = kv.Value;
                    var  info = list[0];
                    bool bad  = false;
                    for (int i = 1; i < list.Count; i++)
                    {
                        // If all hash fields are the same, then we require that RelPathOffset also be
                        // the same. If this is a problem, hash more data, or allow RelPathOffset to be
                        // different (need to add code to verify the string at that location and try
                        // the other offset if it's not a valid file).
                        if (info.RelPathOffset != list[i].RelPathOffset)
                        {
                            bad = true;
                            break;
                        }
                    }
                    if (bad)
                    {
                        Console.WriteLine($"*** ERROR: The following apphosts have the same hash but different RelPathOffset:");
                        foreach (var info2 in list)
                        {
                            Console.WriteLine($"\t{info2.Rid} {info2.Version} RelPathOffset=0x{info2.RelPathOffset.ToString("X8")}");
                        }
                    }
                }

                if (errors.Count > 0)
                {
                    Console.WriteLine();
                    Console.WriteLine("All download errors:");
                    foreach (var error in errors)
                    {
                        Console.WriteLine($"\t{error}");
                    }
                }

                return(0);
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                return(1);
            }
        }