@Override
    public boolean access(CommandContext commandContext, PermissionLevel defaultCmdRequiredPermissionLevel) {
        final InetAddress inetAddress = Optional.ofNullable(commandContext.getRemote())
                .map(Channel::remoteAddress)
                .map(InetSocketAddress.class::cast)
                .map(InetSocketAddress::getAddress)
                .orElse(null);

        QosConfiguration qosConfiguration = commandContext.getQosConfiguration();
        String anonymousAllowCommands = qosConfiguration.getAnonymousAllowCommands();
        if (StringUtils.isNotEmpty(anonymousAllowCommands)
                && Arrays.stream(anonymousAllowCommands.split(","))
                        .filter(StringUtils::isNotEmpty)
                        .map(String::trim)
                        .anyMatch(cmd -> cmd.equals(commandContext.getCommandName()))) {
            return true;
        }

        PermissionLevel currentLevel = qosConfiguration.getAnonymousAccessPermissionLevel();

        // Local has private permission
        if (inetAddress != null && inetAddress.isLoopbackAddress()) {
            currentLevel = PermissionLevel.PRIVATE;
        } else if (inetAddress != null
                && qosConfiguration.getAcceptForeignIpWhitelistPredicate().test(inetAddress.getHostAddress())) {
            currentLevel = PermissionLevel.PROTECTED;
        }

        return currentLevel.getLevel() >= defaultCmdRequiredPermissionLevel.getLevel();
    }
@Test
    void testPermission() throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");

        InetSocketAddress socketAddress = Mockito.mock(InetSocketAddress.class);
        Mockito.when(socketAddress.getAddress()).thenReturn(inetAddress);
        Channel channel = Mockito.mock(Channel.class);
        Mockito.when(channel.remoteAddress()).thenReturn(socketAddress);
        CommandContext commandContext = Mockito.mock(CommandContext.class);
        Mockito.when(commandContext.getRemote()).thenReturn(channel);

        QosConfiguration qosConfiguration = Mockito.mock(QosConfiguration.class);
        Mockito.when(qosConfiguration.getAnonymousAccessPermissionLevel()).thenReturn(PermissionLevel.PUBLIC);
        Mockito.when(qosConfiguration.getAcceptForeignIpWhitelistPredicate()).thenReturn(ip -> false);

        Mockito.when(commandContext.getQosConfiguration()).thenReturn(qosConfiguration);

        DefaultAnonymousAccessPermissionChecker checker = new DefaultAnonymousAccessPermissionChecker();
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PRIVATE));

        inetAddress = InetAddress.getByName("1.1.1.1");
        Mockito.when(socketAddress.getAddress()).thenReturn(inetAddress);

        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));

        Mockito.when(qosConfiguration.getAnonymousAccessPermissionLevel()).thenReturn(PermissionLevel.PROTECTED);

        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));

        Mockito.when(qosConfiguration.getAnonymousAccessPermissionLevel()).thenReturn(PermissionLevel.NONE);

        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));

        Mockito.when(qosConfiguration.getAcceptForeignIpWhitelistPredicate()).thenReturn(ip -> true);
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));

        Mockito.when(qosConfiguration.getAcceptForeignIpWhitelistPredicate()).thenReturn(ip -> false);
        Mockito.when(qosConfiguration.getAnonymousAllowCommands()).thenReturn("test1,test2");

        Mockito.when(commandContext.getCommandName()).thenReturn("test1");
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PRIVATE));

        Mockito.when(commandContext.getCommandName()).thenReturn("test2");
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PRIVATE));

        Mockito.when(commandContext.getCommandName()).thenReturn("test");
        Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PUBLIC));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PROTECTED));
        Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));
    }