2
0

GameServerTable.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 net.sf.l2j.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 javolution.io.UTF8StreamReader;
  35. import javolution.util.FastMap;
  36. import javolution.xml.stream.XMLStreamConstants;
  37. import javolution.xml.stream.XMLStreamException;
  38. import javolution.xml.stream.XMLStreamReaderImpl;
  39. import net.sf.l2j.L2DatabaseFactory;
  40. import net.sf.l2j.loginserver.gameserverpackets.ServerStatus;
  41. import net.sf.l2j.util.Rnd;
  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>().setShared(true);
  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. }
  215. catch (SQLException e)
  216. {
  217. _log.log(Level.SEVERE, "SQL error while saving gameserver.", e);
  218. }
  219. finally
  220. {
  221. try
  222. {
  223. con.close();
  224. }
  225. catch (Exception e)
  226. {
  227. }
  228. }
  229. }
  230. public String getServerNameById(int id)
  231. {
  232. return getServerNames().get(id);
  233. }
  234. public Map<Integer, String> getServerNames()
  235. {
  236. return _serverNames;
  237. }
  238. public KeyPair getKeyPair()
  239. {
  240. return _keyPairs[Rnd.nextInt(10)];
  241. }
  242. private byte[] stringToHex(String string)
  243. {
  244. return new BigInteger(string, 16).toByteArray();
  245. }
  246. private String hexToString(byte[] hex)
  247. {
  248. if (hex == null)
  249. {
  250. return "null";
  251. }
  252. return new BigInteger(hex).toString(16);
  253. }
  254. public static class GameServerInfo
  255. {
  256. // auth
  257. private int _id;
  258. private byte[] _hexId;
  259. private boolean _isAuthed;
  260. // status
  261. private GameServerThread _gst;
  262. private int _status;
  263. // network
  264. private String _internalIp;
  265. private String _externalIp;
  266. private String _externalHost;
  267. private int _port;
  268. // config
  269. private boolean _isPvp = true;
  270. private boolean _isTestServer;
  271. private boolean _isShowingClock;
  272. private boolean _isShowingBrackets;
  273. private int _maxPlayers;
  274. public GameServerInfo(int id, byte[] hexId, GameServerThread gst)
  275. {
  276. _id = id;
  277. _hexId = hexId;
  278. _gst = gst;
  279. _status = ServerStatus.STATUS_DOWN;
  280. }
  281. public GameServerInfo(int id, byte[] hexId)
  282. {
  283. this(id, hexId, null);
  284. }
  285. public void setId(int id)
  286. {
  287. _id = id;
  288. }
  289. public int getId()
  290. {
  291. return _id;
  292. }
  293. public byte[] getHexId()
  294. {
  295. return _hexId;
  296. }
  297. public void setAuthed(boolean isAuthed)
  298. {
  299. _isAuthed = isAuthed;
  300. }
  301. public boolean isAuthed()
  302. {
  303. return _isAuthed;
  304. }
  305. public void setGameServerThread(GameServerThread gst)
  306. {
  307. _gst = gst;
  308. }
  309. public GameServerThread getGameServerThread()
  310. {
  311. return _gst;
  312. }
  313. public void setStatus(int status)
  314. {
  315. _status = status;
  316. }
  317. public int getStatus()
  318. {
  319. return _status;
  320. }
  321. public int getCurrentPlayerCount()
  322. {
  323. if (_gst == null)
  324. return 0;
  325. return _gst.getPlayerCount();
  326. }
  327. public void setInternalIp(String internalIp)
  328. {
  329. _internalIp = internalIp;
  330. }
  331. public String getInternalHost()
  332. {
  333. return _internalIp;
  334. }
  335. public void setExternalIp(String externalIp)
  336. {
  337. _externalIp = externalIp;
  338. }
  339. public String getExternalIp()
  340. {
  341. return _externalIp;
  342. }
  343. public void setExternalHost(String externalHost)
  344. {
  345. _externalHost = externalHost;
  346. }
  347. public String getExternalHost()
  348. {
  349. return _externalHost;
  350. }
  351. public int getPort()
  352. {
  353. return _port;
  354. }
  355. public void setPort(int port)
  356. {
  357. _port = port;
  358. }
  359. public void setMaxPlayers(int maxPlayers)
  360. {
  361. _maxPlayers = maxPlayers;
  362. }
  363. public int getMaxPlayers()
  364. {
  365. return _maxPlayers;
  366. }
  367. public boolean isPvp()
  368. {
  369. return _isPvp;
  370. }
  371. public void setTestServer(boolean val)
  372. {
  373. _isTestServer = val;
  374. }
  375. public boolean isTestServer()
  376. {
  377. return _isTestServer;
  378. }
  379. public void setShowingClock(boolean clock)
  380. {
  381. _isShowingClock = clock;
  382. }
  383. public boolean isShowingClock()
  384. {
  385. return _isShowingClock;
  386. }
  387. public void setShowingBrackets(boolean val)
  388. {
  389. _isShowingBrackets = val;
  390. }
  391. public boolean isShowingBrackets()
  392. {
  393. return _isShowingBrackets;
  394. }
  395. public void setDown()
  396. {
  397. setAuthed(false);
  398. setPort(0);
  399. setGameServerThread(null);
  400. setStatus(ServerStatus.STATUS_DOWN);
  401. }
  402. }
  403. }