Exemplo n.º 1
0
 /// <summary>
 /// Closes the connection with a client, logs a reason, and updates the queue.
 /// </summary>
 /// <param name="reason">Reason to disconnect the client</param>
 public void Close(string reason = "unknown")
 {
     Logging.Log(Logging.LogSeverity.Warning, $"Closing {UserName}({this.User?.ToString() ?? "n/a"}) due to {reason}");
     try
     {
         this.User.Connection = null;
     } catch { }
     try
     {
         Client.Close();
     }
     catch { }
     Listening = false;
     try
     {
         listenThread.Abort();
     }
     catch { }
     lock (LockClient)
     {
         CurrentClients.Remove(this);
         ClientQueue.Remove(this);
         while (CurrentClients.Count < Program.Options.Maximum_Concurrent_Connections)
         {//accepting the next people in the queue
             if (ClientQueue.Count == 0)
             {
                 break;
             }
             ClientQueue[0].AcceptFromQueue();
             CurrentClients.Add(ClientQueue[0]);
             ClientQueue.RemoveAt(0);
         }
     }
 }
Exemplo n.º 2
0
        void s_IpPacketReceived(object sender, EventArgs e)
        {
            IPHeader ipHeader = (IPHeader)sender;

            if (ipHeader.ProtocolType == Protocol.Tcp)
            {
                try {
                    TCPHeader tcp = new TCPHeader(ipHeader.Data, ipHeader.MessageLength);
                    int       sourcePort;
                    int       destinationPort;
                    if (int.TryParse(tcp.SourcePort, out sourcePort) && int.TryParse(tcp.DestinationPort, out destinationPort))
                    {
                        if (destinationPort == ((Pop3Config)Configuration.AgentSettings).Pop3Port)
                        {
                            if (tcp.Data.Length > 0)
                            {
                                AppLayerPop3 pop3 = new AppLayerPop3(tcp.Data, tcp.Data.Length);
                                if (!CurrentClients.ContainsKey(sourcePort))
                                {
                                    CurrentClients.Add(sourcePort, new Pop3Client());
                                }
                                CurrentClients[sourcePort].LastInteraction = DateTime.Now;
                                switch (pop3.Pop3Code.ToUpper())
                                {
                                case AppLayerPop3.POP3_INTERACTION_CODE_LIST:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.LIST;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_DELE:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.DELE;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_NOOP:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.NOOP;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_PASS:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.PASS;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_QUIT:
                                    if (CurrentClients.ContainsKey(sourcePort))
                                    {
                                        CurrentClients.Remove(sourcePort);
                                    }
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_RETR:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.RETR;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_RSET:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.RSET;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_STAT:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.STAT;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_TOP:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.TOP;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_UIDL:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.UIDL;
                                    break;

                                case AppLayerPop3.POP3_INTERACTION_CODE_USER:
                                    CurrentClients[sourcePort].LastMessage = Pop3Message.USER;
                                    break;
                                }
                            }
                            // Console.WriteLine("Flags: {0}\tAck: {1}\tSeq:{2}", tcp.Flags, tcp.AcknowledgementNumber, tcp.SequenceNumber);
                            // Console.WriteLine("Source: {0}:{1}\tDestination: {2}:{3}", ipHeader.SourceAddress, tcp.SourcePort, ipHeader.DestinationAddress, tcp.DestinationPort);
                        }
                    }
                } catch (Exception ex) {
                    Sniffer.LogTrace(ex);
                }
            }
        }