Beispiel #1
0
        /// <summary>
        /// Dispatches the event, parsing the content of the line and raising the relevant event.
        /// </summary>
        /// <param name="line">The line which was received from the control connection.</param>
        /// <returns>
        ///   <c>true</c> if the event is parsed and dispatched successfully; otherwise, <c>false</c>.
        /// </returns>
        public override bool Dispatch(string line)
        {
            ORConnection connection = ORConnection.FromLine(line);

            if (connection == null)
            {
                return(false);
            }

            Client.Events.OnORConnectionChanged(new ORConnectionEventArgs(connection));
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Called when an OR connection changes within the tor service.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="ORConnectionEventArgs"/> instance containing the event data.</param>
        private void OnORConnectionChanged(object sender, ORConnectionEventArgs e)
        {
            if (e.Connection == null)
            {
                return;
            }

            lock (synchronizeORConnections)
            {
                ORConnection existing;

                if (e.Connection.ID != 0)
                {
                    existing = orConnections.Where(o => o.ID == e.Connection.ID).FirstOrDefault();
                }
                else
                {
                    existing = orConnections.Where(o => o.Target.Equals(e.Connection.Target, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                }

                if (existing == null)
                {
                    if (Status.MaximumRecords <= orConnections.Count)
                    {
                        ORConnection removal = orConnections.Where(o => o.Status == ORStatus.Closed || o.Status == ORStatus.Failed).FirstOrDefault();

                        if (removal == null)
                        {
                            return;
                        }

                        orConnections.Remove(removal);
                    }

                    existing = e.Connection;
                    orConnections.Add(existing);
                }
                else
                {
                    existing.CircuitCount = e.Connection.CircuitCount;
                    existing.ID           = e.Connection.ID;
                    existing.Reason       = e.Connection.Reason;
                    existing.Status       = e.Connection.Status;
                    existing.Target       = e.Connection.Target;
                }
            }

            if (ORConnectionsChanged != null)
            {
                ORConnectionsChanged(client, EventArgs.Empty);
            }
        }
        /// <summary>
        /// Dispatches the event, parsing the content of the line and raising the relevant event.
        /// </summary>
        /// <param name="line">The line which was received from the control connection.</param>
        /// <returns>
        ///   <c>true</c> if the event is parsed and dispatched successfully; otherwise, <c>false</c>.
        /// </returns>
        public override bool Dispatch(string line)
        {
            string   target;
            ORStatus status;

            string[] parts = StringHelper.GetAll(line, ' ');

            if (parts.Length < 2)
            {
                return(false);
            }

            target = parts[0];
            status = ReflectionHelper.GetEnumerator <ORStatus, DescriptionAttribute>(attr => parts[1].Equals(attr.Description, StringComparison.CurrentCultureIgnoreCase));

            ORConnection connection = new ORConnection();

            connection.Status = status;
            connection.Target = target;

            for (int i = 2; i < parts.Length; i++)
            {
                string data = parts[i].Trim();

                if (!data.Contains("="))
                {
                    continue;
                }

                string[] values = data.Split(new[] { '=' }, 2);

                if (values.Length < 2)
                {
                    continue;
                }

                string name  = values[0].Trim();
                string value = values[1].Trim();

                if ("REASON".Equals(name, StringComparison.CurrentCultureIgnoreCase))
                {
                    connection.Reason = ReflectionHelper.GetEnumerator <ORReason, DescriptionAttribute>(attr => value.Equals(attr.Description, StringComparison.CurrentCultureIgnoreCase));
                    continue;
                }

                if ("NCIRCS".Equals(name, StringComparison.CurrentCultureIgnoreCase))
                {
                    int circuits;

                    if (int.TryParse(value, out circuits))
                    {
                        connection.CircuitCount = circuits;
                    }

                    continue;
                }

                if ("ID".Equals(name, StringComparison.CurrentCultureIgnoreCase))
                {
                    int id;

                    if (int.TryParse(value, out id))
                    {
                        connection.ID = id;
                    }

                    continue;
                }
            }

            Client.Events.OnORConnectionChanged(new ORConnectionEventArgs(connection));
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// Called when an OR connection has changed within the client.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void OnClientConnectionsChanged(object sender, EventArgs e)
        {
            if (closing)
            {
                return;
            }

            connections = client.Status.ORConnections;

            Invoke((Action) delegate
            {
                connectionTree.BeginUpdate();

                List <TreeNode> removals = new List <TreeNode>();

                foreach (TreeNode n in connectionTree.Nodes)
                {
                    removals.Add(n);
                }

                foreach (ORConnection connection in connections)
                {
                    bool added    = false;
                    TreeNode node = null;

                    if (!showClosedCheckBox.Checked)
                    {
                        if (connection.Status == ORStatus.Closed || connection.Status == ORStatus.Failed)
                        {
                            continue;
                        }
                    }

                    foreach (TreeNode existingNode in connectionTree.Nodes)
                    {
                        ORConnection existing = (ORConnection)existingNode.Tag;

                        if (connection.ID != 0 && connection.ID == existing.ID)
                        {
                            node = existingNode;
                            break;
                        }
                        if (connection.Target.Equals(existing.Target, StringComparison.CurrentCultureIgnoreCase))
                        {
                            node = existingNode;
                            break;
                        }
                    }

                    string text = string.Format("Connection #{0} [{1}] ({2})", connection.ID, connection.Status, connection.Target);

                    if (node == null)
                    {
                        node     = new TreeNode(text);
                        node.Tag = connection;
                        added    = true;
                    }
                    else
                    {
                        node.Text = text;
                        node.Nodes.Clear();

                        removals.Remove(node);
                    }

                    if (added)
                    {
                        connectionTree.Nodes.Add(node);
                    }
                }

                foreach (TreeNode remove in removals)
                {
                    connectionTree.Nodes.Remove(remove);
                }

                connectionTree.EndUpdate();
            });
        }