private void Init(AuthenticationSchemes authenticationSchemes) { _authSchemes = authenticationSchemes; _listener = new TcpListener(_address, _port); _services = new WebSocketServiceManager(_fragmentSize); _state = ServerState.Ready; }
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()); }
private void init() { _authSchemes = WebSocketSharp.Net.AuthenticationSchemes.Anonymous; _listener = new TcpListener(_address, _port); _logger = new Logger(); _services = new WebSocketServiceManager(_logger); _state = ServerState.Ready; _sync = new object(); }
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 void init(string hostname, IPAddress address, int port, bool secure) { this._hostname = hostname ?? address.ToString(); this._address = address; this._port = port; this._secure = secure; this._authSchemes = WebSocketSharp.Net.AuthenticationSchemes.Anonymous; this._dnsStyle = Uri.CheckHostName(hostname) == UriHostNameType.Dns; this._listener = new TcpListener(address, port); this._logger = new Logger(); this._services = new WebSocketServiceManager(this._logger); this._sync = new object(); }
internal bool Authenticate(WebSocketSharp.Net.AuthenticationSchemes scheme, string realm, Func <IIdentity, WebSocketSharp.Net.NetworkCredential> credentialsFinder) { bool flag1; if (scheme == WebSocketSharp.Net.AuthenticationSchemes.Anonymous) { flag1 = true; } else if (scheme != WebSocketSharp.Net.AuthenticationSchemes.None) { string str = (new AuthenticationChallenge(scheme, realm)).ToString(); int num = -1; Func <bool> func = null; func = () => { bool flag; num++; if (num <= 99) { IPrincipal principal = HttpUtility.CreateUser(this._request.Headers["Authorization"], scheme, realm, this._request.HttpMethod, credentialsFinder); if ((principal == null ? false : principal.Identity.IsAuthenticated)) { this._user = principal; flag = true; } else { this.SendAuthenticationChallenge(str); flag = func(); } } else { this.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden); flag = false; } return(flag); }; flag1 = func(); } else { this.Close(WebSocketSharp.Net.HttpStatusCode.Forbidden); flag1 = false; } return(flag1); }
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()); }
/// <summary> /// Initializes a new instance of the <see cref="WebSocketServer"/> class with the specified /// <paramref name="address"/>, <paramref name="port"/>, and <paramref name="secure"/>. /// </summary> /// <remarks> /// An instance initialized by this constructor listens for the incoming connection requests /// on <paramref name="port"/>. /// </remarks> /// <param name="address"> /// A <see cref="System.Net.IPAddress"/> that represents the local IP address of the server. /// </param> /// <param name="port"> /// An <see cref="int"/> that represents the port number on which to listen. /// </param> /// <param name="certificate"> /// A <see cref="X509Certificate2"/> used to secure the connection. /// </param> /// <param name="authenticationSchemes">Supported authentication schemes.</param> /// <exception cref="ArgumentException"> /// <para> /// <paramref name="address"/> isn't a local IP address. /// </para> /// <para> /// -or- /// </para> /// <para> /// Pair of <paramref name="port"/> and <paramref name="certificate"/> is invalid. /// </para> /// </exception> /// <exception cref="ArgumentNullException"> /// <paramref name="address"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> isn't between 1 and 65535.</exception> public WebSocketServer(IPAddress address = null, int port = 80, ServerSslConfiguration certificate = null, AuthenticationSchemes authenticationSchemes = AuthenticationSchemes.Anonymous, int fragmentSize = 102392) { if (address == null) { address = IPAddress.Any; } if (!address.IsLocal()) { throw new ArgumentException("Not a local IP address: " + address, nameof(address)); } if (!port.IsPortNumber()) { throw new ArgumentOutOfRangeException("port", "Not between 1 and 65535: " + port); } var secure = certificate != null; if ((port == 80 && secure) || (port == 443 && !secure)) { throw new ArgumentException(string.Format("An invalid pair of 'port' and 'secure': {0}, {1}", port, secure)); } _address = address; _port = port; _sslConfig = certificate; _fragmentSize = fragmentSize; _secure = secure; _uri = "/".ToUri(); Init(authenticationSchemes); }