electron removed

This commit is contained in:
kitbyte
2025-03-21 23:35:16 +02:00
parent 8e33e58939
commit 4049ade4b1
54 changed files with 3635 additions and 828 deletions
+203
View File
@@ -0,0 +1,203 @@
using System;
using System.Collections.Generic;
using System.IO;
using AsarSharp.PickleTools;
using AsarSharp.Utils;
using Newtonsoft.Json;
namespace AsarSharp.AsarFileSystem
{
public static class Disk
{
private static Dictionary<string, Filesystem> _filesystemCache = new Dictionary<string, Filesystem>();
public class ArchiveHeader
{
public FilesystemEntry Header { get; set; }
public string HeaderString { get; set; }
public int HeaderSize { get; set; }
}
public class FilesystemFilesAndLinks
{
public List<BasicFileInfo> Files { get; set; } = new List<BasicFileInfo>();
public List<BasicFileInfo> Links { get; set; } = new List<BasicFileInfo>();
}
public class BasicFileInfo
{
public string Filename { get; set; }
public bool Unpack { get; set; }
}
#region Reading
public static ArchiveHeader ReadArchiveHeaderSync(string archivePath)
{
using (FileStream fs = File.OpenRead(archivePath))
{
// read the size of the header (8 bytes)
byte[] sizeBuf = new byte[8];
if (fs.Read(sizeBuf, 0, 8) != 8)
{
throw new Exception("Unable to read header size");
}
var sizePickle = Pickle.CreateFromBuffer(sizeBuf);
var size = sizePickle.CreateIterator().ReadUInt32();
// Read the header of the specified size
var headerBuf = new byte[size];
if(fs.Read(headerBuf, 0, (int)size) != size)
{
throw new Exception("Unable to read header");
}
var headerPickle = Pickle.CreateFromBuffer(headerBuf);
var header = headerPickle.CreateIterator().ReadString();
var headerObj = JsonConvert.DeserializeObject<FilesystemEntry>(header);
return new ArchiveHeader
{
Header = headerObj,
HeaderString = header,
HeaderSize = (int)size
};
}
}
public static Filesystem ReadFilesystemSync(string archivePath)
{
if (!_filesystemCache.ContainsKey(archivePath) || _filesystemCache[archivePath] == null)
{
ArchiveHeader header = ReadArchiveHeaderSync(archivePath);
Filesystem filesystem = new Filesystem(archivePath);
filesystem.SetHeader(header.Header, header.HeaderSize);
_filesystemCache[archivePath] = filesystem;
}
return _filesystemCache[archivePath];
}
public static byte[] ReadFileSync(Filesystem filesystem, string filename, FilesystemEntry info)
{
if (!info.IsFile || !info.Size.HasValue)
{
throw new ArgumentException("Entry is not a file", nameof(info));
}
long size = info.Size.Value;
byte[] buffer = new byte[size];
if (size <= 0)
{
return buffer;
}
if (info.Unpacked == true)
{
// It's an unpacked file, read it directly
string filePath = Path.Combine($"{filesystem.GetRootPath()}.unpacked", filename);
return File.ReadAllBytes(filePath);
}
// Read from the ASAR archive
using (FileStream fs = File.OpenRead(filesystem.GetRootPath()))
{
// Important: the offset must take into account the size of the Pickle header (8 bytes)
// and the size of the header itself
long offset = 8 + filesystem.GetHeaderSize() + long.Parse(info.Offset);
fs.Position = offset;
// Read the whole file at once
int bytesRead = fs.Read(buffer, 0, (int)size);
if (bytesRead != size)
{
throw new Exception($"Failed to read entire file, got {bytesRead} bytes instead of {size}");
}
}
return buffer;
}
#endregion
public static bool UncacheFilesystem(string archivePath)
{
if (_filesystemCache.ContainsKey(archivePath))
{
_filesystemCache.Remove(archivePath);
return true;
}
return false;
}
public static void UncacheAll()
{
_filesystemCache.Clear();
}
public static void CopyFile(string dest, string rootPath, string filename)
{
if(dest == null || rootPath == null || filename == null)
throw new ArgumentNullException();
if (dest == rootPath)
{
return;
}
string sourcePath = Path.Combine(rootPath, filename);
string destPath = Path.Combine(dest, filename);
Directory.CreateDirectory(Path.GetDirectoryName(destPath) ?? throw new InvalidOperationException());
using (var sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
using (var destinationStream = new FileStream(destPath, FileMode.Create, FileAccess.Write))
{
sourceStream.CopyTo(destinationStream);
}
}
public static void WriteFileSystem(string dest, Filesystem fileSystem,
FilesystemFilesAndLinks lists,
Dictionary<string, CrawledFileType> metadata)
{
var fsHeader = fileSystem.GetHeader();
var headerPickle = Pickle.CreateEmpty();
var serializerSettings = new JsonSerializerSettings()
{ NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore } ;
var headerJson = JsonConvert.SerializeObject(fsHeader,serializerSettings);
headerPickle.WriteString(headerJson);
var headerBuf = headerPickle.ToBuffer();
var sizePickle = Pickle.CreateEmpty();
sizePickle.WriteUInt32((uint)headerBuf.Length);
var sizeBuf = sizePickle.ToBuffer();
using (FileStream fs = File.Create(dest))
{
fs.Write(sizeBuf, 0, sizeBuf.Length);
fs.Write(headerBuf, 0, headerBuf.Length);
foreach (var file in lists.Files)
{
if (file.Unpack)
{
var filename = Extensions.GetRelativePath(fileSystem.GetRootPath(), file.Filename);
CopyFile($"{dest}.unpacked", fileSystem.GetRootPath(), filename);
continue;
}
using (var transformedFileStream = new FileStream(file.Filename, FileMode.Open, FileAccess.Read))
{
transformedFileStream.CopyTo(fs);
}
}
}
}
}
}
+235
View File
@@ -0,0 +1,235 @@
using System;
using System.Collections.Generic;
using System.IO;
using AsarSharp.Integrity;
using AsarSharp.Utils;
namespace AsarSharp.AsarFileSystem
{
public class Filesystem
{
private readonly string _src;
private FilesystemEntry _header;
private int _headerSize;
private long _offset;
private const uint UINT32_MAX = 0xFFFFFFFF; // 2^32 - 1
public Filesystem(string src)
{
_src = Path.GetFullPath(src);
_header = new FilesystemEntry { Files = new Dictionary<string, FilesystemEntry>(StringComparer.Ordinal) };
_headerSize = 0;
_offset = 0;
}
public string GetRootPath()
{
return _src;
}
public FilesystemEntry GetHeader()
{
return _header;
}
public int GetHeaderSize()
{
return _headerSize;
}
public void SetHeader(FilesystemEntry header, int headerSize)
{
_header = header;
_headerSize = headerSize;
}
public FilesystemEntry SearchNodeFromDirectory(string p)
{
FilesystemEntry json = _header;
// Normalize path delimiters to system delimiters
p = p.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
string[] dirs = p.Split(Path.DirectorySeparatorChar);
foreach (string dir in dirs)
{
if (dir == "." || string.IsNullOrEmpty(dir)) continue;
if (json.IsDirectory)
{
if (!json.Files.ContainsKey(dir))
{
json.Files[dir] = new FilesystemEntry { Files = new Dictionary<string, FilesystemEntry>(StringComparer.Ordinal) };
}
json = json.Files[dir];
}
else
{
throw new Exception($"Unexpected directory state while traversing: {p}");
}
}
return json;
}
public List<string> ListFiles(bool isPack = false)
{
var files = new List<string>();
FillFilesFromMetadata("/", _header);
return files;
void FillFilesFromMetadata(string basePath, FilesystemEntry metadata)
{
if (!metadata.IsDirectory)
{
return;
}
foreach (var entry in metadata.Files)
{
string childPath = entry.Key;
FilesystemEntry childMetadata = entry.Value;
string fullPath = Path.Combine(basePath, childPath).Replace('\\', '/');
string packState =
childMetadata.Unpacked == true ? "unpack" : "pack ";
files.Add(isPack ? $"{packState} : {fullPath}" : fullPath);
FillFilesFromMetadata(fullPath, childMetadata);
}
}
}
public FilesystemEntry GetNode(string p, bool followLinks = true)
{
// Normalize path delimiters
p = p.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar);
FilesystemEntry node = SearchNodeFromDirectory(Extensions.GetDirectoryName(p));
string name = Path.GetFileName(p);
// Process symbolic links
if (node.IsLink && followLinks)
{
return GetNode(Path.Combine(node.Link, name));
}
if (!string.IsNullOrEmpty(name))
{
if (node.IsDirectory && node.Files.TryGetValue(name, out var entry))
{
return entry;
}
return null;
}
return node;
}
public FilesystemEntry GetFile(string p, bool followLinks = true)
{
FilesystemEntry info = GetNode(p, followLinks);
if (info == null)
{
throw new Exception($"\"{p}\" was not found in this archive");
}
// If followLinks=false, do not allow symbolic links (TODO)
if (info.IsLink && followLinks)
{
return GetFile(info.Link, followLinks);
}
return info;
}
public static string ReadLink(string path)
{
throw new NotImplementedException();
return Path.GetFileName(path);
// TODO , NOT IMPLEMENTED
}
#region Writing
public FilesystemEntry SearchNodeFromPath(string p)
{
p = Extensions.GetRelativePath(_src, p);
if (string.IsNullOrEmpty(p))
{
return _header;
}
var name = Path.GetFileName(p);
var node = SearchNodeFromDirectory(Extensions.GetDirectoryName(p));
if (node.Files == null)
{
node.Files = new Dictionary<string, FilesystemEntry>();
}
if (!node.Files.ContainsKey(name))
{
node.Files[name] = new FilesystemEntry();
}
return node.Files[name];
}
public void InsertDirectory(string p, bool unpack)
{
FilesystemEntry node = SearchNodeFromPath(p);
node.Files = node.Files ?? new Dictionary<string, FilesystemEntry>();
node.Unpacked = unpack;
}
public void InsertFile(string path, bool shouldUnpack, CrawledFileType file)
{
var dirName = Path.GetDirectoryName(path);
var dirNode = SearchNodeFromPath(dirName);
var node = SearchNodeFromPath(path);
long size = 0;
if (file.Stat is FileInfo fileInfo)
{
size = fileInfo.Length;
}
else
{
throw new Exception($"{path}: stat is not a file");
}
if (shouldUnpack || dirNode.Unpacked == true)
{
node.Size = size;
node.Unpacked = true;
node.Integrity = IntegrityHelper.GetFileIntegrity(path);
return;
}
// Check that the file size does not exceed UINT32_MAX
if (size > UINT32_MAX)
{
throw new Exception($"{path}: file size cannot be larger than 4.2GB");
}
node.Size = size;
node.Offset = _offset.ToString();
node.Integrity = IntegrityHelper.GetFileIntegrity(path);
if (!Extensions.IsWindowsPlatform() && (file.Stat.Attributes & FileAttributes.Hidden) != 0)
{
node.Executable = true;
}
_offset += size;
}
#endregion
}
}
@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AsarSharp.Utils;
namespace AsarSharp.AsarFileSystem
{
public class CrawledFileType
{
public FileType Type { get; set; }
public FileSystemInfo Stat { get; set; }
public TransformedFile Transformed { get; set; }
}
public class TransformedFile
{
public string Path { get; set; }
public FileSystemInfo Stat { get; set; }
}
public enum FileType
{
File,
Directory,
Link
}
public static class FileSystemCrawler
{
public static CrawledFileType DetermineFileType(string filename)
{
var fileInfo = new FileInfo(filename);
if (fileInfo.Exists)
{
return new CrawledFileType { Type = FileType.File, Stat = fileInfo };
}
var directoryInfo = new DirectoryInfo(filename);
if (directoryInfo.Exists)
{
return new CrawledFileType { Type = FileType.Directory, Stat = directoryInfo };
}
var linkInfo = new FileInfo(filename);
if (linkInfo.Exists && (linkInfo.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
{
return new CrawledFileType { Type = FileType.Link, Stat = linkInfo };
}
return null;
}
public static (List<string> filenames, Dictionary<string, CrawledFileType> metadata) CrawlFileSystem(string dir)
{
var metadata = new Dictionary<string, CrawledFileType>();
var crawled = CrawlIterative(dir);
var results = crawled.Select(filename => new { filename, type = DetermineFileType(filename) }).ToList();
var links = new List<string>();
var filenames = new List<string>();
foreach (var result in results.Where(result => result.type != null))
{
metadata[result.filename] = result.type;
if (result.type.Type == FileType.Link)
{
links.Add(result.filename);
}
filenames.Add(result.filename);
}
var filteredFilenames = new List<string>();
foreach (var filename in filenames)
{
var exactLinkIndex = links.FindIndex(link => filename == link);
var isValid = true;
for (var i = 0; i < links.Count; i++)
{
if (i == exactLinkIndex)
{
continue;
}
var link = links[i];
var isFileWithinSymlinkDir = filename.StartsWith(link, StringComparison.OrdinalIgnoreCase);
var relativePath = Extensions.GetRelativePath(link, Path.GetDirectoryName(filename) ?? string.Empty);
if (isFileWithinSymlinkDir && !relativePath.StartsWith("..", StringComparison.Ordinal))
{
isValid = false;
break;
}
}
if (isValid)
{
filteredFilenames.Add(filename);
}
}
return (filteredFilenames, metadata);
}
// (File order is not important!!!)
public static List<string> CrawlIterative(string dir)
{
var result = new List<string>();
var stack = new Stack<string>();
string basePath = Extensions.GetBasePath(dir);
if (!Directory.Exists(basePath))
return result;
// Add only the base directory to the stack, but not to the result
stack.Push(basePath);
while (stack.Count > 0)
{
string currentDir = stack.Pop();
try
{
// Add all files from the current directory
result.AddRange(Directory.GetFiles(currentDir, "*", SearchOption.TopDirectoryOnly));
// Add subdirectories to the results and to the stack
foreach (var directory in Directory.GetDirectories(currentDir, "*",
SearchOption.TopDirectoryOnly))
{
// Add subdirectories to the result
if (directory != basePath) // Do not add a base directory
{
result.Add(directory);
}
// Add to the stack for processing
stack.Push(directory);
}
}
catch (UnauthorizedAccessException)
{
// Skip directories to which there is no access
continue;
}
}
return result;
}
}
}
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using AsarSharp.Integrity;
using Newtonsoft.Json;
namespace AsarSharp.AsarFileSystem
{
public class FilesystemEntry
{
[JsonProperty("files")]
public Dictionary<string, FilesystemEntry> Files { get; set; }
[JsonProperty("executable")]
public bool? Executable { get; set; }
[JsonProperty("size")]
public long? Size { get; set; }
[JsonProperty("offset")]
public string Offset { get; set; }
[JsonProperty("unpacked")]
public bool? Unpacked { get; set; }
[JsonProperty("integrity")]
public IntegrityHelper.FileIntegrity Integrity { get; set; }
[JsonProperty("link")]
public string Link { get; set; }
[JsonIgnore]
public bool IsDirectory => Files != null;
[JsonIgnore]
public bool IsFile => Size.HasValue;
[JsonIgnore]
public bool IsLink => Link != null;
public override string ToString()
{
return $"Offset: {Offset}, Size: {Size} Unpacked: {Unpacked}";
}
public bool ShouldSerializeUnpacked()
{
return Unpacked == true;
}
}
}