Exemplo n.º 1
0
        internal EndPointListener(
      IPEndPoint endpoint,
      bool secure,
      string certificateFolderPath,
      ServerSslConfiguration sslConfig,
      bool reuseAddress
    )
        {
            if (secure) {
            var cert =
              getCertificate (endpoint.Port, certificateFolderPath, sslConfig.ServerCertificate);

            if (cert == null)
              throw new ArgumentException ("No server certificate could be found.");

            _secure = secure;
            _sslConfig = sslConfig;
            _sslConfig.ServerCertificate = cert;
              }

              _endpoint = endpoint;
              _prefixes = new Dictionary<HttpListenerPrefix, HttpListener> ();
              _unregistered = new Dictionary<HttpConnection, HttpConnection> ();
              _unregisteredSync = ((ICollection) _unregistered).SyncRoot;
              _socket =
            new Socket (endpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

              if (reuseAddress)
            _socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

              _socket.Bind (endpoint);
              _socket.Listen (500);
              _socket.BeginAccept (onAccept, this);
        }
Exemplo n.º 2
0
        internal EndPointListener(IPAddress address, int port, bool secure, string certificateFolderPath, ServerSslConfiguration sslConfig, bool reuseAddress)
        {
            if (secure)
            {
                X509Certificate2 certificate = getCertificate(port, certificateFolderPath, sslConfig.ServerCertificate);
                if (certificate == null)
                {
                    throw new ArgumentException("No server certificate could be found.");
                }
                _secure    = secure;
                _sslConfig = sslConfig;
                _sslConfig.ServerCertificate = certificate;
            }
            _prefixes         = new Dictionary <HttpListenerPrefix, HttpListener>();
            _unregistered     = new Dictionary <HttpConnection, HttpConnection>();
            _unregisteredSync = ((ICollection)_unregistered).SyncRoot;
            _socket           = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (reuseAddress)
            {
                _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, optionValue: true);
            }
            _endpoint = new IPEndPoint(address, port);
            _socket.Bind(_endpoint);
            _socket.Listen(500);
            SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();

            socketAsyncEventArgs.UserToken  = this;
            socketAsyncEventArgs.Completed += onAccept;
            if (!_socket.AcceptAsync(socketAsyncEventArgs))
            {
                onAccept(this, socketAsyncEventArgs);
            }
        }
 internal EndPointListener(IPEndPoint endpoint, bool secure, string certificateFolderPath, ServerSslConfiguration sslConfig, bool reuseAddress)
 {
     if (secure)
     {
         X509Certificate2 certificate = EndPointListener.getCertificate(endpoint.Port, certificateFolderPath, sslConfig.ServerCertificate);
         if (certificate == null)
         {
             throw new ArgumentException("No server certificate could be found.");
         }
         this._secure    = true;
         this._sslConfig = new ServerSslConfiguration(certificate, sslConfig.ClientCertificateRequired, sslConfig.EnabledSslProtocols, sslConfig.CheckCertificateRevocation)
         {
             ClientCertificateValidationCallback = sslConfig.ClientCertificateValidationCallback
         };
     }
     this._endpoint         = endpoint;
     this._prefixes         = new Dictionary <HttpListenerPrefix, WebSocketSharp.Net.HttpListener>();
     this._unregistered     = new Dictionary <HttpConnection, HttpConnection>();
     this._unregisteredSync = ((ICollection)this._unregistered).SyncRoot;
     this._socket           = new Socket(endpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
     if (reuseAddress)
     {
         this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
     }
     this._socket.Bind(endpoint);
     this._socket.Listen(500);
     this._socket.BeginAccept(new AsyncCallback(EndPointListener.onAccept), this);
 }
Exemplo n.º 4
0
 internal EndPointListener(IPAddress address, int port, bool reuseAddress, bool secure, string certificateFolderPath, ServerSslConfiguration sslConfig)
 {
     if (secure)
     {
         X509Certificate2 certificate = getCertificate(port, certificateFolderPath, sslConfig.ServerCertificate);
         if (certificate == null)
         {
             throw new ArgumentException("No server certificate could be found.");
         }
         this._secure    = secure;
         this._sslConfig = sslConfig;
         this._sslConfig.ServerCertificate = certificate;
     }
     this._prefixes         = new Dictionary <HttpListenerPrefix, HttpListener>();
     this._unregistered     = new Dictionary <HttpConnection, HttpConnection>();
     this._unregisteredSync = ((ICollection)this._unregistered).SyncRoot;
     this._socket           = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
     if (reuseAddress)
     {
         this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
     }
     this._endpoint = new IPEndPoint(address, port);
     this._socket.Bind(this._endpoint);
     this._socket.Listen(500);
     if (< > f__mg$cache0 == null)
     {
Exemplo n.º 5
0
        //private static void MakeNotInheritable(TcpListener tcpListener)
        //{
        //    var handle = tcpListener.Server.Handle;
        //    SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
        //}

        internal EndPointListener(
            IPEndPoint endpoint,
            bool secure,
            string certificateFolderPath,
            ServerSslConfiguration sslConfig,
            bool reuseAddress
            )
        {
            _endpoint = endpoint;

            if (secure)
            {
                var cert = getCertificate(
                    endpoint.Port,
                    certificateFolderPath,
                    sslConfig.ServerCertificate
                    );

                if (cert == null)
                {
                    var msg = "No server certificate could be found.";

                    throw new ArgumentException(msg);
                }

                _secure    = true;
                _sslConfig = new ServerSslConfiguration(sslConfig);
                _sslConfig.ServerCertificate = cert;
            }

            _prefixes        = new List <HttpListenerPrefix>();
            _connections     = new Dictionary <HttpConnection, HttpConnection>();
            _connectionsSync = ((ICollection)_connections).SyncRoot;

            _socket = new Socket(
                endpoint.Address.AddressFamily,
                SocketType.Stream,
                ProtocolType.Tcp
                );

            // 避免子进程未关闭时,无法释放socket。
            // https://stackoverflow.com/a/35303171/3335415
            SetHandleInformation(_socket.Handle, HANDLE_FLAG_INHERIT, 0);

            if (reuseAddress)
            {
                _socket.SetSocketOption(
                    SocketOptionLevel.Socket,
                    SocketOptionName.ReuseAddress,
                    true
                    );
            }

            _socket.Bind(endpoint);
            _socket.Listen(500);
            _socket.BeginAccept(onAccept, this);
        }
Exemplo n.º 6
0
        internal EndPointListener(IPEndPoint endpoint, bool secure, string certificateFolderPath,
#if !NETCF || BCC || SSL
                                  ServerSslConfiguration sslConfig,
#endif
                                  bool reuseAddress,
#if SSHARP
                                  EthernetAdapterType adapter,
#endif
                                  Logger logger)
        {
            _logger = logger;

#if !NETCF || BCC || SSL
            if (secure)
            {
                var cert = getCertificate(endpoint.Port, certificateFolderPath, sslConfig.ServerCertificate);

                if (cert == null)
                {
                    throw new ArgumentException("No server certificate could be found.");
                }

                _secure    = true;
                _sslConfig =
                    new ServerSslConfiguration(
                        cert,
                        sslConfig.ClientCertificateRequired,
                        sslConfig.EnabledSslProtocols,
                        sslConfig.CheckCertificateRevocation
                        );

                _sslConfig.ClientCertificateValidationCallback =
                    sslConfig.ClientCertificateValidationCallback;
            }
#endif

            _endpoint         = endpoint;
            _prefixes         = new Dictionary <HttpListenerPrefix, HttpListener> ();
            _unregistered     = new Dictionary <HttpConnection, HttpConnection> ();
            _unregisteredSync = ((ICollection)_unregistered).SyncRoot;
#if SSHARP
            _socket = new CrestronListenerSocket(IPAddress.Any, endpoint.Port, 16, adapter);
#else
            _socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (reuseAddress)
            {
                _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            }
#endif

            _socket.Bind(endpoint);
            _socket.Listen(500);
            _socket.BeginAccept(onAccept, this);
        }
Exemplo n.º 7
0
        internal EndPointListener(
            IPEndPoint endpoint,
            bool secure,
            string certificateFolderPath,
            ServerSslConfiguration sslConfig,
            bool reuseAddress
            )
        {
            _endpoint = endpoint;

            if (secure)
            {
                var cert = getCertificate(
                    endpoint.Port,
                    certificateFolderPath,
                    sslConfig.ServerCertificate
                    );

                if (cert == null)
                {
                    var msg = "No server certificate could be found.";

                    throw new ArgumentException(msg);
                }

                _secure    = true;
                _sslConfig = new ServerSslConfiguration(sslConfig);
                _sslConfig.ServerCertificate = cert;
            }

            _prefixes         = new List <HttpListenerPrefix> ();
            _unregistered     = new List <HttpConnection> ();
            _unregisteredSync = ((ICollection)_unregistered).SyncRoot;

            _socket = new Socket(
                endpoint.Address.AddressFamily,
                SocketType.Stream,
                ProtocolType.Tcp
                );

            if (reuseAddress)
            {
                _socket.SetSocketOption(
                    SocketOptionLevel.Socket,
                    SocketOptionName.ReuseAddress,
                    true
                    );
            }

            _socket.Bind(endpoint);
            _socket.Listen(500);
            _socket.BeginAccept(onAccept, this);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Copies the parameters from the specified <paramref name="configuration"/> to
        /// a new instance of the <see cref="ServerSslConfiguration"/> class.
        /// </summary>
        /// <param name="configuration">
        /// A <see cref="ServerSslConfiguration"/> from which to copy.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="configuration"/> is <see langword="null"/>.
        /// </exception>
        public ServerSslConfiguration(ServerSslConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            _checkCertRevocation          = configuration._checkCertRevocation;
            _clientCertRequired           = configuration._clientCertRequired;
            _clientCertValidationCallback = configuration._clientCertValidationCallback;
            _enabledSslProtocols          = configuration._enabledSslProtocols;
            _serverCert = configuration._serverCert;
        }
Exemplo n.º 9
0
        internal HttpConnection(Socket socket, EndPointListener listener)
        {
            this._socket   = socket;
            this._listener = listener;
            this._secure   = listener.IsSecure;
            NetworkStream innerStream = new NetworkStream(socket, false);

            if (!this._secure)
            {
                this._stream = innerStream;
            }
            else
            {
                ServerSslConfiguration sslConfiguration = listener.SslConfiguration;
                SslStream stream2 = new SslStream(innerStream, false, sslConfiguration.ClientCertificateValidationCallback);
                stream2.AuthenticateAsServer(sslConfiguration.ServerCertificate, sslConfiguration.ClientCertificateRequired, sslConfiguration.EnabledSslProtocols, sslConfiguration.CheckCertificateRevocation);
                this._stream = stream2;
            }
            this._sync    = new object();
            this._timeout = 0x15f90;
            if (< > f__mg$cache0 == null)
            {
Exemplo n.º 10
0
        internal HttpConnection(Socket socket, EndPointListener listener)
        {
            _socket   = socket;
            _listener = listener;
            _secure   = listener.IsSecure;
            NetworkStream networkStream = new NetworkStream(socket, owns_socket: false);

            if (_secure)
            {
                ServerSslConfiguration sslConfiguration = listener.SslConfiguration;
                SslStream sslStream = new SslStream(networkStream, leaveStreamOpen: false, sslConfiguration.ClientCertificateValidationCallback);
                sslStream.AuthenticateAsServer(sslConfiguration.ServerCertificate, sslConfiguration.ClientCertificateRequired, sslConfiguration.EnabledSslProtocols, sslConfiguration.CheckCertificateRevocation);
                _stream = sslStream;
            }
            else
            {
                _stream = networkStream;
            }
            _sync    = new object();
            _timeout = 90000;
            _timer   = new Timer(onTimeout, this, -1, -1);
            init();
        }
Exemplo n.º 11
0
        internal EndPointListener(
      IPAddress address,
      int port,
      bool secure,
      string certificateFolderPath,
      ServerSslConfiguration sslConfig,
      bool reuseAddress)
        {
            if (secure) {
            var cert = getCertificate (port, certificateFolderPath, sslConfig.ServerCertificate);
            if (cert == null)
              throw new ArgumentException ("No server certificate could be found.");

            _secure = secure;
            _sslConfig = sslConfig;
            _sslConfig.ServerCertificate = cert;
              }

              _prefixes = new Dictionary<HttpListenerPrefix, HttpListener> ();

              _unregistered = new Dictionary<HttpConnection, HttpConnection> ();
              _unregisteredSync = ((ICollection) _unregistered).SyncRoot;

              _socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
              if (reuseAddress)
            _socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

              _endpoint = new IPEndPoint (address, port);
              _socket.Bind (_endpoint);
              _socket.Listen (500);

              var args = new SocketAsyncEventArgs ();
              args.UserToken = this;
              args.Completed += onAccept;
              if (!_socket.AcceptAsync (args))
            onAccept (this, args);
        }
Exemplo n.º 12
0
        internal HttpConnection(Socket socket, EndPointListener listener)
        {
            this._socket   = socket;
            this._listener = listener;
            this._secure   = listener.IsSecure;
            NetworkStream networkStream = new NetworkStream(socket, false);

            if (!this._secure)
            {
                this._stream = networkStream;
            }
            else
            {
                ServerSslConfiguration sslConfiguration = listener.SslConfiguration;
                SslStream sslStream = new SslStream(networkStream, false, sslConfiguration.ClientCertificateValidationCallback);
                sslStream.AuthenticateAsServer(sslConfiguration.ServerCertificate, sslConfiguration.ClientCertificateRequired, sslConfiguration.EnabledSslProtocols, sslConfiguration.CheckCertificateRevocation);
                this._stream = sslStream;
            }
            this._sync            = new object();
            this._timeout         = 90000;
            this._timeoutCanceled = new Dictionary <int, bool>();
            this._timer           = new Timer(new TimerCallback(HttpConnection.onTimeout), this, -1, -1);
            this.init();
        }