From 41f1ac3a0972fb9cde7174ed48f07d7079b29b55 Mon Sep 17 00:00:00 2001 From: techberet <81947808+techberet@users.noreply.github.com> Date: Sun, 24 Nov 2024 02:12:56 -0800 Subject: [PATCH] Add Selectable Network Interface in Web Server Form (#1511) * Add Selectable Network Interface in Web Server Update the Web Server port form to allow for editing the network interface the web server is run on. Added storage of the interface to the settings so it is persisted. This should resolve issue #1089 . * Update HttpServer.cs --------- Co-authored-by: PhyxionNL <7643972+PhyxionNL@users.noreply.github.com> --- .../LibreHardwareMonitor.csproj | 8 +-- ...igner.cs => InterfacePortForm.Designer.cs} | 58 ++++++++++++++----- .../UI/{PortForm.cs => InterfacePortForm.cs} | 51 +++++++++++----- .../{PortForm.resx => InterfacePortForm.resx} | 0 LibreHardwareMonitor/UI/MainForm.Designer.cs | 14 ++--- LibreHardwareMonitor/UI/MainForm.cs | 6 +- LibreHardwareMonitor/Utilities/HttpServer.cs | 28 ++++++++- 7 files changed, 119 insertions(+), 46 deletions(-) rename LibreHardwareMonitor/UI/{PortForm.Designer.cs => InterfacePortForm.Designer.cs} (81%) rename LibreHardwareMonitor/UI/{PortForm.cs => InterfacePortForm.cs} (57%) rename LibreHardwareMonitor/UI/{PortForm.resx => InterfacePortForm.resx} (100%) diff --git a/LibreHardwareMonitor/LibreHardwareMonitor.csproj b/LibreHardwareMonitor/LibreHardwareMonitor.csproj index 8d6b444..158ea57 100644 --- a/LibreHardwareMonitor/LibreHardwareMonitor.csproj +++ b/LibreHardwareMonitor/LibreHardwareMonitor.csproj @@ -44,8 +44,8 @@ ParameterForm.cs - - PortForm.cs + + InterfacePortForm.cs @@ -61,8 +61,8 @@ ParameterForm.cs - - PortForm.cs + + InterfacePortForm.cs diff --git a/LibreHardwareMonitor/UI/PortForm.Designer.cs b/LibreHardwareMonitor/UI/InterfacePortForm.Designer.cs similarity index 81% rename from LibreHardwareMonitor/UI/PortForm.Designer.cs rename to LibreHardwareMonitor/UI/InterfacePortForm.Designer.cs index 5672d54..b791af2 100644 --- a/LibreHardwareMonitor/UI/PortForm.Designer.cs +++ b/LibreHardwareMonitor/UI/InterfacePortForm.Designer.cs @@ -6,7 +6,7 @@ namespace LibreHardwareMonitor.UI { - partial class PortForm + partial class InterfacePortForm { /// /// Required designer variable. @@ -34,7 +34,7 @@ namespace LibreHardwareMonitor.UI /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PortForm)); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InterfacePortForm)); this.portOKButton = new System.Windows.Forms.Button(); this.portCancelButton = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); @@ -44,12 +44,14 @@ namespace LibreHardwareMonitor.UI this.webServerLinkLabel = new System.Windows.Forms.LinkLabel(); this.portNumericUpDn = new System.Windows.Forms.NumericUpDown(); this.label5 = new System.Windows.Forms.Label(); + this.interfaceLabel = new System.Windows.Forms.Label(); + this.interfaceComboBox = new System.Windows.Forms.ComboBox(); ((System.ComponentModel.ISupportInitialize)(this.portNumericUpDn)).BeginInit(); this.SuspendLayout(); // // portOKButton // - this.portOKButton.Location = new System.Drawing.Point(244, 137); + this.portOKButton.Location = new System.Drawing.Point(244, 162); this.portOKButton.Name = "portOKButton"; this.portOKButton.Size = new System.Drawing.Size(75, 23); this.portOKButton.TabIndex = 0; @@ -60,7 +62,7 @@ namespace LibreHardwareMonitor.UI // portCancelButton // this.portCancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.portCancelButton.Location = new System.Drawing.Point(162, 137); + this.portCancelButton.Location = new System.Drawing.Point(162, 162); this.portCancelButton.Name = "portCancelButton"; this.portCancelButton.Size = new System.Drawing.Size(75, 23); this.portCancelButton.TabIndex = 1; @@ -71,7 +73,7 @@ namespace LibreHardwareMonitor.UI // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(13, 106); + this.label1.Location = new System.Drawing.Point(13, 131); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(377, 13); this.label1.TabIndex = 3; @@ -81,16 +83,16 @@ namespace LibreHardwareMonitor.UI // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(13, 9); + this.label2.Location = new System.Drawing.Point(13, 34); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(193, 13); + this.label2.Size = new System.Drawing.Size(190, 13); this.label2.TabIndex = 4; - this.label2.Text = "Port number for the remote web server:"; + this.label2.Text = "Port number for the remote web server:"; // // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(13, 39); + this.label3.Location = new System.Drawing.Point(13, 64); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(443, 13); this.label3.TabIndex = 5; @@ -100,7 +102,7 @@ namespace LibreHardwareMonitor.UI // label4 // this.label4.AutoSize = true; - this.label4.Location = new System.Drawing.Point(13, 62); + this.label4.Location = new System.Drawing.Point(13, 86); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(262, 13); this.label4.TabIndex = 6; @@ -109,7 +111,7 @@ namespace LibreHardwareMonitor.UI // webServerLinkLabel // this.webServerLinkLabel.AutoSize = true; - this.webServerLinkLabel.Location = new System.Drawing.Point(269, 62); + this.webServerLinkLabel.Location = new System.Drawing.Point(269, 86); this.webServerLinkLabel.Name = "webServerLinkLabel"; this.webServerLinkLabel.Size = new System.Drawing.Size(55, 13); this.webServerLinkLabel.TabIndex = 7; @@ -119,7 +121,7 @@ namespace LibreHardwareMonitor.UI // // portNumericUpDn // - this.portNumericUpDn.Location = new System.Drawing.Point(208, 7); + this.portNumericUpDn.Location = new System.Drawing.Point(208, 32); this.portNumericUpDn.Maximum = new decimal(new int[] { 20000, 0, @@ -144,18 +146,40 @@ namespace LibreHardwareMonitor.UI // label5 // this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(13, 84); + this.label5.Location = new System.Drawing.Point(13, 109); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(304, 13); this.label5.TabIndex = 9; this.label5.Text = "You will have to start the server by clicking Run from the menu."; // - // PortForm + // interfaceLabel + // + this.interfaceLabel.AutoSize = true; + this.interfaceLabel.Location = new System.Drawing.Point(14, 8); + this.interfaceLabel.Name = "interfaceLabel"; + this.interfaceLabel.Size = new System.Drawing.Size(217, 13); + this.interfaceLabel.TabIndex = 10; + this.interfaceLabel.Text = "Network interface for the remote web server:"; + // + // interfaceComboBox + // + this.interfaceComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.interfaceComboBox.FormattingEnabled = true; + this.interfaceComboBox.Location = new System.Drawing.Point(233, 6); + this.interfaceComboBox.Margin = new System.Windows.Forms.Padding(2); + this.interfaceComboBox.Name = "interfaceComboBox"; + this.interfaceComboBox.Size = new System.Drawing.Size(221, 21); + this.interfaceComboBox.TabIndex = 11; + this.interfaceComboBox.SelectedIndexChanged += new System.EventHandler(this.interfaceComboBox_SelectedIndexChanged); + // + // InterfacePortForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.portCancelButton; - this.ClientSize = new System.Drawing.Size(466, 170); + this.ClientSize = new System.Drawing.Size(466, 202); + this.Controls.Add(this.interfaceComboBox); + this.Controls.Add(this.interfaceLabel); this.Controls.Add(this.label5); this.Controls.Add(this.portNumericUpDn); this.Controls.Add(this.webServerLinkLabel); @@ -169,7 +193,7 @@ namespace LibreHardwareMonitor.UI this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; - this.Name = "PortForm"; + this.Name = "InterfacePortForm"; this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Set Port"; @@ -191,5 +215,7 @@ namespace LibreHardwareMonitor.UI private System.Windows.Forms.LinkLabel webServerLinkLabel; private System.Windows.Forms.NumericUpDown portNumericUpDn; private System.Windows.Forms.Label label5; + private System.Windows.Forms.Label interfaceLabel; + private System.Windows.Forms.ComboBox interfaceComboBox; } } diff --git a/LibreHardwareMonitor/UI/PortForm.cs b/LibreHardwareMonitor/UI/InterfacePortForm.cs similarity index 57% rename from LibreHardwareMonitor/UI/PortForm.cs rename to LibreHardwareMonitor/UI/InterfacePortForm.cs index e30d57b..c02d6c8 100644 --- a/LibreHardwareMonitor/UI/PortForm.cs +++ b/LibreHardwareMonitor/UI/InterfacePortForm.cs @@ -9,36 +9,50 @@ using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Diagnostics; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; +using System.Net.NetworkInformation; namespace LibreHardwareMonitor.UI; -public partial class PortForm : Form +public partial class InterfacePortForm : Form { private readonly MainForm _parent; - private readonly string _localIP; - public PortForm(MainForm m) + private string _localIP; + + public InterfacePortForm(MainForm m) { InitializeComponent(); _parent = m; - _localIP = GetLocalIP(); + _localIP = LoadNetworkInterfaces(_parent.Server.ListenerIp); } - private void PortTextBox_TextChanged(object sender, EventArgs e) - { - - } - - private string GetLocalIP() + private string LoadNetworkInterfaces(string selectedListenerIp) { IPHostEntry host; - string localIP = "?"; + interfaceComboBox.Items.Clear(); host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) - localIP = ip.ToString(); + interfaceComboBox.Items.Add(ip.ToString()); } - return localIP; + // select the last one by default to match the existing behavior + if (interfaceComboBox.Items.Count > 0) + { + interfaceComboBox.SelectedIndex = interfaceComboBox.Items.Count - 1; + } else + { + // default to ? just like previous version + interfaceComboBox.Items.Add("?"); + interfaceComboBox.SelectedIndex = 0; + } + // check to see if the selected listener IP is in our list. + if (interfaceComboBox.Items.Contains(selectedListenerIp)) + { + // default it to the previously selected IP. + interfaceComboBox.SelectedItem = selectedListenerIp; + } + return interfaceComboBox.SelectedItem as string; } private void PortNumericUpDn_ValueChanged(object sender, EventArgs e) @@ -52,6 +66,7 @@ public partial class PortForm : Form private void PortOKButton_Click(object sender, EventArgs e) { _parent.Server.ListenerPort = (int)portNumericUpDn.Value; + _parent.Server.ListenerIp = _localIP; Close(); } @@ -62,6 +77,7 @@ public partial class PortForm : Form private void PortForm_Load(object sender, EventArgs e) { + interfaceComboBox.SelectedValue = _parent.Server.ListenerIp; portNumericUpDn.Value = _parent.Server.ListenerPort; PortNumericUpDn_ValueChanged(null, null); } @@ -79,4 +95,11 @@ public partial class PortForm : Form { PortNumericUpDn_ValueChanged(null, null); } -} \ No newline at end of file + + private void interfaceComboBox_SelectedIndexChanged(object sender, EventArgs e) + { + + _localIP = interfaceComboBox.SelectedItem as string; + PortNumericUpDn_ValueChanged(null, null); + } +} diff --git a/LibreHardwareMonitor/UI/PortForm.resx b/LibreHardwareMonitor/UI/InterfacePortForm.resx similarity index 100% rename from LibreHardwareMonitor/UI/PortForm.resx rename to LibreHardwareMonitor/UI/InterfacePortForm.resx diff --git a/LibreHardwareMonitor/UI/MainForm.Designer.cs b/LibreHardwareMonitor/UI/MainForm.Designer.cs index 1291b25..e7785cd 100644 --- a/LibreHardwareMonitor/UI/MainForm.Designer.cs +++ b/LibreHardwareMonitor/UI/MainForm.Designer.cs @@ -138,7 +138,7 @@ namespace LibreHardwareMonitor.UI this.webMenuItemSeparator = new System.Windows.Forms.ToolStripSeparator(); this.webMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.runWebServerMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.serverPortMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.serverInterfacePortMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.authWebServerMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.helpMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.aboutMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -938,7 +938,7 @@ namespace LibreHardwareMonitor.UI // this.webMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.runWebServerMenuItem, - this.serverPortMenuItem, + this.serverInterfacePortMenuItem, this.authWebServerMenuItem}); this.webMenuItem.Name = "webMenuItem"; this.webMenuItem.Size = new System.Drawing.Size(221, 22); @@ -952,10 +952,10 @@ namespace LibreHardwareMonitor.UI // // serverPortMenuItem // - this.serverPortMenuItem.Name = "serverPortMenuItem"; - this.serverPortMenuItem.Size = new System.Drawing.Size(153, 22); - this.serverPortMenuItem.Text = "Port"; - this.serverPortMenuItem.Click += new System.EventHandler(this.ServerPortMenuItem_Click); + this.serverInterfacePortMenuItem.Name = "serverInterfacePortMenuItem"; + this.serverInterfacePortMenuItem.Size = new System.Drawing.Size(153, 22); + this.serverInterfacePortMenuItem.Text = "Interface / Port"; + this.serverInterfacePortMenuItem.Click += new System.EventHandler(this.ServerInterfacePortMenuItem_Click); // // authWebServerMenuItem // @@ -1141,7 +1141,7 @@ namespace LibreHardwareMonitor.UI private ToolStripRadioButtonMenuItem splitPanelFixedSensorScalingMenuItem; private System.Windows.Forms.ToolStripMenuItem webMenuItem; private System.Windows.Forms.ToolStripMenuItem runWebServerMenuItem; - private System.Windows.Forms.ToolStripMenuItem serverPortMenuItem; + private System.Windows.Forms.ToolStripMenuItem serverInterfacePortMenuItem; private System.Windows.Forms.ToolStripMenuItem menuItemFileHardware; private System.Windows.Forms.ToolStripMenuItem mainboardMenuItem; private System.Windows.Forms.ToolStripMenuItem cpuMenuItem; diff --git a/LibreHardwareMonitor/UI/MainForm.cs b/LibreHardwareMonitor/UI/MainForm.cs index f1b9a13..45bacab 100644 --- a/LibreHardwareMonitor/UI/MainForm.cs +++ b/LibreHardwareMonitor/UI/MainForm.cs @@ -244,6 +244,7 @@ public sealed partial class MainForm : Form fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked; Server = new HttpServer(_root, + _settings.GetValue("listenerIp", "?"), _settings.GetValue("listenerPort", 8085), _settings.GetValue("authenticationEnabled", false), _settings.GetValue("authenticationUserName", ""), @@ -878,6 +879,7 @@ public sealed partial class MainForm : Form foreach (TreeColumn column in treeView.Columns) _settings.SetValue("treeView.Columns." + column.Header + ".Width", column.Width); + _settings.SetValue("listenerIp", Server.ListenerIp); _settings.SetValue("listenerPort", Server.ListenerPort); _settings.SetValue("authenticationEnabled", Server.AuthEnabled); _settings.SetValue("authenticationUserName", Server.UserName); @@ -1296,9 +1298,9 @@ public sealed partial class MainForm : Form } } - private void ServerPortMenuItem_Click(object sender, EventArgs e) + private void ServerInterfacePortMenuItem_Click(object sender, EventArgs e) { - new PortForm(this).ShowDialog(); + new InterfacePortForm(this).ShowDialog(); } private void AuthWebServerMenuItem_Click(object sender, EventArgs e) diff --git a/LibreHardwareMonitor/Utilities/HttpServer.cs b/LibreHardwareMonitor/Utilities/HttpServer.cs index 494f6c6..a975ffa 100644 --- a/LibreHardwareMonitor/Utilities/HttpServer.cs +++ b/LibreHardwareMonitor/Utilities/HttpServer.cs @@ -1,4 +1,4 @@ -// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. +// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. // Copyright (C) LibreHardwareMonitor and Contributors. // Partial Copyright (C) Michael Möller and Contributors. @@ -31,9 +31,10 @@ public class HttpServer private readonly Node _root; private Thread _listenerThread; - public HttpServer(Node node, int port, bool authEnabled = false, string userName = "", string password = "") + public HttpServer(Node node, string ip, int port, bool authEnabled = false, string userName = "", string password = "") { _root = node; + ListenerIp = ip; ListenerPort = port; AuthEnabled = authEnabled; UserName = userName; @@ -60,6 +61,8 @@ public class HttpServer public bool AuthEnabled { get; set; } + public string ListenerIp { get; set; } + public int ListenerPort { get; set; } public string Password @@ -87,7 +90,26 @@ public class HttpServer if (_listener.IsListening) return true; - string prefix = "http://+:" + ListenerPort + "/"; + // validate that the selected IP exists (it could have been previously selected before switching networks) + IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); + bool ipFound = false; + foreach (IPAddress ip in host.AddressList) + { + if (ListenerIp == ip.ToString()) + { + ipFound = true; + break; + } + } + + if (!ipFound) + { + // default to behavior of previous version if we don't know what interface to use. + ListenerIp = "+"; + } + + string prefix = "http://" + ListenerIp + ":" + ListenerPort + "/"; + _listener.Prefixes.Clear(); _listener.Prefixes.Add(prefix); _listener.Realm = "Libre Hardware Monitor";