using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace SharpChat.Flashii { public static class FlashiiUrls { private const string BASE_URL_FILE = "msz_url.txt"; private const string BASE_URL_FALLBACK = "https://flashii.net"; 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}"; 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; } static FlashiiUrls() { BumpURL = GetURL(BUMP); VerifyURL = GetURL(VERIFY); BansCheckURL = GetURL(BANS_CHECK); BansCreateURL = GetURL(BANS_CREATE); BansRevokeURL = GetURL(BANS_REVOKE); BansListURL = GetURL(BANS_LIST); } public static string GetBaseURL() { if(!File.Exists(BASE_URL_FILE)) return BASE_URL_FALLBACK; string url = File.ReadAllText(BASE_URL_FILE).Trim().Trim('/'); if(string.IsNullOrEmpty(url)) return BASE_URL_FALLBACK; 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 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 } } } }