Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. package com.cotex.net.login;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6.  
  7. import org.mindrot.jbcrypt.BCrypt;
  8.  
  9. import com.cotex.GameServer;
  10. import com.cotex.GameSettings;
  11. import com.cotex.net.security.ConnectionHandler;
  12. import com.cotex.util.NameUtils;
  13. import com.cotex.world.World;
  14. import com.cotex.world.entity.impl.player.Player;
  15. import com.cotex.world.entity.impl.player.PlayerLoading;
  16. import com.mchange.v2.c3p0.ComboPooledDataSource;
  17.  
  18. public final class LoginResponses {
  19.  
  20. private static final ComboPooledDataSource database = new ComboPooledDataSource();
  21.  
  22. private static final String IP = "";
  23. private static final String DATABASE = "";
  24. private static final String USERNAME = "";
  25. private static final String PASSWORD = "";
  26. private static final String QUERY = "SELECT members_pass_hash FROM core_members WHERE name = ? LIMIT 1;";
  27.  
  28. public static void init() {
  29. try {
  30. database.setDriverClass("com.mysql.jdbc.Driver");
  31. database.setJdbcUrl("jdbc:mysql://" + IP + ":3306/" + DATABASE);
  32. database.setUser(USERNAME);
  33. database.setPassword(PASSWORD);
  34. try (Connection connection = database.getConnection()) {}
  35. } catch (Exception e) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. private static int login(String username, String password) {
  41. try (final Connection connection = database.getConnection()) {
  42. try (final PreparedStatement statement = connection.prepareStatement(QUERY)) {
  43. statement.setString(1, username);
  44. try (final ResultSet results = statement.executeQuery()) {
  45. if (!results.next()) {
  46. //return NEW_ACCOUNT; //return invalid credentials instead, forcing the user to create an account online.
  47. return ACCOUNT_NOT_FOUND;
  48. }
  49. final String hash = results.getString("members_pass_hash");
  50. final boolean success = BCrypt.checkpw(password, hash);
  51. return success ? LOGIN_SUCCESSFUL : USERNAME_OR_PASSWORD_INCORRECT;
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. return LOGIN_SERVER_OFFLINE;
  62. }
  63.  
  64. public static final int getResponse(Player player, LoginDetailsMessage msg) {
  65. if (GameSettings.FORUM_INT == true) {
  66. if (World.getPlayers().isFull()) {
  67. return LOGIN_WORLD_FULL;
  68. }
  69. if(GameServer.isUpdating()) {
  70. return LOGIN_GAME_UPDATE;
  71. }
  72. if (!NameUtils.isValidName(player.getUsername())) {
  73. return LOGIN_INVALID_CREDENTIALS;
  74. }
  75. if(player.getUsername().startsWith(" ")) {
  76. return USERNAME_STARTS_WITH_SPACE;
  77. }
  78. if(msg.getClientVersion() != GameSettings.GAME_VERSION || msg.getUid() != (350>>2240)) {
  79. return OLD_CLIENT_VERSION;
  80. }
  81. if(World.getPlayerByName(player.getUsername()) != null) {
  82. return LOGIN_ACCOUNT_ONLINE;
  83. }
  84.  
  85. /** BANS AND ACCESS LIMITS **/
  86. int hostHandlerResponse = ConnectionHandler.getResponse(player, msg);
  87. if(hostHandlerResponse != LOGIN_SUCCESSFUL) {
  88. return hostHandlerResponse;
  89. }
  90.  
  91. /** FORUM CREDENTIAL CHECK **/
  92. int forumLoadingResponse = login(player.getUsername(), player.getPassword());
  93. if (forumLoadingResponse != LOGIN_SUCCESSFUL) {
  94. return forumLoadingResponse;
  95. }
  96.  
  97. int playerLoadingResponse = PlayerLoading.getResult(player);
  98. if(playerLoadingResponse != LOGIN_SUCCESSFUL && playerLoadingResponse != NEW_ACCOUNT) {
  99. return playerLoadingResponse;
  100. }
  101.  
  102. return playerLoadingResponse;
  103. } else {
  104. if (World.getPlayers().isFull()) {
  105. return LOGIN_WORLD_FULL;
  106. }
  107. if(GameServer.isUpdating()) {
  108. return LOGIN_GAME_UPDATE;
  109. }
  110. if (!NameUtils.isValidName(player.getUsername())) {
  111. return LOGIN_INVALID_CREDENTIALS;
  112. }
  113. if(player.getUsername().startsWith(" ")) {
  114. return USERNAME_STARTS_WITH_SPACE;
  115. }
  116. if(msg.getClientVersion() != GameSettings.GAME_VERSION || msg.getUid() != (350>>2240)) {
  117. return OLD_CLIENT_VERSION;
  118. }
  119. if(World.getPlayerByName(player.getUsername()) != null) {
  120. return LOGIN_ACCOUNT_ONLINE;
  121. }
  122.  
  123. /** CHAR FILE LOADING **/
  124. int playerLoadingResponse = PlayerLoading.getResult(player);
  125. if(playerLoadingResponse != LOGIN_SUCCESSFUL && playerLoadingResponse != NEW_ACCOUNT) {
  126. return playerLoadingResponse;
  127. }
  128.  
  129. /** PREVENTING IMPERSONATING **/
  130.  
  131. /** BANS AND ACCESS LIMITS **/
  132. int hostHandlerResponse = ConnectionHandler.getResponse(player, msg);
  133. if(hostHandlerResponse != LOGIN_SUCCESSFUL) {
  134. return hostHandlerResponse;
  135. }
  136.  
  137. return playerLoadingResponse;
  138. }
  139. }
  140.  
  141. /**
  142. * This opcode is used for data (session keys, name, password, etc)
  143. * exchange.
  144. */
  145. public static final int LOGIN_EXCHANGE_DATA = 0;
  146.  
  147. /**
  148. * This login opcode is used for a 2000ms delay,
  149. * after which a reconnection is attempted.
  150. */
  151. public static final int LOGIN_DELAY = 1;
  152.  
  153. /**
  154. * This login opcode signifies a successful login.
  155. */
  156. public static final int LOGIN_SUCCESSFUL = 2;
  157.  
  158. /**
  159. * This login opcode is used when the player
  160. * has entered an invalid username and/or password.
  161. */
  162. public static final int LOGIN_INVALID_CREDENTIALS = 3;
  163.  
  164. /**
  165. * This login opcode is used when the account
  166. * has been disabled.
  167. */
  168. public static final int LOGIN_DISABLED_ACCOUNT = 4;
  169.  
  170. /**
  171. * This login opcode is used when the player's IP
  172. * has been disabled.
  173. */
  174. public static final int LOGIN_DISABLED_COMPUTER = 22;
  175.  
  176. /**
  177. * This login opcode is used when the player's IP
  178. * has been disabled.
  179. */
  180. public static final int LOGIN_DISABLED_IP = 27;
  181.  
  182.  
  183. /**
  184. * This login opcode is used when the account
  185. * attempting to connect is already online in the server.
  186. */
  187. public static final int LOGIN_ACCOUNT_ONLINE = 5;
  188.  
  189. /**
  190. * This login opcode is used when the game has been or
  191. * is being updated.
  192. */
  193. public static final int LOGIN_GAME_UPDATE = 6;
  194.  
  195. /**
  196. * This login opcode is used when server is launching.
  197. */
  198. public static final int SERVER_LAUNCHING = 23;
  199.  
  200. /**
  201. * This login opcode is used when the world being
  202. * connected to is full.
  203. */
  204. public static final int LOGIN_WORLD_FULL = 7;
  205.  
  206. /**
  207. * This login opcode is used when server is offline.
  208. */
  209. public static final int LOGIN_SERVER_OFFLINE = 8;
  210.  
  211. /**
  212. * This login opcode is used when the connections
  213. * from an ip address has exceeded the maximum connections
  214. */
  215. public static final int LOGIN_CONNECTION_LIMIT = 9;
  216.  
  217. /**
  218. * This login opcode is used when a connection
  219. * has received a bad session id.
  220. */
  221. public static final int LOGIN_BAD_SESSION_ID = 10;
  222.  
  223. /**
  224. * This login opcode is used when the login procedure
  225. * has rejected the session.
  226. */
  227. public static final int LOGIN_REJECT_SESSION = 11;
  228.  
  229. /**
  230. * This login opcode is used when a non-member player
  231. * is attempting to login to a members world.
  232. */
  233. public static final int LOGIN_WORLD_MEMBER_ACCOUNT_REQUIRED = 12;
  234.  
  235. /**
  236. * This login opcode is used when the login procedure
  237. * could not be completed.
  238. */
  239. public static final int LOGIN_COULD_NOT_COMPLETE = 13;
  240.  
  241. /**
  242. * This login opcode is used when the game is being updated.
  243. */
  244. public static final int LOGIN_WORLD_UPDATE = 14;
  245.  
  246. /**
  247. * This login opcode is received upon a reconnection
  248. * so that chat messages will not dissappear.
  249. */
  250. public static final int LOGIN_RECONNECTION = 15;
  251.  
  252. /**
  253. * This login opcode is used when a player
  254. * has exceeded their attempts to login.
  255. */
  256. public static final int LOGIN_EXCESSIVE_ATTEMPTS = 16;
  257.  
  258. /**
  259. * This login opcode is used when a member is attempting
  260. * to login to a non-members world in a members-only area.
  261. */
  262. public static final int LOGIN_AREA_MEMBER_ACCOUNT_REQUIRED = 17;
  263.  
  264. /**
  265. * This login opcode is used when the login server
  266. * is invalid.
  267. */
  268. public static final int LOGIN_INVALID_LOGIN_SERVER = 20;
  269.  
  270. /**
  271. * This login opcode is used when a player has just
  272. * left another world and has to wait a delay before
  273. * entering a new world.
  274. */
  275. public static final int LOGIN_WORLD_DELAY = 21;
  276.  
  277. /**
  278. * This login opcode is used when a player has
  279. * entered invalid credentials.
  280. */
  281. public static final int INVALID_CREDENTIALS = 28;
  282.  
  283. /**
  284. * This login opcode is used when a player has
  285. * attempted to login with a old client.
  286. */
  287. public static final int OLD_CLIENT_VERSION = 30;
  288.  
  289. /**
  290. * This login opcode is used when a player's username is started with a space
  291. */
  292. public static final int USERNAME_STARTS_WITH_SPACE = 31;
  293.  
  294. /**
  295. *
  296. * The new connection login request
  297. * byte data sent by the client.
  298. */
  299. public static final int NEW_CONNECTION_LOGIN_REQUEST = 16;
  300.  
  301. /**
  302. * The reconnection login request
  303. * byte data sent by the client.
  304. */
  305. public static final int RECONNECTING_LOGIN_REQUEST = 18;
  306.  
  307. /**
  308. * New account
  309. */
  310. public static final int NEW_ACCOUNT = -1;
  311.  
  312. /**
  313. * Account not found - create one on the forum.
  314. */
  315. public static final int ACCOUNT_NOT_FOUND = 50;
  316.  
  317. public static final int USERNAME_OR_PASSWORD_INCORRECT = 51;
  318.  
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement