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")
{
}
}
}

View File

@@ -1,11 +1,17 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using shitweb;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Net.Http;
using System.Text.RegularExpressions;
var builder = WebApplication.CreateBuilder(args);
@@ -25,18 +31,24 @@ builder.Services.AddCors(options =>
var app = builder.Build();
app.UseCors("AllowAll");
var apiClient = new ApiClient();
app.MapGet("/ping", () => "pong");
// Define the API routes
app.MapGet("/api/v1/songs/{hash}", (string hash) =>
{
app.MapGet("/login", () => {
return Results.Redirect("https://proxy.illegalesachen.download/login");
});
return Results.Ok(new { hash });
});
app.MapGet("/api/v1/songs/{hash}", (string hash) =>
{
return Results.Ok(new { hash });
});
app.MapGet("/api/v1/songs/recent", (int? limit, int? offset) =>
{
app.MapGet("/api/v1/songs/recent", (HttpContext httpContext, int? limit, int? offset) =>
{
var limitValue = limit ?? 100; // default to 10 if not provided
var offsetValue = offset ?? 0; // default to 0 if not provided
@@ -225,4 +237,63 @@ static ImageFormat GetImageFormat(string extension)
}
Osudb.Instance.ToString();
app.Run();
startCloudflared();
Task.Run(() =>
{
Thread.Sleep(500);
if (!apiClient.LoadCookies())
{
Console.WriteLine("Please visit this link and paste the Value back into here: ");
var cookie = Console.ReadLine();
apiClient.SaveCookies(cookie);
}
Console.WriteLine("Ur Osu songs should now be available, please delete the cookies.json if it doesnt show up and try again.");
});
await apiClient.InitializeAsync();
app.Run();
async Task startCloudflared() {
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cloudflared",
Arguments = "tunnel --url http://localhost:5153",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.ErrorDataReceived += (sender, e) => {
if (!string.IsNullOrEmpty(e.Data))
{
ParseForUrls(e.Data);
}
};
process.Start();
process.BeginErrorReadLine();
await Task.Run(() => process.WaitForExit());
}
void ParseForUrls(string data)
{
var urlRegex = new Regex(@"https?://[^\s]*\.trycloudflare\.com");
var matches = urlRegex.Matches(data);
foreach (Match match in matches)
{
Console.WriteLine($"Login here if not done already: {match.Value}/login");
apiClient.UpdateSettingsAsync(match.Value);
}
}

4
backend/cookies.json Normal file
View File

@@ -0,0 +1,4 @@
{
"Name": "session_cookie",
"Value": "session_cookie_c_PhxGlrrYcWY1h9cQ6mu_LxsDIVu0u_Nv0qAK1WmvcZiZXQ_FcwXZZXDk5Tm0qrqZTHHs800Pv5bTMGIrfa8Q=="
}