-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverbackup.java
276 lines (244 loc) · 8.16 KB
/
serverbackup.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
package net;
import gui.Main;
import main.PasswordManager;
import main.Root;
import main.User;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
public class ServerSession extends Socket {
public static final int PORT = 6520;
private static final int TIMEOUT_MILLIS = 10000;
private BufferedReader reader;
private PrintWriter writer;
private String oneTimeKey;
private String errorMsg;
private boolean open;
private long tempId;
private boolean promptOnAuthenticationFailure;
public ServerSession() throws IOException {
this("localhost", PORT);
}
protected ServerSession(String host, int port) throws IOException {
super(host, port);
try {
reader = new BufferedReader(new InputStreamReader(this.getInputStream()));
writer = new PrintWriter(this.getOutputStream(), true);
setSoTimeout(TIMEOUT_MILLIS);
} catch (IOException e) {
e.printStackTrace();
}
oneTimeKey = "";
open = false;
tempId = 0;
promptOnAuthenticationFailure = false;
}
public boolean open() {
return open(User.active().getUsername(), new String(User.active().getPassword()));
}
public boolean open(String username, String password) {
try {
byte[] src = PasswordManager.encryptWithLocalSalt(password, username);
String encoded = Base64.getEncoder().encodeToString(src);
if (!command("authenticate", username, encoded)) {
return false;
}
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
setErrorMsg("error : security exception occurred");
return false;
}
String nonce;
try {
nonce = reader.readLine();
if (nonce == null || isError(nonce)) {
setErrorMsg(nonce);
if (promptOnAuthenticationFailure) {
password = Root.getPortal().promptLogin();
if (password == null)
return false;
return open(username, password);
}
return false;
}
} catch (IOException e) {
errorMsg = "error : unable to access the server";
return false;
}
if (nonce.length() == 0) {
errorMsg = "error : authentication failed";
return false;
}
oneTimeKey = nonce;
try {
tempId = Long.parseLong(reader.readLine().trim());
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
tempId = 0;
}
open = true;
return true;
}
protected void closeSocket() throws IOException {
super.close();
}
@Override
public void close() throws IOException {
if (!open)
return;
open = false;
command("close");
oneTimeKey = "";
closeSocket();
}
synchronized boolean command(String name, String... arguments) {
boolean isAuthenticate = name.equals("authenticate");
if (!open && !isAuthenticate)
return false;
if (name.equals("close")) {
open = false;
}
name = escape(name);
for (int i = 0; i < arguments.length; i++) {
arguments[i] = escape(arguments[i]);
}
long id = this instanceof AnonymousServerSession ? 0 : User.active() == null ? 0 : User.active().getUniqueID();
if (id == 0)
id = tempId;
StringBuilder cmd = new StringBuilder(name + " " + oneTimeKey + " " + id);
for (String s : arguments) {
cmd.append(" ").append(s);
}
writeText(cmd.toString());
try {
if (isAuthenticate)
return true;
String s;
try {
s = reader.readLine();
} catch (SocketTimeoutException e) {
e.printStackTrace();
setErrorMsg("error : read timed out");
return false;
}
if (s == null) {
/*
If this was a close command, this is completely normal, since the read fails if the socket dies.
Otherwise, something went horribly wrong on the server.
*/
return false;
}
oneTimeKey = s.trim();
return true;
} catch (IOException e) {
e.printStackTrace();
try {
close();
} catch (IOException ignored) {
}
return false;
}
}
protected void writeText(String cmd) {
writer.println(escape(cmd));
}
public boolean sendOnly(String name, String... arguments) {
String[] returnVal = callAndResponse(name, arguments);
return returnVal.length == 1 && returnVal[0].equals("done");
}
public String[] callAndResponse(String name, String... arguments) {
Main portal = Root.getPortal();
if (portal != null)
portal.progressBar(.5);
String[] s = callAndResponseInner(name, arguments).split(" ");
if (portal != null)
portal.progressBar(1);
return s;
}
private String callAndResponseInner(String name, String... arguments) {
if(!command(name, arguments)) {
String s = "error : call exception occurred";
setErrorMsg(s);
return s;
}
try {
String s = reader.readLine();
String result = URLDecoder.decode(s, StandardCharsets.UTF_8);
if (isError(result)) {
setErrorMsg(result);
return errorMsg;
}
return result;
} catch (SocketTimeoutException e) {
String errorMsg = "error : read timed out";
setErrorMsg(errorMsg);
return errorMsg;
} catch (IOException e) {
String errorMsg = "error : response exception occurred";
setErrorMsg(errorMsg);
return errorMsg;
}
}
protected String escape(String s) {
return URLEncoder.encode(s, StandardCharsets.UTF_8);
}
public final String getErrorMsg() {
return errorMsg;
}
public final String getTruncatedErrorMsg() {
String orig = getErrorMsg();
return orig.substring(orig.indexOf(":") + 1);
}
protected final void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
protected final void setOneTimeKey(String oneTimeKey) {
this.oneTimeKey = oneTimeKey;
}
protected final String getOneTimeKey() {
return oneTimeKey;
}
protected final BufferedReader getReader() {
return reader;
}
protected final PrintWriter getWriter() {
return writer;
}
public static boolean isError(String[] results) {
if (results == null)
return true;
if (results.length == 0)
return false;
return isError(results[0]);
}
protected static boolean isError(String s) {
int space = s.indexOf(" ");
return s.substring(0, space < 0 ? s.length() : space).equals("error");
}
protected final void setOpen(boolean open) {
this.open = open;
}
public boolean connectionTest() {
try {
writeText("connectiontest");
String result = getReader().readLine().trim();
return result.equals("success");
} catch (IOException e) {
return false;
}
}
public boolean isPromptOnAuthenticationFailure() {
return promptOnAuthenticationFailure;
}
public void setPromptOnAuthenticationFailure(boolean promptOnAuthenticationFailure) {
this.promptOnAuthenticationFailure = promptOnAuthenticationFailure;
}
}