private Match BlockUntilAnswered(ITelnetSessionControl session, string p, string csvRegex_id, int pollSec, int pollAttempt)

        {
            Match m   = Match.Empty;
            int   cnt = 0;

            while (cnt < pollAttempt)
            {
                //when something connects to the listner it sends an ID
                //if mega is connection, send a unique # > 0, this is the lookup for that session
                //if you want to listen to what the remote server is sending, send the id of the session
                session.SendToRemote(Encoding.ASCII.GetBytes(p));

                //process ID response
                m = PendForMatch(csvRegex_id);

                if (m.Success)
                {
                    break;
                }

                Thread.Sleep(pollSec * 1000);
                cnt++;
            }

            return(m);
        }
        /// <summary>
        /// remove the remote or tap session from the proxy session
        /// </summary>
        /// <param name="eTelnetProxySession"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public void RemoveSession(ETelnetProxySession eTelnetProxySession)
        {
            switch (eTelnetProxySession)
            {
            case ETelnetProxySession.Client:
                if (this.client == null)
                {
                    return;
                }
                DisconnectSession(eTelnetProxySession);
                this.client = null;
                break;

            case ETelnetProxySession.Remote:
                if (this.remote == null)
                {
                    return;
                }
                DisconnectSession(eTelnetProxySession);
                this.remote = null;
                break;

            case ETelnetProxySession.Tap:
                if (this.tap == null)
                {
                    return;
                }
                DisconnectSession(eTelnetProxySession);
                this.tap = null;
                break;
            }
        }
 private int SendTo(ITelnetSessionControl telnetSession, byte[] buffer)
 {
     if (!telnetSession.IsConnected)
     {
         return(-1);
     }
     return(telnetSession.SendToRemote(buffer));
 }
        /// <summary>
        /// Wraps the local and remote client
        /// </summary>
        /// <param name="ClientSession">the client that just connected, will become the client session</param>
        internal TelnetProxySession(ITelnetSessionControl ClientSession, int Id)
        {
            this.m_id = Id;

            RcvFromClientSession_EventHandler = new EventHandler <DataRcvEvent>(RcvFromClientSession);
            RcvFromRemoteSession_EventHandler = new EventHandler <DataRcvEvent>(RcvFromRemoteSession);
            RcvFromTapSession_EventHandler    = new EventHandler <DataRcvEvent>(RcvFromTapSession);

            ClientSessionDisconnect_EventHandler = new EventHandler(ClientSessionDisconnect);
            RemoteSessionDisconnect_EventHandler = new EventHandler(RemoteSessionDisconnect);
            TapSessionDisconnect_EventHandler    = new EventHandler(TapSessionDisconnect);

            client = ClientSession;
            client.Receive_Event    += RcvFromClientSession_EventHandler;
            client.Disconnect_Event += ClientSessionDisconnect_EventHandler;
            client.Name              = "Client";
        }
        public void Dispose()
        {
            if (this.client != null && this.client.IsConnected)
            {
                this.client.Disconnect();
            }
            if (this.remote != null && this.remote.IsConnected)
            {
                this.remote.Disconnect();
            }
            if (this.tap != null && this.tap.IsConnected)
            {
                this.tap.Disconnect();
            }

            client = null;
            remote = null;
            tap    = null;
        }
        /// <summary>
        /// Adds the remote or tap session to the proxy session
        /// </summary>
        /// <param name="eTelnetProxySession"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public void AddSession(ETelnetProxySession eTelnetProxySession, ITelnetSessionControl session)
        {
            switch (eTelnetProxySession)
            {
            case ETelnetProxySession.Remote:
                this.remote = session;
                this.RemoteSession.BeginRead();
                this.RemoteSession.Receive_Event    += RcvFromRemoteSession_EventHandler;
                this.RemoteSession.Disconnect_Event += RemoteSessionDisconnect_EventHandler;
                break;

            case ETelnetProxySession.Tap:
                this.tap = session;
                this.TapSession.BeginRead();
                this.TapSession.Receive_Event    += RcvFromTapSession_EventHandler;
                this.TapSession.Disconnect_Event += TapSessionDisconnect_EventHandler;
                break;
            }
        }
Beispiel #7
0
        //Every time there is a new connection do this.
        void OnNewClientStart(object newSessionObject)
        {
            ITelnetSessionControl newSession = (ITelnetSessionControl)newSessionObject;

            //blocks while the connection to the remote server is started
            TelnetProxySession_StartScript Script = new TelnetProxySession_StartScript();
            int retVal = Script.Start(newSession, m_sessions);

            Debug.WriteLine("After Script retval = " + retVal);

            if (retVal < 0)
            {
                Debug.WriteLine("New Client failed to complete start script succesfully");
                newSession.Disconnect();
            }
            else
            {
                m_sessions[retVal].DisconnectEvent += newSession_Disconnect_Event;
            }
        }
 private void Disconnect(ITelnetSessionControl telnetSession)
 {
     Debug.WriteLine("TPS, Id=" + this.GetId + " Disconnect Session: " + telnetSession.Name);
     try
     {
         if (telnetSession.IsConnected)
         {
             Debug.WriteLine("TPS, Id=" + this.GetId + " Disconnect Session - client was connected still");
             telnetSession.Disconnect();
         }
         else
         {
             Debug.WriteLine("TPS, Id=" + this.GetId + " Disconnect Session - client was not connected");
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("TPS, Id=" + this.GetId + " DisconnectRemote Session with ex: " + ex.Message + "\r\n" + ex.StackTrace, this);
         return;
     }
 }
        //when someone connects to the listener this will send Action?
        // possible Action
        // CON - Format would be CON,<ip|hostname>,port
        // LISTEN - Format would be LISTEN

        /// <summary>
        /// Prompts the local client for remote info and connects the streams
        /// </summary>
        /// <param name="newTelnetSession"></param>
        /// <returns>int -1 fail, 0 means a lister succesfully attached to a session, > 0 is a new session id</returns>
        public int Start(ITelnetSessionControl newTelnetSession, Dictionary <int, ITelnetProxySessionControl> sessions)
        {
            Debug.WriteLine("Starting New connection script");
            int type, id, result = 0;

            lock (m_lock) {
                //connect to event when the new session talks to us
                newTelnetSession.Receive_Event += ClientRcvdDataEventHandler;
                newTelnetSession.BeginRead();

                //ID,0,<num> - Listen to <num>
                //ID,1,<num> - Create new session called <num>

                //start the sequence
                Match m = BlockUntilAnswered(newTelnetSession, "ID?", csvRegex_id, 5, 5);
                if (m == null || !m.Success)
                {
                    return(BAD_ID_REGEX);
                }

                if (!int.TryParse(m.Groups[1].Value, out type))
                {
                    return(BAD_ID_NON_INT);
                }

                if (!int.TryParse(m.Groups[2].Value, out id))
                {
                    return(BAD_ID_NON_INT);
                }

                string s   = "";
                int    res = id;
                if (type == ID_LISTENER)
                {
                    newTelnetSession.Name = "Tap";
                    if (!sessions.ContainsKey(id))
                    {
                        s   = "Invalid ID, ID not found: " + id;
                        res = -1;
                    }
                    else
                    {
                        ITelnetProxySessionControl proxySession = sessions[id];
                        proxySession.AddSession(ETelnetProxySession.Tap, newTelnetSession);
                        s = "Tap added to session: " + id;
                    }
                }
                else if (type == ID_NEWSESSION)
                {
                    newTelnetSession.Name = "Client";
                    if (sessions.ContainsKey(id))
                    {
                        s = "Session ID already active: " + id;
                    }
                    else
                    {
                        ITelnetProxySessionControl newProxySession = new TelnetProxySession(newTelnetSession, id);
                        int res2 = CreateNewSessionScript(newProxySession);

                        if (res2 != 1)
                        {
                            s   = "Failed to create new remote Session: " + res;
                            res = res2;
                        }
                        else
                        {
                            sessions.Add(id, newProxySession);
                            s = "New Session Created: " + id;
                        }
                    }
                }
                else
                {
                    s   = "Invalid Type -> " + type;
                    res = BAD_ID_TYPE;
                }

                Debug.WriteLine("Start - New Telnet session - " + s);
                newTelnetSession.SendToRemote(Encoding.ASCII.GetBytes(s));

                if (res < 0)
                {
                    newTelnetSession.Disconnect();
                }
                else
                {
                    newTelnetSession.Receive_Event -= ClientRcvdDataEventHandler;
                }

                result = res;
            }
            return(result);
        }