Skip to content

Commit 513f0bb

Browse files
committed
Fix security error
Threads -> BungeeCord scheduler
1 parent 4388f5c commit 513f0bb

7 files changed

Lines changed: 275 additions & 310 deletions

File tree

CommandSyncServer/src/com/fuzzoland/CommandSyncServer/CSS.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
package com.fuzzoland.CommandSyncServer;
22

3-
import java.io.BufferedReader;
4-
import java.io.File;
5-
import java.io.FileOutputStream;
6-
import java.io.FileReader;
7-
import java.io.IOException;
8-
import java.io.OutputStream;
9-
import java.io.PrintStream;
10-
import java.net.InetAddress;
11-
import java.net.ServerSocket;
12-
import java.util.ArrayList;
13-
import java.util.Arrays;
14-
import java.util.Collections;
15-
import java.util.HashMap;
16-
import java.util.HashSet;
17-
import java.util.List;
18-
import java.util.Map;
19-
import java.util.Map.Entry;
20-
import java.util.Set;
21-
3+
import com.fuzzoland.CommandSyncServer.Metrics.Graph;
224
import net.md_5.bungee.api.ProxyServer;
235
import net.md_5.bungee.api.plugin.Plugin;
246

25-
import com.fuzzoland.CommandSyncServer.Metrics.Graph;
7+
import java.io.*;
8+
import java.net.InetAddress;
9+
import java.net.ServerSocket;
10+
import java.util.*;
11+
import java.util.Map.Entry;
12+
import java.util.concurrent.TimeUnit;
2613

2714
public class CSS extends Plugin {
2815

29-
public ServerSocket server;
16+
public static List<String> oq = Collections.synchronizedList(new ArrayList<String>());
17+
public static String spacer = "@#@";
18+
public ServerSocket server;
3019
public Set<String> c = Collections.synchronizedSet(new HashSet<String>());
31-
public static List<String> oq = Collections.synchronizedList(new ArrayList<String>());
3220
public Map<String, List<String>> pq = Collections.synchronizedMap(new HashMap<String, List<String>>());
3321
public Map<String, Integer> qc = Collections.synchronizedMap(new HashMap<String, Integer>());
34-
public static String spacer = "@#@";
3522
public Debugger debugger;
36-
23+
3724
public void onEnable() {
3825
String[] data = loadConfig();
3926
if(data[3].equals("UNSET")) {
@@ -43,8 +30,8 @@ public void onEnable() {
4330
try {
4431
server = new ServerSocket(Integer.parseInt(data[1]), 50, InetAddress.getByName(data[0]));
4532
debugger.debug("Opened server on " + data[0] + ":" + data[1] + ".");
46-
new ClientListener(this, Integer.parseInt(data[2]), data[3]).start();
47-
} catch(Exception e) {
33+
this.getProxy().getScheduler().schedule(this, new ClientListener(this, Integer.parseInt(data[2]), data[3]), 0, 100, TimeUnit.MILLISECONDS);
34+
} catch(Exception e) {
4835
e.printStackTrace();
4936
}
5037
loadData();
Lines changed: 87 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fuzzoland.CommandSyncServer;
22

3+
import net.md_5.bungee.api.connection.ProxiedPlayer;
4+
35
import java.io.BufferedReader;
46
import java.io.IOException;
57
import java.io.InputStreamReader;
@@ -9,9 +11,7 @@
911
import java.util.Arrays;
1012
import java.util.List;
1113

12-
import net.md_5.bungee.api.connection.ProxiedPlayer;
13-
14-
public class ClientHandler extends Thread {
14+
public class ClientHandler implements Runnable {
1515

1616
private CSS plugin;
1717
private Socket socket;
@@ -21,13 +21,15 @@ public class ClientHandler extends Thread {
2121
private String name;
2222
private String pass;
2323
private String version = "2.3";
24-
24+
private Boolean active = false;
25+
2526
public ClientHandler(CSS plugin, Socket socket, Integer heartbeat, String pass) throws IOException {
2627
this.plugin = plugin;
2728
this.socket = socket;
2829
this.heartbeat = heartbeat;
2930
this.pass = pass;
30-
out = new PrintWriter(socket.getOutputStream(), true);
31+
this.active = true;
32+
out = new PrintWriter(socket.getOutputStream(), true);
3133
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
3234
plugin.debugger.debug("Received new connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + ".");
3335
name = in.readLine();
@@ -62,83 +64,84 @@ public ClientHandler(CSS plugin, Socket socket, Integer heartbeat, String pass)
6264
}
6365

6466
public void run() {
65-
while(true) {
66-
try {
67-
out.println("heartbeat");
68-
if(out.checkError()) {
69-
plugin.debugger.debug("Connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " under name " + name + " has disconnected.");
70-
plugin.c.remove(name);
71-
return;
72-
}
73-
while(in.ready()) {
74-
String input = in.readLine();
75-
if(!input.equals("heartbeat")) {
76-
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Received input - " + input);
77-
String[] data = input.split(plugin.spacer);
78-
if(data[0].equals("player")) {
79-
String command = "/" + data[2].replaceAll("\\+", " ");
80-
if(data[1].equals("single")) {
81-
String name = data[3];
82-
Boolean found = false;
83-
for(ProxiedPlayer player : plugin.getProxy().getPlayers()) {
84-
if(name.equals(player.getName())){
85-
player.chat(command);
86-
plugin.debugger.debug("Ran command " + command + " for player " + name + ".");
87-
found = true;
88-
break;
89-
}
90-
}
91-
if(!found) {
92-
if(plugin.pq.containsKey(name)) {
93-
List<String> commands = plugin.pq.get(name);
94-
commands.add(command);
95-
plugin.pq.put(name, commands);
96-
} else {
97-
plugin.pq.put(name, new ArrayList<String>(Arrays.asList(command)));
98-
}
99-
plugin.debugger.debug(" Since " + name + " is offline the command " + command + " will run when they come online.");
100-
}
101-
} else if(data[1].equals("all")) {
102-
for(ProxiedPlayer player : plugin.getProxy().getPlayers()) {
103-
player.chat(command);
104-
}
105-
plugin.debugger.debug("Ran command " + command + " for all online players.");
106-
}
107-
} else {
108-
if(data[1].equals("bungee")) {
109-
String command = data[2].replaceAll("\\+", " ");
110-
plugin.getProxy().getPluginManager().dispatchCommand(plugin.getProxy().getConsole(), command);
111-
plugin.debugger.debug("Ran command /" + command + ".");
112-
} else {
113-
plugin.oq.add(input);
114-
}
115-
}
116-
}
117-
}
118-
Integer size = plugin.oq.size();
119-
Integer count = plugin.qc.get(name);
120-
if(size > count) {
121-
for(int i = count; i < size; i++) {
122-
count++;
123-
String output = plugin.oq.get(i);
124-
String[] data = output.split(plugin.spacer);
125-
if(data[1].equals("single")) {
126-
if(data[3].equals(name)) {
127-
out.println(output);
128-
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Sent output - " + output);
129-
}
130-
} else {
131-
out.println(output);
132-
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Sent output - " + output);
133-
}
134-
}
135-
plugin.qc.put(name, count);
136-
}
137-
sleep(heartbeat);
138-
} catch(Exception e) {
139-
plugin.c.remove(name);
140-
e.printStackTrace();
141-
}
142-
}
143-
}
67+
if (active) {
68+
try {
69+
out.println("heartbeat");
70+
if (out.checkError()) {
71+
plugin.debugger.debug("Connection from " + socket.getInetAddress().getHostName() + ":" + socket.getPort() + " under name " + name + " has disconnected.");
72+
plugin.c.remove(name);
73+
active = false;
74+
return;
75+
}
76+
if (in.ready()) {
77+
String input = in.readLine();
78+
if (!input.equals("heartbeat")) {
79+
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Received input - " + input);
80+
String[] data = input.split(CSS.spacer);
81+
if (data[0].equals("player")) {
82+
String command = "/" + data[2].replaceAll("\\+", " ");
83+
if (data[1].equals("single")) {
84+
String name = data[3];
85+
Boolean found = false;
86+
for (ProxiedPlayer player : plugin.getProxy().getPlayers()) {
87+
if (name.equals(player.getName())) {
88+
player.chat(command);
89+
plugin.debugger.debug("Ran command " + command + " for player " + name + ".");
90+
found = true;
91+
break;
92+
}
93+
}
94+
if (!found) {
95+
if (plugin.pq.containsKey(name)) {
96+
List<String> commands = plugin.pq.get(name);
97+
commands.add(command);
98+
plugin.pq.put(name, commands);
99+
} else {
100+
plugin.pq.put(name, new ArrayList<String>(Arrays.asList(command)));
101+
}
102+
plugin.debugger.debug(" Since " + name + " is offline the command " + command + " will run when they come online.");
103+
}
104+
} else if (data[1].equals("all")) {
105+
for (ProxiedPlayer player : plugin.getProxy().getPlayers()) {
106+
player.chat(command);
107+
}
108+
plugin.debugger.debug("Ran command " + command + " for all online players.");
109+
}
110+
} else {
111+
if (data[1].equals("bungee")) {
112+
String command = data[2].replaceAll("\\+", " ");
113+
plugin.getProxy().getPluginManager().dispatchCommand(plugin.getProxy().getConsole(), command);
114+
plugin.debugger.debug("Ran command /" + command + ".");
115+
} else {
116+
CSS.oq.add(input);
117+
}
118+
}
119+
}
120+
}
121+
Integer size = CSS.oq.size();
122+
Integer count = plugin.qc.get(name);
123+
if (size > count) {
124+
for (int i = count; i < size; i++) {
125+
count++;
126+
String output = CSS.oq.get(i);
127+
String[] data = output.split(CSS.spacer);
128+
if (data[1].equals("single")) {
129+
if (data[3].equals(name)) {
130+
out.println(output);
131+
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Sent output - " + output);
132+
}
133+
} else {
134+
out.println(output);
135+
plugin.debugger.debug("[" + socket.getInetAddress().getHostName() + ":" + socket.getPort() + "] [" + name + "] Sent output - " + output);
136+
}
137+
}
138+
plugin.qc.put(name, count);
139+
}
140+
} catch (Exception e) {
141+
plugin.c.remove(name);
142+
active = false;
143+
e.printStackTrace();
144+
}
145+
}
146+
}
144147
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.fuzzoland.CommandSyncServer;
22

33
import java.io.IOException;
4+
import java.util.concurrent.TimeUnit;
45

5-
public class ClientListener extends Thread {
6+
public class ClientListener implements Runnable {
67

78
private CSS plugin;
89
private Integer heartbeat;
@@ -15,12 +16,10 @@ public ClientListener(CSS plugin, Integer heartbeat, String pass) {
1516
}
1617

1718
public void run() {
18-
while(true) {
19-
try {
20-
new ClientHandler(plugin, plugin.server.accept(), heartbeat, pass).start();
21-
} catch(IOException e) {
22-
e.printStackTrace();
23-
}
24-
}
19+
try {
20+
this.plugin.getProxy().getScheduler().schedule(this.plugin, new ClientHandler(this.plugin, this.plugin.server.accept(), heartbeat, pass), 0, heartbeat, TimeUnit.MILLISECONDS);
21+
} catch (IOException e) {
22+
e.printStackTrace();
23+
}
2524
}
2625
}
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.fuzzoland.CommandSyncServer;
22

3-
import java.util.List;
4-
53
import net.md_5.bungee.api.connection.ProxiedPlayer;
64

7-
public class CommandThread extends Thread {
5+
import java.util.List;
6+
7+
public class CommandThread implements Runnable {
88

99
private CSS plugin;
1010
private ProxiedPlayer player;
@@ -19,21 +19,15 @@ public CommandThread(CSS plugin, ProxiedPlayer player) {
1919
}
2020

2121
public void run() {
22-
while(true) {
23-
try {
24-
for(String command : commands) {
25-
player.chat(command);
26-
plugin.debugger.debug("Ran command " + command + " for player " + name + ".");
27-
}
28-
plugin.pq.remove(name);
29-
return;
30-
} catch(IllegalStateException e1) {
31-
try {
32-
sleep(1000);
33-
} catch(InterruptedException e2) {
34-
e2.printStackTrace();
35-
}
36-
}
37-
}
22+
try {
23+
for (String command : commands) {
24+
player.chat(command);
25+
plugin.debugger.debug("Ran command " + command + " for player " + name + ".");
26+
}
27+
plugin.pq.remove(name);
28+
return;
29+
} catch (IllegalStateException e1) {
30+
e1.printStackTrace();
31+
}
3832
}
3933
}

CommandSyncServer/src/com/fuzzoland/CommandSyncServer/Debugger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
public class Debugger {
1111

12+
private final Boolean log;
1213
private PrintStream ps;
13-
private Boolean log;
1414

1515
public Debugger(CSS plugin, Boolean log) {
1616
this.log = log;

CommandSyncServer/src/com/fuzzoland/CommandSyncServer/EventListener.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55
import net.md_5.bungee.api.plugin.Listener;
66
import net.md_5.bungee.event.EventHandler;
77

8+
import java.util.concurrent.TimeUnit;
9+
810
public class EventListener implements Listener {
911

1012
private CSS plugin;
11-
13+
1214
public EventListener(CSS plugin) {
1315
this.plugin = plugin;
1416
}
15-
17+
1618
@EventHandler
1719
public void onServerConnected(ServerConnectedEvent event) {
1820
ProxiedPlayer player = event.getPlayer();
1921
if(plugin.pq.containsKey(player.getName())) {
20-
new CommandThread(plugin, player).start();
22+
this.plugin.getProxy().getScheduler().schedule(this.plugin, new CommandThread(plugin, player), 0, 100, TimeUnit.MILLISECONDS);
2123
}
2224
}
2325
}

0 commit comments

Comments
 (0)