GameServerTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.loginserver;
  16. import java.io.FileInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.io.InputStream;
  19. import java.math.BigInteger;
  20. import java.security.GeneralSecurityException;
  21. import java.security.InvalidAlgorithmParameterException;
  22. import java.security.KeyPair;
  23. import java.security.KeyPairGenerator;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.security.spec.RSAKeyGenParameterSpec;
  26. import java.sql.Connection;
  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;
  29. import java.sql.SQLException;
  30. import java.util.Map;
  31. import java.util.Map.Entry;
  32. import java.util.logging.Level;
  33. import java.util.logging.Logger;
  34. import com.l2jserver.L2DatabaseFactory;
  35. import com.l2jserver.loginserver.gameserverpackets.ServerStatus;
  36. import com.l2jserver.util.Rnd;
  37. import javolution.io.UTF8StreamReader;
  38. import javolution.util.FastMap;
  39. import javolution.xml.stream.XMLStreamConstants;
  40. import javolution.xml.stream.XMLStreamException;
  41. import javolution.xml.stream.XMLStreamReaderImpl;
  42. /**
  43. *
  44. * @author KenM
  45. */
  46. public class GameServerTable
  47. {
  48. private static Logger _log = Logger.getLogger(GameServerTable.class.getName());
  49. private static GameServerTable _instance;
  50. // Server Names Config
  51. private static Map<Integer, String> _serverNames = new FastMap<Integer, String>();
  52. // Game Server Table
  53. private Map<Integer, GameServerInfo> _gameServerTable = new FastMap<Integer, GameServerInfo>().shared();
  54. // RSA Config
  55. private static final int KEYS_SIZE = 10;
  56. private KeyPair[] _keyPairs;
  57. public static void load() throws SQLException, GeneralSecurityException
  58. {
  59. synchronized (GameServerTable.class)
  60. {
  61. if (_instance == null)
  62. {
  63. _instance = new GameServerTable();
  64. }
  65. else
  66. {
  67. throw new IllegalStateException("Load can only be invoked a single time.");
  68. }
  69. }
  70. }
  71. public static GameServerTable getInstance()
  72. {
  73. return _instance;
  74. }
  75. public GameServerTable() throws SQLException, NoSuchAlgorithmException, InvalidAlgorithmParameterException
  76. {
  77. loadServerNames();
  78. _log.info("Loaded " + _serverNames.size() + " server names");
  79. loadRegisteredGameServers();
  80. _log.info("Loaded " + _gameServerTable.size() + " registered Game Servers");
  81. loadRSAKeys();
  82. _log.info("Cached " + _keyPairs.length + " RSA keys for Game Server communication.");
  83. }
  84. private void loadRSAKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException
  85. {
  86. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  87. RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(512, RSAKeyGenParameterSpec.F4);
  88. keyGen.initialize(spec);
  89. _keyPairs = new KeyPair[KEYS_SIZE];
  90. for (int i = 0; i < KEYS_SIZE; i++)
  91. {
  92. _keyPairs[i] = keyGen.genKeyPair();
  93. }
  94. }
  95. private void loadServerNames()
  96. {
  97. InputStream in = null;
  98. try
  99. {
  100. in = new FileInputStream("servername.xml");
  101. XMLStreamReaderImpl xpp = new XMLStreamReaderImpl();
  102. xpp.setInput(new UTF8StreamReader().setInput(in));
  103. for (int e = xpp.getEventType(); e != XMLStreamConstants.END_DOCUMENT; e = xpp.next())
  104. {
  105. if (e == XMLStreamConstants.START_ELEMENT)
  106. {
  107. if (xpp.getLocalName().toString().equals("server"))
  108. {
  109. Integer id = Integer.valueOf(xpp.getAttributeValue(null, "id").toString());
  110. String name = xpp.getAttributeValue(null, "name").toString();
  111. _serverNames.put(id, name);
  112. }
  113. }
  114. }
  115. }
  116. catch (FileNotFoundException e)
  117. {
  118. _log.warning("servername.xml could not be loaded: file not found");
  119. }
  120. catch (XMLStreamException xppe)
  121. {
  122. xppe.printStackTrace();
  123. }
  124. finally
  125. {
  126. try
  127. {
  128. in.close();
  129. }
  130. catch (Exception e)
  131. {
  132. }
  133. }
  134. }
  135. private void loadRegisteredGameServers() throws SQLException
  136. {
  137. Connection con = null;
  138. PreparedStatement statement = null;
  139. int id;
  140. con = L2DatabaseFactory.getInstance().getConnection();
  141. statement = con.prepareStatement("SELECT * FROM gameservers");
  142. ResultSet rset = statement.executeQuery();
  143. GameServerInfo gsi;
  144. while (rset.next())
  145. {
  146. id = rset.getInt("server_id");
  147. gsi = new GameServerInfo(id, stringToHex(rset.getString("hexid")));
  148. _gameServerTable.put(id, gsi);
  149. }
  150. rset.close();
  151. statement.close();
  152. con.close();
  153. }
  154. public Map<Integer, GameServerInfo> getRegisteredGameServers()
  155. {
  156. return _gameServerTable;
  157. }
  158. public GameServerInfo getRegisteredGameServerById(int id)
  159. {
  160. return _gameServerTable.get(id);
  161. }
  162. public boolean hasRegisteredGameServerOnId(int id)
  163. {
  164. return _gameServerTable.containsKey(id);
  165. }
  166. public boolean registerWithFirstAvaliableId(GameServerInfo gsi)
  167. {
  168. // avoid two servers registering with the same "free" id
  169. synchronized (_gameServerTable)
  170. {
  171. for (Entry<Integer, String> entry : _serverNames.entrySet())
  172. {
  173. if (!_gameServerTable.containsKey(entry.getKey()))
  174. {
  175. _gameServerTable.put(entry.getKey(), gsi);
  176. gsi.setId(entry.getKey());
  177. return true;
  178. }
  179. }
  180. }
  181. return false;
  182. }
  183. public boolean register(int id, GameServerInfo gsi)
  184. {
  185. // avoid two servers registering with the same id
  186. synchronized (_gameServerTable)
  187. {
  188. if (!_gameServerTable.containsKey(id))
  189. {
  190. _gameServerTable.put(id, gsi);
  191. gsi.setId(id);
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. public void registerServerOnDB(GameServerInfo gsi)
  198. {
  199. this.registerServerOnDB(gsi.getHexId(), gsi.getId(), gsi.getExternalHost());
  200. }
  201. public void registerServerOnDB(byte[] hexId, int id, String externalHost)
  202. {
  203. Connection con = null;
  204. PreparedStatement statement = null;
  205. try
  206. {
  207. con = L2DatabaseFactory.getInstance().getConnection();
  208. statement = con.prepareStatement("INSERT INTO gameservers (hexid,server_id,host) values (?,?,?)");
  209. statement.setString(1, hexToString(hexId));
  210. statement.setInt(2, id);
  211. statement.setString(3, externalHost);
  212. statement.executeUpdate();
  213. statement.close();
  214. this.register(id, new GameServerInfo(id, hexId));
  215. }
  216. catch (SQLException e)
  217. {
  218. _log.log(Level.SEVERE, "SQL error while saving gameserver.", e);
  219. }
  220. finally
  221. {
  222. try
  223. {
  224. con.close();
  225. }
  226. catch (Exception e)
  227. {
  228. }
  229. }
  230. }
  231. public String getServerNameById(int id)
  232. {
  233. return getServerNames().get(id);
  234. }
  235. public Map<Integer, String> getServerNames()
  236. {
  237. return _serverNames;
  238. }
  239. public KeyPair getKeyPair()
  240. {
  241. return _keyPairs[Rnd.nextInt(10)];
  242. }
  243. private byte[] stringToHex(String string)
  244. {
  245. return new BigInteger(string, 16).toByteArray();
  246. }
  247. private String hexToString(byte[] hex)
  248. {
  249. if (hex == null)
  250. {
  251. return "null";
  252. }
  253. return new BigInteger(hex).toString(16);
  254. }
  255. public static class GameServerInfo
  256. {
  257. // auth
  258. private int _id;
  259. private byte[] _hexId;
  260. private boolean _isAuthed;
  261. // status
  262. private GameServerThread _gst;
  263. private int _status;
  264. // network
  265. private String _internalIp;
  266. private String _externalIp;
  267. private String _externalHost;
  268. private int _port;
  269. // config
  270. private boolean _isPvp = true;
  271. private boolean _isTestServer;
  272. private boolean _isShowingClock;
  273. private boolean _isShowingBrackets;
  274. private int _maxPlayers;
  275. public GameServerInfo(int id, byte[] hexId, GameServerThread gst)
  276. {
  277. _id = id;
  278. _hexId = hexId;
  279. _gst = gst;
  280. _status = ServerStatus.STATUS_DOWN;
  281. }
  282. public GameServerInfo(int id, byte[] hexId)
  283. {
  284. this(id, hexId, null);
  285. }
  286. public void setId(int id)
  287. {
  288. _id = id;
  289. }
  290. public int getId()
  291. {
  292. return _id;
  293. }
  294. public byte[] getHexId()
  295. {
  296. return _hexId;
  297. }
  298. public void setAuthed(boolean isAuthed)
  299. {
  300. _isAuthed = isAuthed;
  301. }
  302. public boolean isAuthed()
  303. {
  304. return _isAuthed;
  305. }
  306. public void setGameServerThread(GameServerThread gst)
  307. {
  308. _gst = gst;
  309. }
  310. public GameServerThread getGameServerThread()
  311. {
  312. return _gst;
  313. }
  314. public void setStatus(int status)
  315. {
  316. _status = status;
  317. }
  318. public int getStatus()
  319. {
  320. return _status;
  321. }
  322. public int getCurrentPlayerCount()
  323. {
  324. if (_gst == null)
  325. return 0;
  326. return _gst.getPlayerCount();
  327. }
  328. public void setInternalIp(String internalIp)
  329. {
  330. _internalIp = internalIp;
  331. }
  332. public String getInternalHost()
  333. {
  334. return _internalIp;
  335. }
  336. public void setExternalIp(String externalIp)
  337. {
  338. _externalIp = externalIp;
  339. }
  340. public String getExternalIp()
  341. {
  342. return _externalIp;
  343. }
  344. public void setExternalHost(String externalHost)
  345. {
  346. _externalHost = externalHost;
  347. }
  348. public String getExternalHost()
  349. {
  350. return _externalHost;
  351. }
  352. public int getPort()
  353. {
  354. return _port;
  355. }
  356. public void setPort(int port)
  357. {
  358. _port = port;
  359. }
  360. public void setMaxPlayers(int maxPlayers)
  361. {
  362. _maxPlayers = maxPlayers;
  363. }
  364. public int getMaxPlayers()
  365. {
  366. return _maxPlayers;
  367. }
  368. public boolean isPvp()
  369. {
  370. return _isPvp;
  371. }
  372. public void setTestServer(boolean val)
  373. {
  374. _isTestServer = val;
  375. }
  376. public boolean isTestServer()
  377. {
  378. return _isTestServer;
  379. }
  380. public void setShowingClock(boolean clock)
  381. {
  382. _isShowingClock = clock;
  383. }
  384. public boolean isShowingClock()
  385. {
  386. return _isShowingClock;
  387. }
  388. public void setShowingBrackets(boolean val)
  389. {
  390. _isShowingBrackets = val;
  391. }
  392. public boolean isShowingBrackets()
  393. {
  394. return _isShowingBrackets;
  395. }
  396. public void setDown()
  397. {
  398. setAuthed(false);
  399. setPort(0);
  400. setGameServerThread(null);
  401. setStatus(ServerStatus.STATUS_DOWN);
  402. }
  403. }
  404. }