/
Client.java
221 lines (207 loc) · 7.86 KB
/
Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package net.ME1312.SubServers.Bungee.Network;
import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection;
import net.ME1312.SubServers.Bungee.Library.Exception.IllegalPacketException;
import net.ME1312.SubServers.Bungee.Library.Util;
import net.ME1312.SubServers.Bungee.Network.Packet.PacketAuthorization;
import org.msgpack.core.MessageInsufficientBufferException;
import org.msgpack.core.MessagePack;
import org.msgpack.core.MessagePacker;
import org.msgpack.core.MessageUnpacker;
import org.msgpack.value.Value;
import org.yaml.snakeyaml.error.YAMLException;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.*;
/**
* Network Client Class
*/
public class Client {
private Socket socket;
private InetSocketAddress address;
private ClientHandler handler;
private MessagePacker out;
private Timer authorized;
private SubDataServer subdata;
boolean closed;
/**
* Network Client
*
* @param subdata SubData Direct Server
* @param client Socket to Bind
*/
public Client(SubDataServer subdata, Socket client) throws IOException {
if (Util.isNull(subdata, client)) throw new NullPointerException();
this.subdata = subdata;
closed = false;
socket = client;
out = MessagePack.newDefaultPacker(client.getOutputStream());
address = new InetSocketAddress(client.getInetAddress(), client.getPort());
authorized = new Timer();
authorized.schedule(new TimerTask() {
@Override
public void run() {
if (!socket.isClosed()) try {
subdata.removeClient(Client.this);
} catch (IOException e) {
e.printStackTrace();
}
}
}, 15000);
loop();
}
/**
* Network Loop
*/
private void loop() {
new Thread(() -> {
try {
MessageUnpacker in = MessagePack.newDefaultUnpacker(socket.getInputStream());
Value input;
while ((input = in.unpackValue()) != null) {
try {
YAMLSection data = subdata.getCipher().decrypt(subdata.plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), input);
for (PacketIn packet : SubDataServer.decodePacket(this, data)) {
boolean auth = authorized == null;
if (auth || packet instanceof PacketAuthorization) {
try {
if (data.contains("f")) {
if (data.getString("f").length() <= 0) {
List<Client> clients = new ArrayList<Client>();
clients.addAll(subdata.getClients());
for (Client client : clients) {
client.out.packValue(input);
}
} else {
Client client = subdata.getClient(data.getString("f"));
if (client != null) {
client.out.packValue(input);
} else {
throw new IllegalPacketException(getAddress().toString() + ": Unknown Forward Address: " + data.getString("f"));
}
}
} else {
packet.execute(Client.this, (data.contains("c")) ? data.getSection("c") : null);
}
} catch (Throwable e) {
new InvocationTargetException(e, getAddress().toString() + ": Exception while executing PacketIn").printStackTrace();
}
} else {
sendPacket(new PacketAuthorization(-1, "Unauthorized"));
throw new IllegalPacketException(getAddress().toString() + ": Unauthorized call to packet type: " + data.getSection("h"));
}
}
} catch (YAMLException e) { // TODO
new IllegalPacketException(getAddress().toString() + ": Unknown Packet Format: " + input).printStackTrace();
} catch (IllegalPacketException e) {
e.printStackTrace();
} catch (Exception e) {
new InvocationTargetException(e, getAddress().toString() + ": Exception while decoding packet").printStackTrace();
}
}
try {
subdata.removeClient(Client.this);
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (Exception e) {
if (!(e instanceof SocketException || e instanceof MessageInsufficientBufferException)) e.printStackTrace();
try {
subdata.removeClient(Client.this);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}).start();
}
/**
* Authorize Connection
*/
public void authorize() {
if (authorized != null) {
authorized.cancel();
System.out.println("SubData > " + socket.getRemoteSocketAddress().toString() + " logged in");
}
authorized = null;
}
/**
* Send Packet to Client
*
* @param packet Packet to send
*/
public void sendPacket(PacketOut packet) {
if (Util.isNull(packet)) throw new NullPointerException();
try {
out.packValue(subdata.getCipher().encrypt(subdata.plugin.config.get().getSection("Settings").getSection("SubData").getRawString("Password"), SubDataServer.encodePacket(this, packet)));
out.flush();
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
* Get Raw Connection
*
* @return Socket
*/
public Socket getConnection() {
return socket;
}
/**
* Get if the connection has been closed
*
* @return Closed Stauts
*/
public boolean isClosed() {
return closed && socket.isClosed();
}
/**
* Get Remote Address
*
* @return Address
*/
public InetSocketAddress getAddress() {
return address;
}
/**
* If the connection is authorized
*
* @return Authorization Status
*/
public boolean isAuthorized() {
return authorized == null;
}
/**
* Gets the Linked Handler
*
* @return Handler
*/
public ClientHandler getHandler() {
return handler;
}
/**
* Sets the Handler
*
* @param obj Handler
*/
public void setHandler(ClientHandler obj) {
if (handler != null && handler.getSubData() != null && equals(handler.getSubData())) handler.setSubData(null);
handler = obj;
if (handler != null && (handler.getSubData() == null || !equals(handler.getSubData()))) handler.setSubData(this);
}
/**
* Disconnects the Client
*
* @throws IOException
*/
public void disconnect() throws IOException {
if (!socket.isClosed()) getConnection().close();
if (handler != null) {
setHandler(null);
handler = null;
}
closed = true;
if (subdata.getClients().contains(this)) subdata.removeClient(this);
}
}