package chat.server.im; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Base64; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; import org.json.JSONObject; import com.google.protobuf.GeneratedMessage; import com.google.protobuf.InvalidProtocolBufferException; import com.xiaoleilu.hutool.json.JSONArray; import chat.module.entity.MessageRecord; import chat.security.AES; import chat.server.moquette.message.MqttPublishMessage; import chat.user.Session; import cn.wildfirechat.proto.WFCMessage; import cn.wildfirechat.proto.WFCMessage.IMHttpWrapper; import frame.util.ContentBuilder; import frame.variant.translator.Translator; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.FullHttpRequest; public class DataPool { private static Logger logger; private static WFCMessageParseTable messageParseTable; private RequestType requestType; private FullHttpRequest httpRequest; private JSONObject jsonObject; private String body; private String secret; private byte[] rawData; private byte[] data; private byte[] srcBytes; private IMHttpWrapper httpWrapper; private int messageId; private String phone; public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } static { logger = Logger.getLogger(IMDispatcher.class); messageParseTable = WFCMessageParseTable.getInstance(); } //short public DataPool(RequestType requestType, Session session, FullHttpRequest httpRequest) { //1. this.requestType = requestType; this.httpRequest = httpRequest; //2. if (session != null) { secret = session.getSecret(); } //3. body = readHttpRequestJSON(); if (body == null) { return; } if (body.startsWith("{")) { jsonObject = new JSONObject(body); } else if (secret != null) { httpWrapper = createHttpWrapper(body, secret); } } //long public DataPool(RequestType requestType, Session session, MqttPublishMessage message) throws Exception { //1. this.requestType = requestType; this.messageId = message.variableHeader().packetId(); if (session != null) { secret = session.getSecret(); } //2. // ByteBuf payload = message.payload(); // byte[] content = readBytesAndRewind(payload); // // if (content == null) { // return; // } // // data = AES.AESDecrypt(content, secret, true); // // if (secret != null) { // httpWrapper = createHttpWrapper(new String(data), secret); // } ByteBuf payload = message.payload(); byte[] content = readBytesAndRewind(payload); if (content == null) { return; } content = AES.AESDecrypt(content, secret, true); if (content == null) { return; } if (secret != null) { try { body = null; byte[] bytes = Base64.getDecoder().decode(new String(content)); bytes = AES.AESDecrypt(bytes, secret, true); httpWrapper = WFCMessage.IMHttpWrapper.parseFrom(bytes); } catch(Exception e) { httpWrapper = null; srcBytes = content; } } // if (body == null) { // return; // } // // if (body.startsWith("{")) { // jsonObject = new JSONObject(body); // } // else if (secret != null) { // httpWrapper = createHttpWrapper(body, secret); // } } @SuppressWarnings("unchecked") public T getWFCMessage(Class clazz) { try { byte[] bytes = null; if (httpWrapper != null) { bytes = httpWrapper.getData().toByteArray(); } else if (srcBytes != null) { bytes = srcBytes; } else if (body != null) { bytes = body.getBytes(); } if (bytes == null) { return null; } Method method = messageParseTable.getMethod(clazz); T result = (T) method.invoke(clazz, bytes); return result; } catch (Exception e) { if (logger.isDebugEnabled()) e.printStackTrace(); return null; } } protected String readHttpRequestJSON() { String result = null; try { byte[] bytes = readBytesAndRewind(httpRequest.content()); result = new String(bytes, StandardCharsets.UTF_8); } catch (Exception e) { e.printStackTrace(); } return result; } protected String readPublishMessageJSON(MqttPublishMessage message) { String result = null; try { ByteBuf payload = message.payload(); byte[] content = readBytesAndRewind(payload); if (content == null) { return null; } byte[] clearContent = AES.AESDecrypt(content, secret, true); if (clearContent == null) { return null; } srcBytes = clearContent; result = new String(clearContent); } catch (Exception e) { e.printStackTrace(); } return result; } public static byte[] readBytesAndRewind(ByteBuf payload) { byte[] payloadContent = new byte[payload.readableBytes()]; int mark = payload.readerIndex(); payload.readBytes(payloadContent); payload.readerIndex(mark); return payloadContent; } public Object getObject(String name) { if (jsonObject != null) { return jsonObject.get(name); } return null; } public String getString(String name) { try { if (jsonObject != null) { return jsonObject.getString(name); } } catch (Exception e) { return null; } return null; } public Integer getInteger(String name, Integer defaultValue) { Object value = getObject(name); return Translator.toInteger(value, defaultValue); } public Date getDate(String string) throws ParseException { // TODO Auto-generated method stub SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.parse(string); } public JSONArray getJSONArray(String name) { Object obj = getObject(name); if (obj == null) { return null; } return (JSONArray) obj; } public String getHeader(String name) { return httpRequest.headers().get(name); } public MessageRecord getMessageRecord() { // TODO Auto-generated method stub return null; } public List getList(String string) { // TODO Auto-generated method stub return null; } public String getBody() { return body; } public void decode() { data = AES.AESDecrypt(rawData, secret, true); } public byte[] getRawData() { return rawData; } public byte[] getData() { return data; } public IMHttpWrapper getHttpWrapper() { return httpWrapper; } public static IMHttpWrapper createHttpWrapper(String content, String secret) { WFCMessage.IMHttpWrapper result = null; try { byte[] bytes = Base64.getDecoder().decode(content); bytes = AES.AESDecrypt(bytes, secret, true); result = WFCMessage.IMHttpWrapper.parseFrom(bytes); } catch(Exception e) {} return result; } public String getRequestURI() { if (httpRequest == null) { return null; } return httpRequest.uri(); } public String getRequestTopic() { if (httpWrapper == null) { return null; } return httpWrapper.getRequest(); } public RequestType getRequestType() { return requestType; } public FullHttpRequest getHttpRequest() { return httpRequest; } public int getMessageId() { return messageId; } public void setMessageId(int messageId) { this.messageId = messageId; } public JSONObject getJsonObject() { return jsonObject; } public void setJsonObject(JSONObject jsonObject) { this.jsonObject = jsonObject; } @Override public String toString() { ContentBuilder builder = new ContentBuilder(); builder.append("requestType: " + requestType); builder.append("jsonObject: " + jsonObject); builder.append("body: " + body); builder.append("rawData: " + rawData); builder.append("httpWrapper: " + httpWrapper); builder.append("messageId: " + messageId); return builder.toString(); } }