예제 #1
0
        private void AddPortMapForm_Load(object sender, EventArgs e)
        {
            // Fill the groups
            groupComboBox.Items.Clear();
            foreach (string meshid in meshcentral.meshes.Keys)
            {
                MeshClass mesh = meshcentral.meshes[meshid];
                if (mesh.type == 2)
                {
                    int nodeCount = 0;
                    foreach (string nodeid in meshcentral.nodes.Keys)
                    {
                        NodeClass node = meshcentral.nodes[nodeid];
                        if ((node.meshid == mesh.meshid) && ((node.conn & 1) != 0))
                        {
                            nodeCount++;
                        }
                    }
                    if (nodeCount > 0)
                    {
                        groupComboBox.Items.Add(mesh);
                    }
                }
            }

            // Set default selection
            if (groupComboBox.Items.Count > 0)
            {
                groupComboBox.SelectedIndex = 0;
            }
            appComboBox.SelectedIndex = 1;
            fillNodesInDropDown();
        }
        private void fillNodesInDropDown()
        {
            if (selectedNode != null)
            {
                return;
            }
            MeshClass mesh = (MeshClass)groupComboBox.SelectedItem;

            // Fill the nodes dropdown
            nodeComboBox.Items.Clear();
            if (meshcentral.nodes != null)
            {
                foreach (string nodeid in meshcentral.nodes.Keys)
                {
                    NodeClass node = meshcentral.nodes[nodeid];
                    if (((node.meshid == mesh.meshid) || ((mesh.meshid == null) && (meshcentral.userRights.ContainsKey(node.nodeid)))) && ((node.conn & 1) != 0))
                    {
                        nodeComboBox.Items.Add(node);
                    }
                }
            }

            if (nodeComboBox.Items.Count > 0)
            {
                nodeComboBox.SelectedIndex = 0;
            }
        }
예제 #3
0
        private void searchTextBox_TextChanged(object sender, EventArgs e)
        {
            // Filter devices
            int    visibleDevices = 0;
            int    deviceCount    = 0;
            string search         = searchTextBox.Text.ToLower();

            foreach (Control c in devicesPanel.Controls)
            {
                if (c.GetType() == typeof(DeviceUserControl))
                {
                    deviceCount++;
                    NodeClass n           = ((DeviceUserControl)c).node;
                    bool      connVisible = ((showOfflineDevicesToolStripMenuItem.Checked) || ((n.conn & 1) != 0));
                    if ((search == "") || (n.name.ToLower().IndexOf(search) >= 0) || (showGroupNamesToolStripMenuItem.Checked && (((DeviceUserControl)c).mesh.name.ToLower().IndexOf(search) >= 0)))
                    {
                        //if ((search == "") || (n.name.ToLower().IndexOf(search) >= 0)) {
                        c.Visible = connVisible;
                        visibleDevices++;
                    }
                    else
                    {
                        c.Visible = false;
                    }
                }
            }

            noDevicesLabel.Visible       = (deviceCount == 0);
            noSearchResultsLabel.Visible = ((deviceCount > 0) && (visibleDevices == 0));
        }
 public void setRdpPort(NodeClass node, int port)
 {
     if (wc != null)
     {
         if (debug)
         {
             File.AppendAllText("debug.log", "SetRdpPort\r\n");
         }
         wc.WriteStringWebSocket("{\"action\":\"changedevice\",\"nodeid\":\"" + node.nodeid + "\",\"rdpport\":" + port + "}");
     }
 }
예제 #5
0
 public KVMViewer(MeshCentralServer server, NodeClass node)
 {
     InitializeComponent();
     this.Text                     += " - " + node.name;
     this.node                      = node;
     this.server                    = server;
     kvmControl                     = resizeKvmControl.KVM;
     kvmControl.parent              = this;
     kvmControl.DesktopSizeChanged += KvmControl_DesktopSizeChanged;
     resizeKvmControl.ZoomToFit     = true;
     UpdateStatus();
     this.MouseWheel += MainForm_MouseWheel;
 }
예제 #6
0
        private void fillNodesInDropDown()
        {
            MeshClass mesh = (MeshClass)groupComboBox.SelectedItem;

            // Fill the nodes dropdown
            nodeComboBox.Items.Clear();
            foreach (string nodeid in meshcentral.nodes.Keys)
            {
                NodeClass node = meshcentral.nodes[nodeid];
                if ((node.meshid == mesh.meshid) && ((node.conn & 1) != 0))
                {
                    nodeComboBox.Items.Add(node);
                }
            }

            if (nodeComboBox.Items.Count > 0)
            {
                nodeComboBox.SelectedIndex = 0;
            }
        }
예제 #7
0
        public KVMViewer(MeshCentralServer server, NodeClass node)
        {
            InitializeComponent();
            Translate.TranslateControl(this);
            this.Text                     += " - " + node.name;
            this.node                      = node;
            this.server                    = server;
            kvmControl                     = resizeKvmControl.KVM;
            kvmControl.parent              = this;
            kvmControl.DesktopSizeChanged += KvmControl_DesktopSizeChanged;
            resizeKvmControl.ZoomToFit     = true;
            UpdateStatus();
            this.MouseWheel += MainForm_MouseWheel;

            mainToolTip.SetToolTip(connectButton, Translate.T(Properties.Resources.ToggleRemoteDesktopConnection));
            mainToolTip.SetToolTip(cadButton, Translate.T(Properties.Resources.SendCtrlAltDelToRemoteDevice));
            mainToolTip.SetToolTip(settingsButton, Translate.T(Properties.Resources.ChangeRemoteDesktopSettings));
            mainToolTip.SetToolTip(clipOutboundButton, Translate.T(Properties.Resources.PushLocaClipboardToRemoteDevice));
            mainToolTip.SetToolTip(clipInboundButton, Translate.T(Properties.Resources.PullClipboardFromRemoteDevice));
            mainToolTip.SetToolTip(zoomButton, Translate.T(Properties.Resources.ToggleZoomToFitMode));
            mainToolTip.SetToolTip(statsButton, Translate.T(Properties.Resources.DisplayConnectionStatistics));
        }
예제 #8
0
        public void QuickMap(int protocol, int port, int appId, NodeClass node)
        {
            // See if we already have the right port mapping
            foreach (Control c in mapPanel.Controls)
            {
                if (c.GetType() == typeof(MapUserControl))
                {
                    MapUserControl cc = (MapUserControl)c;
                    if ((cc.protocol == protocol) && (cc.remotePort == port) && (cc.appId == appId) && (cc.node == node))
                    {
                        // Found a match
                        cc.appButton_Click(this, null);
                        return;
                    }
                }
            }

            // Add a new port map
            MapUserControl map = new MapUserControl();

            map.xdebug     = debug;
            map.inaddrany  = false;    // Loopback only
            map.protocol   = protocol; // 1 = TCP, 2 = UDP
            map.localPort  = 0;        // Any
            map.remotePort = port;     // HTTP
            map.appId      = appId;    // 0 = Custom, 1 = HTTP, 2 = HTTPS, 3 = RDP, 4 = PuTTY, 5 = WinSCP
            map.node       = node;
            map.host       = serverNameComboBox.Text;
            map.authCookie = meshcentral.authCookie;
            map.certhash   = meshcentral.wshash;
            map.parent     = this;
            map.Dock       = DockStyle.Top;
            map.Start();
            mapPanel.Controls.Add(map);
            noMapLabel.Visible = false;
            map.appButton_Click(this, null);
        }
        private void AddRelayMapForm_Load(object sender, EventArgs e)
        {
            if (selectedNode == null)
            {
                // Fill the groups
                groupComboBox.Items.Clear();
                foreach (string meshid in meshcentral.meshes.Keys)
                {
                    MeshClass mesh = meshcentral.meshes[meshid];
                    if (mesh.type == 2)
                    {
                        int nodeCount = 0;
                        foreach (string nodeid in meshcentral.nodes.Keys)
                        {
                            NodeClass node = meshcentral.nodes[nodeid];
                            if ((node.meshid == mesh.meshid) && ((node.conn & 1) != 0))
                            {
                                nodeCount++;
                            }
                        }
                        if (nodeCount > 0)
                        {
                            groupComboBox.Items.Add(mesh);
                        }
                    }
                }

                // If the user has indivitual device rights, add an extra device group
                if (meshcentral.userRights != null)
                {
                    bool indivitualDevices = false;
                    foreach (string id in meshcentral.userRights.Keys)
                    {
                        if (id.StartsWith("node/"))
                        {
                            indivitualDevices = true;
                        }
                    }
                    if (indivitualDevices)
                    {
                        MeshClass m = new MeshClass();
                        m.name = Properties.Resources.IndividualDevices;
                        groupComboBox.Items.Add(m);
                    }
                }

                // Set default selection
                if (groupComboBox.Items.Count > 0)
                {
                    groupComboBox.SelectedIndex = 0;
                }
                appComboBox.SelectedIndex = 1;
                fillNodesInDropDown();
            }
            else
            {
                if (selectedNode.mesh == null)
                {
                    MeshClass m = new MeshClass();
                    m.name = Properties.Resources.IndividualDevices;
                    groupComboBox.Items.Add(m);
                }
                else
                {
                    groupComboBox.Items.Add(selectedNode.mesh);
                }
                groupComboBox.SelectedIndex = 0;
                groupComboBox.Enabled       = false;
                nodeComboBox.Items.Add(selectedNode);
                nodeComboBox.SelectedIndex = 0;
                nodeComboBox.Enabled       = false;
                appComboBox.SelectedIndex  = 1;
            }
            nameTextBox.Focus();
        }
 public void setNode(NodeClass node)
 {
     selectedNode = node;
 }
        public void processServerData(string data)
        {
            if (debug)
            {
                File.AppendAllText("debug.log", "ServerData-" + data + "\r\n");
            }

            // Parse the received JSON
            Dictionary <string, object> jsonAction = new Dictionary <string, object>();

            jsonAction = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(data);
            if (jsonAction == null || jsonAction["action"].GetType() != typeof(string))
            {
                return;
            }

            string action = jsonAction["action"].ToString();

            switch (action)
            {
            case "close":
            {
                disconnectCause = jsonAction["cause"].ToString();
                disconnectMsg   = jsonAction["msg"].ToString();
                if (jsonAction.ContainsKey("email2fa"))
                {
                    disconnectEmail2FA = (bool)jsonAction["email2fa"];
                }
                else
                {
                    disconnectEmail2FA = false;
                }
                if (jsonAction.ContainsKey("email2fasent"))
                {
                    disconnectEmail2FASent = (bool)jsonAction["email2fasent"];
                }
                else
                {
                    disconnectEmail2FASent = false;
                }
                break;
            }

            case "serverinfo":
            {
                wc.WriteStringWebSocket("{\"action\":\"meshes\"}");
                wc.WriteStringWebSocket("{\"action\":\"nodes\"}");
                wc.WriteStringWebSocket("{\"action\":\"authcookie\"}");
                wc.WriteStringWebSocket("{\"action\":\"logincookie\"}");
                break;
            }

            case "authcookie":
            {
                authCookie = jsonAction["cookie"].ToString();
                changeState(2);
                break;
            }

            case "logincookie":
            {
                loginCookie = jsonAction["cookie"].ToString();
                if (onLoginTokenChanged != null)
                {
                    onLoginTokenChanged();
                }
                break;
            }

            case "userinfo":
            {
                Dictionary <string, object> userinfo = (Dictionary <string, object>)jsonAction["userinfo"];
                userid = (string)userinfo["_id"];
                break;
            }

            case "event":
            {
                Dictionary <string, object> ev = (Dictionary <string, object>)jsonAction["event"];
                string action2 = ev["action"].ToString();
                switch (action2)
                {
                case "meshchange":
                {
                    // Get the new values
                    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))
                    {
                        Dictionary <string, object> urights = ((Dictionary <string, object>)links[userid]);
                        if (urights != null)
                        {
                            if (urights["rights"].GetType() == typeof(int))
                            {
                                meshrights = (ulong)((int)urights["rights"]);
                            }
                            if (urights["rights"].GetType() == typeof(Int64))
                            {
                                meshrights = (ulong)((Int64)urights["rights"]);
                            }
                        }
                    }

                    // Update the mesh
                    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;
                }

                case "changenode":
                {
                    Dictionary <string, object> node = (Dictionary <string, object>)ev["node"];
                    string nodeid = (string)node["_id"];
                    if (nodes.ContainsKey(nodeid))
                    {
                        // Change existing node
                        lock (nodes)
                        {
                            NodeClass n = (NodeClass)nodes[nodeid];
                            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"];
                            }
                            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":
                {
                    string nodeid = (string)ev["nodeid"];
                    if (nodes.ContainsKey(nodeid))
                    {
                        lock (nodes)
                        {
                            NodeClass n = (NodeClass)nodes[nodeid];
                            if (ev.ContainsKey("conn"))
                            {
                                n.conn = (int)ev["conn"];
                            }
                            nodes[n.nodeid] = n;
                        }
                        if (onNodesChanged != null)
                        {
                            onNodesChanged();
                        }
                    }
                    break;
                }
                }
                break;
            }

            case "meshes":
            {
                meshes = new Dictionary <string, MeshClass>();

                ArrayList meshList = (ArrayList)jsonAction["meshes"];
                for (int i = 0; i < meshList.Count; i++)
                {
                    Dictionary <string, object> mesh = (Dictionary <string, object>)meshList[i];
                    MeshClass m = new MeshClass();
                    m.meshid = (string)mesh["_id"];
                    m.name   = (string)mesh["name"];
                    m.desc   = (string)mesh["desc"];
                    m.rights = 0;

                    Dictionary <string, object> links = ((Dictionary <string, object>)mesh["links"]);
                    if (links.ContainsKey(userid))
                    {
                        Dictionary <string, object> urights = ((Dictionary <string, object>)links[userid]);
                        if (urights != null)
                        {
                            if (urights["rights"].GetType() == typeof(int))
                            {
                                m.rights = (ulong)((int)urights["rights"]);
                            }
                            if (urights["rights"].GetType() == typeof(Int64))
                            {
                                m.rights = (ulong)((Int64)urights["rights"]);
                            }
                        }
                    }
                    if (mesh["mtype"].GetType() == typeof(string))
                    {
                        m.type = int.Parse((string)mesh["mtype"]);
                    }
                    if (mesh["mtype"].GetType() == typeof(int))
                    {
                        m.type = (int)mesh["mtype"];
                    }
                    meshes[m.meshid] = m;
                }

                break;
            }

            case "nodes":
            {
                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"];
                    foreach (string meshid in groups.Keys)
                    {
                        ArrayList nodesinMesh = (ArrayList)groups[meshid];
                        for (int i = 0; i < nodesinMesh.Count; i++)
                        {
                            Dictionary <string, object> node = (Dictionary <string, object>)nodesinMesh[i];
                            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;
            }

            default:
            {
                break;
            }
            }
        }
예제 #12
0
        private void addArgMappings()
        {
            // Add mappings
            for (int i = 0; i < this.args.Length; i++)
            {
                if (processedArgs.Contains(i))
                {
                    continue;
                }                                            // This map was already added
                string arg = this.args[i];

                if (arg.Length > 5 && arg.Substring(0, 5).ToLower() == "-map:")
                {
                    string[] x = arg.Substring(5).Split(':');
                    if (x.Length == 5)
                    {
                        // Protocol
                        int protocol = 0;
                        if (x[0].ToLower() == "tcp")
                        {
                            protocol = 1;
                        }
                        if (x[0].ToLower() == "udp")
                        {
                            protocol = 2;
                        }
                        if (protocol == 0)
                        {
                            continue;
                        }

                        // LocalPort
                        ushort localPort = 0;
                        if (ushort.TryParse(x[1], out localPort) == false)
                        {
                            continue;
                        }

                        // Node
                        string    nodename = x[2];
                        NodeClass node     = null;
                        foreach (NodeClass n in meshcentral.nodes.Values)
                        {
                            if (((n.conn & 1) != 0) && (n.name.ToLower() == nodename.ToLower()))
                            {
                                node = n;
                            }
                        }
                        if (node == null)
                        {
                            continue;
                        }

                        // AppId
                        int appId = 0;
                        if (protocol == 1)
                        {
                            if (x[3].ToLower() == "http")
                            {
                                appId = 1;
                            }
                            else if (x[3].ToLower() == "https")
                            {
                                appId = 2;
                            }
                            else if (x[3].ToLower() == "rdp")
                            {
                                appId = 3;
                            }
                            else if (x[3].ToLower() == "putty")
                            {
                                appId = 4;
                            }
                            else if (x[3].ToLower() == "winscp")
                            {
                                appId = 5;
                            }
                        }

                        // RemotePort
                        ushort remotePort = 0;
                        if (ushort.TryParse(x[4], out remotePort) == false)
                        {
                            continue;
                        }

                        // Add a new port map
                        MapUserControl map = new MapUserControl();
                        map.xdebug     = debug;
                        map.inaddrany  = inaddrany;
                        map.protocol   = protocol;
                        map.localPort  = (int)localPort;
                        map.remotePort = (int)remotePort;
                        map.appId      = appId;
                        map.node       = node;
                        map.host       = serverNameComboBox.Text;
                        map.authCookie = meshcentral.authCookie;
                        map.certhash   = meshcentral.wshash;
                        map.parent     = this;
                        map.Dock       = DockStyle.Top;
                        map.Start();

                        mapPanel.Controls.Add(map);
                        noMapLabel.Visible = false;
                        processedArgs.Add(i);
                    }
                }
                else if (arg.Length > 10 && arg.Substring(0, 10).ToLower() == "-relaymap:")
                {
                    string[] x = arg.Substring(10).Split(':');
                    if (x.Length == 6)
                    {
                        // Protocol
                        int protocol = 0;
                        if (x[0].ToLower() == "tcp")
                        {
                            protocol = 1;
                        }
                        if (x[0].ToLower() == "udp")
                        {
                            protocol = 2;
                        }
                        if (protocol == 0)
                        {
                            continue;
                        }

                        // LocalPort
                        ushort localPort = 0;
                        if (ushort.TryParse(x[1], out localPort) == false)
                        {
                            continue;
                        }

                        // Node
                        string    nodename = x[2];
                        NodeClass node     = null;
                        foreach (NodeClass n in meshcentral.nodes.Values)
                        {
                            if (((n.conn & 1) != 0) && (n.name.ToLower() == nodename.ToLower()))
                            {
                                node = n;
                            }
                        }
                        if (node == null)
                        {
                            continue;
                        }

                        // AppId
                        int appId = 0;
                        if (protocol == 1)
                        {
                            if (x[3].ToLower() == "http")
                            {
                                appId = 1;
                            }
                            else if (x[3].ToLower() == "https")
                            {
                                appId = 2;
                            }
                            else if (x[3].ToLower() == "rdp")
                            {
                                appId = 3;
                            }
                            else if (x[3].ToLower() == "putty")
                            {
                                appId = 4;
                            }
                            else if (x[3].ToLower() == "winscp")
                            {
                                appId = 5;
                            }
                        }

                        // Remote host
                        IPAddress remoteIp;
                        if (IPAddress.TryParse(x[4], out remoteIp) == false)
                        {
                            continue;
                        }

                        // RemotePort
                        ushort remotePort = 0;
                        if (ushort.TryParse(x[5], out remotePort) == false)
                        {
                            continue;
                        }

                        // Add a new port map
                        MapUserControl map = new MapUserControl();
                        map.xdebug     = debug;
                        map.inaddrany  = inaddrany;
                        map.protocol   = protocol;
                        map.localPort  = (int)localPort;
                        map.remoteIP   = remoteIp.ToString();
                        map.remotePort = (int)remotePort;
                        map.appId      = appId;
                        map.node       = node;
                        map.host       = serverNameComboBox.Text;
                        map.authCookie = meshcentral.authCookie;
                        map.certhash   = meshcentral.wshash;
                        map.parent     = this;
                        map.Dock       = DockStyle.Top;
                        map.Start();

                        mapPanel.Controls.Add(map);
                        noMapLabel.Visible = false;
                        processedArgs.Add(i);
                    }
                }
            }
        }
예제 #13
0
        private void updateDeviceList()
        {
            string search = searchTextBox.Text.ToLower();

            devicesPanel.SuspendLayout();

            // Untag all devices
            foreach (Control c in devicesPanel.Controls)
            {
                if (c.GetType() == typeof(DeviceUserControl))
                {
                    ((DeviceUserControl)c).present = false;
                }
            }

            /*
             * lock (meshcentral.nodes)
             * {
             *  // Add any missing devices
             *  ArrayList controlsToAdd = new ArrayList();
             *  foreach (MeshClass mesh in meshcentral.meshes.Values)
             *  {
             *      if (mesh.type == 2)
             *      {
             *          foreach (NodeClass node in meshcentral.nodes.Values)
             *          {
             *              if ((node.control == null) && (node.meshid == mesh.meshid))
             *              {
             *                  // Add a new device
             *                  DeviceUserControl device = new DeviceUserControl();
             *                  device.mesh = mesh;
             *                  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))); }
             * }
             */

            ArrayList controlsToAdd = new ArrayList();

            if (meshcentral.nodes != null)
            {
                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;

            foreach (Control c in devicesPanel.Controls)
            {
                if (c.GetType() == typeof(DeviceUserControl))
                {
                    NodeClass n           = ((DeviceUserControl)c).node;
                    bool      connVisible = ((showOfflineDevicesToolStripMenuItem.Checked) || ((n.conn & 1) != 0));
                    if ((search == "") || (n.name.ToLower().IndexOf(search) >= 0) || (showGroupNamesToolStripMenuItem.Checked && (((DeviceUserControl)c).mesh.name.ToLower().IndexOf(search) >= 0)))
                    {
                        c.Visible = connVisible;
                        visibleDevices++;
                    }
                    else
                    {
                        c.Visible = false;
                    }
                }
            }

            // Sort devices
            ArrayList sortlist = new ArrayList();

            foreach (Control c in devicesPanel.Controls)
            {
                if (c.GetType() == typeof(DeviceUserControl))
                {
                    sortlist.Add(c);
                }
            }
            if (sortByNameToolStripMenuItem.Checked)
            {
                DeviceComparer comp = new DeviceComparer();
                sortlist.Sort(comp);
            }
            else
            {
                DeviceGroupComparer comp = new DeviceGroupComparer();
                sortlist.Sort(comp);
            }
            remoteAllDeviceControls();
            devicesPanel.Controls.AddRange((DeviceUserControl[])sortlist.ToArray(typeof(DeviceUserControl)));

            devicesPanel.ResumeLayout();
            noDevicesLabel.Visible       = (sortlist.Count == 0);
            noSearchResultsLabel.Visible = ((sortlist.Count > 0) && (visibleDevices == 0));
        }
        public void processServerData(string data)
        {
            if (debug)
            {
                File.AppendAllText("debug.log", "ServerData-" + data + "\r\n");
            }

            // Parse the received JSON
            Dictionary <string, object> jsonAction = new Dictionary <string, object>();

            jsonAction = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(data);
            if (jsonAction == null || jsonAction["action"].GetType() != typeof(string))
            {
                return;
            }

            string action = jsonAction["action"].ToString();

            switch (action)
            {
            case "close":
            {
                disconnectCause = jsonAction["cause"].ToString();
                disconnectMsg   = jsonAction["msg"].ToString();
                break;
            }

            case "serverinfo":
            {
                wc.WriteStringWebSocket("{\"action\":\"meshes\"}");
                wc.WriteStringWebSocket("{\"action\":\"nodes\"}");
                wc.WriteStringWebSocket("{\"action\":\"authcookie\"}");
                wc.WriteStringWebSocket("{\"action\":\"logincookie\"}");
                break;
            }

            case "authcookie":
            {
                authCookie = jsonAction["cookie"].ToString();
                changeState(2);
                break;
            }

            case "logincookie":
            {
                loginCookie = jsonAction["cookie"].ToString();
                if (onLoginTokenChanged != null)
                {
                    onLoginTokenChanged();
                }
                break;
            }

            case "userinfo":
            {
                break;
            }

            case "event":
            {
                Dictionary <string, object> ev = (Dictionary <string, object>)jsonAction["event"];
                string action2 = ev["action"].ToString();
                switch (action2)
                {
                case "changenode":
                {
                    Dictionary <string, object> node = (Dictionary <string, object>)ev["node"];
                    string nodeid = (string)node["_id"];
                    if (nodes.ContainsKey(nodeid))
                    {
                        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"];
                        nodes[n.nodeid] = n;
                        if (onNodesChanged != null)
                        {
                            onNodesChanged();
                        }
                    }
                    break;
                }

                case "nodeconnect":
                {
                    string nodeid = (string)ev["nodeid"];
                    if (nodes.ContainsKey(nodeid))
                    {
                        NodeClass n = (NodeClass)nodes[nodeid];
                        if (ev.ContainsKey("conn"))
                        {
                            n.conn = (int)ev["conn"];
                        }
                        nodes[n.nodeid] = n;
                        if (onNodesChanged != null)
                        {
                            onNodesChanged();
                        }
                    }
                    break;
                }
                }
                break;
            }

            case "meshes":
            {
                meshes = new Dictionary <string, MeshClass>();

                ArrayList meshList = (ArrayList)jsonAction["meshes"];
                for (int i = 0; i < meshList.Count; i++)
                {
                    Dictionary <string, object> mesh = (Dictionary <string, object>)meshList[i];
                    MeshClass m = new MeshClass();
                    m.meshid = (string)mesh["_id"];
                    m.name   = (string)mesh["name"];
                    m.desc   = (string)mesh["desc"];
                    if (mesh["mtype"].GetType() == typeof(string))
                    {
                        m.type = int.Parse((string)mesh["mtype"]);
                    }
                    if (mesh["mtype"].GetType() == typeof(int))
                    {
                        m.type = (int)mesh["mtype"];
                    }
                    meshes[m.meshid] = m;
                }

                break;
            }

            case "nodes":
            {
                nodes = new Dictionary <string, NodeClass>();

                Dictionary <string, object> groups = (Dictionary <string, object>)jsonAction["nodes"];
                foreach (string meshid in groups.Keys)
                {
                    ArrayList nodesinMesh = (ArrayList)groups[meshid];
                    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.name   = (string)node["name"];
                        n.meshid = meshid;
                        if (node.ContainsKey("conn"))
                        {
                            n.conn = (int)node["conn"];
                        }
                        else
                        {
                            n.conn = 0;
                        }
                        n.icon          = (int)node["icon"];
                        nodes[n.nodeid] = n;
                    }
                }
                if (onNodesChanged != null)
                {
                    onNodesChanged();
                }
                break;
            }

            default:
            {
                break;
            }
            }
        }