com.sedmelluq.lava.extensions.youtuberotator.tools.ip.Ipv6Block Java Examples

The following examples show how to use com.sedmelluq.lava.extensions.youtuberotator.tools.ip.Ipv6Block. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MusicManager.java    From Shadbot with GNU General Public License v3.0 6 votes vote down vote up
private MusicManager() {
    this.audioPlayerManager = new DefaultAudioPlayerManager();
    this.audioPlayerManager.getConfiguration().setFrameBufferFactory(NonAllocatingAudioFrameBuffer::new);
    this.audioPlayerManager.getConfiguration().setFilterHotSwapEnabled(true);
    AudioSourceManagers.registerRemoteSources(this.audioPlayerManager);
    this.guildMusics = new ConcurrentHashMap<>();
    this.guildJoining = new ConcurrentHashMap<>();

    //IPv6 rotation config
    final String ipv6Block = CredentialManager.getInstance().get(Credential.IPV6_BLOCK);
    if (!Config.IS_SNAPSHOT && ipv6Block != null && !ipv6Block.isBlank()) {
        LOGGER.info("Configuring YouTube IP rotator");
        final List<IpBlock> blocks = Collections.singletonList(new Ipv6Block(ipv6Block));
        final AbstractRoutePlanner planner = new RotatingNanoIpRoutePlanner(blocks);

        new YoutubeIpRotatorSetup(planner)
                .forSource(this.audioPlayerManager.source(YoutubeAudioSourceManager.class))
                .setup();
    }
}
 
Example #2
Source File: RotatingNanoIpRoutePlanner.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private InetAddress extractLocalAddress() {
  InetAddress localAddress;
  long triesSinceBlockSkip = 0;
  BigInteger it = BigInteger.valueOf(0);
  do {
    try {
      if (ipBlock.getSize().multiply(BigInteger.valueOf(2)).compareTo(it) < 0) {
        throw new RuntimeException("Can't find a free ip");
      }
      it = it.add(BigInteger.ONE);
      triesSinceBlockSkip++;
      if (triesSinceBlockSkip > 128) {
        this.currentBlock.accumulateAndGet(BigInteger.ONE, BigInteger::add);
      }
      final BigInteger nanoTime = BigInteger.valueOf(System.nanoTime());
      final BigInteger timeOffset = nanoTime.subtract(blockNanoStart.get());
      final BigInteger blockOffset = currentBlock.get().multiply(Ipv6Block.BLOCK64_IPS);
      localAddress = ipBlock.getAddressAtIndex(blockOffset.add(timeOffset));
    } catch (final IllegalArgumentException ex) {
      this.currentBlock.set(BigInteger.ZERO);
      localAddress = null;
    }
  } while (localAddress == null || !ipFilter.test(localAddress) || !isValidAddress(localAddress));
  return localAddress;
}
 
Example #3
Source File: NanoIpRoutePlanner.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
public NanoIpRoutePlanner(final List<IpBlock> ipBlocks, final boolean handleSearchFailure) {
  super(ipBlocks, handleSearchFailure);
  if (ipBlock.getSize().compareTo(Ipv6Block.BLOCK64_IPS) < 0)
    throw new IllegalArgumentException("Nano IP Route planner requires an IPv6Block which is greater or equal to a /64");
  startTime = BigInteger.valueOf(System.nanoTime());
  maskBits = ipBlock.getMaskBits();
}
 
Example #4
Source File: NanoIpRoutePlanner.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private InetAddress getAddress() {
  final BigInteger now = BigInteger.valueOf(System.nanoTime());
  final BigInteger nanoOffset = now.subtract(startTime); // least 64 bit
  if(maskBits == 64) {
    return ipBlock.getAddressAtIndex(nanoOffset);
  }
  final BigInteger randomOffset = random.nextBigInt(Ipv6Block.IPV6_BIT_SIZE - maskBits).shiftLeft(Ipv6Block.IPV6_BIT_SIZE - maskBits); // most {{maskBits}}-64 bit
  return ipBlock.getAddressAtIndex(randomOffset.add(nanoOffset));
}
 
Example #5
Source File: RotatingIpRoutePlanner.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private InetAddress extractLocalAddress() {
  InetAddress localAddress;
  long triesSinceBlockSkip = 0;
  BigInteger it = BigInteger.valueOf(0);
  do {
    if (ipBlock.getSize().multiply(BigInteger.valueOf(2)).compareTo(it) < 0) {
      throw new RuntimeException("Can't find a free ip");
    }
    if (ipBlock.getSize().compareTo(BigInteger.valueOf(128)) > 0)
      index.accumulateAndGet(BigInteger.valueOf(random.nextInt(10) + 10), BigInteger::add);
    else
      index.accumulateAndGet(BigInteger.ONE, BigInteger::add);
    it = it.add(BigInteger.ONE);
    triesSinceBlockSkip++;
    if (ipBlock.getSize().compareTo(Ipv6Block.BLOCK64_IPS) > 0 && triesSinceBlockSkip > 128) {
      triesSinceBlockSkip = 0;
      rotateIndex.accumulateAndGet(Ipv6Block.BLOCK64_IPS.add(BigInteger.valueOf(random.nextLong())), BigInteger::add);
    }
    try {
      localAddress = ipBlock.getAddressAtIndex(index.get().subtract(BigInteger.ONE));
    } catch (final Exception ex) {
      index.set(BigInteger.ZERO);
      localAddress = null;
    }
  } while (localAddress == null || !ipFilter.test(localAddress) || !isValidAddress(localAddress));
  return localAddress;
}
 
Example #6
Source File: RotatingNanoIpRoutePlanner.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
public RotatingNanoIpRoutePlanner(final List<IpBlock> ipBlocks, final Predicate<InetAddress> ipFilter, final boolean handleSearchFailure) {
  super(ipBlocks, handleSearchFailure);
  this.ipFilter = ipFilter;
  this.currentBlock = new AtomicReference<>(BigInteger.ZERO);
  this.blockNanoStart = new AtomicReference<>(BigInteger.valueOf(System.nanoTime()));
  this.next = new AtomicBoolean(false);
  if (ipBlock.getType() != Inet6Address.class || ipBlock.getSize().compareTo(Ipv6Block.BLOCK64_IPS) < 0)
    throw new IllegalArgumentException("Please use a bigger IPv6 Block!");
}
 
Example #7
Source File: MantaroAudioManager.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public MantaroAudioManager() {
    this.musicManagers = new ConcurrentHashMap<>();
    this.playerManager = new DefaultAudioPlayerManager();

    //Youtube is special because rotation stuff.
    YoutubeAudioSourceManager youtubeAudioSourceManager = new YoutubeAudioSourceManager(true);

    //IPv6 rotation config start
    Config config = MantaroData.config().get();
    if (!config.getIpv6Block().isEmpty()) {
        AbstractRoutePlanner planner;
        String block = config.getIpv6Block();
        List<IpBlock> blocks = Collections.singletonList(new Ipv6Block(block));

        //Damn you, YouTube.
        if (config.getExcludeAddress().isEmpty())
            planner = new RotatingNanoIpRoutePlanner(blocks);
        else {
            try {
                InetAddress blacklistedGW = InetAddress.getByName(config.getExcludeAddress());
                planner = new RotatingNanoIpRoutePlanner(blocks, inetAddress -> !inetAddress.equals(blacklistedGW));
            } catch (Exception e) {
                //Fallback: did I screw up putting the IP in? lmao
                planner = new RotatingNanoIpRoutePlanner(blocks);
                e.printStackTrace();
            }
        }

        new YoutubeIpRotatorSetup(planner)
                .forSource(youtubeAudioSourceManager)
                .setup();
    }
    //IPv6 rotation config end

    //Register source manager and configure the Player
    playerManager.registerSourceManager(youtubeAudioSourceManager);
    playerManager.registerSourceManager(SoundCloudAudioSourceManager.createDefault());
    playerManager.registerSourceManager(new BandcampAudioSourceManager());
    playerManager.registerSourceManager(new VimeoAudioSourceManager());
    playerManager.registerSourceManager(new TwitchStreamAudioSourceManager());
    playerManager.registerSourceManager(new BeamAudioSourceManager());
    if (!ExtraRuntimeOptions.DISABLE_NON_ALLOCATING_BUFFER) {
        log.info("Enabled non-allocating audio buffer.");
        playerManager.getConfiguration().setFrameBufferFactory(NonAllocatingAudioFrameBuffer::new);
    }
}