Publish Zonghengjia Windows 0.3.20 client source

This commit is contained in:
2026-11-28 00:26:12 +08:00
parent 54a3433b81
commit 308d71021e
26 changed files with 1643 additions and 968 deletions
+96
View File
@@ -0,0 +1,96 @@
using System.Collections.Concurrent;
using System.Text;
namespace UCVL.VPNPlus;
internal readonly record struct TailReadResult(string Text, int BytesRead);
internal static class BoundedLog
{
internal const long ApplicationMaxBytes = 4L * 1024 * 1024;
internal const long XrayMaxBytes = 4L * 1024 * 1024;
internal const int ErrorTailMaxBytes = 64 * 1024;
private static readonly UTF8Encoding Utf8 = new(false, false);
private static readonly ConcurrentDictionary<string, object> Gates = new(StringComparer.OrdinalIgnoreCase);
internal static void Append(string path, string value, long maxBytes)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(value);
if (maxBytes < 1 || maxBytes > int.MaxValue) throw new ArgumentOutOfRangeException(nameof(maxBytes));
var content = FitToLimit(value, (int)maxBytes);
path = Path.GetFullPath(path);
var gate = Gates.GetOrAdd(path, static _ => new object());
lock (gate)
{
var directory = Path.GetDirectoryName(path);
if (!string.IsNullOrWhiteSpace(directory)) Directory.CreateDirectory(directory);
var existingLength = File.Exists(path) ? new FileInfo(path).Length : 0;
if (existingLength > 0 && existingLength + content.Length > maxBytes)
Rotate(path, maxBytes);
using var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read | FileShare.Delete);
stream.Write(content);
}
}
internal static TailReadResult ReadTail(string path, int maxLines, int maxBytes)
{
if (maxLines < 1) throw new ArgumentOutOfRangeException(nameof(maxLines));
if (maxBytes < 1) throw new ArgumentOutOfRangeException(nameof(maxBytes));
if (!File.Exists(path)) return new("", 0);
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite | FileShare.Delete, bufferSize: 4096, FileOptions.RandomAccess);
var bytesToRead = (int)Math.Min(stream.Length, maxBytes);
if (bytesToRead == 0) return new("", 0);
var startedMidFile = stream.Length > bytesToRead;
stream.Seek(-bytesToRead, SeekOrigin.End);
var buffer = new byte[bytesToRead];
var total = 0;
while (total < buffer.Length)
{
var read = stream.Read(buffer, total, buffer.Length - total);
if (read == 0) break;
total += read;
}
var text = Utf8.GetString(buffer, 0, total).Replace("\0", "", StringComparison.Ordinal)
.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n');
var lines = text.Split('\n', StringSplitOptions.RemoveEmptyEntries).AsEnumerable();
if (startedMidFile) lines = lines.Skip(1);
return new(string.Join(Environment.NewLine, lines.TakeLast(maxLines)), total);
}
private static byte[] FitToLimit(string value, int maxBytes)
{
var content = Utf8.GetBytes(value);
if (content.Length <= maxBytes) return content;
var start = content.Length - maxBytes;
while (start < content.Length && (content[start] & 0xc0) == 0x80) start++;
return content[start..];
}
private static void Rotate(string path, long maxBytes)
{
var backup = path + ".1";
var length = new FileInfo(path).Length;
if (length <= maxBytes)
{
File.Move(path, backup, overwrite: true);
return;
}
// A pre-upgrade unbounded log is trimmed only when the next append
// naturally triggers rotation; loading, building and upgrading do not touch it.
using (var source = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
using (var destination = new FileStream(backup, FileMode.Create, FileAccess.Write, FileShare.Read))
{
source.Seek(-maxBytes, SeekOrigin.End);
source.CopyTo(destination);
}
File.Delete(path);
}
}