Ejemplo n.º 1
0
        /// <summary>
        /// Determines whether client is authorized to invoke the <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHub" /> method.
        /// </summary>
        /// <param name="hubIncomingInvokerContext">An <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHubIncomingInvokerContext" /> providing details regarding the <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHub" /> method invocation.</param>
        /// <param name="appliesToMethod">Indicates whether the interface instance is an attribute applied directly to a method.</param>
        /// <returns>
        /// true if the caller is authorized to invoke the <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHub" /> method; otherwise, false.
        /// </returns>
        public override bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod)
        {
            if (IsAnonymousEnabled)
            {
                return(true);
            }

            if (!base.AuthorizeHubMethodInvocation(hubIncomingInvokerContext, appliesToMethod))
            {
                return(false);
            }

            if (appliesToMethod)
            {
                if (_authorizer == null)
                {
                    return(true);
                }

                IPrincipal           user = hubIncomingInvokerContext.Hub.Context.User;
                IEnumerable <string> keys = GetResourceKeys(hubIncomingInvokerContext);

                return(!keys.Any((key) => Blacklist.Contains(key)) && keys.All((key) =>
                                                                               WhiteList.Contains(key) || _authorizer.HasClaim(
                                                                                   new AuthorizationContext(user, key, PermissionType))));
            }

            return(true);
        }
        public bool removeException(string exception)
        {
            bool removed = false;

            if (WhiteList.Contains(exception.Trim().ToLower()))
            {
                WhiteList.Remove(exception.Trim().ToLower());
                removed = true;
            }
            return(removed);
        }
 public void addException(string exception, bool filter = false, int filterLength = -1)
 {
     if (!WhiteList.Contains(exception.Trim().ToLower()))
     {
         WhiteList.Add(exception.Trim().ToLower());
     }
     if (filter)
     {
         if (filterLength == -1)
         {
             filterLength = exception.Length;
         }
         filterExceptions(filterLength);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 触发Socket的连接事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSocketConnect(object sender, SocketConnectEventArgs <NetState> e)
        {
            if (WhiteList.IsEnable)
            {
                var ip = e.Session.RemoteOnlyIP;
                if (!WhiteList.Contains(ip))
                {
                    e.AllowConnection = false;
                    return;
                }
            }
            var netState = new NetState(e.Session, this);

            e.Session.Data = netState;

            //  网络连接会涉及到一些业务逻辑操作,因此需要把它加到任务队列里进行处理
            //  如果不考虑业务逻辑的处理,则可以不放到任务队列,节约一下处理时间
            mainTask.AppentdTask(RunTaskNetStateConnect, netState);
        }
Ejemplo n.º 5
0
        public async Task <ICollection <IDetectionResultItem> > Scan()
        {
            //await Task.FromResult(0);

            var result = new Collection <IDetectionResultItem>();

            foreach (var parser in RegistryParsers)
            {
                foreach (var view in parser.RegistryViews)
                {
                    using (var regKey = RegistryKey.OpenBaseKey(parser.RegistryHive, view).OpenSubKey(parser.SubKey))
                    {
                        if (regKey == null || regKey.SubKeyCount <= 0)
                        {
                            continue;
                        }

                        var subKeys = regKey.GetSubKeyNames().Where(w => !WhiteList.Contains(w));

                        foreach (var item in subKeys)
                        {
                            using (var subKey = regKey.OpenSubKey(item))
                            {
                                if (subKey == null)
                                {
                                    continue;
                                }

                                var value = subKey.GetValue(KEY_NAME_TEXT, null) as string;
                                if (string.IsNullOrEmpty(value))
                                {
                                    value = "(none)";
                                }

                                result.Add(new AdvancedOptionsResult(AdvancedOptionsResultType.Default, parser.RegistryHive, subKey.View, subKey.Name, KEY_NAME_TEXT, value));
                            }
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        public void CheckPermissions(string userId)
        {
            if (IsBlackListActive && BlackList.Contains(userId))
            {
                throw new Exception(ChatNetworkModule.ErrMsg_UserInBlackList);
            }

            if (!WhiteList.Contains(userId))
            {
                if (IsWhiteListActive)
                {
                    throw new Exception(ChatNetworkModule.ErrMsg_UserInNotInWhiteList);
                }
                else
                {
                    throw new Exception(ChatNetworkModule.ErrMsg_UserDontHaveInvite);
                }
            }
        }
 private bool IsWhiteListed(string value)
 {
     return(WhiteList.Contains(value) ||
            WhitelistPrefixes.Any(preFix => value.StartsWith(preFix, StringComparison.InvariantCultureIgnoreCase)));
 }
 public bool IsExplicitlyAllowed(string value)
 {
     return(WhiteList.Contains(value));
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Method for handling a login request for a new client.
        /// </summary>
        /// <param name="id">The ID of the client.</param>
        /// <param name="endPoint">The IP endpoint of the client.</param>
        /// <param name="loginRequest">The LoginRequest packet data.</param>
        /// <param name="updateManager">The update manager for the client.</param>
        /// <returns>true if the login request was approved, false otherwise.</returns>
        private bool OnLoginRequest(
            ushort id,
            IPEndPoint endPoint,
            LoginRequest loginRequest,
            ServerUpdateManager updateManager
            )
        {
            Logger.Get().Info(this, $"Received login request from IP: {endPoint.Address}, username: {loginRequest.Username}");

            if (_banList.IsIpBanned(endPoint.Address.ToString()) || _banList.Contains(loginRequest.AuthKey))
            {
                updateManager.SetLoginResponse(new LoginResponse {
                    LoginResponseStatus = LoginResponseStatus.Banned
                });
                return(false);
            }

            if (_whiteList.IsEnabled)
            {
                if (!_whiteList.Contains(loginRequest.AuthKey))
                {
                    if (!_whiteList.IsPreListed(loginRequest.Username))
                    {
                        updateManager.SetLoginResponse(new LoginResponse {
                            LoginResponseStatus = LoginResponseStatus.NotWhiteListed
                        });
                        return(false);
                    }

                    Logger.Get().Info(this, "  Username was pre-listed, auth key has been added to whitelist");

                    _whiteList.Add(loginRequest.AuthKey);
                    _whiteList.RemovePreList(loginRequest.Username);
                }
            }

            // Check whether the username is not already in use
            foreach (var existingPlayerData in _playerData.GetCopy().Values)
            {
                if (existingPlayerData.Username.ToLower().Equals(loginRequest.Username.ToLower()))
                {
                    updateManager.SetLoginResponse(new LoginResponse {
                        LoginResponseStatus = LoginResponseStatus.InvalidUsername
                    });
                    return(false);
                }
            }

            var addonData = loginRequest.AddonData;

            // Construct a string that contains all addons and respective versions by mapping the items in the addon data
            var addonStringList = string.Join(", ", addonData.Select(addon => $"{addon.Identifier} v{addon.Version}"));

            Logger.Get().Info(this, $"  Client tries to connect with following addons: {addonStringList}");

            // If there is a mismatch between the number of networked addons of the client and the server,
            // we can immediately invalidate the request
            if (addonData.Count != AddonManager.GetNetworkedAddonData().Count)
            {
                HandleInvalidLoginAddons(updateManager);
                return(false);
            }

            // Create a byte list denoting the order of the addons on the server
            var addonOrder = new List <byte>();

            foreach (var addon in addonData)
            {
                // Check and retrieve the server addon with the same name and version
                if (!AddonManager.TryGetNetworkedAddon(
                        addon.Identifier,
                        addon.Version,
                        out var correspondingServerAddon
                        ))
                {
                    // There was no corresponding server addon, so we send a login response with an invalid status
                    // and the addon data that is present on the server, so the client knows what is invalid
                    HandleInvalidLoginAddons(updateManager);
                    return(false);
                }

                if (!correspondingServerAddon.Id.HasValue)
                {
                    continue;
                }

                // If the addon is also present on the server, we append the addon order with the correct index
                addonOrder.Add(correspondingServerAddon.Id.Value);
            }

            var loginResponse = new LoginResponse {
                LoginResponseStatus = LoginResponseStatus.Success,
                AddonOrder          = addonOrder.ToArray()
            };

            updateManager.SetLoginResponse(loginResponse);

            // Create new player data and store it
            var playerData = new ServerPlayerData(
                id,
                endPoint.Address.ToString(),
                loginRequest.Username,
                loginRequest.AuthKey,
                _authorizedList
                );

            _playerData[id] = playerData;

            return(true);
        }
 public bool containsException(string exception)
 {
     return(WhiteList.Contains(exception.Trim().ToLower()));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Determines whether the specified criteria is allowed.
 /// </summary>
 /// <param name="criteria">The criteria.</param>
 /// <returns>
 ///   <c>true</c> if the specified criteria is allowed; otherwise, <c>false</c>.
 /// </returns>
 public bool IsAllowed(TCriteria criteria)
 {
     return(WhiteList
            .Contains(criteria));
 }