protected override TcpClient OnConnectTarget(SslServer.ConnectedEventArgs args)
		{
			byte[] bytes = new byte[4];
			int read = args.Stream.Read(bytes, 0, 4);
			int origPort = ((int)bytes[0] << 24) | ((int)bytes[1] << 16) | ((int)bytes[2] << 8) | ((int)bytes[3]);
			return _targets[origPort].Clone();
		}
Beispiel #2
0
		private TcpServer MakeListener(string ip, int port, TunnelListenerBase config)
		{
			TcpServer server = null;
			if (String.IsNullOrEmpty(config.ServerCertificate))
				server = new TcpServer(ip, port);
			else
			{
				X509Certificate cert = new X509Certificate(config.ServerCertificate, config.ServerCertPassword);
				server = new SslServer(ip, port, cert, config.AllowedClients);
			}
			return server;
		}
		protected override void OnConnectionEstablished(SslServer.ConnectedEventArgs args, TcpClient client)
		{
			int port = args.LocalEndPoint.Port;
			byte[] bytes = new byte[4]
			{
				(byte)(0x00ff & (port >> 24)),
				(byte)(0x00ff & (port >> 16)),
				(byte)(0x00ff & (port >> 8)),
				(byte)(0x00ff & (port))
			};

			client.Stream.Write(bytes, 0, 4);
		}
        private TcpServer MakeListener(string ip, int port, TunnelListenerBase config)
        {
            TcpServer server = null;

            if (String.IsNullOrEmpty(config.ServerCertificate))
            {
                server = new TcpServer(ip, port);
            }
            else
            {
                X509Certificate cert = new X509Certificate(config.ServerCertificate, config.ServerCertPassword);
                server = new SslServer(ip, port, cert, config.AllowedClients);
            }
            return(server);
        }
        /// <summary> </summary>
		protected override void OnConnectionEstablished(SslServer.ConnectedEventArgs args, TcpClient client)
		{
		}
        /// <summary>
        /// Returns the target of the forwarding channel
        /// </summary>
        protected override TcpClient OnConnectTarget(SslServer.ConnectedEventArgs args)
		{
			return _target.Clone();
		}
		void server_Connected(object sender, SslServer.ConnectedEventArgs args)
		{
			string connectionInfo = args.RemoteEndPoint.ToString();

			try
			{
				using (TcpClient client = OnConnectTarget(args))
                using (BinaryLogging logging = new BinaryLogging(_logDirectory, args.RemoteEndPoint, args.LocalEndPoint))
				{
					client.Connect();
					connectionInfo = String.Format("{0} => {1}", args.RemoteEndPoint, client.Client.RemoteEndPoint);
					string revConnectionInfo = String.Format("{1} => {0}", args.RemoteEndPoint, client.Client.RemoteEndPoint);

					OnConnectionEstablished(args, client);

                    StreamRedirect recv = new StreamRedirect(args.Stream, client.Stream, connectionInfo, logging.FromServer);
                    StreamRedirect send = new StreamRedirect(client.Stream, args.Stream, revConnectionInfo, logging.FromClient);

					Log.Verbose("Streaming from {0}", connectionInfo);
                    WaitHandle.WaitAny(new WaitHandle[] { send.WaitClosed, recv.WaitClosed }, -1, true);
				}
			}
			catch (ThreadAbortException) { throw; }
			catch (Exception e)
			{
				Log.Error(e);
			}
			finally
			{
				args.Close();
				Log.Verbose("Streaming shutdown from {0}", connectionInfo);
			}
		}
        /// <summary>
        /// Called to handle a handshake when connecting to the forwarding target host 
        /// </summary>
		protected abstract void OnConnectionEstablished(SslServer.ConnectedEventArgs args, TcpClient client);
        /// <summary>
        /// Called as a connection is established to determin the target of the forwarding
        /// </summary>
		protected abstract TcpClient OnConnectTarget(SslServer.ConnectedEventArgs args);