Пример #1
0
 internal void SignalAllManagedRPCClients(KeePassRPC.DataExchangeModel.Signal signal)
 {
     lock (_lockRPCClientManagers)
     {
         _lockRPCClientManagers.HeldBy = Thread.CurrentThread.ManagedThreadId;
         foreach (KeePassRPCClientManager manager in _RPCClientManagers.Values)
         {
             manager.SignalAll(signal);
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Sends the specified signal to the client.
        /// </summary>
        /// <param name="signal">The signal.</param>
        public void Signal(KeePassRPC.DataExchangeModel.Signal signal, string methodName)
        {
            try
            {
                Jayrock.Json.JsonObject call = new Jayrock.Json.JsonObject();
                call["id"]     = ++_currentCallBackId;
                call["method"] = methodName;
                call["params"] = new int[] { (int)signal };

                StringBuilder sb = new StringBuilder();
                Jayrock.Json.Conversion.JsonConvert.Export(call, sb);
                if (WebSocketConnection != null)
                {
                    KPRPCMessage data2client = new KPRPCMessage();
                    data2client.protocol = "jsonrpc";
                    data2client.version  = ProtocolVersion;
                    data2client.jsonrpc  = Encrypt(sb.ToString());

                    // If there was a problem encrypting our message, just abort - the
                    // client won't be able to do anything useful with an error message
                    if (data2client.jsonrpc == null)
                    {
                        if (KPRPC.logger != null)
                        {
                            KPRPC.logger.WriteLine("Encryption error when trying to send signal: " + signal);
                        }
                        return;
                    }

                    // Signalling through the websocket needs to be processed on a different thread becuase handling the incoming messages results in a lock on the list of known connections (which also happens before this Signal function is called) so we want to process this as quickly as possible and avoid deadlocks.
                    // Not sure why this doesn't happen on the standard network port implementation in previous versions (maybe the networking stack created threads automatically before passing on the incoming messages?)

                    //TODO1.3: Does this work in .NET 2?
                    // Respond to each message on a different thread
                    ThreadStart work = delegate
                    {
                        WebSocketConnection.Send(Jayrock.Json.Conversion.JsonConvert.ExportToString(data2client));
                    };
                    Thread messageHandler = new Thread(work);
                    messageHandler.Name = "signalDispatcher";
                    messageHandler.Start();
                }
                else
                {
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
                    this.ConnectionStreamWrite(bytes);
                }
            }
            catch (System.IO.IOException)
            {
                // Sometimes a connection is unexpectedly closed e.g. by Firefox
                // or (more likely) dodgy security "protection". From one year's
                // worth of bug reports (35) 100% of unexpected application
                // exceptions were IOExceptions.
                //
                // We will now ignore this type of exception and allow KeeFox to
                // re-establish the link to KeePass as part of its regular polling loop.
                //
                // The requested KPRPC signal will never be recieved by KeeFox
                // but this should be OK in practice becuase KeeFox will
                // re-establish the relevant state information as soon as it reconnects.
                //
                // BUT: the exception to this rule is when KeeFox fails to receive the
                // "shutdown" signal - it then gets itself in an inconsistent state
                // and has no opportunity to recover until KeePass is running again.
                return;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("ERROR! Please click on this box, press CTRL-C on your keyboard and paste into a new post on the KeeFox forum (http://keefox.org/help/forum). Doing this will help other people to use KeeFox without any unexpected error messages like this. Please briefly describe what you were doing when the problem occurred, which version of KeeFox, KeePass and Firefox you use and what other security software you run on your machine. Thanks! Technical detail follows: " + ex.ToString());
            }
        }