Skip to content

Commit

Permalink
feat: 检测并要求以管理员权限运行
Browse files Browse the repository at this point in the history
incoming: 提供服务
  • Loading branch information
SALTWOOD committed Mar 9, 2024
1 parent 3d462de commit e56f318
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 106 deletions.
4 changes: 2 additions & 2 deletions CSharp-OpenBMCLAPI/CSharp-OpenBMCLAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<ItemGroup>
<PackageReference Include="Apache.Avro" Version="1.11.3" />
<PackageReference Include="Downloader" Version="3.0.6" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="log4net" Version="2.0.16" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SaltWood.TeraIO" Version="1.0.2" />
<PackageReference Include="SaltWood.TeraIO" Version="1.0.4" />
<PackageReference Include="SocketIOClient" Version="3.1.1" />
<PackageReference Include="ZstdSharp.Port" Version="0.7.5" />
</ItemGroup>
Expand Down
8 changes: 1 addition & 7 deletions CSharp-OpenBMCLAPI/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI
namespace CSharpOpenBMCLAPI
{
public class Logger
{
Expand Down
8 changes: 1 addition & 7 deletions CSharp-OpenBMCLAPI/Modules/Challenge.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
namespace CSharpOpenBMCLAPI.Modules
{
public class Challenge
{
Expand Down
62 changes: 41 additions & 21 deletions CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
using Avro;
using Avro.IO;
using Avro.File;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TeraIO.Runnable;
using ZstdSharp;
using Avro.Generic;
using Avro.IO;
using Downloader;
using System.Security.Cryptography;
using SocketIOClient;
using SocketIO.Core;
using TeraIO.Extension;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.Json;
using System.Net;
using TeraIO.Network.Http;
using TeraIO.Runnable;
using ZstdSharp;

namespace CSharpOpenBMCLAPI.Modules
{
Expand All @@ -30,6 +24,7 @@ public class Cluster : RunnableBase
public bool IsEnabled { get; private set; }
private Task? _keepAlive;
//List<Task> tasks = new List<Task>();
private HttpServer server;

public Cluster(ClusterInfo info, TokenManager token) : base()
{
Expand All @@ -54,6 +49,7 @@ public Cluster(ClusterInfo info, TokenManager token) : base()
}
});

this.server = new();
}

private void HandleError(SocketIOResponse resp)
Expand All @@ -80,13 +76,7 @@ protected async Task<int> AsyncRun()

Connect();

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

await RequestCertification();

Expand All @@ -109,6 +99,34 @@ protected async Task<int> AsyncRun()
return returns;
}

private void InitializeHttpsServer(HttpServerAppBase server)
{
server.UriPrefixes = new()
{
$"http://*:4000/"
};
server.Started += (current, e) => SharedData.Logger.LogInfo($"HTTP 服务实例 \"<{server} {server.GetHashCode()}>\" 已启动");
server.Stopped += (current, e) => SharedData.Logger.LogInfo($"HTTP 服务实例 \"<{server} {server.GetHashCode()}>\" 已停止");
server.Start();
}

/*
private void RequestFirewall()
{
FtpWebRequest requestDownload;
requestDownload = (FtpWebRequest)WebRequest.Create(uri);
requestDownload.UsePassive = false;
requestDownload.KeepAlive = false;
requestDownload.UseBinary = true;
requestDownload.Method = WebRequestMethods.Ftp.DownloadFile;
requestDownload.Credentials = new NetworkCredential(ftpInfoDownload[3], ftpInfoDownload[4]);
responseDownload = (FtpWebResponse)requestDownload.GetResponse();
Stream ftpStream = responseDownload.GetResponseStream();
}*/

public void Connect()
{
this.socket.ConnectAsync().Wait();
Expand All @@ -128,6 +146,8 @@ public async Task Enable()
await socket.EmitAsync("enable",
(SocketIOResponse resp) =>
{
SharedData.Logger.LogInfo(resp);
// Debugger.Break();
SharedData.Logger.LogInfo($"启用成功");
},
new
Expand Down
9 changes: 1 addition & 8 deletions CSharp-OpenBMCLAPI/Modules/ClusterInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
namespace CSharpOpenBMCLAPI.Modules
{
public struct ClusterInfo
{
Expand Down
10 changes: 3 additions & 7 deletions CSharp-OpenBMCLAPI/Modules/Config.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
{
Expand All @@ -25,8 +20,9 @@ public string HOST
{
if (string.IsNullOrEmpty(this._host))
{
HttpClient client = new HttpClient();
this._host = client.GetAsync("https://4.ipw.cn/").Result.Content.ReadAsStringAsync().Result;
//HttpClient client = new HttpClient();
//this._host = client.GetAsync("https://4.ipw.cn/").Result.Content.ReadAsStringAsync().Result;
this._host = $"";
}
return this._host;
}
Expand Down
7 changes: 1 addition & 6 deletions CSharp-OpenBMCLAPI/Modules/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace CSharpOpenBMCLAPI.Modules
{
Expand Down
8 changes: 1 addition & 7 deletions CSharp-OpenBMCLAPI/Modules/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
namespace CSharpOpenBMCLAPI.Modules
{
internal class HttpRequest
{
Expand Down
17 changes: 9 additions & 8 deletions CSharp-OpenBMCLAPI/Modules/HttpServer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;
using TeraIO.Network.Http;

namespace CSharpOpenBMCLAPI.Modules
{
public class HttpServer : HttpServerAppBase
{
public HttpServer() : base() => LoadNew();
public HttpServer() : base()
{
LoadNew();

X509Certificate2 certificate = new($"{SharedData.Config.clusterFileDirectory}certifications/cert.pem",
$"{SharedData.Config.clusterFileDirectory}certifications/key.pem");

}

[HttpHandler("/measure")]
public void Measure(HttpClientRequest req)
Expand Down
8 changes: 1 addition & 7 deletions CSharp-OpenBMCLAPI/Modules/SharedData.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
namespace CSharpOpenBMCLAPI.Modules
{
public static class SharedData
{
Expand Down
8 changes: 1 addition & 7 deletions CSharp-OpenBMCLAPI/Modules/Token.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
namespace CSharpOpenBMCLAPI.Modules
{
public struct Token
{
Expand Down
4 changes: 0 additions & 4 deletions CSharp-OpenBMCLAPI/Modules/TokenManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
{
Expand Down
37 changes: 33 additions & 4 deletions CSharp-OpenBMCLAPI/Modules/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace CSharpOpenBMCLAPI.Modules
{
Expand Down Expand Up @@ -44,5 +43,35 @@ public static bool CheckSign(string? hash, string? secret, string? s, string? e)
return sign == s && timestamp < (ToDecimal(e) / 100);
}

public static bool IsAdministrator()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return true;
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
//WindowsBuiltInRole可以枚举出很多权限,例如系统用户、User、Guest等等
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

public static void RunAsAdministrator()
{
//创建启动对象
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = Process.GetCurrentProcess().MainModule?.FileName,
WorkingDirectory = Environment.CurrentDirectory,
Verb = "runas",
UseShellExecute = true,
CreateNoWindow = false
};
try
{
Process.Start(startInfo);
}
catch
{
return;
}
Environment.Exit(0);
}
}
}
18 changes: 7 additions & 11 deletions CSharp-OpenBMCLAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using CSharpOpenBMCLAPI.Modules;
using CSharpOpenBMCLAPI.Modules;
using Newtonsoft.Json;
using System.Reflection;
using TeraIO.Runnable;

namespace CSharpOpenBMCLAPI
Expand Down Expand Up @@ -82,6 +73,11 @@ protected async Task<int> AsyncRun()
{
int returns = 0;

if (!Utils.IsAdministrator())
{
Utils.RunAsAdministrator();
}

// 从 .env.json 读取密钥然后 FetchToken
ClusterInfo info = JsonConvert.DeserializeObject<ClusterInfo>(await File.ReadAllTextAsync(".env.json"));
SharedData.ClusterInfo = info;
Expand Down

0 comments on commit e56f318

Please sign in to comment.