Beispiel #1
0
    public bool ShouldCopy(ProxyFile proxyFile, [NotNullWhen(true)] out string?cause)
    {
        cause = null;

        string newHash = Utils.ComputeHash(proxyFile.TempFile);

        _newCache !.FileHashes[proxyFile.TargetFile] = newHash;

        if (!File.Exists(proxyFile.TargetFile))
        {
            cause = $"the output file didn't exist";
            return(true);
        }

        string?oldHash;

        if (!_oldCache !.FileHashes.TryGetValue(proxyFile.TargetFile, out oldHash))
        {
            oldHash = Utils.ComputeHash(proxyFile.TargetFile);
        }

        if (oldHash != newHash)
        {
            cause = $"hash for the file changed";
            return(true);
        }

        return(false);
    }
Beispiel #2
0
        public void DeleteProxy(string name)
        {
            var proxyFile = new ProxyFile(ProxyFilePath);

            proxyFile.DeleteProxy(name);
            proxyFile.Commit();
        }
Beispiel #3
0
        private async Task <IActionResult> ServeProxyFile(string package, ProxyFile proxyFile, Assembly assembly, string buildHash)
        {
            string proxy;

            switch (proxyFile)
            {
            case ProxyFile.JavaScript:
                proxy = await _proxies.GenerateJavaScriptProxy(package, CancellationToken);

                break;

            case ProxyFile.TypeScript:
                proxy = await _proxies.GenerateTypeScriptProxy(package, CancellationToken);

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(proxyFile), proxyFile, null);
            }

            Response.Headers.Add(HeaderNames.ETag, assembly.ComputePackageHash(buildHash));
            Response.Headers.Add(HeaderNames.CacheControl, "public,max-age=31536000");
            switch (proxyFile)
            {
            case ProxyFile.JavaScript:
                return(File(Encoding.UTF8.GetBytes(proxy), "application/javascript"));

            case ProxyFile.TypeScript:
                return(File(Encoding.UTF8.GetBytes(proxy), "application/typescript"));

            default:
                throw new ArgumentOutOfRangeException(nameof(proxyFile), proxyFile, null);
            }
        }
Beispiel #4
0
        public void AddProxy(string name, ProxyDefinition proxyDefinition)
        {
            var proxyFile = new ProxyFile(ProxyFilePath);

            proxyFile.AddProxy(name, proxyDefinition);
            proxyFile.Commit();
        }
Beispiel #5
0
        public Insta(InstaProxy proxy)
        {
            ProxyFile.Create(proxy);

            var options = new ChromeOptions();

            options.AddExtension(@"data/proxy.zip");

            CodePagesEncodingProvider.Instance.GetEncoding(437);
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            Driver = new ChromeDriver(Environment.CurrentDirectory + @"\data", options);
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="edataFile"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public virtual byte[] Write(ProxyFile file, CancellationToken token)
        {
            //Cancel if requested;
            token.ThrowIfCancellationRequested();

            var header            = file.Header;
            var proxyContentFiles = file.ContentFiles.OfType <ProxyContentFile>();

            //This assuemes that all content files are loaded.

            using (MemoryStream target = new MemoryStream())
            {
                uint fileTableOffset = (uint)Marshal.SizeOf(typeof(ProxyHeader));
                //Póki co może być obliczona z ilości proxyContentFiles, bo to z nich zostanę utworzone wpisy fileTableEntries
                //Sytuacja by się zmieniła jeśli te wpisy byłby by przechowywane od momentu odczytu, anie odrzuacane i odtwarzane jak obecnie.
                uint fileTableLength = (uint)proxyContentFiles.Count() * ProxyFileTableEntry.EntryLength;

                uint contentOffset    = fileTableOffset + fileTableLength;
                var  contentWriteInfo = WriteLoadedContentFiles(target, contentOffset, proxyContentFiles, token);

                //We are writing fileTable entries after writing the content, because we need to write actual offsets and lengths of content files.
                var fileTableEntries = CreateFileTableEntries(proxyContentFiles);
                WriteFileTableEntries(target, (uint)fileTableOffset, fileTableEntries);

                uint pathTableOffset  = contentOffset + contentWriteInfo.Length;
                uint pathTableLength  = (uint)proxyContentFiles.Count() * ProxyPathTableEntry.EntryLength;
                var  pathTableEntries = CreatePathTableEntries(proxyContentFiles);
                WritePathTableEntries(target, pathTableOffset, pathTableEntries);

                header.FileTableOffset       = fileTableOffset;
                header.FileTableLength       = fileTableLength;
                header.FileTableEntriesCount = (uint)fileTableEntries.Count();
                header.ContentOffset         = contentOffset;
                header.ContentLength         = contentWriteInfo.Length;
                header.PathTableOffset       = pathTableOffset;
                header.PathTableLength       = pathTableLength;
                header.PathTableEntriesCount = (uint)pathTableEntries.Count();

                WriteHeader(target, header);

                return(target.ToArray());
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="loadContent"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ProxyFile Read(String path, bool loadContent, CancellationToken token)
        {
            //Cancel if requested;
            token.ThrowIfCancellationRequested();

            if (!File.Exists(path))
            {
                throw new ArgumentException(String.Format("File '{0}' doesn't exist.", path), "path");
            }

            ProxyHeader header;
            IEnumerable <ProxyContentFile> contentFiles = new List <ProxyContentFile>();

            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                header = ReadHeader(stream);

                if (header.Version != 8)
                {
                    throw new NotSupportedException(
                              String.Format("ProxyPCPC Version {0} is currently not supported", header.Version));
                }

                header = ReadHeader(stream);
                var fileTableEntries = ReadFileTableEntries(stream, header.FileTableOffset, header.FileTableLength);
                var pathTableEntries = ReadPathTableEntries(stream, header.PathTableOffset, header.PathTableLength);

                contentFiles = CreateContentFiles(fileTableEntries, pathTableEntries);

                SetTotalOffsetsForContentFiles(contentFiles, header.ContentOffset);

                if (loadContent)
                {
                    LoadContentFiles(stream, contentFiles);
                }
            }

            ProxyFile proxyFile = new ProxyFile(path, header, contentFiles);

            return(proxyFile);
        }
Beispiel #8
0
        public ProxyFile Read(byte[] rawProxy, bool loadContent, CancellationToken token)
        {
            //Cancel if requested;
            token.ThrowIfCancellationRequested();

            ProxyHeader header;
            IEnumerable <ProxyContentFile> contentFiles = new List <ProxyContentFile>();

            using (MemoryStream stream = new MemoryStream(rawProxy))
            {
                if (!CanReadHeaderFromBuffer(rawProxy))
                {
                    throw new ArgumentException("Cannot read ProxyPCPC header from the buffer," +
                                                " because header size exceeds size of the buffer", "rawProxy");
                }

                header = ReadHeader(stream);

                if (header.Version != 8)
                {
                    throw new NotSupportedException(
                              String.Format("ProxyPCPC Version {0} is currently not supported", header.Version));
                }

                var fileTableEntries = ReadFileTableEntries(stream, header.FileTableOffset, header.FileTableLength);
                var pathTableEntries = ReadPathTableEntries(stream, header.PathTableOffset, header.PathTableLength);

                contentFiles = CreateContentFiles(fileTableEntries, pathTableEntries);

                SetTotalOffsetsForContentFiles(contentFiles, header.ContentOffset);

                if (loadContent)
                {
                    LoadContentFiles(stream, contentFiles);
                }
            }

            ProxyFile proxyFile = new ProxyFile(header, contentFiles);

            return(proxyFile);
        }
Beispiel #9
0
        private async Task <IActionResult> TryServeProxyFile(string package, ProxyFile proxyFile, string version)
        {
            if (string.IsNullOrWhiteSpace(package))
            {
                return(BadRequest(new { Message = "Package name required." }));
            }

            var(error, buildHash) = await TryGetBuildHash(version);

            if (error != null)
            {
                return(error);
            }

            // FIXME: duplicate code
            if (Request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out var etag))
            {
                var resources = await _packages.GetAvailablePackagesAsync(CancellationToken);

                foreach (var hash in etag)
                {
                    if (resources.Contains(hash))
                    {
                        return(StatusCode((int)HttpStatusCode.NotModified));
                    }
                }
            }

            var assembly = await _packages.FindAssemblyByNameAsync(package, CancellationToken);

            if (assembly == null)
            {
                return(NotFound(new { Message = $"No package assemblies found matching name '{package}." }));
            }

            return(await ServeProxyFile(package, proxyFile, assembly, buildHash));
        }
Beispiel #10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="edataFile"></param>
 /// <returns></returns>
 public virtual byte[] Write(ProxyFile file)
 {
     return(Write(file, CancellationToken.None));
 }
Beispiel #11
0
        public string GetProxy(string name)
        {
            var proxyFile = new ProxyFile(ProxyFilePath);

            return(proxyFile.GetProxy(name));
        }
Beispiel #12
0
        public string GetProxies()
        {
            var proxyFile = new ProxyFile(ProxyFilePath);

            return(proxyFile.GetProxies());
        }
Beispiel #13
0
        public void Write(ProxyFile file, CancellationToken token)
        {
            //Cancel if requested;
            token.ThrowIfCancellationRequested();

            var header            = file.Header;
            var proxyContentFiles = file.ContentFiles.OfType <ProxyContentFile>();

            //For now replacement write is not supported...

            bool allContentFilesLoaded = proxyContentFiles.All(x => x.IsContentLoaded);

            if (allContentFilesLoaded)
            {
                using (var target = new FileStream(file.Path, FileMode.OpenOrCreate))
                {
                    uint fileTableOffset = (uint)Marshal.SizeOf(typeof(ProxyHeader));
                    //Póki co może być obliczona z ilości proxyContentFiles, bo to z nich zostanę utworzone wpisy fileTableEntries
                    //Sytuacja by się zmieniła jeśli te wpisy byłby by przechowywane od momentu odczytu, anie odrzuacane i odtwarzane jak obecnie.
                    uint fileTableLength = (uint)proxyContentFiles.Count() * ProxyFileTableEntry.EntryLength;

                    uint contentOffset    = fileTableOffset + fileTableLength;
                    var  contentWriteInfo = WriteLoadedContentFiles(target, contentOffset, proxyContentFiles, token);

                    //We are writing fileTable entries after writing the content, because we need to write actual offsets and lengths of content files.
                    var fileTableEntries = CreateFileTableEntries(proxyContentFiles);
                    WriteFileTableEntries(target, (uint)fileTableOffset, fileTableEntries);

                    uint pathTableOffset  = contentOffset + contentWriteInfo.Length;
                    uint pathTableLength  = (uint)proxyContentFiles.Count() * ProxyPathTableEntry.EntryLength;
                    var  pathTableEntries = CreatePathTableEntries(proxyContentFiles);
                    WritePathTableEntries(target, pathTableOffset, pathTableEntries);

                    header.FileTableOffset       = fileTableOffset;
                    header.FileTableLength       = fileTableLength;
                    header.FileTableEntriesCount = (uint)fileTableEntries.Count();
                    header.ContentOffset         = contentOffset;
                    header.ContentLength         = contentWriteInfo.Length;
                    header.PathTableOffset       = pathTableOffset;
                    header.PathTableLength       = pathTableLength;
                    header.PathTableEntriesCount = (uint)pathTableEntries.Count();

                    WriteHeader(target, header);
                }
            }
            else
            {
                //We are checking it here, since when the content is not loaded, the file have to exist
                //so it could be read for content.
                if (!File.Exists(file.Path))
                {
                    throw new ArgumentException(
                              String.Format("A following ProxyPCPC file: \"{0}\" doesn't exist", file.Path),
                              "edataFile");
                }

                String temporaryProxyPath = PathUtilities.GetTemporaryPath(file.Path);

                //To avoid too many nested try catches.
                FileStream source = null;
                FileStream target = null;
                try
                {
                    source = new FileStream(file.Path, FileMode.Open);
                    target = new FileStream(temporaryProxyPath, FileMode.Create);

                    uint fileTableOffset = (uint)Marshal.SizeOf(typeof(ProxyHeader));
                    //Póki co może być obliczona z ilości proxyContentFiles, bo to z nich zostanę utworzone wpisy fileTableEntries
                    //Sytuacja by się zmieniła jeśli te wpisy byłby by przechowywane od momentu odczytu, anie odrzuacane i odtwarzane jak obecnie.
                    uint fileTableLength = (uint)proxyContentFiles.Count() * ProxyFileTableEntry.EntryLength;

                    uint contentOffset    = fileTableOffset + fileTableLength;
                    var  contentWriteInfo = WriteNotLoadedContentFiles(source, target, contentOffset, proxyContentFiles, token);

                    //We are writing fileTable entries after writing the content, because we need to write actual offsets and lengths of content files.
                    var fileTableEntries = CreateFileTableEntries(proxyContentFiles);
                    WriteFileTableEntries(target, (uint)fileTableOffset, fileTableEntries);

                    uint pathTableOffset  = contentOffset + contentWriteInfo.Length;
                    uint pathTableLength  = (uint)proxyContentFiles.Count() * ProxyPathTableEntry.EntryLength;
                    var  pathTableEntries = CreatePathTableEntries(proxyContentFiles);
                    WritePathTableEntries(target, pathTableOffset, pathTableEntries);

                    header.FileTableOffset       = fileTableOffset;
                    header.FileTableLength       = fileTableLength;
                    header.FileTableEntriesCount = (uint)fileTableEntries.Count();
                    header.ContentOffset         = contentOffset;
                    header.ContentLength         = contentWriteInfo.Length;
                    header.PathTableOffset       = pathTableOffset;
                    header.PathTableLength       = pathTableLength;
                    header.PathTableEntriesCount = (uint)pathTableEntries.Count();

                    WriteHeader(target, header);

                    //Free file handles before the file move and delete
                    CloseStreams(source, target);

                    //Replace temporary file
                    File.Delete(file.Path);
                    File.Move(temporaryProxyPath, file.Path);
                }
                finally
                {
                    //Spr czy zostały już zwolnione...?
                    CloseStreams(source, target);

                    if (File.Exists(temporaryProxyPath))
                    {
                        File.Delete(temporaryProxyPath);
                    }
                }
            }
        }
Beispiel #14
0
 public void Write(ProxyFile file)
 {
     Write(file, CancellationToken.None);
 }