示例#1
0
        public void RequestAcceptUnitTest()
        {
            Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

            SIPMonitorFilter filter = new SIPMonitorFilter("user test and event full and request invite ");

            Console.WriteLine(filter.GetFilterDescription());

            Assert.IsTrue(filter != null, "The filter was not correctly instantiated.");
            Assert.AreEqual(filter.Username, "test", "The filter username was not correctly set.");
            Assert.AreEqual(filter.EventFilterDescr, "full", "The filter event full was not correctly set.");
            Assert.AreEqual(filter.SIPRequestFilter, "invite", "The sip request filter was not correctly set.");

            string registerRequest =
                "INVITE sip:213.200.94.181 SIP/2.0" + m_CRLF +
                "Via: SIP/2.0/UDP 192.168.1.32:10254;branch=z9hG4bK-d87543-eb7c9f44883c5955-1--d87543-;rport;received=89.100.104.191" + m_CRLF +
                "To: aaronxten <sip:[email protected]>" + m_CRLF +
                "From: test <sip:[email protected]>;tag=774d2561" + m_CRLF +
                "Call-ID: MTBhNGZjZmQ2OTc3MWU5MTZjNWUxMDYxOTk1MjdmYzk." + m_CRLF +
                "CSeq: 2 REGISTER" + m_CRLF +
                "Contact: <sip:[email protected]:10254;rinstance=6d2bbd8014ca7a76>;expires=0" + m_CRLF +
                "Max-Forwards: 69" + m_CRLF +
                "User-Agent: X-Lite release 1006e stamp 34025" + m_CRLF +
                "Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO" + m_CRLF + m_CRLF;

            SIPMonitorEvent monitorEvent    = new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.FullSIPTrace, registerRequest, "test");
            bool            showEventResult = filter.ShowSIPMonitorEvent(monitorEvent);

            Assert.IsTrue(showEventResult, "The filter should have shown this event.");
        }
示例#2
0
        public void UsernameWithAndFilterUnitTest()
        {
            Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

            SIPMonitorFilter filter = new SIPMonitorFilter("user testandtest   and  event full");

            Console.WriteLine(filter.GetFilterDescription());

            Assert.IsTrue(filter != null, "The filter was not correctly instantiated.");
            Assert.AreEqual(filter.Username, "testandtest", "The filter username was not correctly set.");
            Assert.AreEqual(filter.EventFilterDescr, "full", "The filter event full was not correctly set.");
        }
示例#3
0
        public void ShowIPAddressUnitTest()
        {
            Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

            SIPMonitorFilter filter = new SIPMonitorFilter($"ipaddress 10.0.0.1 and event full and user {SIPMonitorFilter.WILDCARD}");

            Console.WriteLine(filter.GetFilterDescription());

            Assert.IsTrue(filter != null, "The filter was not correctly instantiated.");
            Assert.AreEqual(filter.IPAddress, "10.0.0.1", "The filter ip address was not correctly set.");

            SIPMonitorEvent monitorEvent    = new SIPMonitorConsoleEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.FullSIPTrace, "blah blah", String.Empty, null, SIPEndPoint.ParseSIPEndPoint("10.0.0.1"));
            bool            showEventResult = filter.ShowSIPMonitorEvent(monitorEvent);

            Assert.IsTrue(showEventResult, "The filter should have shown this event.");
        }
示例#4
0
        private void ConnectConsole(string filterText)
        {
            try
            {
                if (filterText.IsNullOrBlank())
                {
                    filterText = m_defaultFilter;
                }

                SIPMonitorFilter filter = new SIPMonitorFilter(filterText);
                m_controlfilter = filterText.Trim();
                UIHelper.SetIsEnabled(m_commandEntryTextBox, false);
                LogActivityMessage_External(MessageLevelsEnum.Monitor, "Requesting notifications with filter=" + filterText.Trim() + " at " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + ".");

                UIHelper.SetVisibility(m_connectSocketButton, Visibility.Collapsed);
                UIHelper.SetVisibility(m_closeSocketButton, Visibility.Visible);
                m_sipNotifierClient.SetControlFilter(m_controlfilter);
            }
            catch (Exception filterExp)
            {
                LogActivityMessage_External(MessageLevelsEnum.Warn, "Invalid filter. " + filterExp.Message);
                ConsoleNotificationsClosed();
            }
        }
        public SIPMonitorControlClient(string filename, SIPMonitorFilter filter, string username)
        {
            Filename = filename;
            LogDurationMinutes = filter.FileLogDuration;
            Filter = filter;

            FileStream = new FileStream(filename, FileMode.Create);
            string logStartedMessage = "Log started at " + Created.ToString("dd MMM yyyy HH:mm:ss") + " requested duration " + LogDurationMinutes + " regex " + filter.RegexFilter + ".\r\n";
            FileStream.Write(Encoding.ASCII.GetBytes(logStartedMessage), 0, logStartedMessage.Length);
            FileStream.Flush();
            Username = username;
        }
        public string Username;                     // The authenticated username for the monitor connection.

        public SIPMonitorControlClient(Socket socket, SIPMonitorFilter filter, string username)
		{
			ClientSocket = socket;
			Filter = filter;
            Username = username;
		}
        /// <summary>
        /// Simple state machine that processes commands from the client.
        /// </summary>
        private void ProcessCommand(string command) {
            try {
                Filter = new SIPMonitorFilter(command);

                // If the filter request is for a full SIP trace the user field must not be used since it's
                // tricky to decide which user a SIP message belongs to prior to authentication. If a full SIP
                // trace is requested instead of a user filter a regex will be set that matches the username in
                // the From or To header. If a full SIP trace is not specified then the user filer will be set.
                if (AdminId != m_topLevelAdminId) {
                    // If this user is not the top level admin there are tight restrictions on what filter can be set.
                    if (Filter.EventFilterDescr == "full") {
                        Filter = new SIPMonitorFilter("event full and regex :" + Username + "@");
                    }
                    else {
                        Filter.Username = Username;
                    }
                }

                //OutStream.Write(new byte[] { 0x1B, 0x5B, 0x34, 0x37, 0x6d, 0x00, 0x1b, 0x5b, 0x34, 0x36, 0x6d, 0x00 });
                //OutStream.Write(new byte[] { 0x1B, 0x5B, 0x34, 0x37, 0x6d, 0x1b, 0x5b, 0x34, 0x36, 0x6d });
                OutStream.Write(new byte[] { 0x1B, 0x5B, 0x34, 0x37, 0x3b, 0x33, 0x34, 0x6d });
                OutStream.Write(Encoding.ASCII.GetBytes(CRLF + "filter: " + Filter.GetFilterDescription()));
                OutStream.Write(new byte[] { 0x1B, 0x5B, 0x30, 0x6d });
                OutStream.Flush();
                OutStream.Write(Encoding.ASCII.GetBytes(CRLF));
            }
            catch (ApplicationException appExcp) {
                //OutStream.Write(new byte[] { 0x1B, 0x5B, 0x34, 0x37, 0x6d, 0x00, 0x1b, 0x5b, 0x34, 0x36, 0x6d, 0x00 });
                //OutStream.Write(Encoding.ASCII.GetBytes("\n" + appExcp.Message));
                //OutStream.Write(new byte[] { 0x1B, 0x5B, 0x30, 0x6d, 0x00 });
                WriteError(appExcp.Message);
                Filter = null;
            }
            catch (Exception excp) {
                logger.Error("Exception SIPMonitorClientConnection ProcessCommand. " + excp.Message);
                Filter = null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <remarks>
        /// From vt100.net and the vt100 user guide:
        ///
        /// Control Character Octal Code Action Taken 
        /// NUL 000 Ignored on input (not stored in input buffer; see full duplex protocol). 
        /// ENQ 005 Transmit answerback message. 
        /// BEL 007 Sound bell tone from keyboard. 
        /// BS 010 Move the cursor to the left one character position, unless it is at the left margin, in which case no action occurs. 
        /// HT 011 Move the cursor to the next tab stop, or to the right margin if no further tab stops are present on the line. 
        /// LF 012 This code causes a line feed or a new line operation. (See new line mode). 
        /// VT 013 Interpreted as LF. 
        /// FF 014 Interpreted as LF. 
        /// CR 015 Move cursor to the left margin on the current line. 
        /// SO 016 Invoke G1 character set, as designated by SCS control sequence. 
        /// SI 017 Select G0 character set, as selected by ESC ( sequence. 
        /// XON 021 Causes terminal to resume transmission. 
        /// XOFF 023 Causes terminal to stop transmitted all codes except XOFF and XON. 
        /// CAN 030 If sent during a control sequence, the sequence is immediately terminated and not executed. It also causes the error character to be displayed. 
        /// SUB 032 Interpreted as CAN. 
        /// ESC 033 Invokes a control sequence. 
        /// DEL 177 Ignored on input (not stored in input buffer). 
        /// </remarks>
        private void Listen() {
            try {
                List<char> command = new List<char>();
                int cursorPosn = 0;

                while (!HasClosed) {

                    byte[] inBuffer = new byte[1024];
                    int bytesRead = InStream.Read(inBuffer, 0, 1024);

                    if (bytesRead == 0) {
                        // Connection has been closed.
                        logger.Debug("Monitor SSH connection closed by remote client.");
                        HasClosed = true;
                        break;
                    }

                    //logger.Debug("input character received=" + inputChar + " (" + ((int)inputChar) + ")");

                     // Process any recognised commands.
                    if (inBuffer[0] == 0x03) {
                        // Ctrl-C, disconnect session.
                        Close();
                    }
                    else if (inBuffer[0] == 0x08 || inBuffer[0] == 0x7f) {
                        //logger.Debug("BackSpace");
                        // Backspace, move the cursor left one character and delete the right most character of the current command.
                        if (command.Count > 0) {
                            command.RemoveAt(command.Count - 1);
                            cursorPosn--;
                            // ESC [ 1 D for move left, ESC [ Pn P (Pn=1) for erase single character.
                            OutStream.Write(new byte[] { 0x1b, 0x5b, 0x31, 0x44, 0x1b, 0x5b, 0x31, 0x50 });
                        }
                    }
                    else if (inBuffer[0] == 0x0d || inBuffer[0] == 0x0a) {
                        string commandStr = (command.Count > 0) ? new string(command.ToArray()) : null;
                        logger.Debug("User " + Username + " requested filter=" + commandStr + ".");
                        ProcessCommand(commandStr);

                        command.Clear();
                        cursorPosn = 0;
                    }
                    else if (inBuffer[0] == 0x1b) {
                        // ESC command sequence.
                        //logger.Debug("ESC command sequence: " + BitConverter.ToString(inBuffer, 0, bytesRead));
                        if (inBuffer[1] == 0x5b) {
                            // Arrow scrolling command.
                            if (inBuffer[2] == 0x44) {
                                // Left arrow.
                                if (command.Count > 0 && cursorPosn > 0) {
                                    cursorPosn--;
                                    OutStream.Write(new byte[] { 0x1b, 0x5b, 0x44 });
                                }
                            }
                            else if (inBuffer[2] == 0x43) {
                                // Right arrow.
                                if (command.Count > 0 && cursorPosn < command.Count) {
                                    cursorPosn++;
                                    OutStream.Write(new byte[] { 0x1b, 0x5b, 0x43 });
                                }
                            }
                        }
                    }
                    else if (inBuffer[0] <= 0x1f) {
                        //logger.Debug("Unknown control sequence: " + BitConverter.ToString(inBuffer, 0, bytesRead));
                    }
                    else if (Filter != null) {
                        if (inBuffer[0] == 's' || inBuffer[0] == 'S') {
                            // Stop events.
                            Filter = null;
                            OutStream.Write(Encoding.ASCII.GetBytes(CRLF + FILTER_COMMAND_PROMPT));
                        }
                    }
                    else {
                        for (int index = 0; index < bytesRead; index++) {
                            if (command.Count == 0 || cursorPosn == command.Count) {
                                command.Add((char)inBuffer[index]);
                                cursorPosn++;
                            }
                            else {
                                command[cursorPosn] = (char)inBuffer[index];
                                cursorPosn++;
                            }

                            if (command.Count > MAX_COMMAND_LENGTH) {
                                command.Clear();
                                cursorPosn = 0;
                                WriteError("Command too long.");
                                break;
                            }
                            else {
                                // Echo the character back to the client.
                                OutStream.WriteByte(inBuffer[index]);
                            }
                        }
                    }
                }
            }
            catch (Exception excp) {
                logger.Error("Exception SIPMonitorClientConnection Listen. " + excp.Message);
            }
        }
        /// <summary>
        /// Simple state machine that processes commands from the client.
        /// </summary>
        private void ProcessCommand(string command)
        {
            try
            {
                if (command.IsNullOrBlank())
                {
                    command = DEFAULT_FILTER;
                }

                string subscribeError = null;
                m_notificationsSessionID = Guid.NewGuid().ToString();
                m_publisher.Subscribe(Username, AdminId, m_notificationsAddress, m_notificationsSessionID, SIPMonitorClientTypesEnum.Console.ToString(), command, DEFAULT_SESSION_LENGTH, null, out subscribeError);

                if (subscribeError != null)
                {
                    throw new ApplicationException(subscribeError);
                }
                else
                {
                    m_lastMonitorSessionRenewal = DateTime.Now;
                    ThreadPool.QueueUserWorkItem(delegate { RenewSession(); });
                    SIPMonitorFilter filter = new SIPMonitorFilter(command);
                    WriteFilterDescription(filter.GetFilterDescription());

                    /*if (m_publisher is SIPSorcery.Servers.SIPMonitorClientManager)
                     * {
                     *  logger.Debug("VT100Server is connected to a publisher that fires an event when a notification is ready.");
                     *  // The publisher will fire an event when a new notification is ready.
                     *  m_publisher.NotificationReady += (address) => { GetNotifications(); };
                     * }
                     * else
                     * {
                     *  logger.Debug("VT100Server is connected to a publisher that needs to be polled for events.");
                     *
                     *  // The publisher does not supply an event when a notification is ready and must be polled instead.
                     *  StopPolling = false;
                     *  ThreadPool.QueueUserWorkItem(delegate
                     *  {
                     *      try
                     *      {
                     *          while (!HasClosed && !StopPolling)
                     *          {
                     *              GetNotifications();
                     *              Thread.Sleep(POLL_FOR_NOTIFICATIONS_PERIOD);
                     *          }
                     *
                     *          logger.Debug("Stopped polling for events for address " + m_notificationsAddress + ".");
                     *      }
                     *      catch (ApplicationException appExcp)
                     *      {
                     *          WriteError(appExcp.Message);
                     *      }
                     *      catch (Exception excp)
                     *      {
                     *          logger.Error("Exception SIPSorceryVT100Server ProcessCommand on GetNotifications. " + excp.Message);
                     *      }
                     *  });
                     * }*/
                }
            }
            catch (ApplicationException appExcp)
            {
                WriteError(appExcp.Message);
            }
            catch (Exception excp)
            {
                logger.Error("Exception SIPSorceryVT100Server ProcessCommand. " + excp.Message);
            }
        }
            public void UsernameWithAndFilterUnitTest()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                SIPMonitorFilter filter = new SIPMonitorFilter("user testandtest   and  event full");
                Console.WriteLine(filter.GetFilterDescription());

                Assert.IsTrue(filter != null, "The filter was not correctly instantiated.");
                Assert.AreEqual(filter.Username, "testandtest", "The filter username was not correctly set.");
                Assert.AreEqual(filter.EventFilterDescr, "full", "The filter event full was not correctly set.");
            }
            public void FullTraceSIPSwitchUnitTest()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                SIPMonitorFilter filter = new SIPMonitorFilter("event full and regex :test@");
                Console.WriteLine(filter.GetFilterDescription());

                Assert.IsTrue(filter != null, "The filter was not correctly instantiated.");
                //Assert.AreEqual(filter.Username, "test", "The filter username was not correctly set.");
                Assert.AreEqual(filter.EventFilterDescr, "full", "The filter event full was not correctly set.");
                Assert.AreEqual(filter.RegexFilter, ":test@", "The regex was not correctly set.");

                string inviteRequest =
                    "INVITE sip:213.200.94.181 SIP/2.0" + m_CRLF +
                    "Via: SIP/2.0/UDP 192.168.1.32:10254;branch=z9hG4bK-d87543-eb7c9f44883c5955-1--d87543-;rport;received=89.100.104.191" + m_CRLF +
                    "To: aaronxten <sip:[email protected]>" + m_CRLF +
                    "From: test <sip:[email protected]>;tag=774d2561" + m_CRLF +
                    "Call-ID: MTBhNGZjZmQ2OTc3MWU5MTZjNWUxMDYxOTk1MjdmYzk." + m_CRLF +
                    "CSeq: 2 REGISTER" + m_CRLF +
                    "Contact: <sip:[email protected]:10254;rinstance=6d2bbd8014ca7a76>;expires=0" + m_CRLF +
                    "Max-Forwards: 69" + m_CRLF +
                    "User-Agent: X-Lite release 1006e stamp 34025" + m_CRLF +
                    "Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO" + m_CRLF + m_CRLF;

                SIPMonitorEvent monitorEvent = new SIPMonitorControlClientEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.FullSIPTrace, inviteRequest, null);
                bool showEventResult = filter.ShowSIPMonitorEvent(monitorEvent);

                Assert.IsTrue(showEventResult, "The filter should have shown this event.");
            }
            public void BlockIPAddressUnitTest()
            {
                Console.WriteLine("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);

                SIPMonitorFilter filter = new SIPMonitorFilter("ipaddress 127.0.0.1 and event full");
                Console.WriteLine(filter.GetFilterDescription());

                Assert.IsTrue(filter != null, "The filter was not correctly instantiated.");
                Assert.AreEqual(filter.IPAddress, "127.0.0.1", "The filter ip address was not correctly set.");

                SIPMonitorEvent monitorEvent = new SIPMonitorControlClientEvent(SIPMonitorServerTypesEnum.AppServer, SIPMonitorEventTypesEnum.FullSIPTrace, "blah blah", String.Empty, SIPEndPoint.ParseSIPEndPoint("127.0.0.2"), null);
                bool showEventResult = filter.ShowSIPMonitorEvent(monitorEvent);

                Assert.IsFalse(showEventResult, "The filter should not have shown this event.");
            }