Exemplo n.º 1
0
        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"] = 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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        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);
                    }
                }
            }
        }
Exemplo n.º 3
0
 public bool IsConnectionTrusted(ClientData client)
 {
     return(client.Connection.ServerLink.TrustedCertificateUsed);
 }
Exemplo n.º 4
0
        private void ProcessClient(object argument)
        {
            TcpClient client = (TcpClient)argument;

            logger.Debug($"Client socket accepted..");
            TcpTunnel tunnel = new TcpTunnel(client);

            logger.Debug($"Client tunnel created..");
            ServerLink link = new ServerLink(tunnel);

            logger.Debug($"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.Debug($"Passing certificates into Bifrost..");
            link.LoadCertificatesNonBase64(ca, priv, sign);

            link.OnDataReceived += Link_OnDataReceived;
            link.OnLinkClosed   += Link_OnLinkClosed;

            logger.Debug($"Performing handshake with client..");
            var result = link.PerformHandshake();

            if (result.Type == HandshakeResultType.Successful)
            {
                logger.Debug($"Handshake was a success!");
                var connection = new UserConnection(client, serverLink: link);
                var user       = new ClientData(connection);
                user.ClientKeys.ServerCertificateAuthority = ca;
                user.ClientKeys.PrivateKey = priv;
                user.ClientKeys.SignKey    = sign;
                // for use after handshake and when remembering clientCa (unimplemented)
                //user.Client.ClientKeys.ClientCertificateAuthority = clientCa;

                lock (_UserListLock)
                {
                    if (Clients.Count + 1 > MaxConnections)
                    {
                        link.Close();
                        return;
                    }
                    Clients.Add(user);
                }

                Utilities.RaiseEventOnUIThread(OnUserConnected, user);
            }
            else
            {
                logger.Info($"Handshake failure: {result.Type}");
                link.Close();
            }
        }
Exemplo n.º 5
0
        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) = Bifrost.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);
            }
        }