Skip to content

Commit

Permalink
feat: 提供服务(未完成)
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Mar 8, 2024
1 parent 64d87b2 commit aeab2d1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
16 changes: 14 additions & 2 deletions CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using TeraIO.Extension;
using System.Diagnostics;
using System.Text.Json;
using System.Net;

namespace CSharpOpenBMCLAPI.Modules
{
Expand Down Expand Up @@ -68,10 +69,21 @@ protected async Task<int> AsyncRun()

Connect();

HttpServer hs = new();
hs.PORT = 4000;
hs.UriPrefixes = new()
{
"http://localhost:4000/"
};
hs.Load();
hs.Start();

await RequestCertification();

await Enable();

SharedData.Logger.LogInfo($"工作进程 {guid}{SharedData.Config.HOST}:{SharedData.Config.PORT} 提供服务");

_keepAlive = Task.Run(() =>
{
while (true)
Expand Down Expand Up @@ -170,7 +182,7 @@ protected async Task CheckFiles()

Avro.IO.Decoder decoder = new BinaryDecoder(new MemoryStream(buffer));

object[] f = new GenericDatumReader<object[]>(schema, schema).Read(null!, decoder);
object[] files = new GenericDatumReader<object[]>(schema, schema).Read(null!, decoder);

DownloadConfiguration option = new DownloadConfiguration()
{
Expand All @@ -186,7 +198,7 @@ protected async Task CheckFiles()
}
};

Parallel.ForEach(f, (obj) =>
Parallel.ForEach(files, (obj) =>
{
GenericRecord? record = obj as GenericRecord;
if (record != null)
Expand Down
14 changes: 13 additions & 1 deletion CSharp-OpenBMCLAPI/Modules/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,19 @@ public class Config
public string clusterVersion;
// 用户访问时使用的 IP 或域名
[JsonProperty("host")]
public string HOST { get; set; }
public string HOST
{
get
{
if (string.IsNullOrEmpty(this._host))
{
HttpClient client = new HttpClient();
this._host = client.GetAsync("https://4.ipw.cn/").Result.Content.ReadAsStringAsync().Result;
}
return this._host;
}
set => this._host = value;
}
// 对外服务端口
[JsonProperty("port")]
public int PORT { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static T ThrowIfNull<T>(this T? item)

public static string ToStandardTimeString(this DateTime dt) => dt.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");

public static void PrintTypeInfo<T>(T instance)
public static void PrintTypeInfo<T>(this T instance)
{
Type type = typeof(T);
Console.WriteLine($"Type: {type.Name}");
Expand Down
24 changes: 24 additions & 0 deletions CSharp-OpenBMCLAPI/Modules/HttpServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TeraIO.Network.Http;

namespace CSharpOpenBMCLAPI.Modules
{
public class HttpServer : HttpServerAppBase
{
public HttpServer() : base() { }

[HttpHandler("/measure")]
public void Measure(HttpClientRequest req)
{
var a = req.Request;
Debugger.Break();
}

public void Load() => this.LoadNew();
}
}
24 changes: 24 additions & 0 deletions CSharp-OpenBMCLAPI/Modules/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
{
public static class Utils
{
public static bool CheckSign(string hash, string secret, string s, string e)
{
if (string.IsNullOrEmpty(e) || string.IsNullOrEmpty(s))
{
return false;
}
var sha1 = SHA1.Create();
var sign = Convert.ToBase64String(sha1.ComputeHash(Encoding.UTF8.GetBytes($"{secret}{hash}{e}")));
return sign == s && DateTime.Now.Ticks < (Convert.ToInt32(e, 36) / 100);
}

}
}

0 comments on commit aeab2d1

Please sign in to comment.