private void ConnectThread(CancellationToken cancellationToken, string host, int port) { if (cancellationToken.IsCancellationRequested) { return; } logger.Info($"Attempting to connect to {host}:{port}.."); IsConnecting = true; try { client = new TcpClient(host, port); } catch (Exception ex) { logger.Error($"Connection error: {ex.Message}"); IsConnecting = false; IsConnected = false; return; } logger.Debug($"Connected. Setting up tunnel.."); tunnel = new TcpTunnel(client); logger.Debug($"Setting up link.."); link = new ClientLink(tunnel); link.RememberRemoteCertAuthority = RememberCertificates; link.NoAuthentication = NoAuthentication; logger.Debug($"Creating Keys.."); var(ca, priv, sign) = BifrostNext.CertManager.GenerateKeys(); logger.Debug($"Loading keys into Bifrost.."); link.LoadCertificatesNonBase64(ca, priv, sign); var connection = new UserConnection(client, clientLink: link); var user = new ClientData(connection); user.ClientKeys.ServerCertificateAuthority = ca; user.ClientKeys.PrivateKey = priv; user.ClientKeys.SignKey = sign; ClientData = user; link.OnDataReceived += Link_OnDataReceived; link.OnLinkClosed += Link_OnLinkClosed; var result = link.PerformHandshake(); if (result.Type != HandshakeResultType.Successful) { logger.Warn($"Handshake failed with type {result.Type}"); IsConnecting = false; IsConnected = false; Utilities.RaiseEventOnUIThread(OnClientConnectionChange, this, false); return; } else { logger.Debug($"Handshake was successful!"); IsConnecting = false; IsConnected = true; Utilities.RaiseEventOnUIThread(OnClientConnectionChange, this, true); } }
public void BroadcastMessage(IMessage msg, AuthState minimumAuthState = AuthState.Authenticated, ClientData skipUser = null) { string serialized = JsonConvert.SerializeObject(msg, Formatting.None); Type t = msg.GetType(); Message message = new Message(MessageType.Data, 0x01); message.Store["type"] = Encoding.UTF8.GetBytes(t.Name); message.Store["message"] = Utilities.Compress(Encoding.UTF8.GetBytes(serialized)); lock (_UserListLock) { foreach (var user in Clients) { try { if (skipUser != null && user == skipUser) { continue; } if (user.AuthenticationState >= minimumAuthState) { user.Connection.ServerLink.SendMessage(message); } } catch (Exception ex) { logger.Trace(ex.Message, "Server BroadcastMessage: " + user.ClientName != string.Empty ? user.ClientGuid : user.ClientName); } } } }
private void ProcessClient(object argument) { TcpClient client = (TcpClient)argument; logger.Trace($"Client socket accepted.."); TcpTunnel tunnel = new TcpTunnel(client); logger.Trace($"Client tunnel created.."); ServerLink link = new ServerLink(tunnel); logger.Trace($"Client link created.."); link.RememberRemoteCertAuthority = RememberCertificates; link.NoAuthentication = NoAuthentication; //link.RememberPeerKeys = true; // Get a key from the precomputed keys list string ca, priv; byte[] sign; (ca, priv, sign) = KeyManager.GetNextAvailableKeys(); if (String.IsNullOrEmpty(ca) || String.IsNullOrEmpty(priv) || sign.Length == 0) { logger.Error("GetNextAvailableKeys returned empty data!"); link.Close(); return; } logger.Trace($"Passing certificates into Bifrost.."); link.LoadCertificatesNonBase64(ca, priv, sign); link.OnDataReceived += Link_OnDataReceived; link.OnLinkClosed += Link_OnLinkClosed; var connection = new UserConnection(client, serverLink: link); var user = new ClientData(connection); user.ClientKeys.ServerCertificateAuthority = ca; user.ClientKeys.PrivateKey = priv; user.ClientKeys.SignKey = sign; logger.Debug($"Performing handshake with client.."); var result = link.PerformHandshake(); if (result.Type == HandshakeResultType.Successful) { lock (_UserListLock) { if (Clients.Count + 1 > MaxConnections) { link.Close(); return; } Clients.Add(user); } logger.Debug($"Handshake was a success!"); Utilities.RaiseEventOnUIThread(OnUserConnected, user); } else { logger.Info($"Handshake failure: {result.Type}"); link.Close(); } }
public void BroadcastMessage(Dictionary <string, byte[]> Store, AuthState minimumAuthState = AuthState.Authenticated, ClientData skipUser = null) { Message msg = new Message(MessageType.Data, 0x01); msg.Store = Store; lock (_UserListLock) { foreach (var user in Clients) { try { if (skipUser != null && user == skipUser) { continue; } if (user.AuthenticationState >= minimumAuthState) { user.Connection.ServerLink.SendMessage(msg); } } catch (Exception ex) { logger.Trace(ex.Message, "Server BroadcastMessage: " + user.ClientName != string.Empty ? user.ClientGuid : user.ClientName); } } } }
public bool IsConnectionTrusted(ClientData client) { return(client.Connection.ServerLink.TrustedCertificateUsed); }