1
0
mirror of https://github.com/Ylianst/MeshCentralRouter synced 2025-12-06 00:13:33 +00:00

Fixed registry read exception.

This commit is contained in:
Ylian Saint-Hilaire
2020-04-05 12:43:31 -07:00
parent 9ecafc5af2
commit 503ed7bf08
5 changed files with 160 additions and 35 deletions

View File

@@ -23,11 +23,17 @@ namespace MeshCentralRouter
public void UpdateInfo()
{
if (parent.getShowGroupNames()) { deviceNameLabel.Text = mesh.name + ", " + node.name; } else { deviceNameLabel.Text = node.name; }
if (node.conn == 0) {
devicePictureBox.Image = disabledDeviceImageList.Images[node.icon - 1];
} else {
devicePictureBox.Image = deviceImageList.Images[node.icon - 1];
if (parent.getShowGroupNames() && (mesh != null)) { deviceNameLabel.Text = mesh.name + ", " + node.name; } else { deviceNameLabel.Text = node.name; }
if (node.icon > 1)
{
if (node.conn == 0)
{
devicePictureBox.Image = disabledDeviceImageList.Images[node.icon - 1];
}
else
{
devicePictureBox.Image = deviceImageList.Images[node.icon - 1];
}
}
string status = "";
@@ -51,10 +57,17 @@ namespace MeshCentralRouter
rdpButton.Visible = false;
}
// Compute rights on this device
ulong rights = node.rights;
if (mesh != null) { rights |= mesh.rights; }
// Must have remote control rights
if ((mesh.rights & 8) != 0) {
if ((rights & 8) != 0)
{
sshButton.Enabled = scpButton.Enabled = rdpButton.Enabled = httpsButton.Enabled = httpButton.Enabled = ((node.conn & 1) != 0);
} else {
}
else
{
sshButton.Enabled = scpButton.Enabled = rdpButton.Enabled = httpsButton.Enabled = httpButton.Enabled = false;
}
}

View File

@@ -40,8 +40,12 @@ namespace MeshCentralRouter
public bool forceExit = false;
public bool sendEmailToken = false;
public void setRegValue(string name, string value) { Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value); }
public string getRegValue(string name, string value) { return Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value).ToString(); }
public void setRegValue(string name, string value) {
try { Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value); } catch (Exception) { }
}
public string getRegValue(string name, string value) {
try { return Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value).ToString(); } catch (Exception) { return value; }
}
public class DeviceComparer : IComparer
{
@@ -259,6 +263,7 @@ namespace MeshCentralRouter
if (c.GetType() == typeof(DeviceUserControl)) { ((DeviceUserControl)c).present = false; }
}
/*
lock (meshcentral.nodes)
{
// Add any missing devices
@@ -299,13 +304,49 @@ namespace MeshCentralRouter
// Add all controls at once to make it fast.
if (controlsToAdd.Count > 0) { devicesPanel.Controls.AddRange((DeviceUserControl[])controlsToAdd.ToArray(typeof(DeviceUserControl))); }
}
*/
// Clear all untagged devices
foreach (Control c in devicesPanel.Controls) {
if ((c.GetType() == typeof(DeviceUserControl)) && ((DeviceUserControl)c).present == false) {
devicesPanel.Controls.Remove(c); c.Dispose();
ArrayList controlsToAdd = new ArrayList();
foreach (NodeClass node in meshcentral.nodes.Values)
{
if (node.agentid == -1) { continue; }
if (node.control == null)
{
// Add a new device
DeviceUserControl device = new DeviceUserControl();
if ((node.meshid != null) && meshcentral.meshes.ContainsKey(node.meshid)) { device.mesh = (MeshClass)meshcentral.meshes[node.meshid]; }
device.node = node;
device.parent = this;
device.Dock = DockStyle.Top;
device.present = true;
node.control = device;
device.UpdateInfo();
device.Visible = (search == "") || (node.name.ToLower().IndexOf(search) >= 0);
controlsToAdd.Add(device);
}
else
{
// Tag the device as present
if (node.control != null)
{
node.control.present = true;
node.control.UpdateInfo();
}
}
}
// Add all controls at once to make it fast.
if (controlsToAdd.Count > 0) { devicesPanel.Controls.AddRange((DeviceUserControl[])controlsToAdd.ToArray(typeof(DeviceUserControl))); }
// Clear all untagged devices
bool removed;
do {
removed = false;
foreach (Control c in devicesPanel.Controls) {
if ((c.GetType() == typeof(DeviceUserControl)) && ((DeviceUserControl)c).present == false) {
devicesPanel.Controls.Remove(c); c.Dispose(); removed = true;
}
}
} while (removed == true);
// Filter devices
int visibleDevices = 0;

View File

@@ -16,7 +16,6 @@ limitations under the License.
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Collections;
@@ -226,6 +225,7 @@ namespace MeshCentralRouter
string meshid = ev["meshid"].ToString();
string meshname = (string)ev["name"];
string meshdesc = (string)ev["desc"];
int meshtype = (int)ev["mtype"];
ulong meshrights = 0;
Dictionary<string, object> links = ((Dictionary<string, object>)ev["links"]);
if (links.ContainsKey(userid))
@@ -239,10 +239,23 @@ namespace MeshCentralRouter
}
// Update the mesh
MeshClass mesh = (MeshClass)meshes[meshid];
mesh.name = meshname;
mesh.desc = meshdesc;
mesh.rights = meshrights;
if (meshes.ContainsKey(meshid))
{
MeshClass mesh = (MeshClass)meshes[meshid];
mesh.name = meshname;
mesh.desc = meshdesc;
mesh.rights = meshrights;
}
else
{
MeshClass mesh = new MeshClass();
mesh.name = meshname;
mesh.desc = meshdesc;
mesh.rights = meshrights;
mesh.type = meshtype;
meshes[meshid] = mesh;
}
wc.WriteStringWebSocket("{\"action\":\"nodes\"}");
if (onNodesChanged != null) onNodesChanged();
break;
}
@@ -252,18 +265,40 @@ namespace MeshCentralRouter
string nodeid = (string)node["_id"];
if (nodes.ContainsKey(nodeid))
{
// Change existing node
lock (nodes)
{
NodeClass n = (NodeClass)nodes[nodeid];
n.nodeid = (string)node["_id"];
n.name = (string)node["name"];
if (node.ContainsKey("conn")) { n.conn = (int)node["conn"]; }
n.icon = (int)node["icon"];
if (node.ContainsKey("rdpport")) { n.rdpport = (int)node["rdpport"]; }
nodes[n.nodeid] = n;
ulong nodeRights = 0;
if (node.ContainsKey("links"))
{
Dictionary<string, object> links = ((Dictionary<string, object>)node["links"]);
if (links.ContainsKey(userid))
{
Dictionary<string, object> linksEx = ((Dictionary<string, object>)links[userid]);
if (linksEx.ContainsKey("rights")) { nodeRights = (ulong)(int)linksEx["rights"]; }
}
}
n.rights = nodeRights;
if ((n.rights == 0) && (meshes.ContainsKey(n.meshid) == false)) {
// We have no rights to this device, remove it
nodes.Remove(n.nodeid);
} else {
// Update the node
nodes[n.nodeid] = n;
}
}
if (onNodesChanged != null) onNodesChanged();
}
else
{
wc.WriteStringWebSocket("{\"action\":\"nodes\"}");
}
break;
}
case "nodeconnect":
@@ -317,7 +352,8 @@ namespace MeshCentralRouter
}
case "nodes":
{
nodes = new Dictionary<string, NodeClass>();
if (nodes == null) { nodes = new Dictionary<string, NodeClass>(); }
Dictionary<string, NodeClass> nodes2 = new Dictionary<string, NodeClass>();
lock (nodes)
{
Dictionary<string, object> groups = (Dictionary<string, object>)jsonAction["nodes"];
@@ -327,18 +363,54 @@ namespace MeshCentralRouter
for (int i = 0; i < nodesinMesh.Count; i++)
{
Dictionary<string, object> node = (Dictionary<string, object>)nodesinMesh[i];
NodeClass n = new NodeClass();
n.nodeid = (string)node["_id"];
n.agentid = (int)((Dictionary<string, object>)node["agent"])["id"];
n.name = (string)node["name"];
n.meshid = meshid;
if (node.ContainsKey("rdpport")) { n.rdpport = (int)node["rdpport"]; } else { n.rdpport = 3389; }
if (node.ContainsKey("conn")) { n.conn = (int)node["conn"]; } else { n.conn = 0; }
n.icon = (int)node["icon"];
nodes[n.nodeid] = n;
string nodeid = (string)node["_id"];
if (nodes.ContainsKey(nodeid))
{
NodeClass n = (NodeClass)nodes[nodeid];
n.nodeid = nodeid;
if (node.ContainsKey("agent")) { n.agentid = (int)((Dictionary<string, object>)node["agent"])["id"]; } else { n.agentid = -1; }
n.name = (string)node["name"];
n.meshid = meshid;
if (node.ContainsKey("rdpport")) { n.rdpport = (int)node["rdpport"]; } else { n.rdpport = 3389; }
if (node.ContainsKey("conn")) { n.conn = (int)node["conn"]; } else { n.conn = 0; }
if (node.ContainsKey("icon")) { n.icon = (int)node["icon"]; }
n.rights = 0;
if (node.ContainsKey("links"))
{
Dictionary<string, object> links = ((Dictionary<string, object>)node["links"]);
if (links.ContainsKey(userid))
{
Dictionary<string, object> linksEx = ((Dictionary<string, object>)links[userid]);
if (linksEx.ContainsKey("rights")) { n.rights = (ulong)(int)linksEx["rights"]; }
}
}
nodes2[n.nodeid] = n;
}
else
{
NodeClass n = new NodeClass();
n.nodeid = nodeid;
if (node.ContainsKey("agent")) { n.agentid = (int)((Dictionary<string, object>)node["agent"])["id"]; } else { n.agentid = -1; }
n.name = (string)node["name"];
n.meshid = meshid;
if (node.ContainsKey("rdpport")) { n.rdpport = (int)node["rdpport"]; } else { n.rdpport = 3389; }
if (node.ContainsKey("conn")) { n.conn = (int)node["conn"]; } else { n.conn = 0; }
if (node.ContainsKey("icon")) { n.icon = (int)node["icon"]; }
n.rights = 0;
if (node.ContainsKey("links"))
{
Dictionary<string, object> links = ((Dictionary<string, object>)node["links"]);
if (links.ContainsKey(userid)) {
Dictionary<string, object> linksEx = ((Dictionary<string, object>)links[userid]);
if (linksEx.ContainsKey("rights")) { n.rights = (ulong)(int)linksEx["rights"]; }
}
}
nodes2[n.nodeid] = n;
}
}
}
}
nodes = nodes2;
if (onNodesChanged != null) onNodesChanged();
break;
}
@@ -675,9 +747,8 @@ namespace MeshCentralRouter
private void ProcessWsBuffer(byte[] data, int offset, int len, int op)
{
Debug("Websocket got data.");
try { parent.processServerData(UTF8Encoding.UTF8.GetString(data, offset, len)); } catch (Exception ex) {
int i = 5;
}
//try { parent.processServerData(UTF8Encoding.UTF8.GetString(data, offset, len)); } catch (Exception ex) { }
parent.processServerData(UTF8Encoding.UTF8.GetString(data, offset, len));
}
private Dictionary<string, string> ParseHttpHeader(string header)

View File

@@ -136,8 +136,7 @@ namespace MeshCentralRouter
{
Debug("stopButton_Click()");
exit = true;
if (listener != null)
{
if (listener != null) {
listener.Stop();
listener = null;
}

View File

@@ -15,6 +15,7 @@ namespace MeshCentralRouter
public int agentid;
public int conn;
public int rdpport;
public ulong rights;
public DeviceUserControl control;
public override string ToString() { return name; }