private void ConnectWS(UdpClient client, int counter) { webSocketClient wc = new webSocketClient(); Uri wsurl = new Uri(url + "&auth=" + Uri.EscapeDataString(parent.authCookie)); Debug("#" + counter + ": Connecting web socket to: " + wsurl.ToString()); wc.xdebug = xdebug; wc.Start(wsurl, certhash); wc.tag = client; wc.id = counter; wc.tunneling = false; wc.onStateChanged += Wc_onStateChanged; wc.onBinaryData += Wc_onBinaryData; wc.onStringData += Wc_onStringData; }
private void Main_FormClosing(object sender, FormClosingEventArgs e) { if (wc != null) { // Disconnect state = 0; wc.Dispose(); wc = null; UpdateStatus(); } node.desktopViewer = null; closeKvmStats(); // Save window location Settings.SetRegValue("kvmlocation", Location.X + "," + Location.Y + "," + Size.Width + "," + Size.Height); }
// Read from the local client private void ClientEndReadWS(IAsyncResult ar) { // Fetch the state object[] args = (object[])ar.AsyncState; MeshMapper mm = (MeshMapper)args[0]; webSocketClient wc = (webSocketClient)args[1]; TcpClient client = (TcpClient)args[2]; byte[] buf = (byte[])args[3]; int counter = wc.id; int len = 0; try { // Read the data if (client != null && client.Connected == true) { len = client.GetStream().EndRead(ar); } } catch (Exception) { ShutdownClients(client, null, wc, counter); return; } //Debug("#" + counter + ": ClientEndRead(" + len + ") - Local Read"); if (len > 0) { // Forward the data & read again try { mm.bytesToServer += len; mm.bytesToServerCompressed += wc.SendBinary(buf, 0, len); // TODO: Do Async try { client.GetStream().BeginRead(buf, 0, buf.Length, new AsyncCallback(ClientEndReadWS), new object[] { mm, wc, client, buf }); } catch (Exception) { } } catch (Exception) { ShutdownClients(client, null, wc, counter); return; } } else { ShutdownClients(client, null, wc, counter); return; } }
private void Wc_onStateChanged(webSocketClient sender, webSocketClient.ConnectionStates state) { Debug("#" + sender.id + ": Websocket mapping, connected to server."); switch (state) { case webSocketClient.ConnectionStates.Disconnected: { if (sender.tag.GetType() == typeof(TcpClient)) { ShutdownClients((TcpClient)sender.tag, null, sender, sender.id); } else if (sender.tag.GetType() == typeof(UdpClient)) { ShutdownClients(null, (UdpClient)sender.tag, sender, sender.id); } break; } } }
private void MenuItemDisconnect_Click(object sender, EventArgs e) { if (wc != null) { // Disconnect state = 0; wc.Dispose(); wc = null; UpdateStatus(); } else { // Connect if (sender != null) { consentFlags = 0; } MenuItemConnect_Click(null, null); kvmControl.AttachKeyboard(); } displayMessage(null); }
private void Wc_onStateChanged(webSocketClient sender, webSocketClient.ConnectionStates wsstate) { switch (wsstate) { case webSocketClient.ConnectionStates.Disconnected: { // Disconnect state = 0; wc.Dispose(); wc = null; kvmControl.DetacheKeyboard(); break; } case webSocketClient.ConnectionStates.Connecting: { state = 1; displayMessage(null); break; } case webSocketClient.ConnectionStates.Connected: { // Reset stats bytesIn = 0; bytesInCompressed = 0; bytesOut = 0; bytesOutCompressed = 0; state = 2; string u = "*/meshrelay.ashx?p=2&nodeid=" + node.nodeid + "&id=" + randomIdHex + "&rauth=" + server.rauthCookie; server.sendCommand("{ \"action\": \"msg\", \"type\": \"tunnel\", \"nodeid\": \"" + node.nodeid + "\", \"value\": \"" + u.ToString() + "\", \"usage\": 2 }"); displayMessage(null); break; } } UpdateStatus(); }
private void ShutdownClients(TcpClient c1, UdpClient c2, webSocketClient c3, int counter) { Debug("#" + counter + ": ShutdownWebClients()"); if (c1 != null) { try { if (c1.Client.Connected) { try { c1.Client.Disconnect(false); } catch (Exception) { } try { c1.Client.Close(); } catch (Exception) { } try { c1.Close(); } catch (Exception) { } } } catch (Exception) { } } if (c2 != null) { try { c2.Close(); } catch (Exception) { } } try { c3.Dispose(); } catch (Exception) { } }
private void Wc_onStringData(webSocketClient sender, string data, int orglen) { bytesToClient += data.Length; bytesToClientCompressed += orglen; if ((sender.tunneling == false) && ((data == "c") || (data == "cr"))) { Debug("#" + sender.id + ": Websocket got server 'c' confirmation."); // Server confirmed connection, start reading the TCP stream //Console.Write("WS-Relay Connect\r\n"); if (sender.tag == null) { return; } if (sender.tag.GetType() == typeof(TcpClient)) { TcpClient client = (TcpClient)sender.tag; if (client != null) { byte[] buf1 = new byte[65000]; try { client.GetStream().BeginRead(buf1, 0, buf1.Length, new AsyncCallback(ClientEndReadWS), new object[] { this, sender, client, buf1 }); } catch (Exception) { } sender.tunneling = true; } } if (sender.tag.GetType() == typeof(UdpClient)) { UdpClient uclient = (UdpClient)sender.tag; try { uclient.BeginReceive(new AsyncCallback(UClientEndReadWS), new object[] { this, sender, uclient }); } catch (Exception) { } sender.tunneling = true; } } else if (sender.tunneling == true) { Debug("#" + sender.id + ": Websocket got text frame: " + data); } }
private void Wc_onStringData(webSocketClient sender, string data, int orglen) { bytesIn += data.Length; bytesInCompressed += orglen; if ((state == 2) && ((data == "c") || (data == "cr"))) { if (data == "cr") { sessionIsRecorded = true; } state = 3; // Send any connection options here if (consentFlags != 0) { kvmControl.Send("{ \"type\": \"options\", \"consent\": " + consentFlags + " }"); } // Send remote desktop protocol (2) kvmControl.Send("2"); kvmControl.SendCompressionLevel(); kvmControl.SendPause(false); kvmControl.SendRefresh(); UpdateStatus(); displayMessage(null); return; } if (state != 3) { return; } // Parse the received JSON Dictionary <string, object> jsonAction = new Dictionary <string, object>(); jsonAction = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(data); if ((jsonAction == null) || (jsonAction.ContainsKey("type") == false) || (jsonAction["type"].GetType() != typeof(string))) { return; } string action = jsonAction["type"].ToString(); switch (action) { case "metadata": { if ((jsonAction.ContainsKey("users") == false) || (jsonAction["users"] == null)) { return; } Dictionary <string, object> usersex = (Dictionary <string, object>)jsonAction["users"]; userSessions = new Dictionary <string, int>(); foreach (string user in usersex.Keys) { userSessions.Add(user, (int)usersex[user]); } UpdateStatus(); break; } case "console": { string msg = null; int msgid = -1; if ((jsonAction.ContainsKey("msg")) && (jsonAction["msg"] != null)) { msg = jsonAction["msg"].ToString(); } if (jsonAction.ContainsKey("msgid")) { msgid = (int)jsonAction["msgid"]; } if (msgid == 1) { msg = "Waiting for user to grant access..."; } if (msgid == 2) { msg = "Denied"; } if (msgid == 3) { msg = "Failed to start remote terminal session"; } // , {0} ({1}) if (msgid == 4) { msg = "Timeout"; } if (msgid == 5) { msg = "Received invalid network data"; } displayMessage(msg); break; } } }