mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-02 04:22:04 -04:00
Initial commit
This commit is contained in:
42
KfChatDotNetGui/Helpers/ForumIdentity.cs
Normal file
42
KfChatDotNetGui/Helpers/ForumIdentity.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using KfChatDotNetGui.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace KfChatDotNetGui.Helpers;
|
||||
|
||||
public static class ForumIdentity
|
||||
{
|
||||
public static async Task<ForumIdentityModel?> GetForumIdentity(string xfSession, Uri sneedChatUri, string? antiDdosPowCookie = null)
|
||||
{
|
||||
CookieContainer cookies = new CookieContainer();
|
||||
cookies.Add(new Cookie("xf_session", xfSession, "/", sneedChatUri.Host));
|
||||
if (antiDdosPowCookie != null)
|
||||
{
|
||||
cookies.Add(new Cookie("z_ddos_pow", antiDdosPowCookie, "/", sneedChatUri.Host));
|
||||
}
|
||||
using (var client = new HttpClient(new HttpClientHandler {AutomaticDecompression = DecompressionMethods.All, CookieContainer = cookies}))
|
||||
{
|
||||
client.DefaultRequestHeaders.UserAgent.TryParseAdd("KfChatDotNetGui/1.0");
|
||||
client.DefaultRequestHeaders.Accept.Clear();
|
||||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/html"));
|
||||
client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US"));
|
||||
var response = await client.GetAsync(sneedChatUri);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var html = await response.Content.ReadAsStringAsync();
|
||||
var accountRegex = new Regex(@"user: (.+),");
|
||||
var match = accountRegex.Match(html);
|
||||
if (!match.Success)
|
||||
{
|
||||
throw new Exception("Shitty regex failed to extract account information");
|
||||
}
|
||||
|
||||
var accountJs = match.Groups[1].Value;
|
||||
return JsonConvert.DeserializeObject<ForumIdentityModel>(accountJs);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user