mirror of
https://github.com/lampame/lampac-ukraine.git
synced 2026-04-16 17:32:20 +00:00
BREAKING CHANGE: All module routes changed from /{module} to /lite/{module}
- Implement IModuleLoaded and IModuleOnline interfaces across all modules
- Add HttpHydra support to all Invoke classes for HTTP request handling
- Replace ModuleInvoke.Conf() with ModuleInvoke.Init() in all ModInit classes
- Convert loadKit() from async to synchronous calls in all controllers
- Replace direct AppInit.conf.online.with_search.Add() with reflection-based
RegisterWithSearch() method for decoupled module registration
- Simplify cacheTime() logic by removing mikrotik/multiaccess conditionals
- Add GlobalUsings.cs to all modules for shared namespace imports
- Update OnlineApi to use ModuleOnlineItem instead of value tuples
- Bump all module versions to new major versions
240 lines
7.7 KiB
C#
240 lines
7.7 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Shared;
|
|
using Shared.Engine;
|
|
using Shared.Models.Module;
|
|
using Shared.Models.Module.Interfaces;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Net.Mime;
|
|
using System.Net.Security;
|
|
using System.Security.Authentication;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Uaflix.Models;
|
|
|
|
namespace Uaflix
|
|
{
|
|
public class ModInit : IModuleLoaded
|
|
{
|
|
public static double Version => 5.0;
|
|
|
|
public static UaflixSettings UaFlix;
|
|
|
|
public static bool ApnHostProvided;
|
|
|
|
public static UaflixSettings Settings
|
|
{
|
|
get => UaFlix;
|
|
set => UaFlix = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Модуль завантажено.
|
|
/// </summary>
|
|
public void Loaded(InitspaceModel initspace)
|
|
{
|
|
UaFlix = new UaflixSettings("Uaflix", "https://uafix.net", streamproxy: false, useproxy: false)
|
|
{
|
|
displayname = "UaFlix",
|
|
group = 0,
|
|
group_hide = false,
|
|
globalnameproxy = null,
|
|
displayindex = 0,
|
|
login = null,
|
|
passwd = null,
|
|
proxy = new Shared.Models.Base.ProxySettings()
|
|
{
|
|
useAuth = true,
|
|
username = "a",
|
|
password = "a",
|
|
list = new string[] { "socks5://IP:PORT" }
|
|
}
|
|
};
|
|
|
|
var conf = ModuleInvoke.Init("Uaflix", JObject.FromObject(UaFlix)) ?? JObject.FromObject(UaFlix);
|
|
bool hasApn = ApnHelper.TryGetInitConf(conf, out bool apnEnabled, out string apnHost);
|
|
conf.Remove("apn");
|
|
conf.Remove("apn_host");
|
|
UaFlix = conf.ToObject<UaflixSettings>();
|
|
|
|
if (hasApn)
|
|
ApnHelper.ApplyInitConf(apnEnabled, apnHost, UaFlix);
|
|
|
|
ApnHostProvided = hasApn && apnEnabled && !string.IsNullOrWhiteSpace(apnHost);
|
|
if (hasApn && apnEnabled)
|
|
{
|
|
UaFlix.streamproxy = false;
|
|
}
|
|
else if (UaFlix.streamproxy)
|
|
{
|
|
UaFlix.apnstream = false;
|
|
UaFlix.apn = null;
|
|
}
|
|
|
|
// Показувати «уточнити пошук».
|
|
RegisterWithSearch("uaflix");
|
|
}
|
|
|
|
private static void RegisterWithSearch(string plugin)
|
|
{
|
|
try
|
|
{
|
|
var onlineType = Type.GetType("Online.ModInit");
|
|
if (onlineType == null)
|
|
{
|
|
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
|
|
{
|
|
onlineType = asm.GetType("Online.ModInit");
|
|
if (onlineType != null)
|
|
break;
|
|
}
|
|
}
|
|
var confField = onlineType?.GetField("conf", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
|
|
var conf = confField?.GetValue(null);
|
|
var withSearchProp = conf?.GetType().GetProperty("with_search", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
|
|
|
|
if (withSearchProp?.GetValue(conf) is System.Collections.IList list)
|
|
{
|
|
foreach (var item in list)
|
|
{
|
|
if (string.Equals(item?.ToString(), plugin, StringComparison.OrdinalIgnoreCase))
|
|
return;
|
|
}
|
|
|
|
list.Add(plugin);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
}
|
|
|
|
public static class UpdateService
|
|
{
|
|
private static readonly string _connectUrl = "https://lmcuk.lme.isroot.in/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<ConnectResponse>(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, 4))
|
|
: 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);
|
|
}
|