-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
298 lines (245 loc) · 8.15 KB
/
Copy pathChatServer.java
File metadata and controls
298 lines (245 loc) · 8.15 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetSocketAddress;
import java.net.BindException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import java.util.ArrayList;
import java.util.UUID;
import java.net.SocketAddress;
import java.net.SocketException;
import java.lang.Integer;
import java.io.DataInputStream;
import java.io.PrintStream;
import java.io.InputStreamReader;
import javax.crypto.*;//for crypto
import java.security.*;//for crypto
import java.security.spec.*;
import javax.crypto.spec.*;
/**
* Group members:
* Justin Dorman: DRMJUS001
* Zachary Bresler: BRSZAC002
* Chad Piha: PHXCHA001
* Emil Kenguerli: KNGEMI001
*/
public class ChatServer {
private static ServerSocket serverSocket;
private static int portNumber = 60123;
private static ConcurrentHashMap<UUID,ServiceConnection> connections = new ConcurrentHashMap<UUID,ServiceConnection>();
private static ConcurrentHashMap<UUID,ChatGroup> groups = new ConcurrentHashMap<UUID,ChatGroup>();
public static void main(String args[]){
generateCAKeys(); //creates CA public and private key pair
initialiseServerSocket();
acceptClients();
}
public static void initialiseServerSocket(){
try{
serverSocket = new ServerSocket(portNumber);
}catch(IOException e){
System.out.println(e);
}
}
//accepts client connections
public static void acceptClients(){
while(true){
try{
Socket serviceSocket = serverSocket.accept();
UUID clientId = UUID.randomUUID();
ServiceConnection connection = new ServiceConnection(clientId, serviceSocket);
connection.start();
connections.put(clientId, connection);
}catch(IOException e){
System.out.println(e);
}
}
}
//Generates the public and private key pair for the CA
public static void generateCAKeys()
{
try {
//create private and public keys for CA
System.out.println("\nCREATING CA PRIVATE AND PUBLIC KEY PAIR:");
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair keys = keyGen.generateKeyPair();
//get the key from the generator
PrivateKey caPrivate = keys.getPrivate();
PublicKey caPublic = keys.getPublic();
//convert to bytes
byte[] privateKeyArray = caPrivate.getEncoded();
byte[] publicKeyArray = caPublic.getEncoded();
//write out the public key to a file
System.out.println("\n\tWriting Public Key to file \"CA_public_key.txt\"");
FileOutputStream fosPublic = new FileOutputStream("CA_public_key.txt");
fosPublic.write(publicKeyArray);
fosPublic.close();
//write out the private key to a file
System.out.println("\n\tWriting Private Key to file \"CA_private_key.txt\" \n");
FileOutputStream fosPrivate = new FileOutputStream("CA_private_key.txt");
fosPrivate.write(privateKeyArray);
fosPrivate.close();
}
catch (Exception e) {
System.err.println(e);
}
}
public static ChatGroup addConnectionToNewGroup(String groupName, String password, ServiceConnection connection){
for(ChatGroup group : groups.values()) {
if(group.getGroupName().equals(groupName)) {
return null;
}
}
UUID groupId = UUID.randomUUID();
ChatGroup searchGroup = new ChatGroup(groupName, password, groupId);
searchGroup.addConnection(connection);
groups.put(groupId, searchGroup);
return searchGroup;
}
public static ChatGroup addConnectionToExistingGroup(String groupName, String password, ServiceConnection connection){
ChatGroup searchGroup = null;
for(ChatGroup group : groups.values()) {
if(group.getGroupName().equals(groupName)) {
if(!password.equals(group.getPassword())){
return null;
}else{
searchGroup = group;
connection.setGroupId(group.getGroupId());
searchGroup.addConnection(connection);
break;
}
}
}
System.out.println("Correct function call: " + searchGroup);
return searchGroup;
}
public static ChatGroup getGroup(UUID groupId){
return groups.get(groupId);
}
public static ConcurrentHashMap<UUID,ChatGroup> getGroups(){
return groups;
}
public static ConcurrentHashMap<UUID,ServiceConnection> getConnections(){
return connections;
}
}
//Controls connections to clients
class ServiceConnection extends Thread {
private UUID clientId;
private UUID groupId;
private Socket serviceSocket;
private SocketAddress remoteSocketAddress;
private DataOutputStream out;
private DataInputStream in;
private ChatGroup group;
public ServiceConnection(UUID clientId, Socket serviceSocket){
this.clientId = clientId;
this.serviceSocket = serviceSocket;
this.remoteSocketAddress = serviceSocket.getRemoteSocketAddress();
}
public void run(){
try{
out = new DataOutputStream(serviceSocket.getOutputStream());
in = new DataInputStream(new BufferedInputStream(serviceSocket.getInputStream()));
System.out.println("Just connected to " + remoteSocketAddress);
while(!serviceSocket.isClosed()){
String instruction = in.readUTF();
System.out.println(remoteSocketAddress);
String groupName;
String password;
switch(instruction){
case "set_group_name --new":
groupName = in.readUTF();
password = in.readUTF();
group = ChatServer.addConnectionToNewGroup(groupName,password, this);
if(group == null){
out.writeUTF("false");
}else{
out.writeUTF("true");
}
break;
case "set_group_name --existing":
groupName = in.readUTF();
password = in.readUTF();
group = ChatServer.addConnectionToExistingGroup(groupName,password, this);
if(group == null){
out.writeUTF("false");
}else{
out.writeUTF("true");
}
break;
case "client_out_message --true":
byte[] message = null;
int msgLength = in.readInt();
if (msgLength >0)
{
message = new byte[msgLength];
in.readFully(message, 0, message.length);
}
group.sendMessages(message);
break;
}
}
System.out.println("Connection with " + remoteSocketAddress + " is now closed");
}catch(IOException e){
System.out.println(e);
}
}
public void setGroupId(UUID groupId){
this.groupId = groupId;
}
public UUID getGroupId(){
return groupId;
}
public SocketAddress getRemoteSocketAddress(){
return this.remoteSocketAddress;
}
public DataOutputStream getWriter(){
return out;
}
public UUID getClientId(){
return clientId;
}
}
//Controls chat rooms
class ChatGroup {
ConcurrentHashMap<UUID,ServiceConnection> connections = new ConcurrentHashMap<UUID, ServiceConnection>();
String groupName;
String password;
UUID groupId;
public ChatGroup(String groupName, String password, UUID groupId){
this.groupName = groupName;
this.password = password;
this.groupId = groupId;
}
public void addConnection(ServiceConnection connection){
UUID clientId = connection.getClientId();
connections.put(clientId, connection);
}
public void sendMessages(byte[] message){
for(ServiceConnection connection: connections.values()){
try{
DataOutputStream writer = connection.getWriter();
writer.writeUTF("client_in_message --true");
writer.writeInt(message.length);
writer.write(message);
}catch(IOException e){
connections.remove(connection.getClientId());
System.out.println("Connection with remote socket " + connection.getRemoteSocketAddress() + " has been closed");
}
}
}
public String getGroupName(){
return groupName;
}
public UUID getGroupId(){
return groupId;
}
public ConcurrentHashMap<UUID,ServiceConnection> getConnections(){
return connections;
}
public String getPassword(){
return password;
}
}