sharp-chat/SharpChat/Flashii/FlashiiUrls.cs

93 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
2022-08-30 15:42:03 +00:00
namespace SharpChat.Flashii {
2022-08-30 15:00:58 +00:00
public static class FlashiiUrls {
private const string BASE_URL_FILE = "msz_url.txt";
private const string BASE_URL_FALLBACK = "https://flashii.net";
2022-08-30 15:42:03 +00:00
private const string BUMP = "/_sockchat/bump";
private const string VERIFY = "/_sockchat/verify";
private const string BANS_CHECK = "/_sockchat/bans/check?u={0}&a={1}&x={2}&n={3}";
private const string BANS_CREATE = "/_sockchat/bans/create";
private const string BANS_REVOKE = "/_sockchat/bans/revoke?t={0}&s={1}&x={2}";
private const string BANS_LIST = "/_sockchat/bans/list?x={0}";
2022-08-30 15:42:03 +00:00
public static string BumpURL { get; }
public static string VerifyURL { get; }
public static string BansCheckURL { get; }
public static string BansCreateURL { get; }
public static string BansRevokeURL { get; }
public static string BansListURL { get; }
2023-02-07 15:01:56 +00:00
2022-08-30 15:42:03 +00:00
static FlashiiUrls() {
BumpURL = GetURL(BUMP);
VerifyURL = GetURL(VERIFY);
BansCheckURL = GetURL(BANS_CHECK);
BansCreateURL = GetURL(BANS_CREATE);
BansRevokeURL = GetURL(BANS_REVOKE);
BansListURL = GetURL(BANS_LIST);
2022-08-30 15:42:03 +00:00
}
public static string GetBaseURL() {
if(!File.Exists(BASE_URL_FILE))
2022-08-30 15:44:33 +00:00
return BASE_URL_FALLBACK;
2022-08-30 15:42:03 +00:00
string url = File.ReadAllText(BASE_URL_FILE).Trim().Trim('/');
if(string.IsNullOrEmpty(url))
2022-08-30 15:44:33 +00:00
return BASE_URL_FALLBACK;
2022-08-30 15:42:03 +00:00
return url;
}
public static string GetURL(string path) {
return GetBaseURL() + path;
}
public static async Task BumpUsersOnlineAsync(HttpClient client, IEnumerable<(string userId, string ipAddr)> list) {
if(client == null)
throw new ArgumentNullException(nameof(client));
if(list == null)
throw new ArgumentNullException(nameof(list));
if(!list.Any())
return;
string now = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
StringBuilder sb = new();
sb.AppendFormat("bump#{0}", now);
Dictionary<string, string> formData = new() {
{ "t", now }
};
foreach(var (userId, ipAddr) in list) {
sb.AppendFormat("#{0}:{1}", userId, ipAddr);
formData.Add(string.Format("u[{0}]", userId), ipAddr);
}
HttpRequestMessage req = new(HttpMethod.Post, BumpURL) {
Headers = {
{ "X-SharpChat-Signature", sb.ToString().GetSignedHash() }
},
Content = new FormUrlEncodedContent(formData),
};
using HttpResponseMessage res = await client.SendAsync(req);
try {
res.EnsureSuccessStatusCode();
} catch(HttpRequestException) {
Logger.Debug(await res.Content.ReadAsStringAsync());
#if DEBUG
throw;
#endif
}
}
2022-08-30 15:00:58 +00:00
}
}