Network Interceptor

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Interceptor
{
    class Program
    {
        TcpListener _server;
        TcpClient _channelDest;
        TcpClient _channelSrc;
        NetworkStream _serverStream;
        NetworkStream _connectorStream;
        static int BUF_SIZE = 2048;
        byte[] _serverBuf = new byte[BUF_SIZE];
        byte[] _connectorBuf = new byte[BUF_SIZE];

        void HandleListenerAccept(IAsyncResult ar)
        {
            _channelSrc = _server.EndAcceptTcpClient(ar);
            Console.WriteLine("Accept client from {0}", _channelSrc.Client.RemoteEndPoint.ToString());
            _serverStream = _channelSrc.GetStream();
            _serverStream.BeginRead(_serverBuf, 0, _serverBuf.Length, HandleListenerRead, null);
            // don't call BeginAcceptTcpClient again, only serve one session
        }

        void HandleListenerRead(IAsyncResult ar)
        {
            int count = _serverStream.EndRead(ar);
            if(count > 0)
            {
                _connectorStream.Write(_serverBuf, 0, count);
                Console.WriteLine("[{0} ==> {1}]", _channelSrc.Client.RemoteEndPoint.ToString(), _channelDest.Client.RemoteEndPoint.ToString());
                Console.WriteLine(Encoding.ASCII.GetString(_serverBuf, 0, count));
            }
            _serverStream.BeginRead(_serverBuf, 0, _serverBuf.Length, HandleListenerRead, null);
        }

        void HandleConnectorRead(IAsyncResult ar)
        {
            int count = _connectorStream.EndRead(ar);
            if (count > 0)
            {
                _serverStream.Write(_connectorBuf, 0, count);
                Console.WriteLine("[{0} ==> {1}]", _channelDest.Client.RemoteEndPoint.ToString(), _channelSrc.Client.RemoteEndPoint.ToString());
                Console.WriteLine(Encoding.ASCII.GetString(_connectorBuf, 0, count));
            }
            _connectorStream.BeginRead(_connectorBuf, 0, _connectorBuf.Length, HandleConnectorRead, null);

        }

        void Run(string[] args)
        {
            string listenIp = args[0];
            int listenPort = Convert.ToInt32(args[1]);
            string connectorIp = args[2];
            int connectorPort = Convert.ToInt32(args[3]);

            Console.WriteLine("Listen on {0}:{1}", listenIp, listenPort);
            _server = new TcpListener(IPAddress.Parse(listenIp), listenPort);
            _server.Start();
            _server.BeginAcceptTcpClient(HandleListenerAccept, _server);

            Console.WriteLine("Connecting to {0}:{1}", connectorIp, connectorPort);
            _channelDest = new TcpClient();
            _channelDest.Connect(connectorIp, connectorPort);
            _connectorStream = _channelDest.GetStream();
            _connectorStream.BeginRead(_connectorBuf, 0, _connectorBuf.Length, HandleConnectorRead, null);
        }

        static void Main(string[] args)
        {
            new Program().Run(args);

            while (Console.ReadLine() != "exit")
                Console.WriteLine("Type 'exit' to close the application ...");
        }
    }
}

没有更多推荐了,返回首页