private void UnpackRegularFile(Pack1Meta.FileEntry file, Action <double> onProgress, CancellationToken cancellationToken, string destinationDirPath = null)
        {
            string destPath = Path.Combine(destinationDirPath == null ? _destinationDirPath : destinationDirPath, file.Name + _suffix);

            DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);

            if (file.Size == null)
            {
                throw new NullReferenceException("File size cannot be null for regular file.");
            }

            if (file.Offset == null)
            {
                throw new NullReferenceException("File offset cannot be null for regular file.");
            }

            Files.CreateParents(destPath);

            var rijn = new RijndaelManaged
            {
                Mode    = CipherMode.CBC,
                Padding = PaddingMode.None,
                KeySize = 256
            };

            var aesAlg = new AesCryptoServiceProvider
            {
                IV  = _iv,
                Key = _key
            };

            ICryptoTransform decryptor = aesAlg.CreateDecryptor(
                aesAlg.Key,
                aesAlg.IV);

            DecompressorCreator decompressorCreator = ResolveDecompressor(_metaData);

            using (var fs = new FileStream(_packagePath, FileMode.Open))
            {
                fs.Seek(file.Offset.Value - _range.Start, SeekOrigin.Begin);

                using (var limitedStream = new BufferedStream(new LimitedStream(fs, file.Size.Value), 2 * 1024 * 1024))
                {
                    //using (var bufferedLimitedStream = new ThreadBufferedStream(limitedStream, 8 * 1024 * 1024))
                    {
                        using (var target = new FileStream(destPath, FileMode.Create))
                        {
                            ExtractFileFromStream(limitedStream, target, file.Size.Value, decryptor, decompressorCreator, onProgress, cancellationToken);
                        }
                    }

                    if (Platform.IsPosix())
                    {
                        Chmod.SetMode(file.Mode.Substring(3), destPath);
                    }
                }
            }

            DebugLogger.Log("File " + file.Name + " unpacked successfully!");
        }
Exemplo n.º 2
0
        public void Start()
        {
            DebugLogger.Log("Starting application.");

            PlatformType platformType = Platform.GetPlatformType();
            string       appFilePath  = AppFinder.FindExecutable(_app.LocalDirectory.Path, platformType);

            if (appFilePath == null)
            {
                throw new InvalidOperationException("Couldn't find executable.");
            }

            DebugLogger.Log(string.Format("Found executable {0}", appFilePath));

            if (NeedPermissionFix(platformType))
            {
                foreach (var fileName in _app.LocalMetaData.GetRegisteredEntries())
                {
                    string filePath = _app.LocalDirectory.Path.PathCombine(fileName);
                    if (IsExecutable(filePath, platformType))
                    {
                        DebugLogger.LogFormat("File is recognized as executable {0}", filePath);
                        Chmod.SetExecutableFlag(filePath);
                    }
                }
            }

            var processStartInfo = GetProcessStartInfo(appFilePath, platformType);

            StartAppProcess(processStartInfo);
        }
        public ProcessStartInfo GetProcessStartInfo()
        {
            if (Platform.IsWindows())
            {
                var processStartInfo = new ProcessStartInfo
                {
                    FileName = _streamingAssetsPath.PathCombine(TorrentClientWinPath),
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true
                };

                return(processStartInfo);
            }

            if (Platform.IsOSX())
            {
                var processStartInfo = new ProcessStartInfo
                {
                    FileName = _streamingAssetsPath.PathCombine(TorrentClientOsx64Path),
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true
                };

                // make sure that binary can be executed
                Chmod.SetExecutableFlag(processStartInfo.FileName);

                processStartInfo.EnvironmentVariables["DYLD_LIBRARY_PATH"] = Path.Combine(_streamingAssetsPath, "torrent-client/osx64");

                return(processStartInfo);
            }

            if (Platform.IsLinux() && IntPtr.Size == 8) // Linux 64 bit
            {
                var processStartInfo = new ProcessStartInfo
                {
                    FileName = _streamingAssetsPath.PathCombine(TorrentClientLinux64Path),
                    RedirectStandardInput  = true,
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true
                };

                // make sure that binary can be executed
                Chmod.SetExecutableFlag(processStartInfo.FileName);

                processStartInfo.EnvironmentVariables["LD_LIBRARY_PATH"] = Path.Combine(_streamingAssetsPath, "torrent-client/linux64");

                return(processStartInfo);
            }

            throw new TorrentClientException("Unsupported platform by torrent-client.");
        }
Exemplo n.º 4
0
        public static void CopyFile(MacOSVersion version, String name, String destination, String permissions)
        {
            // Copy the file
            String file = GetPath(version, name);

            File.Copy(file, destination, true);

            if (!String.IsNullOrEmpty(permissions))
            {
                // Apply permissions
                Chmod.ApplyTo("a+x", destination);
            }
        }
Exemplo n.º 5
0
        private void UnpackRegularFile(Pack1Meta.FileEntry file, Action <double> onProgress)
        {
            string destPath = Path.Combine(_destinationDirPath, file.Name + _suffix);

            DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);

            Files.CreateParents(destPath);

            RijndaelManaged rijn = new RijndaelManaged
            {
                Mode    = CipherMode.CBC,
                Padding = PaddingMode.None,
                KeySize = 256
            };

            using (var fs = new FileStream(_packagePath, FileMode.Open))
            {
                fs.Seek(file.Offset.Value, SeekOrigin.Begin);

                using (var limitedStream = new LimitedStream(fs, file.Size.Value))
                {
                    ICryptoTransform decryptor = rijn.CreateDecryptor(_key, _iv);
                    using (var cryptoStream = new CryptoStream(limitedStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (var gzipStream = new GZipStream(cryptoStream, Ionic.Zlib.CompressionMode.Decompress))
                        {
                            using (var fileWritter = new FileStream(destPath, FileMode.Create))
                            {
                                long      bytesProcessed = 0;
                                const int bufferSize     = 131072;
                                var       buffer         = new byte[bufferSize];
                                int       count;
                                while ((count = gzipStream.Read(buffer, 0, bufferSize)) != 0)
                                {
                                    fileWritter.Write(buffer, 0, count);
                                    bytesProcessed += count;
                                    onProgress(bytesProcessed / (double)file.Size.Value);
                                }
                                if (Platform.IsPosix())
                                {
                                    Chmod.SetMode(file.Mode.Substring(3), destPath);
                                }
                            }
                        }
                    }
                }
            }

            DebugLogger.Log("File " + file.Name + " unpacked successfully!");
        }
Exemplo n.º 6
0
        public void ItExecutesChmod()
        {
            var file = Path.Combine(AppContext.BaseDirectory, Path.GetRandomFileName());

            File.WriteAllText(file, "");

            AssertDoesNotHavePermission(file, FileAccessPermissions.UserExecute);
            var task = new Chmod
            {
                File        = file,
                Mode        = "+x",
                BuildEngine = new MockEngine(_output),
            };

            Assert.True(task.Execute(), "Task should pass");

            AssertPermission(file, FileAccessPermissions.UserExecute);
        }
Exemplo n.º 7
0
        public static void CopyTo(this DirectoryInfo source, DirectoryInfo target)
        {
            if (!target.Exists)
            {
                target.Create();
                Chmod.chmod(target.FullName, Chmod.P_755);
            }

            foreach (var i in source.GetFiles())
            {
                i.CopyTo(Path.Combine(target.FullName, i.Name), true);
                Chmod.chmod(Path.Combine(target.FullName, i.Name), Chmod.P_755);
            }

            foreach (var d in source.GetDirectories())
            {
                d.CopyTo(new DirectoryInfo(Path.Combine(target.FullName, d.Name)));
            }
        }
Exemplo n.º 8
0
        private void UnpackRegularFile(Pack1Meta.FileEntry file)
        {
            string destPath = Path.Combine(_destinationDirPath, file.Name);

            DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);

            Files.CreateParents(destPath);

            RijndaelManaged rijn = new RijndaelManaged
            {
                Mode    = CipherMode.CBC,
                Padding = PaddingMode.None,
                KeySize = 256
            };

            using (var fs = new FileStream(_packagePath, FileMode.Open))
            {
                fs.Seek(file.Offset.Value, SeekOrigin.Begin);

                using (var limitedStream = new LimitedStream(fs, file.Size.Value))
                {
                    ICryptoTransform decryptor = rijn.CreateDecryptor(_key, _iv);
                    using (var cryptoStream = new CryptoStream(limitedStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (var gzipStream = new GZipStream(cryptoStream, Ionic.Zlib.CompressionMode.Decompress))
                        {
                            using (var fileWritter = new FileStream(destPath, FileMode.Create))
                            {
                                Streams.Copy(gzipStream, fileWritter);
                                if (Platform.IsPosix())
                                {
                                    Chmod.SetMode(file.Mode.Substring(3), destPath);
                                }
                            }
                        }
                    }
                }
            }

            DebugLogger.Log("File " + file.Name + " unpacked successfully!");
        }
Exemplo n.º 9
0
        private void StartAppVersion(AppVersion?appVersion, string customArgs)
        {
            DebugLogger.Log("Starting application.");

            PlatformType platformType = Platform.GetPlatformType();
            string       appFilePath  = ResolveExecutablePath(appVersion);
            string       appArgs      = customArgs ?? string.Empty;

            if (appVersion != null &&
                appVersion.Value.MainExecutableArgs != null)
            {
                appArgs += " " + appVersion.Value.MainExecutableArgs;
            }

            if (appFilePath == null)
            {
                throw new InvalidOperationException("Couldn't find executable.");
            }

            DebugLogger.Log(string.Format("Found executable {0}", appFilePath));

            if (NeedPermissionFix(platformType))
            {
                foreach (var fileName in _app.LocalMetaData.GetRegisteredEntries())
                {
                    string filePath = _app.LocalDirectory.Path.PathCombine(fileName);
                    if (Files.IsExecutable(filePath, platformType))
                    {
                        DebugLogger.LogFormat("File is recognized as executable {0}", filePath);
                        Chmod.SetExecutableFlag(filePath);
                    }
                }
            }

            var processStartInfo = GetProcessStartInfo(appFilePath, appArgs, platformType);

            StartAppProcess(processStartInfo);
        }
        private void UnpackRegularFile(Pack1Meta.FileEntry file, Action <double> onProgress)
        {
            string destPath = Path.Combine(_destinationDirPath, file.Name + _suffix);

            DebugLogger.LogFormat("Unpacking regular file {0} to {1}", file, destPath);

            Files.CreateParents(destPath);

            RijndaelManaged rijn = new RijndaelManaged
            {
                Mode    = CipherMode.CBC,
                Padding = PaddingMode.None,
                KeySize = 256
            };

            using (var fs = new FileStream(_packagePath, FileMode.Open))
            {
                fs.Seek(file.Offset.Value, SeekOrigin.Begin);

                using (var limitedStream = new LimitedStream(fs, file.Size.Value))
                {
                    ICryptoTransform decryptor = rijn.CreateDecryptor(_key, _iv);
                    using (var cryptoStream = new CryptoStream(limitedStream, decryptor, CryptoStreamMode.Read))
                    {
                        using (var gzipStream = new GZipStream(cryptoStream, Ionic.Zlib.CompressionMode.Decompress))
                        {
                            try
                            {
                                using (var fileWritter = new FileStream(destPath, FileMode.Create))
                                {
                                    long      bytesProcessed = 0;
                                    const int bufferSize     = 131072;
                                    var       buffer         = new byte[bufferSize];
                                    int       count;
                                    while ((count = gzipStream.Read(buffer, 0, bufferSize)) != 0)
                                    {
                                        fileWritter.Write(buffer, 0, count);
                                        bytesProcessed += count;
                                        onProgress(bytesProcessed / (double)file.Size.Value);
                                    }
                                    if (Platform.IsPosix())
                                    {
                                        Chmod.SetMode(file.Mode.Substring(3), destPath);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                DebugLogger.LogException(e);

                                var ravenClient
                                    = new RavenClient("https://*****:*****@sentry.io/175617");

                                var sentryEvent = new SentryEvent(e);
                                var logManager  = PatcherLogManager.Instance;
                                PatcherLogSentryRegistry.AddDataToSentryEvent(sentryEvent, logManager.Storage.Guid.ToString());

                                ravenClient.Capture(sentryEvent);

                                throw;
                            }
                        }
                    }
                }
            }

            DebugLogger.Log("File " + file.Name + " unpacked successfully!");
        }
Exemplo n.º 11
0
        private List <(string Docker, string Host, bool Readonly)> instantiateNode(JsonObject node, DirectoryInfo targetDirectory, string relativePath)
        {
            var rv = new List <(string Docker, string Host, bool Readonly)>();

            bool   shared;
            string path;
            bool   ro;

            if (!node.TryGetJsonString(PathKey, out var pathv))
            {
                return(rv);
            }
            else
            {
                path = pathv;
            }
            if (node.TryGetJsonBool(SharedKey, out var sharedv))
            {
                shared = sharedv;
            }
            else
            {
                shared = false;
            }
            if (node.TryGetJsonBool(ReadonlyKey, out var readonlyv))
            {
                ro = readonlyv;
            }
            else
            {
                ro = shared;
            }
            if (!node.TryGetJsonObject(SubKey, out JsonObject sub))
            {
                sub = null;
            }

            if (!targetDirectory.Exists)
            {
                targetDirectory.Create();
                Chmod.chmod(targetDirectory.FullName, Chmod.P_755);
            }

            if (shared)
            {
                rv.Add((relativePath, path, ro));

                if (sub != null)
                {
                    foreach (var i in sub.Values)
                    {
                        if (!(i.Value is JsonObject nextNode))
                        {
                            continue;
                        }
                        var name          = i.Key;
                        var nextTargetDir = new DirectoryInfo(Path.Combine(targetDirectory.FullName, i.Key));
                        var nextRelPath   = relativePath + DirSeparator + i.Key;

                        rv.AddRange(instantiateNode(nextNode, nextTargetDir, nextRelPath));
                    }
                }
            }
            else
            {
                if (relativePath == ".")
                {
                    rv.Add((relativePath, targetDirectory.FullName, ro));
                }

                var pathInfo = new DirectoryInfo(path);
                foreach (var i in pathInfo.GetFiles())
                {
                    i.CopyTo(Path.Combine(targetDirectory.FullName, i.Name), true);
                    Chmod.chmod(Path.Combine(targetDirectory.FullName, i.Name), Chmod.P_755);
                }

                foreach (var i in pathInfo.GetDirectories())
                {
                    if (sub != null && sub.TryGetJsonObject(i.Name, out var nextNode))
                    {
                        rv.AddRange(instantiateNode(nextNode, new DirectoryInfo(Path.Combine(targetDirectory.FullName, i.Name)), relativePath + DirSeparator + i.Name));
                    }
                    else
                    {
                        i.CopyTo(new DirectoryInfo(Path.Combine(targetDirectory.FullName, i.Name)));
                    }
                }
            }

            return(rv);
        }
Exemplo n.º 12
0
        // encoding is utf-8 no bom
        // wo yi jing qin ding le
        private string ensuredirNode(string path, JsonObject node)
        {
            bool   isFile        = false;
            string pathSpecified = null;
            string content       = null;
            bool   returnthis    = false;
            bool   overwrite     = false;

            string rv = null;

            if (node.TryGetJsonBool(IsFileKey, out var isFileBool))
            {
                isFile = isFileBool;
            }
            if (node.TryGetJsonBool(OverwriteKey, out var overwriteBool))
            {
                overwrite = overwriteBool;
            }
            if (node.TryGetJsonString(ContentKey, out var contentStr))
            {
                content = contentStr;
            }
            if (node.TryGetJsonBool(ReturnThisKey, out var returnThisBool))
            {
                returnthis = returnThisBool;
            }
            if (node.TryGetJsonString(PathSpecifiedKey, out var pathSpecifiedStr))
            {
                pathSpecified = pathSpecifiedStr;
            }

            if (pathSpecified != null)
            {
                path = pathSpecified;
            }
            if (isFile)
            {
                var info = new FileInfo(path);
                content = content ?? "";

                if (!info.Exists || overwrite)
                {
                    writeToPath(info, content);
                }

                Chmod.chmod(path, Chmod.P_755);
            }
            else
            {
                var info = new DirectoryInfo(path);
                if (!info.Exists)
                {
                    info.Create();
                }

                Chmod.chmod(path, Chmod.P_755);

                foreach (var i in node.Values)
                {
                    if (((string)(i.Key))[0] != '@' && i.Value is JsonObject nextNode)
                    {
                        rv = ensuredirNode(Path.Combine(path, i.Key), nextNode);
                    }
                }
            }

            if (returnthis)
            {
                return(path);
            }
            return(rv == null ? path : rv);
        }