Skip to content

Commit a76e244

Browse files
committed
fix
1 parent f43a747 commit a76e244

5 files changed

Lines changed: 52 additions & 27 deletions

File tree

Core/Middlewares/ProxyMedia/ProxyAPI.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ async public Task InvokeAsync(HttpContext httpContext)
113113
{
114114
using (var ctsHttp = CancellationTokenSource.CreateLinkedTokenSource(httpContext.RequestAborted))
115115
{
116-
if (ctsHttp.IsCancellationRequested)
117-
return;
118-
119116
ctsHttp.CancelAfter(TimeSpan.FromSeconds(30));
120117

121118
httpContext.Response.Headers["PX-Cache"] = "HIT";
@@ -267,11 +264,11 @@ async public Task InvokeAsync(HttpContext httpContext)
267264
await InvokeProxyApiCreateHttpRequestHandlers(em).ConfigureAwait(false);
268265
}
269266

267+
if (httpContext.RequestAborted.IsCancellationRequested)
268+
return;
269+
270270
using (var ctsHttp = CancellationTokenSource.CreateLinkedTokenSource(httpContext.RequestAborted))
271271
{
272-
if (ctsHttp.IsCancellationRequested)
273-
return;
274-
275272
ctsHttp.CancelAfter(TimeSpan.FromSeconds(30));
276273

277274
if (init.showOrigUri)
@@ -299,11 +296,16 @@ async public Task InvokeAsync(HttpContext httpContext)
299296
if (response.Content?.Headers != null && response.Content.Headers.TryGetValues("Content-Type", out var _contentType))
300297
contentType = _contentType?.FirstOrDefault();
301298

299+
ReadOnlySpan<char> ext = servPath.AsSpan();
300+
int extIndex = ext.LastIndexOf('.');
301+
if (extIndex > 0)
302+
ext = ext.Slice(extIndex);
303+
302304
bool ists =
303-
servPath.EndsWith(".ts", StringComparison.OrdinalIgnoreCase) ||
304-
servPath.EndsWith(".m4s", StringComparison.OrdinalIgnoreCase);
305+
ext.StartsWith(".ts", StringComparison.OrdinalIgnoreCase) ||
306+
ext.StartsWith(".m4s", StringComparison.OrdinalIgnoreCase);
305307

306-
bool ism3u = servPath.Contains(".m3u", StringComparison.OrdinalIgnoreCase);
308+
bool ism3u = ext.StartsWith(".m3u", StringComparison.OrdinalIgnoreCase);
307309

308310
if (!ism3u)
309311
{
@@ -320,7 +322,7 @@ async public Task InvokeAsync(HttpContext httpContext)
320322
{
321323
await ProxyM3u8(httpContext, init, decryptLink, response, contentType, ctsHttp);
322324
}
323-
else if (servPath.Contains(".mpd", StringComparison.OrdinalIgnoreCase) || contentType?.StartsWith("application/dash+xml") == true)
325+
else if (ext.StartsWith(".mpd", StringComparison.OrdinalIgnoreCase) || contentType?.StartsWith("application/dash+xml") == true)
324326
{
325327
await ProxyMpd(httpContext, init, decryptLink, response, contentType, ctsHttp);
326328
}

Core/Middlewares/ProxyMedia/ProxyM3u8.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ void writePipe(ReadOnlySpan<char> chars)
100100
if (completed)
101101
break;
102102

103+
if (charsUsed == 0 && bytesUsed == 0)
104+
break;
105+
103106
chars = chars.Slice(charsUsed);
104107
}
105108
}

Modules/Proxy/TmdbProxy/Controller.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.Buffers;
1111
using System.Collections.Generic;
12+
using System.Linq;
1213
using System.Net;
1314
using System.Net.Http;
1415
using System.Text.Json;
@@ -115,6 +116,8 @@ async public Task TmdbAPI()
115116
return;
116117
}
117118

119+
proxyManager?.Success();
120+
118121
int statusCode = (int)result.response.StatusCode;
119122
HttpContext.Response.StatusCode = statusCode;
120123

@@ -134,10 +137,6 @@ async public Task TmdbIMG()
134137
ReadOnlySpan<char> path = RequestPath(HttpContext.Request.Path.Value, "/tmdb/img/");
135138
string uri = RequestUri(tmdbImgHost, path, HttpContext.Request.Query);
136139

137-
HttpContext.Response.ContentType = path.Contains(".png", StringComparison.OrdinalIgnoreCase)
138-
? "image/png"
139-
: path.Contains(".svg", StringComparison.OrdinalIgnoreCase) ? "image/svg+xml" : "image/jpeg";
140-
141140
var proxyManager = ModInit.conf.proxyimg?.useproxy == true
142141
? new ProxyManager("tmdb_img", ModInit.conf.proxyimg)
143142
: null;
@@ -179,6 +178,9 @@ async public Task TmdbIMG()
179178
if (response.Content.Headers.ContentLength.HasValue)
180179
HttpContext.Response.ContentLength = response.Content.Headers.ContentLength.Value;
181180

181+
if (response.Content.Headers.TryGetValues("Content-Type", out var _contentType))
182+
HttpContext.Response.ContentType = _contentType?.FirstOrDefault();
183+
182184
await using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
183185
{
184186
using (var nbuf = new BufferPool())
@@ -252,4 +254,4 @@ static string RequestUri(ReadOnlySpan<char> host, ReadOnlySpan<char> path, IQuer
252254
return uri.ToString();
253255
}
254256
#endregion
255-
}
257+
}

Shared/Services/CSharpEval.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Shared.Services;
1818

1919
public static class CSharpEval
2020
{
21-
static ConcurrentDictionary<string, dynamic> scripts = new();
21+
static ConcurrentDictionary<Fnv1aHash, dynamic> scripts = new();
2222

2323
#region Execute<T>
2424
public static T Execute<T>(string cs, object model, ScriptOptions options = null)
@@ -28,7 +28,7 @@ public static T Execute<T>(string cs, object model, ScriptOptions options = null
2828

2929
public static Task<T> ExecuteAsync<T>(string cs, object model, ScriptOptions options = null)
3030
{
31-
var entry = scripts.GetOrAdd(CrypTo.md5(cs), _ =>
31+
var entry = scripts.GetOrAdd(Fnv1a.Hash(cs), _ =>
3232
{
3333
if (options == null)
3434
options = ScriptOptions.Default;
@@ -60,7 +60,7 @@ public static T BaseExecute<T>(string cs, object model, ScriptOptions options =
6060

6161
public static Task<T> BaseExecuteAsync<T>(string cs, object model, ScriptOptions options = null, InteractiveAssemblyLoader loader = null)
6262
{
63-
var entry = scripts.GetOrAdd(CrypTo.md5(cs), _ =>
63+
var entry = scripts.GetOrAdd(Fnv1a.Hash(cs), _ =>
6464
{
6565
return CSharpScript.Create<T>(
6666
cs,
@@ -82,7 +82,7 @@ public static void Execute(string cs, object model, ScriptOptions options = null
8282

8383
public static Task ExecuteAsync(string cs, object model, ScriptOptions options = null)
8484
{
85-
var entry = scripts.GetOrAdd(CrypTo.md5(cs), _ =>
85+
var entry = scripts.GetOrAdd(Fnv1a.Hash(cs), _ =>
8686
{
8787
if (options == null)
8888
options = ScriptOptions.Default;
@@ -113,15 +113,16 @@ public static Task ExecuteAsync(string cs, object model, ScriptOptions options =
113113
static readonly object lockCompilationObj = new();
114114

115115
[MethodImpl(MethodImplOptions.NoInlining)]
116-
public static (Assembly assembly, AssemblyLoadContext alc, string path) Compilation(RootModule mod)
116+
public static (Fnv1aHash sumhash, Assembly assembly, AssemblyLoadContext alc, string path) Compilation(RootModule mod)
117117
{
118118
lock (lockCompilationObj)
119119
{
120120
string path = mod.path;
121121

122122
if (Directory.Exists(path))
123123
{
124-
var sumhash = new StringBuilder(vshared);
124+
var sumhash = Fnv1a.Empty;
125+
Fnv1a.Append(ref sumhash, vshared);
125126

126127
#region syntaxTree
127128
var syntaxTree = new List<SyntaxTree>();
@@ -178,7 +179,7 @@ public static (Assembly assembly, AssemblyLoadContext alc, string path) Compilat
178179
syntaxTree.Add(CSharpSyntaxTree.ParseText(sourceText, parseOptions, csfile));
179180

180181
var checksum = sourceText.GetChecksum();
181-
sumhash.Append(Convert.ToHexString(checksum.ToArray()));
182+
Fnv1a.Append(ref sumhash, checksum);
182183
}
183184
}
184185
#endregion
@@ -230,15 +231,15 @@ public static (Assembly assembly, AssemblyLoadContext alc, string path) Compilat
230231
#endregion
231232

232233
#region cache dll
233-
string cachePath = Path.Combine("cache", "module", $"{CrypTo.md5(sumhash)}.dll");
234+
string cachePath = Path.Combine("cache", "module", $"{Fnv1a.Base64Url(sumhash)}.dll");
234235

235236
if (File.Exists(cachePath))
236237
{
237238
using (var fs = File.OpenRead(cachePath))
238239
{
239240
var alc = new AssemblyLoadContext(mod.name, isCollectible: true);
240241
var assembly = alc.LoadFromStream(fs);
241-
return (assembly, alc, path);
242+
return (sumhash, assembly, alc, path);
242243
}
243244
}
244245
#endregion
@@ -261,7 +262,7 @@ public static (Assembly assembly, AssemblyLoadContext alc, string path) Compilat
261262
var alc = new AssemblyLoadContext(mod.name, isCollectible: true);
262263
var assembly = alc.LoadFromStream(ms);
263264

264-
return (assembly, alc, path);
265+
return (sumhash, assembly, alc, path);
265266
}
266267
else
267268
{
@@ -274,7 +275,6 @@ public static (Assembly assembly, AssemblyLoadContext alc, string path) Compilat
274275
Console.WriteLine("\n");
275276
}
276277
}
277-
278278
}
279279

280280
return default;

Shared/Services/Utilities/Fnv1a.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ public static void Append(ref Fnv1aHash hash, ReadOnlySpan<byte> value)
119119
hash.H1 = h1;
120120
hash.H2 = h2;
121121
}
122+
123+
public static void Append(ref Fnv1aHash hash, IReadOnlyList<byte> value)
124+
{
125+
ulong h1 = hash.H1;
126+
ulong h2 = hash.H2;
127+
128+
foreach (byte b in value)
129+
{
130+
h1 ^= b;
131+
h1 *= _prime;
132+
133+
h2 ^= b;
134+
h2 *= _prime;
135+
}
136+
137+
hash.H1 = h1;
138+
hash.H2 = h2;
139+
}
122140
#endregion
123141

124142
#region Base64Url
@@ -135,4 +153,4 @@ public static string Base64Url(in Fnv1aHash hash)
135153
return System.Buffers.Text.Base64Url.EncodeToString(bytes);
136154
}
137155
#endregion
138-
}
156+
}

0 commit comments

Comments
 (0)