Make portal and proxy settings configurable

This commit is contained in:
2026-08-27 09:29:22 +08:00
parent 6b5e9a991c
commit 70da7ffda6
6 changed files with 277 additions and 140 deletions
+69 -12
View File
@@ -16,6 +16,8 @@ public sealed class MainForm : Form
private NumericUpDown _numPort = null!;
private TextBox _txtNginxExe = null!;
private TextBox _txtConfigPath = null!;
private TextBox _txtPortalDomain = null!;
private NumericUpDown _numPortalPort = null!;
private Label _lblStatus = null!;
private Button _btnAdd = null!;
@@ -27,6 +29,7 @@ public sealed class MainForm : Form
private Button _btnReload = null!;
private Button _btnStop = null!;
private Button _btnRefreshStatus = null!;
private Button _btnSavePortal = null!;
private string? _selectedDomain;
@@ -66,7 +69,7 @@ public sealed class MainForm : Form
BackColor = Color.FromArgb(245, 247, 250),
AutoScroll = true
};
root.RowStyles.Add(new RowStyle(SizeType.Absolute, 96));
root.RowStyles.Add(new RowStyle(SizeType.Absolute, 132));
root.RowStyles.Add(new RowStyle(SizeType.Absolute, 88));
root.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
root.RowStyles.Add(new RowStyle(SizeType.Absolute, 154));
@@ -83,21 +86,31 @@ public sealed class MainForm : Form
var panel = new TableLayoutPanel
{
Dock = DockStyle.Fill,
ColumnCount = 4,
RowCount = 3,
ColumnCount = 5,
RowCount = 4,
BackColor = Color.White,
Padding = new Padding(10, 8, 10, 8)
};
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 76));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 84));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 88));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 104));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 104));
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 28));
_txtConfigPath = new TextBox { Dock = DockStyle.Fill, ReadOnly = true, BorderStyle = BorderStyle.FixedSingle };
_txtNginxExe = new TextBox { Dock = DockStyle.Fill, ReadOnly = true, BorderStyle = BorderStyle.FixedSingle };
_txtPortalDomain = new TextBox { Dock = DockStyle.Fill, PlaceholderText = "例如:portal.example.com" };
_numPortalPort = new NumericUpDown
{
Dock = DockStyle.Fill,
Minimum = 1,
Maximum = 65535,
Value = 80
};
_lblStatus = new Label
{
Dock = DockStyle.Fill,
@@ -114,18 +127,29 @@ public sealed class MainForm : Form
btnBrowseExe.Click += (s, e) => BrowseNginxExe();
_btnRefreshStatus = CreateButton("刷新状态", 96);
_btnRefreshStatus.Click += (s, e) => RefreshStatus();
_btnSavePortal = CreateButton("保存入口设置", 96);
_btnSavePortal.Click += (s, e) => SavePortalSettings();
panel.Controls.Add(CreateLabel("配置文件"), 0, 0);
panel.Controls.Add(_txtConfigPath, 1, 0);
panel.Controls.Add(btnOpenConfig, 2, 0);
panel.Controls.Add(btnOpenFolder, 3, 0);
panel.SetColumnSpan(_txtConfigPath, 2);
panel.Controls.Add(btnOpenConfig, 3, 0);
panel.Controls.Add(btnOpenFolder, 4, 0);
panel.Controls.Add(CreateLabel("nginx.exe"), 0, 1);
panel.Controls.Add(_txtNginxExe, 1, 1);
panel.Controls.Add(btnBrowseExe, 2, 1);
panel.SetColumnSpan(_txtNginxExe, 2);
panel.Controls.Add(btnBrowseExe, 3, 1);
panel.SetColumnSpan(btnBrowseExe, 2);
panel.Controls.Add(CreateLabel("运行状态"), 0, 2);
panel.Controls.Add(_lblStatus, 1, 2);
panel.Controls.Add(_btnRefreshStatus, 2, 2);
panel.Controls.Add(CreateLabel("入口页域名"), 0, 2);
panel.Controls.Add(_txtPortalDomain, 1, 2);
panel.Controls.Add(CreateLabel("入口页端口"), 2, 2);
panel.Controls.Add(_numPortalPort, 3, 2);
panel.Controls.Add(_btnSavePortal, 4, 2);
panel.Controls.Add(CreateLabel("运行状态"), 0, 3);
panel.Controls.Add(_lblStatus, 1, 3);
panel.SetColumnSpan(_lblStatus, 2);
panel.Controls.Add(_btnRefreshStatus, 3, 3);
panel.SetColumnSpan(_btnRefreshStatus, 2);
return panel;
}
@@ -232,7 +256,7 @@ public sealed class MainForm : Form
_txtDescription = new TextBox
{
Dock = DockStyle.Fill,
PlaceholderText = "显示在 www.m3frame.com 入口卡片上的简介"
PlaceholderText = "显示在统一入口页卡片上的简介"
};
_numPort = new NumericUpDown
{
@@ -353,6 +377,10 @@ public sealed class MainForm : Form
{
_txtConfigPath.Text = _manager.ConfigPath;
_txtNginxExe.Text = _manager.NginxExePath;
_txtPortalDomain.Text = _manager.PortalDomain;
_numPortalPort.Value = _manager.PortalListenPort is >= 1 and <= 65535
? _manager.PortalListenPort
: 80;
_txtNginxExe.BackColor = HasConfiguredNginxExe()
? Color.White
: Color.FromArgb(255, 244, 214);
@@ -377,6 +405,35 @@ public sealed class MainForm : Form
_grid.DataSource = _binding;
}
private void SavePortalSettings()
{
try
{
var domain = _txtPortalDomain.Text.Trim();
if (!string.IsNullOrWhiteSpace(domain) &&
(domain.Contains("://", StringComparison.Ordinal) || domain.Contains('/') ||
domain.Any(char.IsWhiteSpace) || Uri.CheckHostName(domain) == UriHostNameType.Unknown))
{
throw new ArgumentException("请输入有效的入口页域名,例如 portal.example.com;留空可关闭入口站点。");
}
_manager.UpdatePortalSettings(domain, (int)_numPortalPort.Value);
_manager.RenderConfig();
RefreshBasicInfo();
MessageBox.Show(
string.IsNullOrWhiteSpace(domain)
? "入口站点已关闭,Nginx 配置已更新。"
: "入口页域名和监听端口已保存,Nginx 配置已更新。",
"NginxMan",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "NginxMan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void RefreshStatus()
{
if (!HasConfiguredNginxExe())
@@ -601,7 +658,7 @@ public sealed class MainForm : Form
if (domain.Contains("://", StringComparison.Ordinal) || domain.Contains('/') ||
domain.Any(char.IsWhiteSpace) || Uri.CheckHostName(domain) == UriHostNameType.Unknown)
{
throw new ArgumentException("请输入有效域名,例如 team.m3frame.com。");
throw new ArgumentException("请输入有效域名,例如 app.example.com。");
}
if (!Uri.TryCreate(upstream, UriKind.Absolute, out var upstreamUri) ||