56 lines
2.1 KiB
C#
56 lines
2.1 KiB
C#
|
|
using System.Text;
|
||
|
|
|
||
|
|
namespace UCVL.VPNPlus;
|
||
|
|
|
||
|
|
internal static class AppPaths
|
||
|
|
{
|
||
|
|
internal static readonly string DataDirectory = Path.Combine(
|
||
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||
|
|
"UCVL",
|
||
|
|
"VPNPlus");
|
||
|
|
|
||
|
|
internal static string DeviceIdFile => Path.Combine(DataDirectory, "device-id.txt");
|
||
|
|
internal static string TokenFile => Path.Combine(DataDirectory, "session.bin");
|
||
|
|
internal static string ProxyStateFile => Path.Combine(DataDirectory, "proxy-state.json");
|
||
|
|
internal static string TunStateFile => Path.Combine(DataDirectory, "tun-state.json");
|
||
|
|
internal static string LearnedRoutesFile => Path.Combine(DataDirectory, "learned-routes.json");
|
||
|
|
internal static string ConfigFile => Path.Combine(DataDirectory, "xray-config.json");
|
||
|
|
internal static string PidFile => Path.Combine(DataDirectory, "xray.pid");
|
||
|
|
internal static string LogFile => Path.Combine(DataDirectory, "vpnplus.log");
|
||
|
|
internal static string XrayLogFile => Path.Combine(DataDirectory, "xray.log");
|
||
|
|
internal static string ConnectionSelfTestFile => Path.Combine(DataDirectory, "connection-self-test.json");
|
||
|
|
|
||
|
|
internal static void EnsureDirectory() => Directory.CreateDirectory(DataDirectory);
|
||
|
|
|
||
|
|
internal static string GetOrCreateDeviceId()
|
||
|
|
{
|
||
|
|
EnsureDirectory();
|
||
|
|
if (File.Exists(DeviceIdFile))
|
||
|
|
{
|
||
|
|
var existing = File.ReadAllText(DeviceIdFile, Encoding.UTF8).Trim();
|
||
|
|
if (Guid.TryParse(existing, out _))
|
||
|
|
{
|
||
|
|
return existing;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var deviceId = Guid.NewGuid().ToString("D");
|
||
|
|
File.WriteAllText(DeviceIdFile, deviceId, new UTF8Encoding(false));
|
||
|
|
return deviceId;
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static void WriteLog(string message)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
EnsureDirectory();
|
||
|
|
File.AppendAllText(LogFile, $"{DateTimeOffset.Now:O} {message}{Environment.NewLine}", Encoding.UTF8);
|
||
|
|
}
|
||
|
|
catch
|
||
|
|
{
|
||
|
|
// Logging must never prevent connecting or restoring the system proxy.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
internal static void WriteError(Exception exception) => WriteLog($"ERROR {exception}");
|
||
|
|
}
|