SelectorHelper.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package net.sf.l2j.loginserver;
  14. import java.net.InetAddress;
  15. import java.nio.channels.SocketChannel;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.Map.Entry;
  19. import java.util.concurrent.LinkedBlockingQueue;
  20. import java.util.concurrent.ThreadPoolExecutor;
  21. import java.util.concurrent.TimeUnit;
  22. import net.sf.l2j.loginserver.serverpackets.Init;
  23. import org.mmocore.network.IAcceptFilter;
  24. import org.mmocore.network.IClientFactory;
  25. import org.mmocore.network.IMMOExecutor;
  26. import org.mmocore.network.MMOConnection;
  27. import org.mmocore.network.ReceivablePacket;
  28. /**
  29. *
  30. * @author KenM
  31. */
  32. public class SelectorHelper extends Thread implements IMMOExecutor<L2LoginClient>,
  33. IClientFactory<L2LoginClient>, IAcceptFilter
  34. {
  35. private HashMap<Integer, Flood> _ipFloodMap;
  36. private ThreadPoolExecutor _generalPacketsThreadPool;
  37. public SelectorHelper()
  38. {
  39. _generalPacketsThreadPool = new ThreadPoolExecutor(4, 6, 15L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
  40. _ipFloodMap = new HashMap<Integer, Flood>();
  41. super.setDaemon(true);
  42. super.start();
  43. }
  44. /**
  45. *
  46. * @see org.mmocore.network.IMMOExecutor#execute(org.mmocore.network.ReceivablePacket)
  47. */
  48. public void execute(ReceivablePacket<L2LoginClient> packet)
  49. {
  50. _generalPacketsThreadPool.execute(packet);
  51. }
  52. /**
  53. *
  54. * @see org.mmocore.network.IClientFactory#create(org.mmocore.network.MMOConnection)
  55. */
  56. public L2LoginClient create(MMOConnection<L2LoginClient> con)
  57. {
  58. L2LoginClient client = new L2LoginClient(con);
  59. client.sendPacket(new Init(client));
  60. return client;
  61. }
  62. /**
  63. *
  64. * @see org.mmocore.network.IAcceptFilter#accept(java.nio.channels.SocketChannel)
  65. */
  66. public boolean accept(SocketChannel sc)
  67. {
  68. InetAddress addr = sc.socket().getInetAddress();
  69. int h = hash(addr.getAddress());
  70. long current = System.currentTimeMillis();
  71. Flood f;
  72. synchronized (_ipFloodMap)
  73. {
  74. f = _ipFloodMap.get(h);
  75. }
  76. if (f != null)
  77. {
  78. if (f.trys == -1)
  79. {
  80. f.lastAccess = current;
  81. return false;
  82. }
  83. if (f.lastAccess + 1000 > current)
  84. {
  85. f.lastAccess = current;
  86. if (f.trys >= 3)
  87. {
  88. f.trys = -1;
  89. return false;
  90. }
  91. f.trys++;
  92. }
  93. else
  94. {
  95. f.lastAccess = current;
  96. }
  97. }
  98. else
  99. {
  100. synchronized (_ipFloodMap)
  101. {
  102. _ipFloodMap.put(h, new Flood());
  103. }
  104. }
  105. return !LoginController.getInstance().isBannedAddress(addr);
  106. }
  107. /**
  108. *
  109. * @param ip
  110. * @return
  111. */
  112. private int hash(byte[] ip)
  113. {
  114. return ip[0] & 0xFF | ip[1] << 8 & 0xFF00 | ip[2] << 16 & 0xFF0000 | ip[3] << 24
  115. & 0xFF000000;
  116. }
  117. private class Flood
  118. {
  119. long lastAccess;
  120. int trys;
  121. Flood()
  122. {
  123. lastAccess = System.currentTimeMillis();
  124. trys = 0;
  125. }
  126. }
  127. /**
  128. *
  129. * @see java.lang.Thread#run()
  130. */
  131. @Override
  132. public void run()
  133. {
  134. while (true)
  135. {
  136. long reference = System.currentTimeMillis() - (1000 * 300);
  137. ArrayList<Integer> toRemove = new ArrayList<Integer>(50);
  138. synchronized (_ipFloodMap)
  139. {
  140. for (Entry<Integer, Flood> e : _ipFloodMap.entrySet())
  141. {
  142. Flood f = e.getValue();
  143. if (f.lastAccess < reference)
  144. toRemove.add(e.getKey());
  145. }
  146. }
  147. synchronized (_ipFloodMap)
  148. {
  149. for (Integer i : toRemove)
  150. {
  151. _ipFloodMap.remove(i);
  152. }
  153. }
  154. try
  155. {
  156. Thread.sleep(5000);
  157. }
  158. catch (InterruptedException e)
  159. {
  160. }
  161. }
  162. }
  163. }