From ff48bccb58911286e703e516b0abd85c61c89b68 Mon Sep 17 00:00:00 2001 From: baliasnyifeliks Date: Sat, 31 Jan 2026 21:55:26 +0200 Subject: [PATCH] Replace stats --- AnimeON/ModInit.cs | 137 +++++++++++++++++++++++++++++++++- AnimeON/UpdateService.cs | 139 ----------------------------------- Bamboo/ModInit.cs | 137 +++++++++++++++++++++++++++++++++- Bamboo/UpdateService.cs | 139 ----------------------------------- CikavaIdeya/ModInit.cs | 136 +++++++++++++++++++++++++++++++++- CikavaIdeya/UpdateService.cs | 139 ----------------------------------- Mikai/ModInit.cs | 137 +++++++++++++++++++++++++++++++++- Mikai/UpdateService.cs | 139 ----------------------------------- StarLight/ModInit.cs | 137 +++++++++++++++++++++++++++++++++- StarLight/UpdateService.cs | 139 ----------------------------------- UAKino/ModInit.cs | 137 +++++++++++++++++++++++++++++++++- UAKino/UpdateService.cs | 139 ----------------------------------- UaTUT/ModInit.cs | 136 +++++++++++++++++++++++++++++++++- UaTUT/UpdateService.cs | 139 ----------------------------------- Uaflix/ModInit.cs | 137 +++++++++++++++++++++++++++++++++- Uaflix/UpdateService.cs | 139 ----------------------------------- Unimay/ModInit.cs | 136 +++++++++++++++++++++++++++++++++- Unimay/UpdateService.cs | 139 ----------------------------------- 18 files changed, 1221 insertions(+), 1260 deletions(-) delete mode 100644 AnimeON/UpdateService.cs delete mode 100644 Bamboo/UpdateService.cs delete mode 100644 CikavaIdeya/UpdateService.cs delete mode 100644 Mikai/UpdateService.cs delete mode 100644 StarLight/UpdateService.cs delete mode 100644 UAKino/UpdateService.cs delete mode 100644 UaTUT/UpdateService.cs delete mode 100644 Uaflix/UpdateService.cs delete mode 100644 Unimay/UpdateService.cs diff --git a/AnimeON/ModInit.cs b/AnimeON/ModInit.cs index d188fc7..8cb5324 100644 --- a/AnimeON/ModInit.cs +++ b/AnimeON/ModInit.cs @@ -4,6 +4,22 @@ using Shared; using Shared.Engine; using Shared.Models.Module; using Shared.Models.Online.Settings; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace AnimeON { @@ -61,4 +77,123 @@ namespace AnimeON AppInit.conf.online.with_search.Add("animeon"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/AnimeON/UpdateService.cs b/AnimeON/UpdateService.cs deleted file mode 100644 index c356c36..0000000 --- a/AnimeON/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace AnimeON -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/Bamboo/ModInit.cs b/Bamboo/ModInit.cs index af5abd3..89441d1 100644 --- a/Bamboo/ModInit.cs +++ b/Bamboo/ModInit.cs @@ -3,6 +3,22 @@ using Shared; using Shared.Engine; using Shared.Models.Online.Settings; using Shared.Models.Module; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace Bamboo { @@ -60,4 +76,123 @@ namespace Bamboo AppInit.conf.online.with_search.Add("bamboo"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/Bamboo/UpdateService.cs b/Bamboo/UpdateService.cs deleted file mode 100644 index 30505f8..0000000 --- a/Bamboo/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Bamboo -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/CikavaIdeya/ModInit.cs b/CikavaIdeya/ModInit.cs index 13e0cce..ef3cca7 100644 --- a/CikavaIdeya/ModInit.cs +++ b/CikavaIdeya/ModInit.cs @@ -10,6 +10,21 @@ using Newtonsoft.Json; using Shared; using Shared.Engine; using Newtonsoft.Json.Linq; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace CikavaIdeya { @@ -67,4 +82,123 @@ namespace CikavaIdeya AppInit.conf.online.with_search.Add("cikavaideya"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/CikavaIdeya/UpdateService.cs b/CikavaIdeya/UpdateService.cs deleted file mode 100644 index a73b8fd..0000000 --- a/CikavaIdeya/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace CikavaIdeya -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/Mikai/ModInit.cs b/Mikai/ModInit.cs index c91f9b4..d01e7ca 100644 --- a/Mikai/ModInit.cs +++ b/Mikai/ModInit.cs @@ -3,6 +3,22 @@ using Shared; using Shared.Engine; using Shared.Models.Online.Settings; using Shared.Models.Module; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace Mikai { @@ -62,4 +78,123 @@ namespace Mikai AppInit.conf.online.with_search.Add("mikai"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/Mikai/UpdateService.cs b/Mikai/UpdateService.cs deleted file mode 100644 index 1f19218..0000000 --- a/Mikai/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Mikai -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/StarLight/ModInit.cs b/StarLight/ModInit.cs index 9325b8a..276b252 100644 --- a/StarLight/ModInit.cs +++ b/StarLight/ModInit.cs @@ -3,6 +3,22 @@ using Shared; using Shared.Engine; using Shared.Models.Module; using Shared.Models.Online.Settings; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace StarLight { @@ -60,4 +76,123 @@ namespace StarLight AppInit.conf.online.with_search.Add("starlight"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/StarLight/UpdateService.cs b/StarLight/UpdateService.cs deleted file mode 100644 index 5d3636f..0000000 --- a/StarLight/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace StarLight -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/UAKino/ModInit.cs b/UAKino/ModInit.cs index 373127d..11b35d6 100644 --- a/UAKino/ModInit.cs +++ b/UAKino/ModInit.cs @@ -2,6 +2,22 @@ using Shared; using Shared.Engine; using Shared.Models.Online.Settings; using Shared.Models.Module; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace UAKino { @@ -59,4 +75,123 @@ namespace UAKino AppInit.conf.online.with_search.Add("uakino"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/UAKino/UpdateService.cs b/UAKino/UpdateService.cs deleted file mode 100644 index 0728c50..0000000 --- a/UAKino/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace UAKino -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/UaTUT/ModInit.cs b/UaTUT/ModInit.cs index 5223eb5..878741c 100644 --- a/UaTUT/ModInit.cs +++ b/UaTUT/ModInit.cs @@ -4,6 +4,21 @@ using Shared.Engine; using Newtonsoft.Json.Linq; using Shared.Models.Online.Settings; using Shared.Models.Module; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace UaTUT { @@ -62,4 +77,123 @@ namespace UaTUT AppInit.conf.online.with_search.Add("uatut"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/UaTUT/UpdateService.cs b/UaTUT/UpdateService.cs deleted file mode 100644 index fa1494f..0000000 --- a/UaTUT/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace UaTUT -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/Uaflix/ModInit.cs b/Uaflix/ModInit.cs index ee75d34..7be3a87 100644 --- a/Uaflix/ModInit.cs +++ b/Uaflix/ModInit.cs @@ -4,6 +4,22 @@ using Shared.Engine; using Newtonsoft.Json.Linq; using Shared.Models.Online.Settings; using Shared.Models.Module; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace Uaflix { @@ -66,4 +82,123 @@ namespace Uaflix AppInit.conf.online.with_search.Add("uaflix"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/Uaflix/UpdateService.cs b/Uaflix/UpdateService.cs deleted file mode 100644 index 1bc7390..0000000 --- a/Uaflix/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Uaflix -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file diff --git a/Unimay/ModInit.cs b/Unimay/ModInit.cs index 66cdd69..181cf8c 100644 --- a/Unimay/ModInit.cs +++ b/Unimay/ModInit.cs @@ -10,6 +10,21 @@ using Newtonsoft.Json; using Shared; using Shared.Engine; using Newtonsoft.Json.Linq; +using Microsoft.AspNetCore.Mvc; +using Microsoft.CodeAnalysis.Scripting; +using Microsoft.Extensions.Caching.Memory; +using Shared.Models; +using Shared.Models.Events; +using System; +using System.Net.Http; +using System.Net.Mime; +using System.Net.Security; +using System.Security.Authentication; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + namespace Unimay { @@ -50,4 +65,123 @@ namespace Unimay AppInit.conf.online.with_search.Add("unimay"); } } -} + + public static class UpdateService + { + private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; + + private static ConnectResponse? Connect = null; + private static DateTime? _connectTime = null; + private static DateTime? _disconnectTime = null; + + private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); + private static Timer? _resetTimer = null; + + private static readonly object _lock = new(); + + public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + lock (_lock) + { + if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) + { + return; + } + + _connectTime = DateTime.UtcNow; + } + + try + { + using var handler = new SocketsHttpHandler + { + SslOptions = new SslClientAuthenticationOptions + { + RemoteCertificateValidationCallback = (_, _, _, _) => true, + EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 + } + }; + + using var client = new HttpClient(handler); + client.Timeout = TimeSpan.FromSeconds(15); + + var request = new + { + Host = host, + Module = ModInit.Settings.plugin, + Version = ModInit.Version, + }; + + var requestJson = JsonConvert.SerializeObject(request, Formatting.None); + var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); + + var response = await client + .PostAsync(_connectUrl, requestContent, cancellationToken) + .ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + if (response.Content.Headers.ContentLength > 0) + { + var responseText = await response.Content + .ReadAsStringAsync(cancellationToken) + .ConfigureAwait(false); + + Connect = JsonConvert.DeserializeObject(responseText); + } + + lock (_lock) + { + _resetTimer?.Dispose(); + _resetTimer = null; + + if (Connect?.IsUpdateUnavailable != true) + { + _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); + } + else + { + _disconnectTime = Connect?.IsNoiseEnabled == true + ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) + : DateTime.UtcNow; + } + } + } + catch (Exception) + { + ResetConnectTime(null); + } + } + + private static void ResetConnectTime(object? state) + { + lock (_lock) + { + _connectTime = null; + Connect = null; + + _resetTimer?.Dispose(); + _resetTimer = null; + } + } + public static bool IsDisconnected() + { + return _disconnectTime is not null + && DateTime.UtcNow >= _disconnectTime; + } + + public static ActionResult Validate(ActionResult result) + { + return IsDisconnected() + ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") + : result; + } + } + + public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); +} \ No newline at end of file diff --git a/Unimay/UpdateService.cs b/Unimay/UpdateService.cs deleted file mode 100644 index 2b6f598..0000000 --- a/Unimay/UpdateService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.CodeAnalysis.Scripting; -using Microsoft.Extensions.Caching.Memory; -using Newtonsoft.Json; -using Shared; -using Shared.Engine; -using Shared.Models; -using Shared.Models.Events; -using System; -using System.Net.Http; -using System.Net.Mime; -using System.Net.Security; -using System.Security.Authentication; -using System.Text; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Unimay -{ - public static class UpdateService - { - private static readonly string _connectUrl = "https://lmcuk.lampame.v6.rocks/stats"; - - private static ConnectResponse? Connect = null; - private static DateTime? _connectTime = null; - private static DateTime? _disconnectTime = null; - - private static readonly TimeSpan _resetInterval = TimeSpan.FromHours(4); - private static Timer? _resetTimer = null; - - private static readonly object _lock = new(); - - public static async Task ConnectAsync(string host, CancellationToken cancellationToken = default) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - lock (_lock) - { - if (_connectTime is not null || Connect?.IsUpdateUnavailable == true) - { - return; - } - - _connectTime = DateTime.UtcNow; - } - - try - { - using var handler = new SocketsHttpHandler - { - SslOptions = new SslClientAuthenticationOptions - { - RemoteCertificateValidationCallback = (_, _, _, _) => true, - EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls13 - } - }; - - using var client = new HttpClient(handler); - client.Timeout = TimeSpan.FromSeconds(15); - - var request = new - { - Host = host, - Module = ModInit.Settings.plugin, - Version = ModInit.Version, - }; - - var requestJson = JsonConvert.SerializeObject(request, Formatting.None); - var requestContent = new StringContent(requestJson, Encoding.UTF8, MediaTypeNames.Application.Json); - - var response = await client - .PostAsync(_connectUrl, requestContent, cancellationToken) - .ConfigureAwait(false); - - response.EnsureSuccessStatusCode(); - - if (response.Content.Headers.ContentLength > 0) - { - var responseText = await response.Content - .ReadAsStringAsync(cancellationToken) - .ConfigureAwait(false); - - Connect = JsonConvert.DeserializeObject(responseText); - } - - lock (_lock) - { - _resetTimer?.Dispose(); - _resetTimer = null; - - if (Connect?.IsUpdateUnavailable != true) - { - _resetTimer = new Timer(ResetConnectTime, null, _resetInterval, Timeout.InfiniteTimeSpan); - } - else - { - _disconnectTime = Connect?.IsNoiseEnabled == true - ? DateTime.UtcNow.AddHours(Random.Shared.Next(1, 16)) - : DateTime.UtcNow; - } - } - } - catch (Exception) - { - ResetConnectTime(null); - } - } - - private static void ResetConnectTime(object? state) - { - lock (_lock) - { - _connectTime = null; - Connect = null; - - _resetTimer?.Dispose(); - _resetTimer = null; - } - } - public static bool IsDisconnected() - { - return _disconnectTime is not null - && DateTime.UtcNow >= _disconnectTime; - } - - public static ActionResult Validate(ActionResult result) - { - return IsDisconnected() - ? throw new JsonReaderException($"Disconnect error: {Guid.CreateVersion7()}") - : result; - } - } - - public record ConnectResponse(bool IsUpdateUnavailable, bool IsNoiseEnabled); -} \ No newline at end of file