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() public void UpdateInfo()
{ {
if (parent.getShowGroupNames()) { deviceNameLabel.Text = mesh.name + ", " + node.name; } else { deviceNameLabel.Text = node.name; } if (parent.getShowGroupNames() && (mesh != null)) { deviceNameLabel.Text = mesh.name + ", " + node.name; } else { deviceNameLabel.Text = node.name; }
if (node.conn == 0) { if (node.icon > 1)
devicePictureBox.Image = disabledDeviceImageList.Images[node.icon - 1]; {
} else { if (node.conn == 0)
devicePictureBox.Image = deviceImageList.Images[node.icon - 1]; {
devicePictureBox.Image = disabledDeviceImageList.Images[node.icon - 1];
}
else
{
devicePictureBox.Image = deviceImageList.Images[node.icon - 1];
}
} }
string status = ""; string status = "";
@@ -51,10 +57,17 @@ namespace MeshCentralRouter
rdpButton.Visible = false; rdpButton.Visible = false;
} }
// Compute rights on this device
ulong rights = node.rights;
if (mesh != null) { rights |= mesh.rights; }
// Must have remote control 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); 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; 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 forceExit = false;
public bool sendEmailToken = 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 void setRegValue(string name, string value) {
public string getRegValue(string name, string value) { return Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value).ToString(); } 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 public class DeviceComparer : IComparer
{ {
@@ -259,6 +263,7 @@ namespace MeshCentralRouter
if (c.GetType() == typeof(DeviceUserControl)) { ((DeviceUserControl)c).present = false; } if (c.GetType() == typeof(DeviceUserControl)) { ((DeviceUserControl)c).present = false; }
} }
/*
lock (meshcentral.nodes) lock (meshcentral.nodes)
{ {
// Add any missing devices // Add any missing devices
@@ -299,13 +304,49 @@ namespace MeshCentralRouter
// Add all controls at once to make it fast. // Add all controls at once to make it fast.
if (controlsToAdd.Count > 0) { devicesPanel.Controls.AddRange((DeviceUserControl[])controlsToAdd.ToArray(typeof(DeviceUserControl))); } if (controlsToAdd.Count > 0) { devicesPanel.Controls.AddRange((DeviceUserControl[])controlsToAdd.ToArray(typeof(DeviceUserControl))); }
} }
*/
// Clear all untagged devices ArrayList controlsToAdd = new ArrayList();
foreach (Control c in devicesPanel.Controls) { foreach (NodeClass node in meshcentral.nodes.Values)
if ((c.GetType() == typeof(DeviceUserControl)) && ((DeviceUserControl)c).present == false) { {
devicesPanel.Controls.Remove(c); c.Dispose(); 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 // Filter devices
int visibleDevices = 0; int visibleDevices = 0;

View File

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

View File

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

View File

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