예제 #1
0
        void newSession_Disconnect_Event(object sender, EventArgs e)
        {
            ITelnetProxySessionControl session = (ITelnetProxySessionControl)sender;

            m_sessions.Remove(session.GetId);
            session.Dispose();
        }
        //private static int AttachListenerToSession(int id, ITelnetProxySessionControl newSession, ITelnetProxySessionControl existingSession)
        //{
        //    int result = -1;

        //    if (!existingSession.AttachListener(ETelnetProxySession.Remote, newSession))
        //    {
        //        Debug.WriteLine("Failed to attach listner to Session " + result, DBG_CAT);
        //    }

        //    if (!newSession.AttachListener(ETelnetProxySession.Client, existingSession))
        //    {
        //        Debug.WriteLine("Failed to attach writer to Session " + result, DBG_CAT);
        //    }

        //    return 1;
        //}

        private int CreateNewSessionScript(ITelnetProxySessionControl newProxySession)
        {
            Match m = BlockUntilAnswered(newProxySession, ETelnetProxySession.Client, "CON?", csvRegex_new, 5, 5);

            if (m == null && !m.Success)
            {
                return(BAD_ID_REGEX);
            }

            string ip   = m.Groups[1].Value;
            int    port = -1;

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

            try
            {
                System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry(ip);
                if (entry.AddressList.Length == 0)
                {
                    Debug.WriteLine("Failed to resolve: " + ip, "StartScript");
                    return(BAD_ID);
                }
                ip = entry.AddressList[0].ToString();
            }
            catch (Exception)
            {
                Debug.WriteLine("Failed to parse ip or port from: " + ip + " " + port, "StartScript");
                return(BAD_ID);
            }

            ITelnetSessionControl remoteSession = new TelnetSession(ip, port);

            remoteSession.Name = "Remote";
            if (!remoteSession.Connect())
            {
                Debug.WriteLine("Failed to connected to Remote Server");
                return(BAD_REMOTE_CONNECTION);
            }

            newProxySession.AddSession(ETelnetProxySession.Remote, remoteSession);
            remoteSession.BeginRead();
            return(1);
        }
        private Match BlockUntilAnswered(ITelnetProxySessionControl newProxySession, ETelnetProxySession eTelnetProxySession,
                                         string p1, string csvRegex_new, int p2, int p3)
        {
            switch (eTelnetProxySession)
            {
            case ETelnetProxySession.Client:
                return(BlockUntilAnswered(newProxySession.ClientSession, p1, csvRegex_new, p2, p3));

            case ETelnetProxySession.Remote:
                return(BlockUntilAnswered(newProxySession.RemoteSession, p1, csvRegex_new, p2, p3));

            case ETelnetProxySession.Tap:
                return(BlockUntilAnswered(newProxySession.TapSession, p1, csvRegex_new, p2, p3));

            default:
                Debug.WriteLine("Bad switch Block Until Answered");
                break;
            }
            return(Match.Empty);
        }
        //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);
        }