Search in sources :

Example 1 with RemoteSource

use of org.spongepowered.api.command.source.RemoteSource in project LanternServer by LanternPowered.

the class LanternContextCalculator method buildAddressCache.

private LoadingCache<RemoteSource, Set<Context>> buildAddressCache(final String contextKey, final Function<RemoteSource, InetAddress> function) {
    return Caffeine.newBuilder().weakKeys().build(key -> {
        final ImmutableSet.Builder<Context> builder = ImmutableSet.builder();
        final InetAddress address = checkNotNull(function.apply(key), "address");
        builder.add(new Context(contextKey, address.getHostAddress()));
        for (String set : Maps.filterValues(Lantern.getGame().getGlobalConfig().getIpSets(), input -> input.test(address)).keySet()) {
            builder.add(new Context(contextKey, set));
        }
        return builder.build();
    });
}
Also used : Context(org.spongepowered.api.service.context.Context) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) ImmutableSet(com.google.common.collect.ImmutableSet) LoadingCache(com.github.benmanes.caffeine.cache.LoadingCache) CommandSource(org.spongepowered.api.command.CommandSource) Context(org.spongepowered.api.service.context.Context) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Maps(com.google.common.collect.Maps) Function(java.util.function.Function) RemoteSource(org.spongepowered.api.command.source.RemoteSource) InetAddress(java.net.InetAddress) ContextCalculator(org.spongepowered.api.service.context.ContextCalculator) Locatable(org.spongepowered.api.world.Locatable) Lantern(org.lanternpowered.server.game.Lantern) World(org.spongepowered.api.world.World) Optional(java.util.Optional) Subject(org.spongepowered.api.service.permission.Subject) ImmutableSet(com.google.common.collect.ImmutableSet) InetAddress(java.net.InetAddress)

Example 2 with RemoteSource

use of org.spongepowered.api.command.source.RemoteSource in project LanternServer by LanternPowered.

the class LanternContextCalculator method accumulateContexts.

@Override
public void accumulateContexts(Subject subject, Set<Context> accumulator) {
    final Optional<CommandSource> subjSource = subject.getCommandSource();
    if (subjSource.isPresent()) {
        final CommandSource source = subjSource.get();
        if (source instanceof Locatable) {
            final World currentExt = ((Locatable) source).getWorld();
            accumulator.add(currentExt.getContext());
            accumulator.add((currentExt.getDimension().getContext()));
        }
        if (source instanceof RemoteSource) {
            final RemoteSource rem = (RemoteSource) source;
            accumulator.addAll(this.remoteIpCache.get(rem));
            accumulator.addAll(this.localIpCache.get(rem));
            accumulator.add(new Context(Context.LOCAL_PORT_KEY, String.valueOf(rem.getConnection().getVirtualHost().getPort())));
            accumulator.add(new Context(Context.LOCAL_HOST_KEY, rem.getConnection().getVirtualHost().getHostName()));
        }
    }
}
Also used : Context(org.spongepowered.api.service.context.Context) RemoteSource(org.spongepowered.api.command.source.RemoteSource) CommandSource(org.spongepowered.api.command.CommandSource) World(org.spongepowered.api.world.World) Locatable(org.spongepowered.api.world.Locatable)

Example 3 with RemoteSource

use of org.spongepowered.api.command.source.RemoteSource in project SpongeCommon by SpongePowered.

the class SpongeContextCalculator method accumulateContexts.

@Override
public void accumulateContexts(Subject subject, Set<Context> accumulator) {
    Optional<CommandSource> subjSource = subject.getCommandSource();
    if (subjSource.isPresent()) {
        CommandSource source = subjSource.get();
        if (source instanceof Locatable) {
            World currentExt = ((Locatable) source).getWorld();
            accumulator.add(currentExt.getContext());
            accumulator.add((currentExt.getDimension().getContext()));
        }
        if (source instanceof RemoteSource) {
            RemoteSource rem = (RemoteSource) source;
            accumulator.addAll(this.remoteIpCache.getUnchecked(rem));
            accumulator.addAll(this.localIpCache.getUnchecked(rem));
            accumulator.add(new Context(Context.LOCAL_PORT_KEY, String.valueOf(rem.getConnection().getVirtualHost().getPort())));
            accumulator.add(new Context(Context.LOCAL_HOST_KEY, rem.getConnection().getVirtualHost().getHostName()));
        }
    }
}
Also used : Context(org.spongepowered.api.service.context.Context) RemoteSource(org.spongepowered.api.command.source.RemoteSource) CommandSource(org.spongepowered.api.command.CommandSource) World(org.spongepowered.api.world.World) Locatable(org.spongepowered.api.world.Locatable)

Aggregations

CommandSource (org.spongepowered.api.command.CommandSource)3 RemoteSource (org.spongepowered.api.command.source.RemoteSource)3 Context (org.spongepowered.api.service.context.Context)3 Locatable (org.spongepowered.api.world.Locatable)3 World (org.spongepowered.api.world.World)3 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 LoadingCache (com.github.benmanes.caffeine.cache.LoadingCache)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 InetAddress (java.net.InetAddress)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Lantern (org.lanternpowered.server.game.Lantern)1 ContextCalculator (org.spongepowered.api.service.context.ContextCalculator)1 Subject (org.spongepowered.api.service.permission.Subject)1