public HttpConnection(ILogger logger, Socket sock, EndPointListener epl, bool secure, X509Certificate cert, string connectionId)
        {
            _connectionId = connectionId;
            _logger = logger;
            this.sock = sock;
            this.epl = epl;
            this.secure = secure;
            this.cert = cert;
            this.SetSocketTimeout(sock);
            if (secure == false)
            {
                stream = new NetworkStream(sock, false);
            }
            else
            {
                //ssl_stream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
                //{
                //    if (c == null)
                //        return true;
                //    var c2 = c as X509Certificate2;
                //    if (c2 == null)
                //        c2 = new X509Certificate2(c.GetRawCertData());
                //    client_cert = c2;
                //    client_cert_errors = new int[] { (int)e };
                //    return true;
                //});
                //stream = ssl_stream.AuthenticatedStream;

                ssl_stream = new SslStream(new NetworkStream(sock, false), false);
                ssl_stream.AuthenticateAsServer(cert);
                stream = ssl_stream;
            }
            timer = new ResumableTimer(OnTimeout);
            Init();
        }
        public HttpConnection(
			Socket socket,
			EndPointListener listener,
			bool secure,
			X509Certificate2 cert
		)
        {
            _socket = socket;
            _epListener = listener;
            _secure = secure;

            var netStream = new NetworkStream (socket, false);
            if (!secure)
            {
                _stream = netStream;
            }
            else
            {
                var sslStream = new SslStream (netStream, false);
                sslStream.AuthenticateAsServer (cert);
                _stream = sslStream;
            }

            _timer = new Timer (OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init ();
        }
        internal HttpConnection(Socket socket, EndPointListener listener)
        {
            _socket = socket;
              _listener = listener;
              _secure = listener.IsSecure;

              var netStream = new NetworkStream (socket, false);
              if (_secure) {
            var conf = listener.SslConfiguration;
            var sslStream = new SslStream (netStream, false, conf.ClientCertificateValidationCallback);
            sslStream.AuthenticateAsServer (
              conf.ServerCertificate,
              conf.ClientCertificateRequired,
              conf.EnabledSslProtocols,
              conf.CheckCertificateRevocation
            );

            _stream = sslStream;
              }
              else {
            _stream = netStream;
              }

              _sync = new object ();
              _timeout = 90000; // 90k ms for first request, 15k ms from then on.
              _timeoutCanceled = new Dictionary<int, bool> ();
              _timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite);

              init ();
        }
Esempio n. 4
0
        private bool checkCertificate(out string message)
        {
            message = null;

            var byUser = _listener.SslConfiguration.ServerCertificate != null;

            var path     = _listener.CertificateFolderPath;
            var withPort = EndPointListener.CertificateExists(_port, path);

            var either = byUser || withPort;

            if (!either)
            {
                message = "There is no server certificate for secure connection.";

                return(false);
            }

            var both = byUser && withPort;

            if (both)
            {
                var msg = "The server certificate associated with the port is used.";

                _log.Warn(msg);
            }

            return(true);
        }
Esempio n. 5
0
    public HttpConnection (Socket socket, EndPointListener listener)
    {
      _socket = socket;
      _listener = listener;
      _secure = listener.IsSecure;

      var netStream = new NetworkStream (socket, false);
      if (_secure) {
        var sslStream = new SslStream (netStream, false);
        var certificateConfig = listener.CertificateConfig;
        sslStream.AuthenticateAsServer(certificateConfig.ServerCertificate,
            certificateConfig.ClientCertificateRequired, certificateConfig.EnabledSslProtocols,
            certificateConfig.CheckCertificateRevocation);
        _stream = sslStream;
      }
      else {
        _stream = netStream;
      }

      _sync = new object ();
      _timeout = 90000; // 90k ms for first request, 15k ms from then on.
      _timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite);

      init ();
    }
Esempio n. 6
0
 public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate cert)
 {
     _sock = sock;
     _epl = epl;
     IsSecure = secure;
     _cert = cert;
     if (secure == false)
     {
         _stream = new NetworkStream(sock, false);
     }
     else
     {
     #if SSL
         ssl_stream = epl.Listener.CreateSslStream(new NetworkStream(sock, false), false, (t, c, ch, e) =>
         {
             if (c == null)
                 return true;
             var c2 = c as X509Certificate2;
             if (c2 == null)
                 c2 = new X509Certificate2(c.GetRawCertData());
             client_cert = c2;
             client_cert_errors = new int[] { (int)e };
             return true;
         });
         stream = ssl_stream.AuthenticatedStream;
     #else
         throw new Exception("SSL is not supported");
     #endif
     }
     _timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
     Init();
 }
Esempio n. 7
0
 private string checkIfCertificateExists()
 {
     return(_secure &&
            !EndPointListener.CertificateExists(_port, _listener.CertificateFolderPath) &&
            _listener.DefaultSslAuthenticationConfig == null
      ? "The secure connection requires a server certificate."
      : null);
 }
 private string checkIfCertExists()
 {
     return(_secure &&
            !EndPointListener.CertificateExists(_port, _listener.CertificateFolderPath) &&
            Certificate == null
      ? "The secure connection requires a server certificate."
      : null);
 }
        private static void OnAccept(object sender, EventArgs e)
        {
            SocketAsyncEventArgs args = (SocketAsyncEventArgs)e;
            EndPointListener     epl  = (EndPointListener)args.UserToken;

#if SSHARP
            CrestronServerSocket accepted = null;
#else
            Socket accepted = null;
#endif
            if (args.SocketError == SocketError.Success)
            {
                accepted          = args.AcceptSocket;
                args.AcceptSocket = null;
            }

            try
            {
                if (epl.sock != null)
#if SSHARP
                { epl.sock.BeginAccept(acceptCallback, Tuple.Create(epl.sock, args)); }
#else
                { epl.sock.AcceptAsync(args); }
#endif
            }
            catch
            {
                if (accepted != null)
                {
                    try
                    {
                        accepted.Close();
                    }
                    catch
                    {
                    }
                    accepted = null;
                }
            }

            if (accepted == null)
            {
                return;
            }

            if (epl.secure && (epl.cert == null || epl.key == null))
            {
                accepted.Close();
                return;
            }
            HttpConnection conn = new HttpConnection(accepted, epl, epl.secure, epl.cert, epl.key);
            lock (epl.unregistered)
            {
                epl.unregistered[conn] = conn;
            }
            conn.BeginReadRequest();
        }
Esempio n. 10
0
        internal static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
        {
            if (IPToEndpoints.TryGetValue(ep.Address, out var p))
            {
                if (p.TryRemove(ep.Port, out _) && p.Count == 0)
                {
                    IPToEndpoints.TryRemove(ep.Address, out _);
                }
            }

            epl.Dispose();
        }
Esempio n. 11
0
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (ip_to_endpoints) {
         // Dictionary<int, EndPointListener> p
         Hashtable p = null;
         p = (Hashtable) ip_to_endpoints [ep.Address];
         p.Remove (ep.Port);
         if (p.Count == 0) {
             ip_to_endpoints.Remove (ep.Address);
         }
         epl.Close ();
     }
 }
 public static void RemoveEndPoint(EndPointListener epl, IPEndPoint ep)
 {
     lock (ip_to_endpoints)
     {
         // Dictionary<int, EndPointListener> p
         Hashtable p = null;
         p = (Hashtable)ip_to_endpoints[ep.Address];
         p.Remove(ep.Port);
         if (p.Count == 0)
         {
             ip_to_endpoints.Remove(ep.Address);
         }
         epl.Close();
     }
 }
Esempio n. 13
0
        private string checkIfCertificateExists()
        {
            if (!_secure)
            {
                return(null);
            }
            bool flag  = _listener.SslConfiguration.ServerCertificate != null;
            bool flag2 = EndPointListener.CertificateExists(_port, _listener.CertificateFolderPath);

            if (flag && flag2)
            {
                _logger.Warn("The server certificate associated with the port number already exists.");
                return(null);
            }
            return((flag || flag2) ? null : "The secure connection requires a server certificate.");
        }
		public HttpConnection (Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
		{
			this.sock = sock;
			this.epl = epl;
			this.secure = secure;
			this.key = key;
			if (secure == false) {
				stream = new NetworkStream (sock, false);
			} else {
				SslServerStream ssl_stream = new SslServerStream (new NetworkStream (sock, false), cert, false, true, false);
				ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
				ssl_stream.ClientCertValidationDelegate += OnClientCertificateValidation;
				stream = ssl_stream;
			}
			timer = new Timer (OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
			Init ();
		}
Esempio n. 15
0
		private string checkIfCertificateExists ()
			{
			if (!_secure)
				return null;

			var usr = _listener.SslConfiguration.ServerCertificate != null;
			var port = EndPointListener.CertificateExists (_port, _listener.CertificateFolderPath);
			if (usr && port)
				{
				_logger.Warn ("The server certificate associated with the port number already exists.");
				return null;
				}

			return !(usr || port)
					? "The secure connection requires a server certificate."
					: null;
			}
        private static void RemovePrefixInternal(string prefix, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(prefix);

            if (lp.Path.IndexOf('%') != -1)
            {
                return;
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)
            {
                return;
            }

            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.RemovePrefix(lp, listener);
        }
        private static void AddPrefixInternal(string p, HttpListener listener)
        {
            ListenerPrefix lp = new ListenerPrefix(p);

            if (lp.Path.IndexOf('%') != -1)
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            if (lp.Path.IndexOf("//", StringComparison.Ordinal) != -1)              // TODO: Code?
            {
                throw new HttpListenerException(400, "Invalid path.");
            }

            // listens on all the interfaces if host name cannot be parsed by IPAddress.
            EndPointListener epl = GetEPListener(lp.Host, lp.Port, listener, lp.Secure);

            epl.AddPrefix(lp, listener);
        }
 public HttpConnection(ILogger logger, Socket sock, EndPointListener epl, bool secure, string connectionId, X509Certificate cert)
 {
     _connectionId = connectionId;
     _logger = logger;
     this.sock = sock;
     this.epl = epl;
     this.secure = secure;
     if (secure == false)
     {
         stream = new NetworkStream(sock, false);
     }
     else
     {
         SslStream ssl_stream = new SslStream(new NetworkStream(sock, false), false);
         ssl_stream.AuthenticateAsServer(cert);
         stream = ssl_stream;
     }
     Init();
 }
Esempio n. 19
0
		static EndPointListener GetEPListener (IPAddress addr, int port, HttpListener listener, bool secure)
		{
			Dictionary<int, EndPointListener> p = null;
			if (ip_to_endpoints.ContainsKey (addr)) {
				p = ip_to_endpoints [addr];
			} else {
				p = new Dictionary<int, EndPointListener> ();
				ip_to_endpoints [addr] = p;
			}

			EndPointListener epl = null;
			if (p.ContainsKey (port)) {
				epl = p [port];
			} else {
				epl = new EndPointListener (addr, port, secure);
				p [port] = epl;
			}

			return epl;
		}
Esempio n. 20
0
    public HttpConnection (Socket socket, EndPointListener listener)
    {
      _socket = socket;
      _listener = listener;
      _secure = listener.IsSecure;

      var netStream = new NetworkStream (socket, false);
      if (_secure) {
        var sslStream = new SslStream (netStream, false);
        sslStream.AuthenticateAsServer (listener.Certificate);
        _stream = sslStream;
      }
      else {
        _stream = netStream;
      }

      _sync = new object ();
      _timeout = 90000; // 90k ms for first request, 15k ms from then on.
      _timer = new Timer (onTimeout, this, Timeout.Infinite, Timeout.Infinite);

      init ();
    }
        public HttpConnection(Socket sock, EndPointListener epl, bool secure, X509Certificate2 cert, AsymmetricAlgorithm key)
        {
            this.sock   = sock;
            this.epl    = epl;
            this.secure = secure;
            this.key    = key;
            if (secure == false)
            {
                stream = new NetworkStream(sock, false);
            }
            else
            {
#if SSL
                SslServerStream ssl_stream = new SslServerStream(new NetworkStream(sock, false), cert, false, true, false);
                ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
                ssl_stream.ClientCertValidationDelegate    += OnClientCertificateValidation;
                stream = ssl_stream;
#else
                stream = new NetworkStream(sock, false);
#endif
            }
            timer = new Timer(OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init();
        }
Esempio n. 22
0
    private static void processAccepted (Socket socket, EndPointListener listener)
    {
      HttpConnection conn = null;
      try {
        conn = new HttpConnection (socket, listener);
        lock (listener._unregisteredSync)
          listener._unregistered[conn] = conn;

        conn.BeginReadRequest ();
      }
      catch {
        if (conn != null) {
          conn.Close (true);
          return;
        }

        socket.Close ();
      }
    }
Esempio n. 23
0
    private static EndPointListener getEndPointListener (
      HttpListenerPrefix prefix, HttpListener listener)
    {
      var addr = convertToIPAddress (prefix.Host);

      Dictionary<int, EndPointListener> eps = null;
      if (_addressToEndpoints.ContainsKey (addr)) {
        eps = _addressToEndpoints[addr];
      }
      else {
        eps = new Dictionary<int, EndPointListener> ();
        _addressToEndpoints[addr] = eps;
      }

      var port = prefix.Port;

      EndPointListener lsnr = null;
      if (eps.ContainsKey (port)) {
        lsnr = eps[port];
      }
      else {
        lsnr = new EndPointListener (
          addr,
          port,
          listener.ReuseAddress,
          prefix.IsSecure,
          listener.CertificateFolderPath,
          listener.SslConfiguration);

        eps[port] = lsnr;
      }

      return lsnr;
    }
Esempio n. 24
0
 private string checkIfCertExists()
 {
     return((!this._secure || EndPointListener.CertificateExists(this._port, this._listener.CertificateFolderPath) || this.Certificate != null) ? null : "The secure connection requires a server certificate.");
 }
        private static EndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;

            if (host == "*")
            {
                addr = IPAddress.Any;
            }
#if SSHARP
            else if (IPAddressTryParser.IPAddressTryParse(host, out addr) == false)
#else
            else if (IPAddress.TryParse(host, out addr) == false)
#endif
            {
                try
                {
#if SSHARP
                    IPAddress[] addrs = DnsEx.GetHostAddresses(host);
                    if (addrs.Length != 0)
                    {
                        addr = addrs[0];
                    }
#else
                    IPHostEntry iphost = Dns.GetHostByName(host);
                    if (iphost != null)
                    {
                        addr = iphost.AddressList[0];
                    }
#endif
                    else
                    {
                        addr = IPAddress.Any;
                    }
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;             // Dictionary<int, EndPointListener>
            if (ip_to_endpoints.ContainsKey(addr))
            {
                p = (Hashtable)ip_to_endpoints[addr];
            }
            else
            {
                p = new Hashtable();
                ip_to_endpoints[addr] = p;
            }

            EndPointListener epl = null;
            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl     = new EndPointListener(addr, port, secure);
                p[port] = epl;
            }

            return(epl);
        }
Esempio n. 26
0
        private static EndPointListener GetEpListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;
            if (host == "*")
                addr = IPAddress.Any;
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try
                {
                    var iphost = new IPHostEntry
                    {
                        HostName = host,
                        AddressList = Dns.GetHostAddressesAsync(host).Result
                    };

                    addr = iphost.AddressList[0];
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;  // Dictionary<int, EndPointListener>
            if (_ipToEndpoints.ContainsKey(addr))
            {
                p = (Hashtable)_ipToEndpoints[addr];
            }
            else
            {
                p = new Hashtable();
                _ipToEndpoints[addr] = p;
            }

            EndPointListener epl;
            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl = new EndPointListener(listener, addr, port, secure);
                p[port] = epl;
            }

            return epl;
        }
Esempio n. 27
0
        public static void RemoveEndPoint(EndPointListener epListener, IPEndPoint endpoint)
        {
            lock (((ICollection) _ipToEndpoints).SyncRoot) {
            var eps = _ipToEndpoints[endpoint.Address];
            eps.Remove (endpoint.Port);
            if (eps.Count == 0)
              _ipToEndpoints.Remove (endpoint.Address);

            epListener.Close ();
              }
        }
Esempio n. 28
0
        private static EndPointListener getEndPointListener(
      IPAddress address, int port, bool secure, HttpListener httpListener)
        {
            Dictionary<int, EndPointListener> eps = null;
              if (_ipToEndpoints.ContainsKey (address)) {
            eps = _ipToEndpoints[address];
              }
              else {
            eps = new Dictionary<int, EndPointListener> ();
            _ipToEndpoints[address] = eps;
              }

              EndPointListener epl = null;
              if (eps.ContainsKey (port)) {
            epl = eps[port];
              }
              else {
            epl = new EndPointListener (
              address,
              port,
              secure,
              httpListener.CertificateFolderPath,
              httpListener.DefaultCertificate,
              httpListener.ReuseAddress);

            eps[port] = epl;
              }

              return epl;
        }
        public HttpConnection(
			Socket              sock,
			EndPointListener    epl,
			bool                secure,
			X509Certificate2    cert,
			AsymmetricAlgorithm key
		)
        {
            this.sock   = sock;
            this.epl    = epl;
            this.secure = secure;
            this.key    = key;
            //			if (secure == false) {
            //				stream = new NetworkStream (sock, false);
            //			} else {
            //				var ssl_stream = new SslServerStream (new NetworkStream (sock, false), cert, false, false);
            //				ssl_stream.PrivateKeyCertSelectionDelegate += OnPVKSelection;
            //				stream = ssl_stream;
            //			}
            var net_stream = new NetworkStream (sock, false);
            if (!secure) {
                stream = net_stream;
            } else {
                var ssl_stream = new SslStream(net_stream, false);
                ssl_stream.AuthenticateAsServer(cert);
                stream = ssl_stream;
            }
            timer = new Timer (OnTimeout, null, Timeout.Infinite, Timeout.Infinite);
            Init ();
        }
Esempio n. 30
0
        private static EndPointListener getEndPointListener(
      string host, int port, HttpListener listener, bool secure)
        {
            var addr = convertToAddress (host);

              Dictionary<int, EndPointListener> eps = null;
              if (_addressToEndpoints.ContainsKey (addr)) {
            eps = _addressToEndpoints[addr];
              }
              else {
            eps = new Dictionary<int, EndPointListener> ();
            _addressToEndpoints[addr] = eps;
              }

              EndPointListener lsnr = null;
              if (eps.ContainsKey (port)) {
            lsnr = eps[port];
              }
              else {
            lsnr = new EndPointListener (
              addr,
              port,
              secure,
              listener.CertificateFolderPath,
              listener.SslConfiguration,
              listener.ReuseAddress);

            eps[port] = lsnr;
              }

              return lsnr;
        }
Esempio n. 31
0
        internal static void RemoveEndPoint(EndPointListener listener)
        {
            lock (((ICollection) _addressToEndpoints).SyncRoot) {
            var addr = listener.Address;
            var eps = _addressToEndpoints[addr];
            eps.Remove (listener.Port);
            if (eps.Count == 0)
              _addressToEndpoints.Remove (addr);

            listener.Close ();
              }
        }
Esempio n. 32
0
        static EndPointListener GetEPListener(string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;
            if (IPAddress.TryParse(host, out addr) == false)
                addr = IPAddress.Any;

            Hashtable p = null;  // Dictionary<int, EndPointListener>
            if (ip_to_endpoints.ContainsKey (addr)) {
                p = (Hashtable) ip_to_endpoints [addr];
            } else {
                p = new Hashtable ();
                ip_to_endpoints [addr] = p;
            }

            EndPointListener epl = null;
            if (p.ContainsKey (port)) {
                epl = (EndPointListener) p [port];
            } else {
                epl = new EndPointListener (addr, port, secure);
                p [port] = epl;
            }

            return epl;
        }
        static EndPointListener GetEPListener(ILogger logger, string host, int port, HttpListener listener, bool secure)
        {
            IPAddress addr;
            if (host == "*" || host == "+")
                addr = IPAddress.Any;
            else if (IPAddress.TryParse(host, out addr) == false)
            {
                try
                {
                    IPHostEntry iphost = Dns.GetHostByName(host);
                    if (iphost != null)
                        addr = iphost.AddressList[0];
                    else
                        addr = IPAddress.Any;
                }
                catch
                {
                    addr = IPAddress.Any;
                }
            }
            Hashtable p = null;  // Dictionary<int, EndPointListener>
            if (ip_to_endpoints.ContainsKey(addr))
            {
                p = (Hashtable)ip_to_endpoints[addr];
            }
            else
            {
                p = new Hashtable();
                ip_to_endpoints[addr] = p;
            }

            EndPointListener epl = null;
            if (p.ContainsKey(port))
            {
                epl = (EndPointListener)p[port];
            }
            else
            {
                epl = new EndPointListener(logger, addr, port, secure, listener.CertificateLocation);
                p[port] = epl;
            }

            return epl;
        }
Esempio n. 34
0
    public HttpConnection (
      Socket socket,
      EndPointListener listener,
      bool secure,
      X509Certificate2 cert)
    {
      _socket = socket;
      _epListener = listener;
      _secure = secure;

      var netStream = new NetworkStream (socket, false);
      if (!secure)
        _stream = netStream;
      else {
        var sslStream = new SslStream (netStream, false);
        sslStream.AuthenticateAsServer (cert);
        _stream = sslStream;
      }

      _timeout = 90000; // 90k ms for first request, 15k ms from then on.
      _timer = new Timer (onTimeout, null, Timeout.Infinite, Timeout.Infinite);
      init ();
    }
Esempio n. 35
0
		public static void RemoveEndPoint (EndPointListener epl, IPEndPoint ep)
		{
			lock (((ICollection)ip_to_endpoints).SyncRoot) {
				Dictionary<int, EndPointListener> p = null;
				p = ip_to_endpoints [ep.Address];
				p.Remove (ep.Port);
				if (p.Count == 0) {
					ip_to_endpoints.Remove (ep.Address);
				}
				epl.Close ();
			}
		}