Files
IM-server-new/broker/src/main/java/com/xiaoleilu/loServer/action/RouteAction.java
T

162 lines
6.3 KiB
Java
Raw Normal View History

2019-03-16 17:04:23 +08:00
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package com.xiaoleilu.loServer.action;
import cn.wildfirechat.proto.ProtoConstants;
2019-03-16 17:04:23 +08:00
import cn.wildfirechat.proto.WFCMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.xiaoleilu.loServer.annotation.HttpMethod;
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.moquette.persistence.MemorySessionStore;
import io.moquette.persistence.ServerAPIHelper;
2019-03-16 17:04:23 +08:00
import io.moquette.spi.impl.Utils;
import io.moquette.spi.impl.security.AES;
import io.moquette.spi.security.Tokenor;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2019-05-22 20:24:21 +08:00
import cn.wildfirechat.common.ErrorCode;
2019-03-16 17:04:23 +08:00
import java.util.Base64;
import java.util.concurrent.Executor;
@Route("/route")
@HttpMethod("POST")
public class RouteAction extends Action {
private static final Logger LOG = LoggerFactory.getLogger(RouteAction.class);
2019-06-29 16:42:55 +08:00
2019-03-16 17:04:23 +08:00
@Override
2021-05-24 10:31:09 +08:00
public boolean action(Request request, Response response) {
2019-03-16 17:04:23 +08:00
if (request.getNettyRequest() instanceof FullHttpRequest) {
response.setContentType("application/octet-stream");
2019-06-29 16:42:55 +08:00
response.setHeader("Access-Control-Allow-Origin", "*");
2019-06-28 17:27:37 +08:00
FullHttpRequest fullHttpRequest = (FullHttpRequest) request.getNettyRequest();
2019-03-16 17:04:23 +08:00
byte[] bytes = Utils.readBytesAndRewind(fullHttpRequest.content());
String str = new String(bytes);
try {
bytes = Base64.getDecoder().decode(str);
} catch (IllegalArgumentException e) {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
return true;
2019-03-16 17:04:23 +08:00
}
String cid = fullHttpRequest.headers().get("cid");
byte[] cbytes = Base64.getDecoder().decode(cid);
2023-03-12 18:55:14 +08:00
boolean[] invalidTime = new boolean[1];
cbytes = AES.AESDecrypt(cbytes, "", true, invalidTime);
2019-03-16 17:04:23 +08:00
if (cbytes == null) {
2023-03-12 18:55:14 +08:00
if(invalidTime[0]) {
sendResponse(response, ErrorCode.ERROR_CODE_TIME_INCONSISTENT, null);
} else {
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
}
return true;
2019-03-16 17:04:23 +08:00
}
cid = new String(cbytes);
String uid = fullHttpRequest.headers().get("uid");
byte[] ubytes = Base64.getDecoder().decode(uid);
2019-04-29 20:40:59 +08:00
ubytes = AES.AESDecrypt(ubytes, "", true);
2019-03-16 17:04:23 +08:00
if (ubytes == null) {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
return true;
2019-03-16 17:04:23 +08:00
}
uid = new String(ubytes);
MemorySessionStore.Session session = sessionsStore.sessionForClientAndUser(uid, cid);
if (session == null) {
2019-11-27 10:22:20 +08:00
ErrorCode errorCode = sessionsStore.loadActiveSession(uid, cid);
2019-03-16 17:04:23 +08:00
if (errorCode != ErrorCode.ERROR_CODE_SUCCESS) {
2023-03-12 18:55:14 +08:00
sendResponse(response, errorCode, null);
return true;
2019-03-16 17:04:23 +08:00
}
session = sessionsStore.sessionForClientAndUser(uid, cid);
}
if (session != null) {
bytes = AES.AESDecrypt(bytes, session.getSecret(), true);
} else {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_SECRECT_KEY_MISMATCH, null);
return true;
2019-03-16 17:04:23 +08:00
}
if (bytes == null) {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_SECRECT_KEY_MISMATCH, null);
return true;
2019-03-16 17:04:23 +08:00
}
if(messagesStore.getUserStatus(uid) == ProtoConstants.UserStatus.Forbidden) {
sendResponse(response, ErrorCode.ERROR_CODE_USER_BLOCKED, null);
return true;
}
2019-03-16 17:04:23 +08:00
try {
WFCMessage.IMHttpWrapper wrapper = WFCMessage.IMHttpWrapper.parseFrom(bytes);
String token = wrapper.getToken();
String userId = Tokenor.getUserId(token.getBytes());
LOG.info("RouteAction token={}, userId={}", token, userId);
if (userId == null) {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_TOKEN_ERROR, null);
2019-03-16 17:04:23 +08:00
} else {
2021-04-23 09:13:03 +08:00
ServerAPIHelper.sendRequest(userId, wrapper.getClientId(), wrapper.getRequest(), wrapper.getData().toByteArray(), new ServerAPIHelper.Callback() {
2019-03-16 17:04:23 +08:00
@Override
public void onSuccess(byte[] result) {
2023-03-12 18:55:14 +08:00
sendResponse(response, null, result);
2019-03-16 17:04:23 +08:00
}
@Override
public void onError(ErrorCode errorCode) {
2023-03-12 18:55:14 +08:00
sendResponse(response, errorCode, null);
2019-03-16 17:04:23 +08:00
}
@Override
public void onTimeout() {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_TIMEOUT, null);
2019-03-16 17:04:23 +08:00
}
@Override
public Executor getResponseExecutor() {
return command -> {
ctx.executor().execute(command);
};
}
}, ProtoConstants.RequestSourceType.Request_From_User);
return false;
2019-03-16 17:04:23 +08:00
}
} catch (InvalidProtocolBufferException e) {
2023-03-12 18:55:14 +08:00
sendResponse(response, ErrorCode.ERROR_CODE_INVALID_DATA, null);
2019-03-16 17:04:23 +08:00
}
}
return true;
2019-03-16 17:04:23 +08:00
}
2023-03-12 18:55:14 +08:00
private void sendResponse(Response response, ErrorCode errorCode, byte[] contents) {
2019-03-16 17:04:23 +08:00
response.setStatus(HttpResponseStatus.OK);
2019-06-28 17:27:37 +08:00
if (contents == null) {
2019-03-16 17:04:23 +08:00
ByteBuf ackPayload = Unpooled.buffer();
ackPayload.ensureWritable(1).writeByte(errorCode.getCode());
2019-06-28 17:27:37 +08:00
contents = ackPayload.array();
}
2023-03-12 18:55:14 +08:00
response.setContent(contents);
2019-03-16 17:04:23 +08:00
response.send();
}
}