sochkeal/Satori/Program.cs
2023-06-28 22:12:23 +02:00

80 lines
3 KiB
C#

using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace SockChatKeepAlive {
public static class Program {
public const string PERSIST_FILE = "Persist.dat";
public const string AUTH_TOKEN = "AuthToken.txt";
public const string STORAGE_DIR_NAME = ".sochkeal";
public static readonly ManualResetEvent ManualReset = new(false);
public static string StorageDirectory => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), STORAGE_DIR_NAME);
public static async Task Main() {
Console.WriteLine("Starting Sock Chat Keep Alive...");
if(!Directory.Exists(StorageDirectory)) {
Console.WriteLine("Storage directory does not exist, it will be created...");
Directory.CreateDirectory(StorageDirectory).Attributes |= FileAttributes.Hidden;
}
string authTokenPath = Path.Combine(StorageDirectory, AUTH_TOKEN);
if(!File.Exists(authTokenPath)) {
File.WriteAllLines(authTokenPath, new[] {
"Misuzu",
"Token goes here",
"Futami shared url goes here"
});
Console.WriteLine("Auth token file not found! Configure it at:");
Console.WriteLine(authTokenPath);
return;
}
string[] getAuthInfo() { return File.ReadAllLines(authTokenPath); };
using ManualResetEvent mre = new(false);
bool hasCancelled = false;
void cancelKeyPressHandler(object sender, ConsoleCancelEventArgs ev) {
Console.CancelKeyPress -= cancelKeyPressHandler;
hasCancelled = true;
ev.Cancel = true;
mre.Set();
};
Console.CancelKeyPress += cancelKeyPressHandler;
if(hasCancelled) return;
using HttpClient httpClient = new();
httpClient.DefaultRequestHeaders.Add("Accept-Language", "en-GB,en;q=0.5");
httpClient.DefaultRequestHeaders.Add("DNT", "1");
httpClient.DefaultRequestHeaders.Add("User-Agent", "SockChatKeepAlive/20230319 (+https://fii.moe/beans)");
if(hasCancelled) return;
Console.WriteLine("Loading persistent data file...");
using PersistentData persist = new(Path.Combine(StorageDirectory, PERSIST_FILE));
if(hasCancelled) return;
Console.WriteLine("Loading Futami common...");
FutamiCommon common = await FutamiCommon.FetchAsync(httpClient, getAuthInfo()[2]);
if(hasCancelled) return;
Console.WriteLine("Connecting to Sock Chat server...");
using SockChatClient client = new(common, persist, getAuthInfo);
client.Connect();
mre.WaitOne();
persist.WasGracefulDisconnect = false;
Console.WriteLine(@"Bye!");
}
}
}