Skip to content

Commit

Permalink
121.1
Browse files Browse the repository at this point in the history
  • Loading branch information
immisterio committed Oct 10, 2024
1 parent c0df7aa commit 0559609
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 24 deletions.
11 changes: 10 additions & 1 deletion Lampac/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Hosting;
using Shared.Engine;

namespace Lampac
{
Expand Down Expand Up @@ -183,7 +185,7 @@ public void ConfigureServices(IServiceCollection services)
#endregion


public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IMemoryCache memory, System.Net.Http.IHttpClientFactory httpClientFactory)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IMemoryCache memory, System.Net.Http.IHttpClientFactory httpClientFactory, IHostApplicationLifetime applicationLifetime)
{
memoryCache = memory;
Shared.Startup.Configure(app, memory);
Expand All @@ -192,6 +194,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IMemoryC
HttpClient.httpClientFactory = httpClientFactory;

app.UseDeveloperExceptionPage();
applicationLifetime.ApplicationStopping.Register(OnShutdown);

#region UseForwardedHeaders
var forwarded = new ForwardedHeadersOptions
Expand Down Expand Up @@ -246,5 +249,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IMemoryC
endpoints.MapControllers();
});
}


private void OnShutdown()
{
PuppeteerTo.FullDispose();
}
}
}
8 changes: 6 additions & 2 deletions Online/OnlineApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,13 @@ void send(string name, BaseSettings init, string plugin = null, string arg_title

if (kinopoisk_id > 0)
{
send("VDBmovies", conf.VDBmovies);
send("VideoDB", conf.VideoDB);
send("Zetflix", conf.Zetflix);

if (AppInit.conf.puppeteer.enable)
{
send("VDBmovies", conf.VDBmovies);
send("Zetflix", conf.Zetflix);
}

if (serial == -1 || serial == 0)
send("FanCDN", conf.FanCDN);
Expand Down
2 changes: 1 addition & 1 deletion Shared/AppInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static void LoadModules()

public WebLogConf weblog = new WebLogConf();

public RchConf rch = new RchConf() { enable = false, keepalive = 180 };
public RchConf rch = new RchConf() { enable = true, keepalive = 45 };

public string anticaptchakey;

Expand Down
4 changes: 2 additions & 2 deletions Shared/Engine/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class BaseController : Controller, IDisposable
{
IServiceScope serviceScope;

public static string appversion => "120";
public static string appversion => "121";

public static string minorversion => "8";
public static string minorversion => "1";

public HybridCache hybridCache { get; private set; }

Expand Down
31 changes: 24 additions & 7 deletions Shared/Engine/PuppeteerTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using PuppeteerSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,17 +15,17 @@ public class PuppeteerTo : IDisposable

static DateTime exLifetime = default;

static bool isdev = File.Exists(@"C:\ProgramData\lampac\disablesync");
static bool shutdown = false;

public static bool IsKeepOpen => AppInit.conf.multiaccess || AppInit.conf.puppeteer.keepopen;

static PuppeteerTo()
{
ThreadPool.QueueUserWorkItem(async _ =>
{
while (true)
while (!shutdown)
{
await Task.Delay(TimeSpan.FromMinutes(2));
await Task.Delay(TimeSpan.FromMinutes(2)).ConfigureAwait(false);
try
{
Expand Down Expand Up @@ -67,6 +66,9 @@ async private static void Browser_keepopen_Closed(object sender, EventArgs e)

async public static ValueTask<PuppeteerTo> Browser()
{
if (shutdown)
return null;

if (IsKeepOpen && browser_keepopen == null)
LaunchKeepOpen();

Expand All @@ -78,15 +80,15 @@ async public static ValueTask<PuppeteerTo> Browser()

static Task<IBrowser> Launch()
{
if (!AppInit.conf.puppeteer.enable)
if (!AppInit.conf.puppeteer.enable || shutdown)
return null;

try
{
var option = new LaunchOptions()
{
Headless = !isdev, /*false*/
Devtools = isdev,
Headless = AppInit.conf.puppeteer.headless,
Devtools = AppInit.conf.puppeteer.headless,
IgnoreHTTPSErrors = true,
Args = new string[] { "--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage", "--disable-gpu", "--renderer-process-limit=1" },
Timeout = 12_000
Expand Down Expand Up @@ -190,5 +192,20 @@ public void Dispose()
}
catch { }
}

public static void FullDispose()
{
if (browser_keepopen == null)
return;

shutdown = true;

try
{
browser_keepopen.CloseAsync().Wait();
browser_keepopen.Dispose();
}
catch { }
}
}
}
2 changes: 2 additions & 0 deletions Shared/Models/AppConf/PuppeteerConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class PuppeteerConf

public bool keepopen { get; set; }

public bool headless { get; set; }

public string executablePath { get; set; }
}
}
16 changes: 8 additions & 8 deletions TorrServer/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async public Task Index()

if (HttpContext.Request.Path.Value.StartsWith("/ts/echo"))
{
await HttpContext.Response.WriteAsync("MatriX.API", HttpContext.RequestAborted);
await HttpContext.Response.WriteAsync("MatriX.API", HttpContext.RequestAborted).ConfigureAwait(false);
return;
}

Expand All @@ -132,31 +132,31 @@ async public Task TorAPI()
if (HttpContext.Request.Method != "POST")
{
HttpContext.Response.StatusCode = 404;
await HttpContext.Response.WriteAsync("404 page not found", HttpContext.RequestAborted);
await HttpContext.Response.WriteAsync("404 page not found", HttpContext.RequestAborted).ConfigureAwait(false);
return;
}

MemoryStream mem = new MemoryStream();
await HttpContext.Request.Body.CopyToAsync(mem, HttpContext.RequestAborted);
string requestJson = Encoding.UTF8.GetString(mem.ToArray());

using (HttpClient client = new HttpClient())
using (HttpClient client = Engine.CORE.HttpClient.httpClientFactory.CreateClient("base"))
{
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.Add("Authorization", $"Basic {Engine.CORE.CrypTo.Base64($"ts:{ModInit.tspass}")}");

if (requestJson.Contains("\"get\""))
{
var response = await client.PostAsync($"http://{AppInit.conf.localhost}:{ModInit.tsport}/settings", new StringContent("{\"action\":\"get\"}", Encoding.UTF8, "application/json"), HttpContext.RequestAborted);
var response = await client.PostAsync($"http://{AppInit.conf.localhost}:{ModInit.tsport}/settings", new StringContent("{\"action\":\"get\"}", Encoding.UTF8, "application/json"));
await response.Content.CopyToAsync(HttpContext.Response.Body, HttpContext.RequestAborted);
}
else if (!ModInit.conf.rdb || HttpContext.Connection.RemoteIpAddress.ToString() == "127.0.0.1" || HttpContext.Connection.RemoteIpAddress.ToString().StartsWith("192.168."))
{
await client.PostAsync($"http://{AppInit.conf.localhost}:{ModInit.tsport}/settings", new StringContent(requestJson, Encoding.UTF8, "application/json"), HttpContext.RequestAborted);
await client.PostAsync($"http://{AppInit.conf.localhost}:{ModInit.tsport}/settings", new StringContent(requestJson, Encoding.UTF8, "application/json"));
}
}

await HttpContext.Response.WriteAsync(string.Empty, HttpContext.RequestAborted);
await HttpContext.Response.WriteAsync(string.Empty, HttpContext.RequestAborted).ConfigureAwait(false);
return;
}
#endregion
Expand All @@ -165,7 +165,7 @@ async public Task TorAPI()
string pathRequest = Regex.Replace(HttpContext.Request.Path.Value, "^/ts", "");
string servUri = $"http://{AppInit.conf.localhost}:{ModInit.tsport}{pathRequest + HttpContext.Request.QueryString.Value}";

using (var client = new HttpClient())
using (var client = Engine.CORE.HttpClient.httpClientFactory.CreateClient("base"))
{
var request = CreateProxyHttpRequest(HttpContext, new Uri(servUri));

Expand Down Expand Up @@ -252,7 +252,7 @@ void UpdateHeaders(HttpHeaders headers)
if (!response.Body.CanWrite)
throw new NotSupportedException("NotSupported_UnwritableStream");

byte[] buffer = ArrayPool<byte>.Shared.Rent(4096);
byte[] buffer = ArrayPool<byte>.Shared.Rent(response.ContentLength > 0 ? (5000000 > response.ContentLength ? (int)response.ContentLength : (int)Math.Min((long)response.ContentLength, 512000)) : 4096);

try
{
Expand Down
37 changes: 34 additions & 3 deletions TorrServer/ModInit.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Lampac.Engine.CORE;
using Lampac;
using Lampac.Engine.CORE;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared.Engine;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace TorrServer
{
Expand Down Expand Up @@ -90,8 +93,35 @@ public static void loaded()
tspath = $"{homedir}/TorrServer-windows-amd64.exe";
#endregion

#region update accs.db
File.WriteAllText($"{homedir}/accs.db", $"{{\"ts\":\"{tspass}\"}}");

//ThreadPool.QueueUserWorkItem(async _ =>
//{
// while (true)
// {
// await Task.Delay(TimeSpan.FromMinutes(1)).ConfigureAwait(false);

// try
// {
// if (AppInit.conf.accsdb.enable)
// {
// Dictionary<string, string> accs = new Dictionary<string, string>()
// {
// ["ts"] = tspass
// };

// foreach (var user in AppInit.conf.accsdb.accounts)
// accs.TryAdd(user.Key, conf.defaultPasswd);

// File.WriteAllText($"{homedir}/accs.db", JsonConvert.SerializeObject(accs));
// }
// }
// catch { }
// }
//});
#endregion

ThreadPool.QueueUserWorkItem(async _ =>
{
#region updatet/install
Expand Down Expand Up @@ -136,7 +166,7 @@ public static void loaded()
}
}
catch { }
#endregion
#endregion
reset: try
{
Expand All @@ -154,10 +184,11 @@ public static void loaded()
tsprocess.BeginOutputReadLine();
tsprocess.BeginErrorReadLine();
await tsprocess.WaitForExitAsync();
await tsprocess.WaitForExitAsync().ConfigureAwait(false);
}
catch { }
await Task.Delay(5_000).ConfigureAwait(false);
goto reset;
});
}
Expand Down

0 comments on commit 0559609

Please sign in to comment.