auth finished

This commit is contained in:
ju09279
2024-09-06 22:56:08 +02:00
parent b1be8502c9
commit f26529b1a3
11 changed files with 397 additions and 35 deletions

85
backend/HttpClient.cs Normal file
View File

@@ -0,0 +1,85 @@
using OsuParsers.Enums.Replays;
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace shitweb
{
public class ApiClient
{
private const string CookiesFilePath = "cookies.json";
private HttpClient _client;
private HttpClientHandler _handler;
public ApiClient()
{
_handler = new HttpClientHandler();
_client = new HttpClient(_handler);
}
public async Task InitializeAsync()
{
LoadCookies();
}
public void SaveCookies(String cookie)
{
var cookies = _handler.CookieContainer.GetCookies(new Uri("https://proxy.illegalesachen.download"));
var Cookie = new CookieInfo();
Cookie.Name = "session_cookie";
Cookie.Value = cookie;
var json = JsonSerializer.Serialize(Cookie, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(CookiesFilePath, json);
LoadCookies();
}
public Boolean LoadCookies()
{
if (File.Exists(CookiesFilePath))
{
var json = File.ReadAllText(CookiesFilePath);
var cookieInfo = JsonSerializer.Deserialize<CookieInfo>(json);
var cookie = new Cookie(cookieInfo.Name, cookieInfo.Value);
_handler.CookieContainer.Add(new Uri("https://proxy.illegalesachen.download"), cookie);
return true;
}
return false;
}
public async Task UpdateSettingsAsync(string endpoint)
{
var requestContent = new JsonContent(new { endpoint = endpoint });
var response = await _client.PostAsync("https://proxy.illegalesachen.download/settings", requestContent);
try
{
response.EnsureSuccessStatusCode();
}
catch (Exception ex) {
System.Console.WriteLine(ex.Message);
}
}
}
public class CookieInfo
{
public string Name { get; set; }
public string Value { get; set; }
}
public class JsonContent : StringContent
{
public JsonContent(object obj)
: base(JsonSerializer.Serialize(obj), System.Text.Encoding.UTF8, "application/json")
{
}
}
}