Skip to content

Commit

Permalink
feat: 向主控请求证书
Browse files Browse the repository at this point in the history
fix: Config.byoc 字段应为 bool 而不是 string
  • Loading branch information
SALTWOOD committed Mar 8, 2024
1 parent a328ef7 commit 5c6b267
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
16 changes: 12 additions & 4 deletions CSharp-OpenBMCLAPI/DefaultConfig.json5
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
// 指示 token 应当在距离其失效前的多少毫秒进行刷新
"refreshTokenTime": 1800000,
// 指示应该将要服务的文件放在哪里(服务路径)
"clusterFileDirectory": "./"
// 指示 token 应当在距离其失效前的多少毫秒进行刷新
"refreshTokenTime": 1800000,
// 指示应该将要服务的文件放在哪里(服务路径)
"clusterFileDirectory": "./",
// 用户访问时使用的 IP 或域名
"host": "",
// 对外服务端口
"port": 4000,
// 是否使用自定义域名
"byoc": false,
// 指示是否执行快速上线,若为 true 则每次都不执行
"noFastEnable": false
}
44 changes: 40 additions & 4 deletions CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using SocketIOClient;
using SocketIO.Core;
using TeraIO.Extension;
using System.Diagnostics;
using System.Text.Json;

namespace CSharpOpenBMCLAPI.Modules
{
Expand Down Expand Up @@ -66,6 +68,8 @@ protected async Task<int> AsyncRun()

Connect();

await RequestCertification();

await Enable();

_keepAlive = Task.Run(() =>
Expand All @@ -74,6 +78,7 @@ protected async Task<int> AsyncRun()
{
Thread.Sleep(25 * 1000);
KeepAlive().Wait();
this.token.FetchToken().Wait();
}
});

Expand Down Expand Up @@ -114,8 +119,8 @@ await socket.EmitAsync("enable",
},
new
{
host = SharedData.Config.host,
port = SharedData.Config.port,
host = SharedData.Config.HOST,
port = SharedData.Config.PORT,
version = SharedData.Config.clusterVersion,
byoc = SharedData.Config.byoc,
noFastEnable = SharedData.Config.noFastEnable
Expand All @@ -133,8 +138,8 @@ await socket.EmitAsync("disable",

public async Task KeepAlive()
{
string time = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
socket.Connected.Dump();
string time = DateTime.Now.ToStandardTimeString();
// socket.Connected.Dump();
await socket.EmitAsync("keep-alive",
(SocketIOResponse resp) =>
{
Expand All @@ -148,6 +153,7 @@ await socket.EmitAsync("keep-alive",

protected async Task CheckFiles()
{
SharedData.Logger.LogInfo("开始检查文件");
var resp = await client.GetAsync("openbmclapi/files");
byte[] buffer = await resp.Content.ReadAsByteArrayAsync();
var decomporess = new Decompressor();
Expand Down Expand Up @@ -227,5 +233,35 @@ private void CheckFileAfterDownload(string path, string hash, DownloadService se
service.DownloadFileTaskAsync($"{client.BaseAddress}openbmclapi/download/{hash}").Wait();
}
}

public async Task RequestCertification()
{
await socket.EmitAsync("request-cert", (SocketIOResponse resp) =>
{
var data = resp;
//Debugger.Break();
var json = data.GetValue<JsonElement>(0)[1];
JsonElement cert; json.TryGetProperty("cert", out cert);
JsonElement key; json.TryGetProperty("key", out key);
string? certString = cert.GetString();
string? keyString = key.GetString();
string certPath = $"{SharedData.Config.clusterFileDirectory}certifications/cert.pem";
string keyPath = $"{SharedData.Config.clusterFileDirectory}certifications/key.pem";
Directory.CreateDirectory($"{SharedData.Config.clusterFileDirectory}certifications");
using (var file = File.Create(certPath))
{
if (certString != null) file.Write(Encoding.UTF8.GetBytes(certString));
}
using (var file = File.Create(keyPath))
{
if (keyString != null) file.Write(Encoding.UTF8.GetBytes(keyString));
}
});
}
}
}
24 changes: 16 additions & 8 deletions CSharp-OpenBMCLAPI/Modules/Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand All @@ -16,21 +17,28 @@ public class Config
// 指示节点端的版本,不应由用户更改
[Browsable(false)]
public string clusterVersion;
// Host
public string host;
public int port;
public string byoc;
// 用户访问时使用的 IP 或域名
[JsonProperty("host")]
public string HOST { get; set; }
// 对外服务端口
[JsonProperty("port")]
public int PORT { get; set; }
// 是否使用自定义域名
public bool byoc;
// 指示是否执行快速上线,若为 true 则每次都不执行
public bool noFastEnable;

private string _host = "";

Check warning on line 31 in CSharp-OpenBMCLAPI/Modules/Config.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

The field 'Config._host' is assigned but its value is never used

Check warning on line 31 in CSharp-OpenBMCLAPI/Modules/Config.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

The field 'Config._host' is assigned but its value is never used

Check warning on line 31 in CSharp-OpenBMCLAPI/Modules/Config.cs

View workflow job for this annotation

GitHub Actions / build (Release)

The field 'Config._host' is assigned but its value is never used

Check warning on line 31 in CSharp-OpenBMCLAPI/Modules/Config.cs

View workflow job for this annotation

GitHub Actions / build (Release)

The field 'Config._host' is assigned but its value is never used

public Config()
{
this.refreshTokenTime = 1800000;
this.clusterFileDirectory = "./";
this.clusterVersion = "1.9.7";

this.host = "";
this.port = 4000;
this.byoc = "";
this.HOST = "";
this.PORT = 4000;
this.byoc = false;
this.noFastEnable = false;
}
}
Expand Down
2 changes: 2 additions & 0 deletions CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static T ThrowIfNull<T>(this T? item)
return item;
}

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

public static void PrintTypeInfo<T>(T instance)
{
Type type = typeof(T);
Expand Down

0 comments on commit 5c6b267

Please sign in to comment.