Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.30 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.DataInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.ArrayList;
  11. import java.util.Properties;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.SQLException;
  15. import java.util.concurrent.TimeUnit;
  16. import java.io.*;
  17.  
  18. public class server implements Runnable {
  19. public static final int CYCLE_TIME = 600;
  20. public static final int S_CYCLE_TIME = CYCLE_TIME;
  21. public static MultiThreader MultiThreader = new MultiThreader();
  22. public static MultiThreader getMultiThreaderCore() {
  23. return MultiThreader;
  24. }
  25. public static boolean pickup = true;
  26. public static boolean fightcaves = true;
  27. public static ArrayList<String> banned = new ArrayList<String>();
  28. public static ArrayList<Integer> bannedUid = new ArrayList<Integer>();
  29. public static server clientHandler = null; // handles all the clients
  30. public static java.net.ServerSocket clientListener = null;
  31. public static int MaxConnections = 100000;
  32. public static int[] ConnectionCount = new int[MaxConnections];
  33. public static ArrayList<String> connections = new ArrayList<String>();
  34. public static String[] Connections = new String[MaxConnections];
  35. //public static final int cycleTime = 500;
  36. public static int delay = 50;
  37. public static long delayUpdate = 0, lastRunite = 0;
  38. public static DoorHandler doorHandler;
  39. public static int EnergyRegian = 60;
  40. public static boolean enforceClient = false;
  41. public static PlayerHandler playerHandler = null;
  42. public static GraphicsHandler GraphicsHandler = null;
  43. public static ItemHandler itemHandler = null;
  44. public static server s = null;
  45. public static java.lang.Object a = new java.lang.Object();
  46. //public static checkPlayerCapes checkPlayerCapes = null;
  47. /*handlers made by Delta*/
  48. public static Wearing Wearing = null;
  49. public static special special = null;
  50. public static setConfig setConfig = null;
  51. public static textHandler textHandler = null;
  52. public static potions potions = null;
  53. public static clickingMost clickingMost = null;
  54. /*end of handlers made by Delta*/
  55. public static boolean loginServerConnected = true;
  56. public static NPCHandler npcHandler = null;
  57.  
  58.  
  59.  
  60. static UpTime u = new UpTime();
  61.  
  62.  
  63.  
  64. public static ArrayList<Object> objects = new ArrayList<Object>();
  65.  
  66. public static int[][] runesRequired = new int[24][9];
  67. public static int serverlistenerPort = 6666; // 43594=default
  68. public static ShopHandler shopHandler = null;
  69. public static boolean ShutDown = false;
  70. public static boolean shutdownClientHandler; // signals ClientHandler to shut
  71. // down
  72. public static int ShutDownCounter = 0;
  73. public static boolean shutdownServer = false; // set this to true in order to
  74. // shut down and kill the server
  75. public static long startTime;
  76. // TODO: yet to figure out proper value for timing, but 500 seems good
  77. public static boolean trading = true, dueling = true, pking = true;
  78. public static int updateSeconds = 1800; // 180 because it doesnt make the
  79. // time jump at the start :P
  80.  
  81. public static boolean updateServer = false;
  82.  
  83. public static int world = 1;
  84.  
  85. private static Connection conn;
  86. private static final String HOST = "127.0.0.1";
  87. private static final String USERNAME = "";
  88. private static final String PASSWORD = "";
  89. private static final String DATABASE = "";
  90.  
  91. /*
  92. public static void calcTime() {
  93. long curTime = System.currentTimeMillis();
  94. updateSeconds = 180 - ((int) (curTime - startTime) / 1000);
  95. if (updateSeconds == 0) {
  96. shutdownServer = true;
  97. }
  98. }
  99. */
  100. public static void calcTime() {
  101. long curTime = System.currentTimeMillis();
  102. updateSeconds = 1800 - ((int) (curTime - startTime) / 1800);
  103. if (updateSeconds == 0) {
  104. shutdownServer = true;
  105. }
  106. }
  107.  
  108. public static void logError(String message) {
  109. misc.println(message);
  110. }
  111.  
  112. public static void main(java.lang.String args[])
  113. throws NullPointerException {
  114. try {
  115. (new Thread(new Network())).start();
  116. s.playerHandler = new PlayerHandler();
  117. s.MultiThreader = new MultiThreader();
  118. s.getMultiThreaderCore().getServerLogicExecutor().scheduleAtFixedRate(new MultiThreadedCoreExecutor(), 0, S_CYCLE_TIME, TimeUnit.MILLISECONDS);
  119. File f = new File("server.ini");
  120. if (!f.exists()) {
  121. misc.println("server.ini doesn't exist!");
  122. }
  123. Properties p = new Properties();
  124. p.load(new FileInputStream("./server.ini"));
  125. int client = Integer.parseInt(p.getProperty("ClientRequired")
  126. .trim());
  127. world = Integer.parseInt(p.getProperty("WorldId"));
  128. serverlistenerPort = Integer.parseInt(p.getProperty("ServerPort")
  129. .trim());
  130. if (client > 0) {
  131. misc.println("Enforcing Devolution client requirement");
  132. enforceClient = true;
  133. }
  134. } catch (Exception e) {
  135. misc.println("Error loading settings");
  136. e.printStackTrace();
  137. }
  138.  
  139. connectToDatabase();
  140. clientHandler = new server();
  141. (new Thread(clientHandler)).start(); // launch server listener
  142. npcHandler = new NPCHandler();
  143. itemHandler = new ItemHandler();
  144. doorHandler = new DoorHandler();
  145. GraphicsHandler = new GraphicsHandler();
  146. //checkPlayerCapes = new checkPlayerCapes();
  147. /*handlers made by Delta*/
  148. Wearing = new Wearing();
  149. special = new special();
  150. setConfig = new setConfig();
  151. textHandler = new textHandler();
  152. potions = new potions();
  153. clickingMost = new clickingMost();
  154. /*end of handlers made by Delta*/
  155. if (itemHandler == null) {
  156. misc.println("ERROR NULL");
  157. }
  158. shopHandler = new ShopHandler();
  159. GraphicsHandler = new GraphicsHandler();
  160. process proc = new process();
  161. new Thread(proc).start();
  162. /*
  163. * int waitFails = 0; long lastTicks = System.currentTimeMillis(); long
  164. * totalTimeSpentProcessing = 0; int cycle = 0; while(!shutdownServer) {
  165. * if(updateServer) calcTime(); // could do game updating stuff in here... //
  166. * maybe do all the major stuff here in a big loop and just do the packet //
  167. * sending/receiving in the client subthreads. The actual packet forming
  168. * code // will reside within here and all created packets are then relayed
  169. * by the subthreads. // This way we avoid all the sync'in issues // The
  170. * rough outline could look like: playerHandler.process(); // updates all
  171. * player related stuff npcHandler.process(); itemHandler.process();
  172. * shopHandler.process(); objectHandler.process();
  173. * objectHandler.firemaking_process(); // doNpcs() // all npc related stuff //
  174. * doObjects() // doWhatever()
  175. * // taking into account the time spend in the processing code for more
  176. * accurate timing long timeSpent = System.currentTimeMillis() - lastTicks;
  177. * totalTimeSpentProcessing += timeSpent; if(timeSpent >= cycleTime) {
  178. * timeSpent = cycleTime; if(++waitFails > 100) { shutdownServer = true;
  179. * misc.println("[KERNEL]: machine is too slow to run this server!"); } }
  180. * try { Thread.sleep(cycleTime-timeSpent); } catch(java.lang.Exception _ex) { }
  181. * lastTicks = System.currentTimeMillis(); cycle++; if(cycle % 100 == 0) {
  182. * float time = ((float)totalTimeSpentProcessing)/cycle;
  183. * misc.println_debug("[KERNEL]: "+(time*100/cycleTime)+"% processing
  184. * time"); } if (ShutDown == true) { if (ShutDownCounter >= 100) {
  185. * shutdownServer = true; } ShutDownCounter++; } }
  186. * // shut down the server playerHandler.destruct();
  187. * clientHandler.killServer(); clientHandler = null;
  188. */
  189. }
  190. public int[] ips = new int[1000];
  191.  
  192. public long[] lastConnect = new long[1000];
  193.  
  194. public server() {
  195. // the current way of controlling the server at runtime and a great
  196. // debugging/testing tool
  197. // jserv js = new jserv(this);
  198. // js.start();
  199.  
  200. }
  201.  
  202. public void banHost(String host, int num) {
  203. if (false) {
  204. banned.add(host);
  205. } else {
  206. try {
  207. misc.println("BANNING HOST " + host + " (flooding)"); // What should we change this to? This is causing our crashes. Its set to 60 seconds.
  208. banned.add(host);
  209. delay = 2000;
  210. delayUpdate = System.currentTimeMillis() + 60000;
  211. } catch (Exception e) {
  212. e.printStackTrace();
  213. }
  214. }
  215. }
  216.  
  217. public boolean checkHost(String host) {
  218. for (String h : banned) {
  219. if (h.equals(host))
  220. return false;
  221. }
  222. int num = 0;
  223. for (String h : connections) {
  224. if (host.equals(h)) {
  225. num++;
  226. }
  227. }
  228. if (num > 10) {
  229. banHost(host, num);
  230. return false;
  231. }
  232.  
  233. if (checkLog("ipbans", host)
  234. || host.startsWith("74.115.0")
  235. || host.startsWith("74.115.1")
  236. || host.startsWith("74.115.2")
  237. || host.startsWith("74.115.3")
  238. || host.startsWith("74.115.4")
  239. || host.startsWith("74.115.5")
  240. || host.startsWith("74.115.6")
  241. || host.startsWith("74.115.7")) {
  242. System.out.println("They are in ip ban list!");
  243. return false; // ip ban added by bakatool
  244. }
  245. return true;
  246. }
  247.  
  248. public boolean checkLog(String file, String playerName) {
  249. // check ipbans -bakatool
  250. try {
  251. BufferedReader in = new BufferedReader(new FileReader("config//"
  252. + file + ".txt"));
  253. String data = null;
  254. while ((data = in.readLine()) != null) {
  255. if (playerName.equalsIgnoreCase(data)) {
  256. return true;
  257. }
  258. }
  259. } catch (IOException e) {
  260. System.out.println("Critical error while checking for data!");
  261. System.out.println(file + ":" + playerName);
  262. e.printStackTrace();
  263. }
  264. return false;
  265. }
  266.  
  267. public int getConnections(String host) {
  268. int count = 0;
  269. for (Player p : PlayerHandler.players) {
  270. if ((p != null) && !p.disconnected
  271. && p.connectedFrom.equalsIgnoreCase(host)) {
  272. count++;
  273. }
  274. }
  275. return count;
  276. }
  277.  
  278. public void killServer() {
  279. try {
  280. shutdownClientHandler = true;
  281. if (clientListener != null)
  282. clientListener.close();
  283. clientListener = null;
  284. } catch (java.lang.Exception __ex) {
  285. __ex.printStackTrace();
  286. }
  287. }
  288.  
  289. public void run() {
  290. // setup the listener
  291. try {
  292. shutdownClientHandler = false;
  293. clientListener = new java.net.ServerSocket(serverlistenerPort, 10, null);
  294. misc.println("YanilleScape Server");
  295. while (true) {
  296. try {
  297. java.net.Socket s = clientListener.accept();
  298. s.setTcpNoDelay(true);
  299. String connectingHost = s.getInetAddress().getHostName();
  300. if ( /*
  301. * connectingHost.startsWith("localhost") ||
  302. * connectingHost.equals("127.0.0.1")
  303. */true) {
  304. if (connectingHost.contains("74-129-182-147.dhcp.insightbb.com")) {
  305. misc.println("Checking Server Status...");
  306. s.close();
  307. } else {
  308. connections.add(connectingHost);
  309. if (checkHost(connectingHost)) {
  310. misc.println("Connection from "
  311. + connectingHost + ":" + s.getPort());
  312. playerHandler.newPlayerClient(s, connectingHost);
  313. } else {
  314. misc.println("ClientHandler: Rejected "
  315. + connectingHost + ":" + s.getPort());
  316. s.close();
  317. }
  318. }
  319. } else {
  320. misc.println("ClientHandler: Rejected "
  321. + connectingHost + ":" + s.getPort());
  322. s.close();
  323. }
  324. if ((delayUpdate > 0)
  325. && (System.currentTimeMillis() > delayUpdate)) {
  326. delay = 50;
  327. delayUpdate = 0;
  328. }
  329. } catch (Exception e) {
  330. logError(e.getMessage());
  331. }
  332. }
  333. } catch (java.io.IOException ioe) {
  334. if (!shutdownClientHandler) {
  335. misc.println("Server is already in use.");
  336. } else {
  337. misc.println("ClientHandler was shut down.");
  338. }
  339. }
  340. }
  341.  
  342. public static void connectToDatabase() {
  343. try {
  344. Class.forName("com.mysql.jdbc.Driver").newInstance();
  345. } catch (InstantiationException e) {
  346. e.printStackTrace();
  347. } catch (IllegalAccessException e) {
  348. e.printStackTrace();
  349. } catch (ClassNotFoundException e) {
  350. e.printStackTrace();
  351. }
  352. try {
  353. conn = DriverManager.getConnection("jdbc:mysql://" + HOST + "/" + DATABASE + "?" + "user=" + USERNAME + "&password=" + PASSWORD);
  354. System.out.println("Connected to Player database");
  355. } catch (SQLException ex) {
  356. System.out.println("SQLException: " + ex.getMessage());
  357. System.out.println("SQLState: " + ex.getSQLState());
  358. }
  359. }
  360.  
  361. public static Connection connection() {
  362. try {
  363. if (conn.isClosed())
  364. connectToDatabase();
  365. } catch (SQLException e) {}
  366.  
  367. return conn;
  368. }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement