Skip to content

Commit 041325f

Browse files
committed
feat: 开放到局域网可设置登录
1 parent ad953ac commit 041325f

File tree

7 files changed

+272
-90
lines changed

7 files changed

+272
-90
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace MaiChartManager;
2+
3+
public class AuthenticationMiddleware
4+
{
5+
private readonly RequestDelegate _next;
6+
7+
public AuthenticationMiddleware(RequestDelegate next)
8+
{
9+
_next = next;
10+
}
11+
12+
public async Task Invoke(HttpContext context)
13+
{
14+
if (!context.User.Identity.IsAuthenticated)
15+
{
16+
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
17+
context.Response.Headers.Append("WWW-Authenticate", "Basic");
18+
await context.Response.WriteAsync("Unauthorized");
19+
return;
20+
}
21+
22+
await _next(context);
23+
}
24+
}

MaiChartManager/Config.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ public class Config
55
public bool Export { get; set; } = false;
66
public string GamePath { get; set; } = "";
77
public string OfflineKey { get; set; } = "";
8-
}
8+
public bool UseAuth { get; set; } = false;
9+
public string AuthUsername { get; set; } = "";
10+
public string AuthPassword { get; set; } = "";
11+
}

MaiChartManager/Launcher.Designer.cs

Lines changed: 124 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MaiChartManager/Launcher.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public Launcher()
2121
# endif
2222
checkBox1.Checked = StaticSettings.Config.Export;
2323
textBox1.Text = StaticSettings.Config.GamePath;
24+
checkBoxLanAuth.Checked = StaticSettings.Config.UseAuth;
25+
textBoxLanAuthUser.Text = StaticSettings.Config.AuthUsername;
26+
textBoxLanAuthPass.Text = StaticSettings.Config.AuthPassword;
2427
CheckStartupStatus();
2528
# if DEBUG
2629
checkBox1.Checked = true;
@@ -110,6 +113,9 @@ private void StartClicked(object sender, EventArgs e)
110113
textBox1.Enabled = true;
111114
button1.Enabled = true;
112115
checkBox1.Enabled = true;
116+
checkBoxLanAuth.Enabled = true;
117+
textBoxLanAuthUser.Enabled = true;
118+
textBoxLanAuthPass.Enabled = true;
113119
label1.Text = "";
114120
ServerManager.StopAsync();
115121
return;
@@ -142,12 +148,18 @@ private void StartClicked(object sender, EventArgs e)
142148

143149
# if !DEBUG
144150
StaticSettings.Config.GamePath = textBox1.Text;
151+
StaticSettings.Config.UseAuth = checkBoxLanAuth.Checked;
152+
StaticSettings.Config.AuthUsername = textBoxLanAuthUser.Text;
153+
StaticSettings.Config.AuthPassword = textBoxLanAuthPass.Text;
145154
File.WriteAllText(Path.Combine(StaticSettings.appData, "config.json"), JsonSerializer.Serialize(StaticSettings.Config));
146155
# endif
147156

148157
textBox1.Enabled = false;
149158
button1.Enabled = false;
150159
checkBox1.Enabled = false;
160+
checkBoxLanAuth.Enabled = false;
161+
textBoxLanAuthUser.Enabled = false;
162+
textBoxLanAuthPass.Enabled = false;
151163
button2.Text = "停止";
152164

153165
ServerManager.StartApp(checkBox1.Checked, () =>
@@ -197,6 +209,11 @@ private void checkBox1_CheckedChanged(object sender, EventArgs e)
197209
{
198210
StaticSettings.Config.Export = checkBox1.Checked;
199211
checkBox_startup.Visible = checkBox1.Checked;
212+
checkBoxLanAuth.Visible = checkBox1.Checked;
213+
if (!checkBox1.Checked)
214+
{
215+
checkBoxLanAuth.Checked = false;
216+
}
200217
}
201218

202219
private async void checkBox_startup_Click(object sender, EventArgs e)
@@ -246,4 +263,10 @@ private async void label3_Click(object sender, EventArgs e)
246263
await SaveConfigFileAsync();
247264
await IapManager.Init();
248265
}
266+
267+
private void checkBoxLanAuth_CheckedChanged(object sender, EventArgs e)
268+
{
269+
textBoxLanAuthUser.Visible = checkBoxLanAuth.Checked;
270+
textBoxLanAuthPass.Visible = checkBoxLanAuth.Checked;
271+
}
249272
}

MaiChartManager/Launcher.resx

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
362
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
463
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
564
<xsd:element name="root" msdata:IsDataSet="true">
@@ -58,17 +117,17 @@
58117
<resheader name="writer">
59118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
60119
</resheader>
61-
<metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
120+
<metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
62121
<value>17, 17</value>
63122
</metadata>
64-
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
123+
<metadata name="toolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
65124
<value>223, 17</value>
66125
</metadata>
67-
<metadata name="notifyIcon1.TrayLocation" type="System.Drawing.Point, System.Drawing.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
126+
<metadata name="notifyIcon1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
68127
<value>339, 17</value>
69128
</metadata>
70-
<assembly alias="System.Drawing.Common" name="System.Drawing.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
71-
<data name="notifyIcon1.Icon" type="System.Drawing.Icon, System.Drawing.Common" mimetype="application/x-microsoft.net.object.bytearray.base64">
129+
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
130+
<data name="notifyIcon1.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
72131
<value>
73132
AAABAAUAAAAAAAEAIAA4HgAAVgAAAICAAAABACAAKAgBAI4eAABAQAAAAQAgAChCAAC2JgEAICAAAAEA
74133
IACoEAAA3mgBABAQAAABACAAaAQAAIZ5AQCJUE5HDQoaCgAAAA1JSERSAAABAAAAAQAIBgAAAFxyqGYA
@@ -1702,7 +1761,7 @@
17021761
AAAAwAAAAAgAALJNAAD//wAA//8AAP//AAD//wAA//8AAA==
17031762
</value>
17041763
</data>
1705-
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing.Common" mimetype="application/x-microsoft.net.object.bytearray.base64">
1764+
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
17061765
<value>
17071766
AAABAAUAAAAAAAEAIAA4HgAAVgAAAICAAAABACAAKAgBAI4eAABAQAAAAQAgAChCAAC2JgEAICAAAAEA
17081767
IACoEAAA3mgBABAQAAABACAAaAQAAIZ5AQCJUE5HDQoaCgAAAA1JSERSAAABAAAAAQAIBgAAAFxyqGYA

MaiChartManager/MaiChartManager.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
<ItemGroup>
5454
<PackageReference Include="DesktopBridge.Helpers" Version="1.2.2"/>
55+
<PackageReference Include="idunno.Authentication.Basic" Version="2.4.0"/>
5556
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
5657
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
5758
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />

MaiChartManager/ServerManager.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Net;
22
using System.Net.NetworkInformation;
33
using System.Net.Sockets;
4+
using System.Security.Claims;
45
using System.Security.Cryptography.X509Certificates;
56
using System.Text.Json.Serialization;
67
using System.Windows.Forms;
8+
using idunno.Authentication.Basic;
79
using Microsoft.AspNetCore.Http.Features;
810
using Microsoft.AspNetCore.Server.Kestrel.Https;
911
using Microsoft.Extensions.FileProviders;
@@ -133,6 +135,28 @@ public static void StartApp(bool export, Action? onStart = null)
133135
.AddJsonOptions(options =>
134136
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
135137

138+
if (StaticSettings.Config.UseAuth)
139+
{
140+
builder.Services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
141+
.AddBasic(options =>
142+
{
143+
options.Events = new BasicAuthenticationEvents
144+
{
145+
OnValidateCredentials = context =>
146+
{
147+
if (context.Username == StaticSettings.Config.AuthUsername && context.Password == StaticSettings.Config.AuthPassword)
148+
{
149+
context.Principal = new ClaimsPrincipal(new ClaimsIdentity([], context.Scheme.Name));
150+
context.Success();
151+
}
152+
153+
return Task.CompletedTask;
154+
}
155+
};
156+
});
157+
builder.Services.AddAuthorization();
158+
}
159+
136160
# if !DEBUG
137161
builder.WebHost.ConfigureKestrel((context, serverOptions) =>
138162
{
@@ -151,6 +175,13 @@ public static void StartApp(bool export, Action? onStart = null)
151175
# endif
152176

153177
app = builder.Build();
178+
if (StaticSettings.Config.UseAuth)
179+
{
180+
app.UseAuthentication();
181+
app.UseAuthorization();
182+
app.UseMiddleware<AuthenticationMiddleware>();
183+
}
184+
154185
app.Lifetime.ApplicationStarted.Register(() => { app.Services.GetService<StaticSettings>(); });
155186

156187
if (onStart != null)

0 commit comments

Comments
 (0)