-
Notifications
You must be signed in to change notification settings - Fork 51
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
base: master
Are you sure you want to change the base?
Changes from 16 commits
9935466
da663ce
3fa23ba
346d01d
cb2aabd
9b404a1
71ba5f7
89f30c5
474e915
d8465e8
b831311
5be43a8
1ab060d
3398bbe
d6bfac4
b3e57a4
704dbe3
30fa0ba
aed7ef5
753dc9b
891717f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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" ) ) | ||
{ | ||
|
@@ -52,6 +55,16 @@ public void execute(CommandSender sender, String[] args) | |
{ | ||
export( sender, args ); | ||
sender.sendMessage( "§aКоманда выполнена" ); | ||
} else if ( args[0].equalsIgnoreCase( "generate" ) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
@@ -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 | ||
|
@@ -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() ); | ||
|
@@ -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(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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