Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сделать загрузку капчи асинхронной #115

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions proxy/src/main/java/net/md_5/bungee/BungeeCord.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public class BungeeCord extends ProxyServer
* Current operation state.
*/
public volatile boolean isRunning;
//BotFilter
@Getter
private volatile boolean enabled;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why volatile? I don't see the use of it in this scenario

/**
* Configuration.
*/
Expand Down Expand Up @@ -334,6 +337,7 @@ public void run()
independentThreadStop( getTranslation( "restart" ), false );
}
} );
enabled = true;
}

public void startListeners()
Expand Down
8 changes: 6 additions & 2 deletions proxy/src/main/java/ru/leymooo/botfilter/BotFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.netty.HandlerBoss;
import net.md_5.bungee.protocol.Protocol;
import ru.leymooo.botfilter.caching.CachedCaptcha;
import ru.leymooo.botfilter.caching.PacketUtils;
import ru.leymooo.botfilter.caching.PacketUtils.KickType;
import ru.leymooo.botfilter.captcha.CaptchaGeneration;
import ru.leymooo.botfilter.captcha.CaptchaGenerationException;
import ru.leymooo.botfilter.config.Settings;
import ru.leymooo.botfilter.utils.GeoIp;
import ru.leymooo.botfilter.utils.ManyChecksUtils;
Expand Down Expand Up @@ -74,9 +74,13 @@ public BotFilter(boolean startup)
Settings.IMP.reload( new File( "BotFilter", "config.yml" ) );
Scoreboard.DISABLE_DUBLICATE = Settings.IMP.FIX_SCOREBOARD_TEAMS;
checkForUpdates( startup );
if ( !CachedCaptcha.generated )
//Глушим исключение при попытке сгенерировать капчу. Если капча уже генерируется и бот фильтр был почему-то
//перезапущен, капча сгенерируется все равно и доступ к ней будет прежний.
try
{
CaptchaGeneration.generateImages();
} catch ( CaptchaGenerationException ignored )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't just ignore Exceptions. Either provide a config option for that or print a message every time

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't just ignore Exceptions. Either provide a config option for that or print a message every time

The main reason why the exception is ignored here is that when the user executes the /botfilter reload command, it may happen that at that moment the captcha is already in the process of being generated (for example, execute the /botfilter reload command twice), in this case it makes no sense to show the stacktrace to the user in which has no information at all.
I think this problem can be solved if you forcibly stop the generation and start it again

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright

{
}
normalState = getCheckState( Settings.IMP.PROTECTION.NORMAL );
attackState = getCheckState( Settings.IMP.PROTECTION.ON_ATTACK );
Expand Down
13 changes: 13 additions & 0 deletions proxy/src/main/java/ru/leymooo/botfilter/BotFilterCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;
import ru.leymooo.botfilter.captcha.CaptchaGeneration;
import ru.leymooo.botfilter.captcha.CaptchaGenerationException;
import ru.leymooo.botfilter.config.Settings;

public class BotFilterCommand extends Command
Expand All @@ -39,6 +41,7 @@ public void execute(CommandSender sender, String[] args)
sender.sendMessage( "§r> §lbotfilter stat §6- §aПоказать статистику" );
sender.sendMessage( "§r> §lbotfilter export §6- §aВыгрузить список игроков, которые прошли проверку" );
sender.sendMessage( "§r> §lbotfilter protection on/off §6- §aВключить или выключить ручной режим 'под атакой'" );
sender.sendMessage( "§r> §lbotfilter generate §6- §aСгенерировать новую капчу" );
sender.sendMessage( "§r--------------- §bBotFilter §r-----------------" );
} else if ( args[0].equalsIgnoreCase( "reload" ) )
{
Expand All @@ -52,6 +55,16 @@ public void execute(CommandSender sender, String[] args)
{
export( sender, args );
sender.sendMessage( "§aКоманда выполнена" );
} else if ( args[0].equalsIgnoreCase( "generate" ) )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what is the command? The captchas are being generated from the startup anyway

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For what is the command? The captchas are being generated from the startup anyway

Custom ability to regenerate captcha at any time. There is an option for automatic regeneration, so I thought it would be nice to manually regenerate when needed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you should really add this. Some people might don't get it and just spam the command because nothing happens. You could at least provide some type of boolean so that you can't spam it until it is done.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if you should really add this. Some people might don't get it and just spam the command because nothing happens. You could at least provide some type of boolean so that you can't spam it until it is done.

There is an exception interception, and an error message output to the user if the captcha is already in the process of being generated. The command is only for the console, players cannot execute it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ok, sorry for that haha

{
try
{
CaptchaGeneration.generateImages();
sender.sendMessage( "§aКоманда выполнена" );
} catch ( CaptchaGenerationException e )
{
sender.sendMessage( "§cОшибка при попытке сгенерировать капчу: " + e.getMessage() );
}
} else if ( args[0].equalsIgnoreCase( "protection" ) )
{
if ( args.length >= 2 )
Expand Down
25 changes: 22 additions & 3 deletions proxy/src/main/java/ru/leymooo/botfilter/BotFilterThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import ru.leymooo.botfilter.BotFilter.CheckState;
import ru.leymooo.botfilter.caching.PacketUtils.KickType;
import ru.leymooo.botfilter.caching.PacketsPosition;
import ru.leymooo.botfilter.captcha.CaptchaGeneration;
import ru.leymooo.botfilter.captcha.CaptchaGenerationException;
import ru.leymooo.botfilter.config.Settings;
import ru.leymooo.botfilter.utils.FailedUtils;
import ru.leymooo.botfilter.utils.ManyChecksUtils;
Expand Down Expand Up @@ -107,12 +109,13 @@ public static void startCleanUpThread()
{
Thread t = new Thread( () ->
{
byte counter = 0;
byte counterClean = 0;
int counterCaptcha = 0;
while ( !Thread.interrupted() && sleep( 5 * 1000 ) )
{
if ( ++counter == 12 )
if ( ++counterClean == 12 )
{
counter = 0;
counterClean = 0;
ManyChecksUtils.cleanUP();
if ( bungee.getBotFilter() != null )
{
Expand All @@ -132,6 +135,22 @@ public static void startCleanUpThread()
}
}
FailedUtils.flushQueue();
int captchaMin = Settings.IMP.CAPTCHA.CAPTCHA_REGENERATION_TIME;
if ( captchaMin <= 0 )
{
captchaMin = 1;
}
if ( ++counterCaptcha == ( 12 * captchaMin ) )
{
counterCaptcha = 0;

try
{
CaptchaGeneration.generateImages();
} catch ( CaptchaGenerationException ignored )
{
}
}
}
}, "CleanUp thread" );
t.setDaemon( true );
Expand Down
5 changes: 5 additions & 0 deletions proxy/src/main/java/ru/leymooo/botfilter/Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ public void sendPing()
private void sendCaptcha()
{
CaptchaHolder captchaHolder = PacketUtils.captchas.randomCaptcha();
if ( captchaHolder == null )
{
failed( KickType.FAILED_CAPTCHA, "Captcha was not generated" );
return;
}
captchaAnswer = captchaHolder.getAnswer();
channel.write( PacketUtils.getCachedPacket( PacketsPosition.SETSLOT_MAP ).get( version ), channel.voidPromise() );
captchaHolder.write( channel, version, true );
Expand Down
56 changes: 39 additions & 17 deletions proxy/src/main/java/ru/leymooo/botfilter/caching/CachedCaptcha.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,30 @@

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import net.md_5.bungee.protocol.ProtocolConstants;
import ru.leymooo.botfilter.packets.MapDataPacket;

/**
* @author Leymooo
*/
@Setter
public class CachedCaptcha
{

//уже пора с этим чтото придумать
//В принципе я вроде чтото придумал для версии под Velocity, но будет ли она?....
private static final int PACKETID_18 = 0x34;
private static final int PACKETID_19and119 = 0x24;

private static final int PACKETID_113and114and116 = 0x26;
private static final int PACKETID_115and117 = 0x27;
private static final int PACKETID_1162 = 0x25;
private final Random random = new Random();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not ThreadLocalRandom

private List<CaptchaHolder> captchas = null;


private static final Random random = new Random();

private static final CaptchaHolder[] captchas = new CaptchaHolder[900];
private static final AtomicInteger counter = new AtomicInteger();

public static boolean generated = false;

public void createCaptchaPacket(MapDataPacket map, String answer)
public static CaptchaHolder createCaptchaPacket(MapDataPacket map, String answer)
{

ByteBuf byteBuf18 = PacketUtils.createPacket( map, PACKETID_18, ProtocolConstants.MINECRAFT_1_8 );
Expand All @@ -44,14 +37,33 @@ public void createCaptchaPacket(MapDataPacket map, String answer)
ByteBuf byteBuf117 = PacketUtils.createPacket( map, PACKETID_115and117, ProtocolConstants.MINECRAFT_1_17 );
ByteBuf byteBuf119 = PacketUtils.createPacket( map, PACKETID_19and119, ProtocolConstants.MINECRAFT_1_19 );

captchas[counter.getAndIncrement()] = new CaptchaHolder( answer, byteBuf18, byteBuf19, byteBuf113, byteBuf114And116, byteBuf115, byteBuf1162, byteBuf117, byteBuf119 );
return new CaptchaHolder( answer, byteBuf18, byteBuf19, byteBuf113, byteBuf114And116, byteBuf115, byteBuf1162, byteBuf117, byteBuf119 );
}

//TODO: Do something with this shit.
public void clear()
{
if ( captchas == null )
{
return;
}
for ( CaptchaHolder holder : captchas )
{
holder.release();
}
captchas = null;
}

public CaptchaHolder randomCaptcha()
{
return captchas[random.nextInt( captchas.length )];
if ( this.captchas == null )
{
return null;
}
if ( this.captchas.size() == 0 )
{
return null;
}
return captchas.get( random.nextInt( captchas.size() ) );
}

@RequiredArgsConstructor
Expand All @@ -63,7 +75,6 @@ public static class CaptchaHolder

public void write(Channel channel, int version, boolean flush)
{

if ( version == ProtocolConstants.MINECRAFT_1_8 )
{
channel.write( buf18.retainedDuplicate(), channel.voidPromise() );
Expand Down Expand Up @@ -100,5 +111,16 @@ public void write(Channel channel, int version, boolean flush)
channel.flush();
}
}
public void release()
{
buf18.release();
buf19.release();
buf113.release();
buf114And116.release();
buf115.release();
buf1162.release();
buf117.release();
buf119.release();
}
}
}
Loading