Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.42 KB | None | 0 0
  1. package net.sf.l2j.gameserver.instancemanager;
  2.  
  3. import java.io.File;
  4. import java.io.RandomAccessFile;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9. import java.util.Calendar;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Random;
  14. import java.util.concurrent.Future;
  15.  
  16. import net.sf.l2j.commons.concurrent.ThreadPool;
  17. import net.sf.l2j.commons.lang.StringUtil;
  18.  
  19. import net.sf.l2j.Config;
  20. import net.sf.l2j.L2DatabaseFactory;
  21. import net.sf.l2j.gameserver.data.xml.MapRegionData;
  22. import net.sf.l2j.gameserver.model.actor.Creature;
  23. import net.sf.l2j.gameserver.model.actor.Player;
  24. import net.sf.l2j.gameserver.model.actor.instance.Monster;
  25. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  26. import net.sf.l2j.gameserver.network.serverpackets.PledgeCrest;
  27.  
  28. public class BotsPreventionManager
  29. {
  30. private class PlayerData
  31. {
  32. public PlayerData()
  33. {
  34. firstWindow = true;
  35. }
  36.  
  37. public int mainpattern;
  38. public List<Integer> options = new ArrayList<>();
  39. public boolean firstWindow;
  40. public int patternid;
  41. }
  42.  
  43. protected Random _randomize;
  44. protected static Map<Integer, Integer> _monsterscounter;
  45. protected static Map<Integer, Future<?>> _beginvalidation;
  46. protected static Map<Integer, PlayerData> _validation;
  47. protected static Map<Integer, byte[]> _images;
  48. protected int WINDOW_DELAY = 3; //delay used to generate new window if previous have been closed.
  49. protected int VALIDATION_TIME = Config.VALIDATION_TIME * 1000;
  50.  
  51. public static final BotsPreventionManager getInstance()
  52. {
  53. return SingletonHolder._instance;
  54. }
  55.  
  56. BotsPreventionManager()
  57. {
  58. _randomize = new Random();
  59. _monsterscounter = new HashMap<>();
  60. _beginvalidation = new HashMap<>();
  61. _validation = new HashMap<>();
  62. _images = new HashMap<>();
  63. _beginvalidation = new HashMap<>();
  64.  
  65. getimages();
  66. }
  67.  
  68. public void updatecounter(Creature player, Creature monster)
  69. {
  70. if ((player instanceof Player) && (monster instanceof Monster))
  71. {
  72. Player killer = (Player) player;
  73.  
  74. if (_validation.get(killer.getObjectId()) != null)
  75. {
  76. return;
  77. }
  78.  
  79. int count = 1;
  80. if (_monsterscounter.get(killer.getObjectId()) != null)
  81. {
  82. count = _monsterscounter.get(killer.getObjectId()) + 1;
  83. }
  84.  
  85. int next = _randomize.nextInt(Config.KILLS_COUNTER_RANDOMIZATION);
  86. if (Config.KILLS_COUNTER + next < count)
  87. {
  88. validationtasks(killer);
  89. _monsterscounter.remove(killer.getObjectId());
  90. }
  91. else
  92. {
  93. _monsterscounter.put(killer.getObjectId(), count);
  94. }
  95. }
  96. }
  97.  
  98. private static void getimages()
  99. {
  100. String CRESTS_DIR = "data/html/mods/prevention";
  101.  
  102. final File directory = new File(CRESTS_DIR);
  103. directory.mkdirs();
  104.  
  105. int i = 0;
  106. for (File file : directory.listFiles())
  107. {
  108. if (!file.getName().endsWith(".dds"))
  109. continue;
  110.  
  111. byte[] data;
  112.  
  113. try (RandomAccessFile f = new RandomAccessFile(file, "r"))
  114. {
  115. data = new byte[(int) f.length()];
  116. f.readFully(data);
  117. }
  118. catch (Exception e)
  119. {
  120. continue;
  121. }
  122. _images.put(i, data);
  123. i++;
  124. }
  125. }
  126.  
  127. public void prevalidationwindow(Player player)
  128. {
  129. NpcHtmlMessage html = new NpcHtmlMessage(1);
  130. StringBuilder tb = new StringBuilder();
  131. StringUtil.append(tb, "<html>");
  132. StringUtil.append(tb, "<title>Bots prevention</title>");
  133. StringUtil.append(tb, "<body><center><br><br><img src=\"L2UI_CH3.herotower_deco\" width=\"256\" height=\"32\">");
  134. StringUtil.append(tb, "<br><br><font color=\"a2a0a2\">if such window appears it means server suspect,<br1>that you may using cheating software.</font>");
  135. StringUtil.append(tb, "<br><br><font color=\"b09979\">if given answer results are incorrect or no action is made<br1>server is going to punish character instantly.</font>");
  136. StringUtil.append(tb, "<br><br><button value=\"CONTINUE\" action=\"bypass report_continue\" width=\"75\" height=\"21\" back=\"L2UI_CH3.Btn1_normal\" fore=\"L2UI_CH3.Btn1_normal\">");
  137. StringUtil.append(tb, "</center></body>");
  138. StringUtil.append(tb, "</html>");
  139. html.setHtml(tb.toString());
  140. player.sendPacket(html);
  141. }
  142.  
  143. private static void validationwindow(Player player)
  144. {
  145. PlayerData container = _validation.get(player.getObjectId());
  146. NpcHtmlMessage html = new NpcHtmlMessage(1);
  147.  
  148. StringBuilder tb = new StringBuilder();
  149. StringUtil.append(tb, "<html>");
  150. StringUtil.append(tb, "<title>Bots prevention</title>");
  151. StringUtil.append(tb, "<body><center><br><br><img src=\"L2UI_CH3.herotower_deco\" width=\"256\" height=\"32\">");
  152. StringUtil.append(tb, "<br><br><font color=\"a2a0a2\">in order to prove you are a human being<br1>you've to</font> <font color=\"b09979\">match colours within generated pattern:</font>");
  153.  
  154. // generated main pattern.
  155. StringUtil.append(tb, "<br><br><img src=\"Crest.crest_" + Config.SERVER_ID + "_" + (_validation.get(player.getObjectId()).patternid) + "\" width=\"32\" height=\"32\"></td></tr>");
  156. StringUtil.append(tb, "<br><br><font color=b09979>click-on pattern of your choice beneath:</font>");
  157.  
  158. // generate random colours.
  159. StringUtil.append(tb, "<table><tr>");
  160. for (int i = 0; i < container.options.size(); i++)
  161. {
  162. StringUtil.append(tb, "<td><button action=\"bypass -h report_" + i + "\" width=32 height=32 back=\"Crest.crest_" + Config.SERVER_ID + "_" + (container.options.get(i) + 1500) + "\" fore=\"Crest.crest_" + Config.SERVER_ID + "_" + (container.options.get(i) + 1500) + "\"></td>");
  163. }
  164. StringUtil.append(tb, "</tr></table>");
  165. StringUtil.append(tb, "</center></body>");
  166. StringUtil.append(tb, "</html>");
  167.  
  168. html.setHtml(tb.toString());
  169. player.sendPacket(html);
  170. }
  171.  
  172. public void punishmentnwindow(Player player)
  173. {
  174. NpcHtmlMessage html = new NpcHtmlMessage(1);
  175. StringBuilder tb = new StringBuilder();
  176. StringUtil.append(tb, "<html>");
  177. StringUtil.append(tb, "<title>Bots prevention</title>");
  178. StringUtil.append(tb, "<body><center><br><br><img src=\"L2UI_CH3.herotower_deco\" width=\"256\" height=\"32\">");
  179. StringUtil.append(tb, "<br><br><font color=\"a2a0a2\">if such window appears, it means character haven't<br1>passed through prevention system.");
  180. StringUtil.append(tb, "<br><br><font color=\"b09979\">in such case character get moved to nearest town.</font>");
  181. StringUtil.append(tb, "</center></body>");
  182. StringUtil.append(tb, "</html>");
  183. html.setHtml(tb.toString());
  184. player.sendPacket(html);
  185. }
  186.  
  187. public void validationtasks(Player player)
  188. {
  189. PlayerData container = new PlayerData();
  190. randomizeimages(container, player);
  191.  
  192. for (int i = 0; i < container.options.size(); i++)
  193. {
  194. PledgeCrest packet = new PledgeCrest((container.options.get(i) + 1500), _images.get(container.options.get(i)));
  195. player.sendPacket(packet);
  196.  
  197. }
  198.  
  199. PledgeCrest packet = new PledgeCrest(container.patternid, _images.get(container.options.get(container.mainpattern)));
  200. player.sendPacket(packet);
  201.  
  202. _validation.put(player.getObjectId(), container);
  203.  
  204. Future<?> newTask = ThreadPool.scheduleGeneral(new ReportCheckTask(player), VALIDATION_TIME);
  205. ThreadPool.scheduleGeneral(new countdown(player, VALIDATION_TIME / 1000), 0);
  206. _beginvalidation.put(player.getObjectId(), newTask);
  207. }
  208.  
  209. protected void randomizeimages(PlayerData container,Player player)
  210. {
  211. int buttonscount = 4;
  212. int imagescount = _images.size();
  213.  
  214. for (int i = 0; i < buttonscount; i++)
  215. {
  216. int next = _randomize.nextInt(imagescount);
  217. while (container.options.indexOf(next) > -1)
  218. {
  219. next = _randomize.nextInt(imagescount);
  220. }
  221. container.options.add(next);
  222. }
  223.  
  224. int mainIndex = _randomize.nextInt(buttonscount);
  225. container.mainpattern = mainIndex;
  226.  
  227. Calendar token = Calendar.getInstance();
  228. String uniquetoken = Integer.toString(token.get(Calendar.DAY_OF_MONTH))+Integer.toString(token.get(Calendar.HOUR_OF_DAY))+Integer.toString(token.get(Calendar.MINUTE))+Integer.toString(token.get(Calendar.SECOND))+Integer.toString(token.get(Calendar.MILLISECOND)/100);
  229. container.patternid = Integer.parseInt(uniquetoken);
  230. }
  231.  
  232. protected void banpunishment(Player player)
  233. {
  234. _validation.remove(player.getObjectId());
  235. _beginvalidation.get(player.getObjectId()).cancel(true);
  236. _beginvalidation.remove(player.getObjectId());
  237.  
  238. switch (Config.PUNISHMENT)
  239. {
  240. // 0 = move character to the closest village.
  241. // 1 = kick characters from the server.
  242. // 2 = put character to jail.
  243. // 3 = ban character from the server.
  244. case 0:
  245. player.stopMove(null);
  246. player.teleToLocation(MapRegionData.TeleportWhereType.Town);
  247. punishmentnwindow(player);
  248. break;
  249. case 1:
  250. if (player.isOnline())
  251. {
  252. player.logout(true);
  253. }
  254. break;
  255. case 2:
  256. jailpunishment(player, Config.PUNISHMENT_TIME * 60);
  257. break;
  258. case 3:
  259. //player.setAccessLevel(-100);
  260. changeaccesslevel(player, -100);
  261. break;
  262. }
  263.  
  264. player.sendMessage("Unfortunately, colours doesn't match.");
  265. }
  266.  
  267. private static void changeaccesslevel(Player targetPlayer, int lvl)
  268. {
  269. if (targetPlayer.isOnline())
  270. {
  271. targetPlayer.setAccessLevel(lvl);
  272. targetPlayer.logout();
  273. }
  274. else
  275. {
  276. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  277. {
  278. PreparedStatement statement = con.prepareStatement("UPDATE characters SET accesslevel=? WHERE obj_id=?");
  279. statement.setInt(1, lvl);
  280. statement.setInt(2, targetPlayer.getObjectId());
  281. statement.execute();
  282. statement.close();
  283. }
  284. catch (SQLException se)
  285. {
  286. if (Config.DEBUG)
  287. se.printStackTrace();
  288. }
  289. }
  290. }
  291.  
  292. private static void jailpunishment(Player activeChar, int delay)
  293. {
  294. if (activeChar.isOnline())
  295. {
  296. activeChar.setPunishLevel(Player.PunishLevel.JAIL, Config.PUNISHMENT_TIME);
  297. }
  298. else
  299. {
  300. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  301. {
  302. PreparedStatement statement = con.prepareStatement("UPDATE characters SET x=?, y=?, z=?, punish_level=?, punish_timer=? WHERE obj_id=?");
  303. statement.setInt(1, -114356);
  304. statement.setInt(2, -249645);
  305. statement.setInt(3, -2984);
  306. statement.setInt(4, Player.PunishLevel.JAIL.value());
  307. statement.setLong(5, (delay > 0 ? delay * Config.PUNISHMENT_TIME * 100 : 0));
  308. statement.setInt(6, activeChar.getObjectId());
  309.  
  310. statement.execute();
  311. statement.close();
  312. }
  313. catch (SQLException se)
  314. {
  315. activeChar.sendMessage("SQLException while jailing player");
  316. if (Config.DEBUG)
  317. se.printStackTrace();
  318. }
  319. }
  320. }
  321.  
  322. public void AnalyseBypass(String command, Player player)
  323. {
  324. if (!_validation.containsKey(player.getObjectId()))
  325. return;
  326.  
  327. String params = command.substring(command.indexOf("_") + 1);
  328.  
  329. if (params.startsWith("continue"))
  330. {
  331. validationwindow(player);
  332. _validation.get(player.getObjectId()).firstWindow = false;
  333. return;
  334. }
  335.  
  336. int choosenoption = -1;
  337. if (tryParseInt(params))
  338. {
  339. choosenoption = Integer.parseInt(params);
  340. }
  341.  
  342. if (choosenoption > -1)
  343. {
  344. PlayerData playerData = _validation.get(player.getObjectId());
  345. if (choosenoption != playerData.mainpattern)
  346. {
  347. banpunishment(player);
  348. }
  349. else
  350. {
  351. player.sendMessage("Congratulations, colours match!");
  352. _validation.remove(player.getObjectId());
  353. _beginvalidation.get(player.getObjectId()).cancel(true);
  354. _beginvalidation.remove(player.getObjectId());
  355. }
  356. }
  357. }
  358.  
  359. protected class countdown implements Runnable
  360. {
  361. private final Player _player;
  362. private int _time;
  363.  
  364. public countdown(Player player, int time)
  365. {
  366. _time = time;
  367. _player = player;
  368. }
  369.  
  370. @Override
  371. public void run()
  372. {
  373. if (_player.isOnline())
  374. {
  375. if (_validation.containsKey(_player.getObjectId()) && _validation.get(_player.getObjectId()).firstWindow)
  376. {
  377. if (_time % WINDOW_DELAY == 0)
  378. {
  379. prevalidationwindow(_player);
  380. }
  381. }
  382.  
  383. switch (_time)
  384. {
  385. case 300:
  386. case 240:
  387. case 180:
  388. case 120:
  389. case 60:
  390. _player.sendMessage(_time / 60 + " minute(s) to match colors.");
  391. break;
  392. case 30:
  393. case 10:
  394. case 5:
  395. case 4:
  396. case 3:
  397. case 2:
  398. case 1:
  399. _player.sendMessage(_time + " second(s) to match colors!");
  400. break;
  401. }
  402. if (_time > 1 && _validation.containsKey(_player.getObjectId()))
  403. {
  404. ThreadPool.getInstance().scheduleGeneral(new countdown(_player, _time - 1), 1000);
  405. }
  406. }
  407. }
  408. }
  409.  
  410. protected boolean tryParseInt(String value)
  411. {
  412. try
  413. {
  414. Integer.parseInt(value);
  415. return true;
  416. }
  417.  
  418. catch (NumberFormatException e)
  419. {
  420. return false;
  421. }
  422. }
  423.  
  424. public void CaptchaSuccessfull(Player player)
  425. {
  426. if (_validation.get(player.getObjectId()) != null)
  427. {
  428. _validation.remove(player.getObjectId());
  429. }
  430. }
  431.  
  432. public Boolean IsAlredyInReportMode(Player player)
  433. {
  434. if (_validation.get(player.getObjectId()) != null)
  435. {
  436. return true;
  437. }
  438. return false;
  439. }
  440.  
  441. private class ReportCheckTask implements Runnable
  442. {
  443. private final Player _player;
  444.  
  445. public ReportCheckTask(Player player)
  446. {
  447. _player = player;
  448. }
  449.  
  450. @Override
  451. public void run()
  452. {
  453. if (_validation.get(_player.getObjectId()) != null)
  454. {
  455. banpunishment(_player);
  456. }
  457. }
  458. }
  459.  
  460. private static class SingletonHolder
  461. {
  462. protected static final BotsPreventionManager _instance = new BotsPreventionManager();
  463. }
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement