diff --git a/FileViewer.cs b/FileViewer.cs index ad7f647..c51e06c 100644 --- a/FileViewer.cs +++ b/FileViewer.cs @@ -263,6 +263,7 @@ namespace MeshCentralRouter if (i >= 0) { ux = ux.Substring(0, i); } Uri u = new Uri(ux + "meshrelay.ashx?browser=1&p=5&nodeid=" + node.nodeid + "&id=" + randomIdHex + "&auth=" + server.authCookie); wc = new webSocketClient(); + wc.xdebug = server.debug; wc.onStateChanged += Wc_onStateChanged; wc.onBinaryData += Wc_onBinaryData; wc.onStringData += Wc_onStringData; diff --git a/KVMViewer.cs b/KVMViewer.cs index 239a88a..79c9fdb 100644 --- a/KVMViewer.cs +++ b/KVMViewer.cs @@ -105,6 +105,7 @@ namespace MeshCentralRouter if (i >= 0) { ux = ux.Substring(0, i); } Uri u = new Uri(ux + "meshrelay.ashx?browser=1&p=2&nodeid=" + node.nodeid + "&id=" + randomIdHex + "&auth=" + server.authCookie); wc = new webSocketClient(); + wc.xdebug = server.debug; wc.onStateChanged += Wc_onStateChanged; wc.onBinaryData += Wc_onBinaryData; wc.onStringData += Wc_onStringData; diff --git a/MeshMapper.cs b/MeshMapper.cs index 8511485..7231f2f 100644 --- a/MeshMapper.cs +++ b/MeshMapper.cs @@ -75,6 +75,8 @@ namespace MeshCentralRouter wsurl = new Uri(url); //wshash = serverHashTextBox.Text; + Debug(string.Format("MeshMapper-Start: Protcol={0}, LocalPort={1}, Url={2}, RemotePort={3}, RemoteIP={4}", protocol, localPort, url, remotePort, remoteIP)); + if (protocol == 1) { // Start the TCP listener @@ -201,6 +203,7 @@ namespace MeshCentralRouter { webSocketClient wc = new webSocketClient(); Debug("#" + counter + ": Connecting web socket to: " + wsurl.ToString()); + wc.xdebug = xdebug; wc.Start(wsurl, certhash); wc.tag = client; wc.id = counter; @@ -213,6 +216,7 @@ namespace MeshCentralRouter { webSocketClient wc = new webSocketClient(); Debug("#" + counter + ": Connecting web socket to: " + wsurl.ToString()); + wc.xdebug = xdebug; wc.Start(wsurl, certhash); wc.tag = client; wc.id = counter; diff --git a/Program.cs b/Program.cs index 0d90126..77fbb4e 100644 --- a/Program.cs +++ b/Program.cs @@ -15,6 +15,7 @@ limitations under the License. */ using System; +using System.IO; using System.Windows.Forms; namespace MeshCentralRouter @@ -33,6 +34,10 @@ namespace MeshCentralRouter Application.SetCompatibleTextRenderingDefault(false); Properties.Settings.Default.Upgrade(); + Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(ExceptionSink); + AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventSink); + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true); + foreach (string arg in args) { if (arg.Length > 3 && string.Compare(arg.Substring(0, 3), "-l:", true) == 0) { @@ -51,5 +56,16 @@ namespace MeshCentralRouter while (currentCulture.Equals(System.Threading.Thread.CurrentThread.CurrentUICulture) == false); } + public static void Debug(string msg) { try { File.AppendAllText("debug.log", msg + "\r\n"); } catch (Exception) { } } + + public static void ExceptionSink(object sender, System.Threading.ThreadExceptionEventArgs args) + { + Debug("ExceptionSink: " + args.Exception.ToString()); + } + + public static void UnhandledExceptionEventSink(object sender, UnhandledExceptionEventArgs args) + { + Debug("UnhandledExceptionEventSink: " + ((Exception)args.ExceptionObject).ToString()); + } } } diff --git a/WebSocketClient.cs b/WebSocketClient.cs index 4aa64f7..906baa7 100644 --- a/WebSocketClient.cs +++ b/WebSocketClient.cs @@ -106,6 +106,8 @@ namespace MeshCentralRouter this.tlsCertFingerprint = tlsCertFingerprint; Uri proxyUri = null; + Debug("Websocket Start, URL=" + url.ToString()); + // Check if we need to use a HTTP proxy (Auto-proxy way) try { @@ -130,6 +132,7 @@ namespace MeshCentralRouter if (proxyUri != null) { // Proxy in use + Debug("Websocket proxyUri: " + proxyUri.ToString()); proxyInUse = true; wsclient = new TcpClient(); wsclient.BeginConnect(proxyUri.Host, proxyUri.Port, new AsyncCallback(OnConnectSink), this); @@ -137,6 +140,7 @@ namespace MeshCentralRouter else { // No proxy in use + Debug("Websocket noProxy"); proxyInUse = false; wsclient = new TcpClient(); wsclient.BeginConnect(url.Host, url.Port, new AsyncCallback(OnConnectSink), this); @@ -461,17 +465,26 @@ namespace MeshCentralRouter private bool VerifyServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { + string hash1 = GetMeshKeyHash(certificate); + string hash2 = certificate.GetCertHashString(); + Debug("VerifyServerCertificate: tlsCertFingerprint = " + ((tlsCertFingerprint == null) ? "NULL" : tlsCertFingerprint)); + Debug("VerifyServerCertificate: Hash1 = " + hash1); + Debug("VerifyServerCertificate: Hash2 = " + hash2); return ((tlsCertFingerprint == GetMeshKeyHash(certificate)) || (tlsCertFingerprint == certificate.GetCertHashString())); } public int SendString(string data) { if (state != ConnectionStates.Connected) return 0; + Debug("WebSocketClient-SEND-String: " + data); byte[] buf = UTF8Encoding.UTF8.GetBytes(data); return SendFragment(buf, 0, buf.Length, 129); } - public int SendBinary(byte[] data, int offset, int len) { return SendFragment(data, offset, len, 130); } + public int SendBinary(byte[] data, int offset, int len) { + Debug("WebSocketClient-SEND-Binary-Len:" + len); + return SendFragment(data, offset, len, 130); + } // Fragment op code (129 = text, 130 = binary) public int SendFragment(byte[] data, int offset, int len, byte op)