Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9.  
  10. import server.model.players.Client;
  11.  
  12. /**
  13. * Connection Check Class
  14. *
  15. * @author Ryan / Lmctruck30
  16. *
  17. */
  18.  
  19. public class Connection {
  20.  
  21. public static ArrayList<String> bannedIps = new ArrayList<String>();
  22. public static ArrayList<String> bannedNames = new ArrayList<String>();
  23. public static ArrayList<String> mutedIps = new ArrayList<String>();
  24. public static ArrayList<String> mutedNames = new ArrayList<String>();
  25. public static ArrayList<String> loginLimitExceeded = new ArrayList<String>();
  26.  
  27. /**
  28. * Adds the banned usernames and ips from the text file to the ban list
  29. **/
  30. public static void initialize() {
  31. banUsers();
  32. banIps();
  33. muteUsers();
  34. muteIps();
  35. }
  36.  
  37. /**
  38. * Adding Name To List
  39. */
  40. public static void addIpToLoginList(String IP) {
  41. loginLimitExceeded.add(IP);
  42. }
  43.  
  44. /**
  45. * Remove Ip From List
  46. */
  47. public static void removeIpFromLoginList(String IP) {
  48. loginLimitExceeded.remove(IP);
  49. }
  50.  
  51. /**
  52. * Clear Name List
  53. */
  54. public static void clearLoginList() {
  55. loginLimitExceeded.clear();
  56. }
  57.  
  58. public static boolean checkLoginList(String IP) {
  59. loginLimitExceeded.add(IP);
  60. int num = 0;
  61. for (String ips : loginLimitExceeded) {
  62. if (IP.equals(ips)) {
  63. num++;
  64. }
  65. }
  66. if (num > 5) {
  67. return true;
  68. }
  69. return false;
  70. }
  71.  
  72. public static void unMuteUser(String name) {
  73. mutedNames.remove(name);
  74. deleteFromFile("./Data/bans/UsersMuted.txt", name);
  75. }
  76.  
  77. public static void unIPMuteUser(String name) {
  78. mutedIps.remove(name);
  79. deleteFromFile("./Data/bans/IpsMuted.txt", name);
  80. }
  81.  
  82. /**
  83. * Adding Ban IP
  84. **/
  85. public static void addIpToBanList(String IP) {
  86. bannedIps.add(IP);
  87. }
  88.  
  89. public static void addIpToMuteList(String IP) {
  90. mutedIps.add(IP);
  91. addIpToMuteFile(IP);
  92. }
  93.  
  94. /**
  95. * Removing Ban IP
  96. **/
  97. public static void removeIpFromBanList(String IP) {
  98. bannedIps.remove(IP);
  99. }
  100.  
  101. /**
  102. * Contains Ban IP
  103. **/
  104. public static boolean isIpBanned(String IP) {
  105. if (bannedIps.contains(IP)) {
  106. return true;
  107. }
  108. return false;
  109. }
  110.  
  111. /**
  112. * Adding banned username
  113. **/
  114. public static void addNameToBanList(String name) {
  115. bannedNames.add(name.toLowerCase());
  116. }
  117.  
  118. public static void addNameToMuteList(String name) {
  119. mutedNames.add(name.toLowerCase());
  120. addUserToFile(name);
  121. }
  122.  
  123. /**
  124. * Removing banned username
  125. **/
  126. public static void removeNameFromBanList(String name) {
  127. bannedNames.remove(name.toLowerCase());
  128. deleteFromFile("./Data/bans/UsersBanned.txt", name);
  129. }
  130.  
  131. public static void removeNameFromMuteList(String name) {
  132. bannedNames.remove(name.toLowerCase());
  133. deleteFromFile("./Data/bans/UsersMuted.txt", name);
  134. }
  135.  
  136. public static void deleteFromFile(String file, String name) {
  137. try {
  138. BufferedReader r = new BufferedReader(new FileReader(file));
  139. ArrayList<String> contents = new ArrayList<String>();
  140. while (true) {
  141. String line = r.readLine();
  142. if (line == null) {
  143. break;
  144. } else {
  145. line = line.trim();
  146. }
  147. if (!line.equalsIgnoreCase(name)) {
  148. contents.add(line);
  149. }
  150. }
  151. r.close();
  152. BufferedWriter w = new BufferedWriter(new FileWriter(file));
  153. for (String line : contents) {
  154. w.write(line, 0, line.length());
  155. w.newLine();
  156. }
  157. w.flush();
  158. w.close();
  159. } catch (Exception e) {
  160. }
  161. }
  162.  
  163. /**
  164. * Contains banned username
  165. **/
  166. public static boolean isNamedBanned(String name) {
  167. if (bannedNames.contains(name.toLowerCase())) {
  168. return true;
  169. }
  170. return false;
  171. }
  172.  
  173. /**
  174. * Reads all usernames from text file then adds them all to the ban list
  175. **/
  176. public static void banUsers() {
  177. try {
  178. BufferedReader in = new BufferedReader(new FileReader(
  179. "./Data/bans/UsersBanned.txt"));
  180. String data = null;
  181. try {
  182. while ((data = in.readLine()) != null) {
  183. addNameToBanList(data);
  184. }
  185. } finally {
  186. in.close();
  187. }
  188. } catch (IOException e) {
  189. e.printStackTrace();
  190. }
  191. }
  192.  
  193. public static void muteUsers() {
  194. try {
  195. BufferedReader in = new BufferedReader(new FileReader(
  196. "./Data/bans/UsersMuted.txt"));
  197. String data = null;
  198. try {
  199. while ((data = in.readLine()) != null) {
  200. mutedNames.add(data);
  201. }
  202. } finally {
  203. in.close();
  204. }
  205. } catch (IOException e) {
  206. e.printStackTrace();
  207. }
  208. }
  209.  
  210. /**
  211. * Reads all the Ips from text file then adds them all to ban list
  212. **/
  213. public static void banIps() {
  214. try {
  215. BufferedReader in = new BufferedReader(new FileReader(
  216. "./Data/bans/IpsBanned.txt"));
  217. String data = null;
  218. try {
  219. while ((data = in.readLine()) != null) {
  220. addIpToBanList(data);
  221. }
  222. } finally {
  223. in.close();
  224. }
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. }
  228. }
  229.  
  230. public static void muteIps() {
  231. try {
  232. BufferedReader in = new BufferedReader(new FileReader(
  233. "./Data/bans/IpsMuted.txt"));
  234. String data = null;
  235. try {
  236. while ((data = in.readLine()) != null) {
  237. mutedIps.add(data);
  238. }
  239. } finally {
  240. in.close();
  241. }
  242. } catch (IOException e) {
  243. e.printStackTrace();
  244. }
  245. }
  246.  
  247. /**
  248. * Writes the username into the text file - when using the ::ban playername
  249. * command
  250. **/
  251. public static void addNameToFile(String Name) {
  252. try {
  253. BufferedWriter out = new BufferedWriter(new FileWriter(
  254. "./Data/bans/UsersBanned.txt", true));
  255. try {
  256. out.newLine();
  257. out.write(Name);
  258. } finally {
  259. out.close();
  260. }
  261. } catch (IOException e) {
  262. e.printStackTrace();
  263. }
  264. }
  265.  
  266. public static void addUserToFile(String Name) {
  267. try {
  268. BufferedWriter out = new BufferedWriter(new FileWriter(
  269. "./Data/bans/UsersMuted.txt", true));
  270. try {
  271. out.newLine();
  272. out.write(Name);
  273. } finally {
  274. out.close();
  275. }
  276. } catch (IOException e) {
  277. e.printStackTrace();
  278. }
  279. }
  280.  
  281. /**
  282. * Writes the IP into the text file - use ::ipban username
  283. **/
  284. public static void addIpToFile(String Name) {
  285. try {
  286. BufferedWriter out = new BufferedWriter(new FileWriter(
  287. "./Data/bans/IpsBanned.txt", true));
  288. try {
  289. out.newLine();
  290. out.write(Name);
  291. } finally {
  292. out.close();
  293. }
  294. } catch (IOException e) {
  295. e.printStackTrace();
  296. }
  297. }
  298.  
  299. public static void addIpToMuteFile(String Name) {
  300. try {
  301. BufferedWriter out = new BufferedWriter(new FileWriter(
  302. "./Data/bans/IpsMuted.txt", true));
  303. try {
  304. out.newLine();
  305. out.write(Name);
  306. } finally {
  307. out.close();
  308. }
  309. } catch (IOException e) {
  310. e.printStackTrace();
  311. }
  312. }
  313.  
  314. public static boolean isMuted(Client c) {
  315. if (mutedNames.contains(c.playerName.toLowerCase())
  316. || mutedIps.contains(c.connectedFrom))
  317. return true;
  318. else
  319. return false;
  320. }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement