服务检查时间是否一致

This commit is contained in:
heavyrian2012
2023-03-12 18:55:14 +08:00
parent 6a806aa10b
commit b7b262be00
3 changed files with 33 additions and 28 deletions
@@ -39,17 +39,10 @@ public class RouteAction extends Action {
@Override @Override
public boolean action(Request request, Response response) { public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) { if (request.getNettyRequest() instanceof FullHttpRequest) {
response.setContentType("application/octet-stream"); response.setContentType("application/octet-stream");
response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Origin", "*");
FullHttpRequest fullHttpRequest = (FullHttpRequest) request.getNettyRequest(); FullHttpRequest fullHttpRequest = (FullHttpRequest) request.getNettyRequest();
boolean b = false;
if ("web".equalsIgnoreCase(fullHttpRequest.headers().get("p"))) {
b = true;
}
final boolean base64Response = b;
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content()); byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
@@ -57,15 +50,20 @@ public class RouteAction extends Action {
try { try {
bytes = Base64.getDecoder().decode(str); bytes = Base64.getDecoder().decode(str);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null, base64Response); sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
return true; return true;
} }
String cid = fullHttpRequest.headers().get("cid"); String cid = fullHttpRequest.headers().get("cid");
byte[] cbytes = Base64.getDecoder().decode(cid); byte[] cbytes = Base64.getDecoder().decode(cid);
cbytes = AES.AESDecrypt(cbytes, "", true); boolean[] invalidTime = new boolean[1];
cbytes = AES.AESDecrypt(cbytes, "", true, invalidTime);
if (cbytes == null) { if (cbytes == null) {
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null, base64Response); if(invalidTime[0]) {
sendResponse(response, ErrorCode.ERROR_CODE_TIME_INCONSISTENT, null);
} else {
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
}
return true; return true;
} }
cid = new String(cbytes); cid = new String(cbytes);
@@ -74,7 +72,7 @@ public class RouteAction extends Action {
byte[] ubytes = Base64.getDecoder().decode(uid); byte[] ubytes = Base64.getDecoder().decode(uid);
ubytes = AES.AESDecrypt(ubytes, "", true); ubytes = AES.AESDecrypt(ubytes, "", true);
if (ubytes == null) { if (ubytes == null) {
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null, base64Response); sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
return true; return true;
} }
uid = new String(ubytes); uid = new String(ubytes);
@@ -84,7 +82,7 @@ public class RouteAction extends Action {
if (session == null) { if (session == null) {
ErrorCode errorCode = sessionsStore.loadActiveSession(uid, cid); ErrorCode errorCode = sessionsStore.loadActiveSession(uid, cid);
if (errorCode != ErrorCode.ERROR_CODE_SUCCESS) { if (errorCode != ErrorCode.ERROR_CODE_SUCCESS) {
sendResponse(response, errorCode, null, base64Response); sendResponse(response, errorCode, null);
return true; return true;
} }
session = sessionsStore.sessionForClientAndUser(uid, cid); session = sessionsStore.sessionForClientAndUser(uid, cid);
@@ -94,13 +92,13 @@ public class RouteAction extends Action {
if (session != null) { if (session != null) {
bytes = AES.AESDecrypt(bytes, session.getSecret(), true); bytes = AES.AESDecrypt(bytes, session.getSecret(), true);
} else { } else {
sendResponse(response, ErrorCode.ERROR_CODE_SECRECT_KEY_MISMATCH, null, base64Response); sendResponse(response, ErrorCode.ERROR_CODE_SECRECT_KEY_MISMATCH, null);
return true; return true;
} }
if (bytes == null) { if (bytes == null) {
sendResponse(response, ErrorCode.ERROR_CODE_SECRECT_KEY_MISMATCH, null, base64Response); sendResponse(response, ErrorCode.ERROR_CODE_SECRECT_KEY_MISMATCH, null);
return true; return true;
} }
@@ -111,22 +109,22 @@ public class RouteAction extends Action {
String userId = Tokenor.getUserId(token.getBytes()); String userId = Tokenor.getUserId(token.getBytes());
LOG.info("RouteAction token={}, userId={}", token, userId); LOG.info("RouteAction token={}, userId={}", token, userId);
if (userId == null) { if (userId == null) {
sendResponse(response, ErrorCode.ERROR_CODE_TOKEN_ERROR, null, base64Response); sendResponse(response, ErrorCode.ERROR_CODE_TOKEN_ERROR, null);
} else { } else {
ServerAPIHelper.sendRequest(userId, wrapper.getClientId(), wrapper.getRequest(), wrapper.getData().toByteArray(), new ServerAPIHelper.Callback() { ServerAPIHelper.sendRequest(userId, wrapper.getClientId(), wrapper.getRequest(), wrapper.getData().toByteArray(), new ServerAPIHelper.Callback() {
@Override @Override
public void onSuccess(byte[] result) { public void onSuccess(byte[] result) {
sendResponse(response, null, result, base64Response); sendResponse(response, null, result);
} }
@Override @Override
public void onError(ErrorCode errorCode) { public void onError(ErrorCode errorCode) {
sendResponse(response, errorCode, null, base64Response); sendResponse(response, errorCode, null);
} }
@Override @Override
public void onTimeout() { public void onTimeout() {
sendResponse(response, ErrorCode.ERROR_CODE_TIMEOUT, null, base64Response); sendResponse(response, ErrorCode.ERROR_CODE_TIMEOUT, null);
} }
@Override @Override
@@ -139,25 +137,21 @@ public class RouteAction extends Action {
return false; return false;
} }
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null, false); sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
} }
} }
return true; return true;
} }
private void sendResponse(Response response, ErrorCode errorCode, byte[] contents, boolean base64Response) { private void sendResponse(Response response, ErrorCode errorCode, byte[] contents) {
response.setStatus(HttpResponseStatus.OK); response.setStatus(HttpResponseStatus.OK);
if (contents == null) { if (contents == null) {
ByteBuf ackPayload = Unpooled.buffer(); ByteBuf ackPayload = Unpooled.buffer();
ackPayload.ensureWritable(1).writeByte(errorCode.getCode()); ackPayload.ensureWritable(1).writeByte(errorCode.getCode());
contents = ackPayload.array(); contents = ackPayload.array();
} }
if (base64Response) {
byte[] base64Data = Base64.getEncoder().encode(contents); response.setContent(contents);
response.setContent(base64Data);
} else {
response.setContent(contents);
}
response.send(); response.send();
} }
} }
@@ -48,6 +48,9 @@ public enum ErrorCode {
ERROR_CODE_NOT_YOUR_LOCKED(26, "unload failure, not your lock"), ERROR_CODE_NOT_YOUR_LOCKED(26, "unload failure, not your lock"),
ERROR_CODE_ROBOT_NO_TOKEN(27, "robot no token"), ERROR_CODE_ROBOT_NO_TOKEN(27, "robot no token"),
ERROR_CODE_WS_NOT_CONFIGURED_CORRECTLY(28, "ws not configured correctly"), ERROR_CODE_WS_NOT_CONFIGURED_CORRECTLY(28, "ws not configured correctly"),
ERROR_CODE_TIME_INCONSISTENT(30, "time inconsistent"),
ERROR_CODE_CONFERENCE_ROOM_NOT_EXIST(60, "conference room not exist"), ERROR_CODE_CONFERENCE_ROOM_NOT_EXIST(60, "conference room not exist"),
ERROR_CODE_CONFERENCE_SERVICE_NOT_AVAILABLE(61, "conference service not available"), ERROR_CODE_CONFERENCE_SERVICE_NOT_AVAILABLE(61, "conference service not available"),
@@ -107,15 +107,20 @@ public class AES {
} }
return key; return key;
} }
public static byte[] AESDecrypt(byte[] sSrc, String userKey, boolean checkTime) { public static byte[] AESDecrypt(byte[] sSrc, String userKey, boolean checkTime) {
return AESDecrypt(sSrc, userKey, checkTime, null);
}
public static byte[] AESDecrypt(byte[] sSrc, String userKey, boolean checkTime, boolean[] invalidTime) {
byte[] aesKey = aes_key; byte[] aesKey = aes_key;
if (userKey != null && !userKey.isEmpty()) { if (userKey != null && !userKey.isEmpty()) {
aesKey = convertUserKey(userKey); aesKey = convertUserKey(userKey);
} }
return AESDecrypt(sSrc, aesKey, checkTime); return AESDecrypt(sSrc, aesKey, checkTime, invalidTime);
} }
public static byte[] AESDecrypt(byte[] sSrc, byte[] aesKey, boolean checkTime) { public static byte[] AESDecrypt(byte[] sSrc, byte[] aesKey, boolean checkTime, boolean[] invalidTime) {
try { try {
// 判断Key是否正确 // 判断Key是否正确
@@ -152,6 +157,9 @@ public class AES {
int curhour = (int) ((System.currentTimeMillis()/1000 - 1514736000)/3600); int curhour = (int) ((System.currentTimeMillis()/1000 - 1514736000)/3600);
if (curhour - hours > 24 && checkTime) { if (curhour - hours > 24 && checkTime) {
if(invalidTime != null) {
invalidTime[0] = true;
}
return null; return null;
} }
byte[] neworiginal = new byte[original.length - 4]; byte[] neworiginal = new byte[original.length - 4];