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
| public class SocketHandler implements Runnable {
private static ConcurrentHashMap<UUID, Socket> clientSocketList = new ConcurrentHashMap<>();
private UUID mUUID; private List<Socket> mSockets = new ArrayList<>();
SocketHandler(List<Socket> sockets) { mSockets = sockets; }
public static Socket getClientSocket(UUID clientUUID) { return clientSocketList.get(clientUUID); }
static int getClientServingCount() { if (clientSocketList != null) return clientSocketList.size(); else return 0; }
int getSocketCount() { return mSockets.size(); }
public UUID getUUID() { return mUUID; }
@Override public void run() { for (Socket socket : mSockets) { try { try { mUUID = UUID.randomUUID(); clientSocketList.put(mUUID, socket); System.out.println("\nADD:" + getUUID() + "/ " + getClientServingCount() + " serving"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; StringBuilder identity = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { identity.append(line); identity.append("\r\n"); if (line.isEmpty()) break; } RequestParser requestParser; requestParser = new RequestParser(identity.toString()); boolean doneFlag = false;
JSONObject clientJSON = requestParser.getJSON();
if (!doneFlag && requestParser.isProtocolHeader (Xcr3TProtocol.REQUEST_ADD)) { try {
System.out.println(clientJSON.toString()); String uid = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("uid")); String pswMD5 = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("identity")); Statement statement = Xcr3TServer.dbConn .createStatement(); ResultSet rs = statement.executeQuery("SELECT * " + "FROM ClientInfo WHERE `uid`='" + uid + "';"); if (rs.next()) throw new IllegalStateException("username is " + "already exist");
int affected = statement.executeUpdate("INSERT " + "INTO ClientInfo (uid,identity,status) " + "values('" + uid + "','" + pswMD5 + "'," + "'OFFLINE');"); if (affected == 0) throw new IllegalStateException("Cannot add " + "user."); rs = statement.executeQuery("SELECT * FROM " + "ClientInfo WHERE `uid`='" + uid + "' AND" + " `identity`='" + pswMD5 + "';"); if (!rs.next()) throw new IllegalStateException("Cannot read " + "user.");
Response response = new Response.Builder (Xcr3TProtocol.RESPONSE_200_OK) .setDestinationPublicKey(clientJSON .getString("publicKey")) .put("status", "OK") .put("id", rs.getString("id")) .put("uid", rs.getString("uid")) .build();
doneFlag = sendResponse(socket, response);
} catch (JSONException e) { throw new IllegalStateException("JSON Error"); } catch (SQLException e) { e.printStackTrace(); throw new IllegalStateException("SQL Server " + "Unavailable"); } }
if (!doneFlag && requestParser.isProtocolHeader (Xcr3TProtocol.REQUEST_HANDSHAKE)) { try {
System.out.println(clientJSON.toString()); String uid = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("uid")); String psw = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("identity")); String port = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("port"));
Statement statement = Xcr3TServer.dbConn .createStatement(); String clientQueryStr = "WHERE `uid` = '" + uid + "'";
ResultSet rs = statement.executeQuery("SELECT * " + "FROM ClientInfo " + clientQueryStr + ";"); if (!rs.next()) throw new IllegalStateException("Unknown " + "identity");
if (rs.getString("status").equals("ONLINE")) throw new IllegalStateException("You have " + "logged in. If it isn't you, please " + "contact the server.");
String md5DB = rs.getString("identity");
if (!CryptorUtil.equalsSaltedMD5(psw, md5DB)) throw new IllegalStateException("Unknown " + "identity");
statement.execute("UPDATE ClientInfo SET " + "`status`='ONLINE', `token`= '" + getUUID () + "' " + ", `location`= '" + socket.getInetAddress () + "', `port`= '" + port + "' " + clientQueryStr + ";");
Response response = new Response.Builder (Xcr3TProtocol.RESPONSE_200_OK) .setDestinationPublicKey(clientJSON .getString("publicKey")) .put("status", "OK") .put("token", getUUID().toString()) .build();
doneFlag = sendResponse(socket, response);
} catch (JSONException e) { throw new IllegalStateException("JSON Error"); } catch (SQLException e) { e.printStackTrace(); throw new IllegalStateException("SQL Error"); } } if (!doneFlag && requestParser.isProtocolHeader (Xcr3TProtocol.REQUEST_FIND)) { try {
System.out.println(clientJSON.toString()); String destUID = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("destUID")); String token = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("token"));
Statement statement = Xcr3TServer.dbConn .createStatement(); ResultSet rs = statement.executeQuery("SELECT " + "`uid` FROM ClientInfo WHERE `token`='" + token + "';"); if (!rs.next()) throw new IllegalStateException("Bad token"); if (rs.getString("uid").equals(destUID)) throw new IllegalStateException("Please don't" + " find yourself :(");
rs = statement.executeQuery("SELECT * FROM " + "ClientInfo WHERE `uid`='" + destUID + "';"); if (!rs.next()) throw new IllegalStateException("Invalid user");
boolean isReady; String status = rs.getString("status"); if (isReady = status.equals("ONLINE")) { Response response = new Response.Builder (Xcr3TProtocol.RESPONSE_200_OK) .setDestinationPublicKey(clientJSON .getString("publicKey")) .put("status", "OK") .put("valid", "true") .put("ready", String.valueOf(isReady)) .put("ip", rs.getString("location") .substring(1)) .put("port", rs.getString("port")) .build(); doneFlag = sendResponse(socket, response); } else { if (status.equals("OFFLINE")) throw new IllegalStateException(destUID + " is OFFLINE."); throw new IllegalStateException(destUID + "is" + " UNKNOWN."); }
} catch (JSONException e) { throw new IllegalStateException("JSON Error"); } catch (SQLException e) { e.printStackTrace(); throw new IllegalStateException("SQL Error"); } } if (!doneFlag && requestParser.isProtocolHeader (Xcr3TProtocol.REQUEST_GOODBYE)) { try { System.out.println(clientJSON.toString()); String uid = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("uid")); String token = CryptorUtil.unpack(Xcr3TServer .SERVER_PRIVATE_KEY, clientJSON.getString ("token")); String clientQueryStr = "WHERE `uid` = '" + uid + "' AND `token`='" + token + "'"; Statement statement = Xcr3TServer.dbConn .createStatement(); statement.execute("UPDATE ClientInfo SET " + "`location`= null, `port`= null, " + "`status`='OFFLINE', `token`= '' " + clientQueryStr + ";");
Response response = new Response.Builder (Xcr3TProtocol.RESPONSE_200_OK) .setDestinationPublicKey(clientJSON .getString("publicKey")) .put("status", "OK") .put("goodbye", "true") .build();
doneFlag = sendResponse(socket, response);
} catch (JSONException e) { throw new IllegalStateException("JSON Error"); } catch (SQLException e) { e.printStackTrace(); throw new IllegalStateException("SQL Error"); } } if (!doneFlag) { throw new IllegalStateException("Unsupported Function"); } bufferedReader.close(); closeSocket(socket);
} catch (IllegalStateException e) { String err = "Received a bad/empty request: " + e .getMessage(); Response response = new Response.Builder(Xcr3TProtocol .RESPONSE_400_BAD_REQUEST) .put("status", "error") .put("error", e.getMessage()) .build();
sendResponse(socket, response); System.out.println(err); closeSocket(socket); } } catch (IOException e) { e.printStackTrace(); } } }
private boolean sendResponse(Socket socket, Response response) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bufferedWriter.write(response.toString()); bufferedWriter.flush(); bufferedWriter.close(); return true; }
private void closeSocket(Socket socket) throws IOException { socket.close(); clientSocketList.remove(mUUID); } }
|