-
Notifications
You must be signed in to change notification settings - Fork 0
/
IdServer.java
430 lines (373 loc) · 17.6 KB
/
IdServer.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import java.util.UUID;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import javax.rmi.ssl.SslRMIServerSocketFactory;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import org.apache.commons.cli.*;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import redis.clients.jedis.*;
public class IdServer implements ServerInterface {
private static boolean verbosity = false;
private HashSet<IdAccount> accounts;
static int port = 5128; //our assigned port
JedisPool pool = new JedisPool("localhost", 6379);
final static String RESET = "\u001B[0m";
final static String ANSI_BLACK = "\u001B[30m";
final static String ERRORC = "\u001B[31m";
final static String COMMANDC = "\u001B[32m";
final static String ANSI_YELLOW = "\u001B[33m";
final static String ANSI_PURPLE = "\u001B[35m";
final static String ACCOUNTC = "\u001B[36m";
final static String ANSI_WHITE = "\u001B[37m";
final static String SYSTEMFLAG = "[" + COMMANDC + "System" + RESET + "]";
final static String ERRORFLAG = "[" + ERRORC + "ERROR" + RESET + "]";
//constructor
public IdServer() throws RemoteException{
this.accounts = new HashSet<IdAccount>();
Runtime.getRuntime().addShutdownHook(new DeathToServer());
SavePeriodically saveThread = new SavePeriodically();
saveThread.start();
readData();
}
/////////////////////////////
//INTERNAL METHODS FOR SERVER
/////////////////////////////
/**
* Writes the data to the database
*/
private void writeData() {
synchronized(this){
try(Jedis jedis = pool.getResource()){
jedis.flushAll(); //I know this isn't scalable, but I don't have all day
int i = 0;
for(IdAccount acc : accounts) {
Map <String, String> accMap = new HashMap<String, String>();
accMap.put("username", acc.getUsername());
accMap.put("realName", acc.getRealname());
accMap.put("uuid", acc.getUuid().toString());
accMap.put("dateCreated", acc.getDateCreated().toString());
accMap.put("dateModified", acc.getDateModified().toString());
accMap.put("original ip", acc.getOriginalIP());
accMap.put("recent ip", acc.getRecentIp());
jedis.hset("account" + ++i, accMap);
if (acc.getPassword() == null) jedis.set(("accountp" + i).getBytes(), "null".getBytes());
else jedis.set(("accountp" + i).getBytes(), acc.getPassword());
}
}
if(verbosity) System.out.println( SYSTEMFLAG + " Data written to database.");
if(accounts.size() == 0 && verbosity) System.out.println(SYSTEMFLAG + ERRORFLAG + " No accounts currently exist, database is empty.");
if(accounts.size() == 0 && verbosity) System.out.println(SYSTEMFLAG + " Above message is an ONLY if accounts should be present on system.");
}
}
/**
* Reads data from database
*/
private void readData() {
HashSet<IdAccount> accountsRead = new HashSet<IdAccount>();
try(Jedis jedis = pool.getResource()){
//jedis.flushAll(); //used to delete all accounts when the devs screw up the backend
int i = 0;
//loop through accounts, starting at 1
while (jedis.hgetAll("account" + ++i).size() > 0) {
Map <String, String> accMap = jedis.hgetAll("account" + i);;
System.out.println(accMap.toString());
IdAccount skeeyyah = new IdAccount(
accMap.get("username"),
accMap.get("realName"),
UUID.fromString(accMap.get("uuid")),
LocalDateTime.parse(accMap.get("dateCreated")),
LocalDateTime.parse(accMap.get("dateModified")),
null,
accMap.get("original ip"),
accMap.get("recent ip")
);
//This line is so hard to read.
//If the password is not null, then get the its bytes from jedis
if(!(new String(jedis.get(("accountp" + i).getBytes())).equals("null"))) skeeyyah.setPassword(jedis.get(("accountp" + i).getBytes()));
accountsRead.add(skeeyyah);
}
}
accounts = accountsRead;
if(verbosity) System.out.println(SYSTEMFLAG + " Database read has been executed");
if(verbosity) System.out.println(SYSTEMFLAG + " There are " + accounts.size() + "accounts.");
}
/**
* Checks the passwords to look for match
* @param p1 password 1
* @param p2 password 2
* @return boolean true/false
*/
private boolean checkPassword(byte[] p1, byte[] p2) {
if (p1.length != p2.length) {
return false;
};
//for(int i = 0; i < p1.length; i++) if(p1[i] != p2[i]) System.out.println("actual password: " + p1[i] + "\n given password: " + p2[i]); //Debug line
for(int i = 0; i < p1.length; i++) if(p1[i] != p2[i]) return false; //Java is cool too
return true;
}
////////////////////////
//METHODS FOR CLIENT USE
////////////////////////
/**
* Creates the account
*/
public String createAccount(String username, String realname, byte[] password, String ipAddress) {
if(verbosity){System.out.println(SYSTEMFLAG + COMMANDC + " called --create" + RESET);}
for (IdAccount acc : accounts){
if(acc.getUsername().equals(username)){
if(verbosity){System.out.println(ERRORFLAG + " Account with name " + ACCOUNTC + username + RESET + " already exists. Please choose another name.");}
return ERRORC + " Account with name " + ACCOUNTC + username + RESET + " already exists. Please choose another name." + RESET;
}
}
IdAccount newAcc = new IdAccount(ipAddress);
newAcc.setUsername(username);
newAcc.setRealname(realname);
newAcc.setPassword(password);
if(realname == null){
newAcc.setRealname(System.getProperty("user.name"));
}
if(verbosity){
System.out.println(ACCOUNTC + " == ACCOUNT CREATED ==" + RESET);
System.out.println(ACCOUNTC + newAcc.toString() + "\n" + RESET);
}
newAcc.setDateModified(java.time.LocalDateTime.now());
newAcc.setRecentIp(ipAddress);
accounts.add(newAcc);
writeData();
return ACCOUNTC + "Account " + username + " created on server." + RESET;
}
/**
* Returns a string if the account is found or not based on username
*/
public String lookupUser(String username, String recentIp) {
if(verbosity){System.out.println(SYSTEMFLAG + COMMANDC + " called --lookup" + RESET);}
for (IdAccount acc : accounts) {
if(acc.getUsername().equals(username)) {
if(verbosity){System.out.println(SYSTEMFLAG + ACCOUNTC + "Account found: " + username + "\n" + RESET);}
acc.setRecentIp(recentIp);
return acc.toString();
}
}
if(verbosity){System.out.println(ERRORFLAG + ACCOUNTC + "Account not found: " + username + "\n" + RESET);}
return ERRORC + "[ERROR] user " + username + " not found" + RESET;
}
/**
* Looks up user and returns string based on UUID
*/
public String lookupUUID(String uuid, String recentIp) {
if(verbosity){System.out.println(SYSTEMFLAG + COMMANDC + " called --reverse-lookup" + RESET);}
UUID temp = UUID.fromString(uuid);
for (IdAccount acc : accounts) {
if(acc.getUuid().equals(temp)) {
if(verbosity){System.out.println(SYSTEMFLAG + ACCOUNTC + "Account found: " + uuid + "\n" + RESET);}
acc.setDateModified(java.time.LocalDateTime.now());
return acc.toString();
} else{
acc.setDateModified(java.time.LocalDateTime.now());
}
}
if(verbosity){System.out.println(ERRORFLAG + ACCOUNTC + "Account not found: " + uuid + "\n" + RESET);}
return ERRORC + "[ERROR] user " + uuid + "not found" + RESET;
}
/**
* Modifies the username, checks passord if needed
*/
public String changeUsername(String oldName, String newName, byte[] password, String recentIp) {
if(verbosity){System.out.println(SYSTEMFLAG + COMMANDC + " called --modify" + RESET);}
for (IdAccount acc : accounts){
if(acc.getUsername().equals(newName)){
if(verbosity){System.out.println(ERRORFLAG + " Account with name " + newName + " already exists. Please choose another name." + RESET);}
acc.setDateModified(java.time.LocalDateTime.now());
return ERRORC + "[ERROR] Account with name " + newName + " already exists. Please choose another name." + RESET;
}
}
for (IdAccount acc : accounts) {
if(acc.getUsername().equals(oldName)) {
if(verbosity){System.out.println(ERRORFLAG + ACCOUNTC + "Account found: " + oldName + "\n" + RESET);}
if(password == null && acc.getPassword() != null){
return ERRORC + "[ERROR] PASSWORD ASSOCIATED WITH ACCOUNT WAS NOT ENTERED" + RESET;
} else if (acc.getPassword() == null){
acc.setDateModified(java.time.LocalDateTime.now());
acc.setUsername(newName); //NO PASSWORD
if(verbosity){System.out.println(SYSTEMFLAG + ACCOUNTC + "No password associated with account." + RESET);}
return ACCOUNTC + "Account " + oldName + " found and login name updated to " + newName;
} else if (checkPassword(acc.getPassword(), password)){
acc.setDateModified(java.time.LocalDateTime.now());
acc.setUsername(newName);
if(verbosity){System.out.println(SYSTEMFLAG + ACCOUNTC + "Password associated with account is correct." + RESET);}
return ACCOUNTC + "Account " + oldName + " found and login name updated to " + newName;
} else {
acc.setDateModified(java.time.LocalDateTime.now());
return ERRORC + "[ERROR] PASSWORD DENIED" + RESET;
}
} else{
acc.setDateModified(java.time.LocalDateTime.now());
}
}
writeData();
return ERRORC + "[ERROR] NO ACCOUNT FOUND OR ACCOUNT NOT CREATED" + RESET;
}
/**
* Deletes user, checks if password is the same before delting, fails if no match
*/
public String deleteAccount(String username, byte[] password) {
if(verbosity){System.out.println(SYSTEMFLAG + COMMANDC + " called --delete" + RESET);}
for (IdAccount acc : accounts) {
if(acc.getUsername().equals(username)) {
if(verbosity){System.out.println(SYSTEMFLAG+ACCOUNTC + "Account found: " + username + "\n" + RESET);}
if(password == null && acc.getPassword() != null){
return ERRORC + "[ERROR] PASSWORD ASSOCIATED WITH ACCOUNT WAS NOT ENTERED" + RESET;
}else if(acc.getPassword() == null){
accounts.remove(acc);
writeData();
return ACCOUNTC + "Account \"" + username + "\" removed." + RESET;
}else if (checkPassword(acc.getPassword(), password)) {
accounts.remove(acc);
writeData();
return ACCOUNTC + "Account \"" + username + "\" removed." + RESET;
}else return ERRORC + "[ERROR] PASSWORD DENIED" + RESET;
}
}
return ERRORC + "[ERROR] NO ACCOUNT FOUND OR ACCOUNT NOT CREATED" + RESET;
}
/**
* Gets the accounts in the database on the server if with output depending on what you want to be returned
*/
public String getAccounts(String attribute) {
if(verbosity == true){System.out.println(SYSTEMFLAG+COMMANDC + " called --get" + RESET);}
if(accounts.size() == 0) return ERRORC + "[ERROR] There are no accounts, yet..." + RESET;
String out = null;
int i = 0;
switch (attribute.toLowerCase()) {
case "users":
out = "Registered USERS:\n";
for (IdAccount acc : accounts) out += ++i + ": " + acc.getUsername() + "\n";
break;
case "uuids":
out = "Registered UUIDS:\n";
for (IdAccount acc : accounts) out += ++i + ": " + acc.getUuid() + "\n";
break;
case "all":
out = "Registered Accounts:\n";
for (IdAccount acc : accounts) out += ++i + ": " + acc.toString() + "\n\n";
break;
default:
out += ERRORC + "[ERROR] INVALID ARG WITH -get: " + attribute + RESET;
break;
}
return out;
}
/**
* Sets verbosity of server
* @param verbosity
*/
public void setVerbose(boolean verbosity){
verbosity = true;
System.out.println(SYSTEMFLAG+"Verbsoity set to " + COMMANDC + verbosity + RESET);
}
/**
* Shutdown hook handler
*/
private class DeathToServer extends Thread {
public void run () {
System.out.println("\n\n" + SYSTEMFLAG + ERRORC + "!!!!SERVER SHUTTING DOWN!!!!" + RESET);
writeData();
}
}
/**
* Saves the database in intervals in case of shutdown
*/
public class SavePeriodically extends Thread{
long time = System.currentTimeMillis();
public void run () {
while (true) {
if(System.currentTimeMillis() - time > 180000) {
writeData();
time = System.currentTimeMillis();
}
}
}
}
/**
* Basically runs the server. It's outside of the main method because we need to call nonstatic methods (I think?)
* Sets bind of the registry
*/
public void bind() {
try {
RMIClientSocketFactory rmiClientSocketFactory = new SslRMIClientSocketFactory();
RMIServerSocketFactory rmiServerSocketFactory = new SslRMIServerSocketFactory();
ServerInterface severus = (ServerInterface) UnicastRemoteObject.exportObject(this, 0, rmiClientSocketFactory, rmiServerSocketFactory);
Registry reg = LocateRegistry.getRegistry(port);
if(verbosity){System.out.println(SYSTEMFLAG + "Registry " + COMMANDC + "SET\n" + RESET);}
reg.rebind("severus", severus);
if(verbosity){System.out.println(SYSTEMFLAG + "Registy Rebind " + COMMANDC + "SET\n" + RESET);}
} catch (Exception e) {
e.printStackTrace();
}
}
//////////
// MAIN //
//////////
/**
* Main server method
* @param args
*/
public static void main(String[] args) {
try {
handleServerArgs(args);
if(verbosity){System.out.println("\n\n" + SYSTEMFLAG + " Starting IdSever at Host: localhost " + "Port: " + port);}
System.setProperty("javax.net.ssl.keyStore", "Server_Keystore");
System.setProperty("javax.net.ssl.keyStorePassword", "123456");
System.setProperty("java.security.policy", "rmisslex2/resources/mysecurity.policy");
SSLServerSocketFactory sslSrvFact = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) sslSrvFact.createServerSocket(port);
IdServer server = new IdServer();
server.bind();
} catch (RemoteException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Parses args for server
* @param args
*/
public static void handleServerArgs(String[] args){
try {
Options serverOptions = new Options();
serverOptions.addOption(new Option("n", "numport", true, "Server port #"));
serverOptions.addOption(new Option("v", "verbose", false, "Sets vebosity of server"));
CommandLineParser serverParser = new DefaultParser();
CommandLine serverCommand = serverParser.parse(serverOptions, args);
String arg = null;
for(Option serverOption : serverCommand.getOptions()){
//Handles the numport option for port specification
if(serverOption.getOpt().equals("n")){
arg = serverOption.getOpt();
port = Integer.parseInt(serverCommand.getOptionValue(arg));
System.out.println(SYSTEMFLAG + " Setting server port to " + serverCommand.getOptionValue(arg));
}
//Handles the verbosity specification
if(serverOption.getOpt().equals("v")){
verbosity = true;
System.out.println(SYSTEMFLAG + " Verbosity set to " + COMMANDC + verbosity + RESET);
}
}
} catch (ParseException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}