Пример #1
0
        private void processRequest(TcpListenerWebSocketContext context)
        {
            Uri requestUri = context.RequestUri;

            if (requestUri == null)
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.BadRequest);
                return;
            }
            if (_uri.IsAbsoluteUri)
            {
                string dnsSafeHost  = requestUri.DnsSafeHost;
                string dnsSafeHost2 = _uri.DnsSafeHost;
                if (Uri.CheckHostName(dnsSafeHost) == UriHostNameType.Dns && Uri.CheckHostName(dnsSafeHost2) == UriHostNameType.Dns && dnsSafeHost != dnsSafeHost2)
                {
                    context.Close(WebSocketSharp.Net.HttpStatusCode.NotFound);
                    return;
                }
            }
            if (!_services.InternalTryGetServiceHost(requestUri.AbsolutePath, out var host))
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.NotImplemented);
            }
            else
            {
                host.StartSession(context);
            }
        }
Пример #2
0
        private void processRequest(TcpListenerWebSocketContext context)
        {
            var uri = context.RequestUri;

            if (uri == null || uri.Port != _port)
            {
                context.Close(HttpStatusCode.BadRequest);
                return;
            }

            if (_dnsStyle)
            {
                var hostname = uri.DnsSafeHost;
                if (Uri.CheckHostName(hostname) == UriHostNameType.Dns && hostname != _hostname)
                {
                    context.Close(HttpStatusCode.NotFound);
                    return;
                }
            }

            WebSocketServiceHost host;

            if (!_services.InternalTryGetServiceHost(uri.AbsolutePath, out host))
            {
                context.Close(HttpStatusCode.NotImplemented);
                return;
            }

            host.StartSession(context);
        }
        private void processRequest(TcpListenerWebSocketContext context)
        {
            WebSocketServiceHost webSocketServiceHost;
            Uri requestUri = context.RequestUri;

            if ((requestUri == null ? false : requestUri.Port == this._port))
            {
                if (this._dnsStyle)
                {
                    string dnsSafeHost = requestUri.DnsSafeHost;
                    if ((Uri.CheckHostName(dnsSafeHost) != UriHostNameType.Dns ? false : dnsSafeHost != this._hostname))
                    {
                        context.Close(WebSocketSharp.Net.HttpStatusCode.NotFound);
                        return;
                    }
                }
                if (this._services.InternalTryGetServiceHost(requestUri.AbsolutePath, out webSocketServiceHost))
                {
                    webSocketServiceHost.StartSession(context);
                }
                else
                {
                    context.Close(WebSocketSharp.Net.HttpStatusCode.NotImplemented);
                }
            }
            else
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.BadRequest);
            }
        }
Пример #4
0
        private static bool authenticate(TcpListenerWebSocketContext context, WebSocketSharp.Net.AuthenticationSchemes scheme, string realm, Func <IIdentity, WebSocketSharp.Net.NetworkCredential> credentialsFinder)
        {
            string chal = ((scheme == WebSocketSharp.Net.AuthenticationSchemes.Basic) ? AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString() : ((scheme != WebSocketSharp.Net.AuthenticationSchemes.Digest) ? null : AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()));

            if (chal == null)
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                return(false);
            }
            int         retry = -1;
            Func <bool> auth  = null;

            auth = delegate
            {
                retry++;
                if (retry > 99)
                {
                    context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                    return(false);
                }
                IPrincipal principal = HttpUtility.CreateUser(context.Headers["Authorization"], scheme, realm, context.HttpMethod, credentialsFinder);
                if (principal != null && principal.Identity.IsAuthenticated)
                {
                    context.SetUser(principal);
                    return(true);
                }
                context.SendAuthenticationChallenge(chal);
                return(auth());
            };
            return(auth());
        }
Пример #5
0
        private void acceptWebSocket(TcpListenerWebSocketContext context)
        {
            var reqUri = context.RequestUri;

            if (reqUri == null)
            {
                context.Close(HttpStatusCode.BadRequest);
                return;
            }

            if (_uri.IsAbsoluteUri)
            {
                var req      = reqUri.DnsSafeHost;
                var expected = _uri.DnsSafeHost;
                if (Uri.CheckHostName(req) == UriHostNameType.Dns &&
                    Uri.CheckHostName(expected) == UriHostNameType.Dns &&
                    req != expected)
                {
                    context.Close(HttpStatusCode.NotFound);
                    return;
                }
            }

            WebSocketServiceHost host;

            if (!_services.TryGetServiceHostInternally(reqUri.AbsolutePath, out host))
            {
                context.Close(HttpStatusCode.NotImplemented);
                return;
            }

            host.StartSession(context);
        }
Пример #6
0
        private void processRequest(TcpListenerWebSocketContext context)
        {
            var uri = context.RequestUri;

            if (uri == null)
            {
                context.Close(HttpStatusCode.BadRequest);
                return;
            }

            if (!_allowForwardedRequest)
            {
                if (uri.Port != _port)
                {
                    context.Close(HttpStatusCode.BadRequest);
                    return;
                }

                if (!checkHostNameForRequest(uri.DnsSafeHost))
                {
                    context.Close(HttpStatusCode.NotFound);
                    return;
                }
            }

            WebSocketServiceHost host;

            if (!_services.InternalTryGetServiceHost(uri.AbsolutePath, out host))
            {
                context.Close(HttpStatusCode.NotImplemented);
                return;
            }

            host.StartSession(context);
        }
Пример #7
0
        private void processRequest(TcpListenerWebSocketContext context)
        {
            var uri = context.RequestUri;

            if (uri == null)
            {
                context.Close(HttpStatusCode.BadRequest);
                return;
            }

            if (_uri.IsAbsoluteUri)
            {
                var actual   = uri.DnsSafeHost;
                var expected = _uri.DnsSafeHost;
                if (Uri.CheckHostName(actual) == UriHostNameType.Dns &&
                    Uri.CheckHostName(expected) == UriHostNameType.Dns &&
                    actual != expected)
                {
                    context.Close(HttpStatusCode.NotFound);
                    return;
                }
            }

            WebSocketServiceHost host;

            if (!_services.InternalTryGetServiceHost(uri.AbsolutePath, out host))
            {
                context.Close(HttpStatusCode.NotImplemented);
                return;
            }

            host.StartSession(context);
        }
Пример #8
0
        private void processRequest(TcpListenerWebSocketContext context)
        {
            if (!authenticateClient(context))
            {
                context.Close(HttpStatusCode.Forbidden);

                return;
            }

            var uri = context.RequestUri;

            if (uri == null)
            {
                context.Close(HttpStatusCode.BadRequest);

                return;
            }

            if (!_allowForwardedRequest)
            {
                if (uri.Port != _port)
                {
                    context.Close(HttpStatusCode.BadRequest);

                    return;
                }

                if (!checkHostNameForRequest(uri.DnsSafeHost))
                {
                    context.Close(HttpStatusCode.NotFound);

                    return;
                }
            }

            var path = uri.AbsolutePath;

            if (path.IndexOfAny(new[] { '%', '+' }) > -1)
            {
                path = HttpUtility.UrlDecode(path, Encoding.UTF8);
            }

            WebSocketServiceHost host;

            if (!_services.InternalTryGetServiceHost(path, out host))
            {
                context.Close(HttpStatusCode.NotImplemented);

                return;
            }

            host.StartSession(context);
        }
Пример #9
0
        private async Task <bool> AuthenticateRequest(AuthenticationSchemes scheme, TcpListenerWebSocketContext context)
        {
            var chal = scheme == AuthenticationSchemes.Basic
                       ? AuthenticationChallenge.CreateBasicChallenge(Realm).ToBasicString()
                       : scheme == AuthenticationSchemes.Digest
                         ? AuthenticationChallenge.CreateDigestChallenge(Realm).ToDigestString()
                         : null;

            if (chal == null)
            {
                await context.Close(HttpStatusCode.Forbidden).ConfigureAwait(false);

                return(false);
            }

            var retry                = -1;
            var schm                 = scheme.ToString();
            var realm                = Realm;
            var credFinder           = UserCredentialsFinder;
            Func <Task <bool> > auth = () => Task.FromResult(false);

            auth = async() =>
            {
                var auth1 = auth;
                retry++;
                if (retry > 99)
                {
                    await context.Close(HttpStatusCode.Forbidden).ConfigureAwait(false);

                    return(false);
                }

                var res = await context.GetHeader("Authorization").ConfigureAwait(false);

                if (res == null || !res.StartsWith(schm, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthenticationChallenge(chal);
                    return(await auth1().ConfigureAwait(false));
                }

                await context.SetUser(scheme, realm, credFinder).ConfigureAwait(false);

                if (!context.IsAuthenticated)
                {
                    context.SendAuthenticationChallenge(chal);
                    return(await auth1().ConfigureAwait(false));
                }

                return(true);
            };

            return(await auth().ConfigureAwait(false));
        }
        private bool authenticateRequest(
            AuthenticationSchemes authScheme, TcpListenerWebSocketContext context)
        {
            var challenge = authScheme == AuthenticationSchemes.Basic
                    ? HttpUtility.CreateBasicAuthChallenge(Realm)
                    : authScheme == AuthenticationSchemes.Digest
                      ? HttpUtility.CreateDigestAuthChallenge(Realm)
                      : null;

            if (challenge == null)
            {
                context.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var         retry             = -1;
            var         expected          = authScheme.ToString();
            var         realm             = Realm;
            var         credentialsFinder = UserCredentialsFinder;
            Func <bool> auth = null;

            auth = () => {
                retry++;
                if (retry > 99)
                {
                    context.Close(HttpStatusCode.Forbidden);
                    return(false);
                }

                var header = context.Headers ["Authorization"];
                if (header == null ||
                    !header.StartsWith(expected, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthChallenge(challenge);
                    return(auth());
                }

                context.SetUser(authScheme, realm, credentialsFinder);
                if (context.IsAuthenticated)
                {
                    return(true);
                }

                context.SendAuthChallenge(challenge);
                return(auth());
            };

            return(auth());
        }
Пример #11
0
        private bool authenticateRequest(
            AuthenticationSchemes scheme, TcpListenerWebSocketContext context)
        {
            var chal = scheme == AuthenticationSchemes.Basic
                 ? AuthenticationChallenge.CreateBasicChallenge(Realm).ToBasicString()
                 : scheme == AuthenticationSchemes.Digest
                   ? AuthenticationChallenge.CreateDigestChallenge(Realm).ToDigestString()
                   : null;

            if (chal == null)
            {
                context.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var         retry      = -1;
            var         schm       = scheme.ToString();
            var         realm      = Realm;
            var         credFinder = UserCredentialsFinder;
            Func <bool> auth       = null;

            auth = () => {
                retry++;
                if (retry > 99)
                {
                    context.Close(HttpStatusCode.Forbidden);
                    return(false);
                }

                var res = context.Headers["Authorization"];
                if (res == null || !res.StartsWith(schm, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthenticationChallenge(chal);
                    return(auth());
                }

                context.SetUser(scheme, realm, credFinder);
                if (!context.IsAuthenticated)
                {
                    context.SendAuthenticationChallenge(chal);
                    return(auth());
                }

                return(true);
            };

            return(auth());
        }
Пример #12
0
 internal WebSocket(TcpListenerWebSocketContext context)
     : this()
 {
     _wsStream     = context.Stream;
     _closeContext = () => context.Close();
     init(context);
 }
Пример #13
0
        private static bool authenticate(
            TcpListenerWebSocketContext context,
            AuthenticationSchemes scheme,
            string realm,
            Func <IIdentity, NetworkCredential> credentialsFinder)
        {
            var chal = scheme == AuthenticationSchemes.Basic
                                           ? AuthenticationChallenge.CreateBasicChallenge(realm).ToBasicString()
                                           : scheme == AuthenticationSchemes.Digest
                                                 ? AuthenticationChallenge.CreateDigestChallenge(realm).ToDigestString()
                                                 : null;

            if (chal == null)
            {
                context.Close(HttpStatusCode.Forbidden);
                return(false);
            }

            var         retry = -1;
            Func <bool> auth  = null;

            auth = () =>
            {
                retry++;
                if (retry > 99)
                {
                    context.Close(HttpStatusCode.Forbidden);
                    return(false);
                }

                var user = HttpUtility.CreateUser(
                    context.Headers["Authorization"], scheme, realm, context.HttpMethod, credentialsFinder);

                if (user != null && user.Identity.IsAuthenticated)
                {
                    context.SetUser(user);
                    return(true);
                }

                context.SendAuthenticationChallenge(chal);
                return(auth());
            };

            return(auth());
        }
Пример #14
0
        private bool authenticateRequest(WebSocketSharp.Net.AuthenticationSchemes scheme, TcpListenerWebSocketContext context)
        {
            string challenge = (scheme != WebSocketSharp.Net.AuthenticationSchemes.Basic) ? ((scheme != WebSocketSharp.Net.AuthenticationSchemes.Digest) ? null : HttpUtility.CreateDigestAuthChallenge(this.Realm)) : HttpUtility.CreateBasicAuthChallenge(this.Realm);

            if (challenge == null)
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                return(false);
            }
            int    retry    = -1;
            string expected = scheme.ToString();
            string realm    = this.Realm;
            Func <IIdentity, WebSocketSharp.Net.NetworkCredential> credentialsFinder = this.UserCredentialsFinder;
            Func <bool> auth = null;

            auth = delegate()
            {
                retry++;
                if (retry > 99)
                {
                    context.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden);
                    return(false);
                }
                string text = context.Headers["Authorization"];
                if (text == null || !text.StartsWith(expected, StringComparison.OrdinalIgnoreCase))
                {
                    context.SendAuthChallenge(challenge);
                    return(auth());
                }
                context.SetUser(scheme, realm, credentialsFinder);
                if (context.IsAuthenticated)
                {
                    return(true);
                }
                context.SendAuthChallenge(challenge);
                return(auth());
            };
            return(auth());
        }
        private void acceptWebSocket(TcpListenerWebSocketContext context)
        {
            var path = context.Path;

            WebSocketServiceHost host;

            if (path == null ||
                !_serviceHosts.TryGetServiceHostInternally(path, out host))
            {
                context.Close(HttpStatusCode.NotImplemented);
                return;
            }

            host.StartSession(context);
        }
Пример #16
0
        private async Task ProcessWebSocketRequest(TcpListenerWebSocketContext context)
        {
            var uri = await context.GetRequestUri().ConfigureAwait(false);

            if (uri == null)
            {
                await context.Close(HttpStatusCode.BadRequest).ConfigureAwait(false);

                return;
            }

            if (_uri.IsAbsoluteUri)
            {
                var actual   = uri.DnsSafeHost;
                var expected = _uri.DnsSafeHost;
                if (Uri.CheckHostName(actual) == UriHostNameType.Dns &&
                    Uri.CheckHostName(expected) == UriHostNameType.Dns &&
                    actual != expected)
                {
                    await context.Close(HttpStatusCode.NotFound).ConfigureAwait(false);

                    return;
                }
            }

            WebSocketServiceHost host;

            if (!_services.InternalTryGetServiceHost(uri.AbsolutePath, out host))
            {
                await context.Close(HttpStatusCode.NotImplemented).ConfigureAwait(false);

                return;
            }

            await host.StartSession(context).ConfigureAwait(false);
        }
Пример #17
0
        private void acceptWebSocket(TcpListenerWebSocketContext context)
        {
            string path = context.Path;
            WebSocketServiceHost webSocketServiceHost;

            if (path == null || !this._services.TryGetServiceHostInternally(path, out webSocketServiceHost))
            {
                context.Close(WebSocketSharp.Net.HttpStatusCode.NotImplemented);
                return;
            }
            if (this._uri.IsAbsoluteUri)
            {
                context.WebSocket.Url = new Uri(this._uri, path);
            }
            webSocketServiceHost.StartSession(context);
        }