Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
新增 加密和压缩flags
- Loading branch information
Showing
20 changed files
with
199 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 31 additions & 33 deletions
64
mpush-core/src/main/java/com/shinemo/mpush/core/handler/HandShakeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,59 +1,57 @@ | |||
package com.shinemo.mpush.core.handler; | package com.shinemo.mpush.core.handler; | ||
|
|
||
import com.shinemo.mpush.api.SessionInfo; | |||
import com.shinemo.mpush.api.Constants; | import com.shinemo.mpush.api.Constants; | ||
import com.shinemo.mpush.api.Request; | import com.shinemo.mpush.api.Request; | ||
import com.shinemo.mpush.api.SessionInfo; | |||
import com.shinemo.mpush.api.protocol.Packet; | import com.shinemo.mpush.api.protocol.Packet; | ||
import com.shinemo.mpush.core.message.HandShakeMessage; | import com.shinemo.mpush.core.message.HandshakeMessage; | ||
import com.shinemo.mpush.core.security.CredentialManager; | import com.shinemo.mpush.core.message.HandshakeSuccessMsg; | ||
import com.shinemo.mpush.core.security.ReusableToken; | import com.shinemo.mpush.core.security.CipherManager; | ||
import com.shinemo.mpush.core.security.ReusableTokenManager; | import com.shinemo.mpush.core.security.ReusableSession; | ||
import com.shinemo.mpush.core.security.ReusableSessionManager; | |||
import com.shinemo.mpush.tools.Jsons; | import com.shinemo.mpush.tools.Jsons; | ||
import com.shinemo.mpush.tools.MPushUtil; | import com.shinemo.mpush.tools.MPushUtil; | ||
import com.shinemo.mpush.tools.crypto.CryptoUtils; | import com.shinemo.mpush.tools.crypto.AESUtils; | ||
import com.shinemo.mpush.tools.crypto.DESUtils; | |||
import com.shinemo.mpush.tools.crypto.RSAUtils; | import com.shinemo.mpush.tools.crypto.RSAUtils; | ||
import org.apache.commons.lang3.RandomStringUtils; | |||
import org.slf4j.Logger; | import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||
|
|
||
import java.io.Serializable; | |||
import java.security.interfaces.RSAPrivateKey; | import java.security.interfaces.RSAPrivateKey; | ||
import java.util.HashMap; | |||
import java.util.Map; | |||
|
|
||
/** | /** | ||
* Created by ohun on 2015/12/24. | * Created by ohun on 2015/12/24. | ||
*/ | */ | ||
public class HandShakeHandler extends BaseMessageHandler<HandShakeMessage> { | public class HandshakeHandler extends BaseMessageHandler<HandshakeMessage> { | ||
public static final Logger LOGGER = LoggerFactory.getLogger(HandShakeHandler.class); | public static final Logger LOGGER = LoggerFactory.getLogger(HandshakeHandler.class); | ||
|
|
||
@Override | @Override | ||
public HandShakeMessage decodeBody(Packet packet) { | public HandshakeMessage decodeBody(byte[] body) { | ||
RSAPrivateKey privateKey = CredentialManager.INSTANCE.getPrivateKey(); | RSAPrivateKey privateKey = CipherManager.INSTANCE.getPrivateKey(); | ||
byte[] rawData = RSAUtils.decryptByPrivateKey(packet.body, privateKey); | byte[] rawData = RSAUtils.decryptByPrivateKey(body, privateKey); | ||
return Jsons.fromJson(new String(rawData, Constants.UTF_8), HandShakeMessage.class); | return Jsons.fromJson(new String(rawData, Constants.UTF_8), HandshakeMessage.class); | ||
} | } | ||
|
|
||
@Override | @Override | ||
public void handle(HandShakeMessage body, Request request) { | public void handle(HandshakeMessage body, Request request) { | ||
String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE); | byte[] iv = body.iv; | ||
String clientKey = body.clientKey; | byte[] clientKey = body.clientKey; | ||
String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥 | byte[] serverKey = CipherManager.INSTANCE.randomAESKey(); | ||
SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey); | byte[] sessionKey = CipherManager.INSTANCE.mixKey(clientKey, serverKey);//会话密钥混淆 Client random | ||
SessionInfo info = new SessionInfo(body.osName, body.osVersion, body.clientVersion, | |||
body.deviceId, sessionKey, iv); | |||
request.getConnection().setSessionInfo(info); | request.getConnection().setSessionInfo(info); | ||
ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info); | ReusableSession session = ReusableSessionManager.INSTANCE.genSession(info); | ||
ReusableTokenManager.INSTANCE.saveToken(token); | ReusableSessionManager.INSTANCE.saveSession(session); | ||
Map<String, Serializable> resp = new HashMap<String, Serializable>(); | HandshakeSuccessMsg resp = new HandshakeSuccessMsg(); | ||
resp.put("serverKey", serverKey); | resp.serverKey = serverKey; | ||
resp.put("serverHost", MPushUtil.getLocalIp()); | resp.serverHost = MPushUtil.getLocalIp(); | ||
resp.put("serverTime", System.currentTimeMillis()); | resp.serverTime = System.currentTimeMillis(); | ||
resp.put("heartbeat", Constants.HEARTBEAT_TIME); | resp.heartbeat = Constants.HEARTBEAT_TIME; | ||
resp.put("tokenId", token.tokenId); | resp.sessionId = session.sessionId; | ||
resp.put("tokenExpire", token.expireTime); | resp.expireTime = session.expireTime; | ||
byte[] responseData = DESUtils.encryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey); | byte[] responseData = AESUtils.encrypt(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey, iv); | ||
request.getResponse().sendRaw(responseData); | request.getResponse().sendRaw(responseData); | ||
LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", desKey, clientKey, serverKey); | LOGGER.info("会话密钥:{},clientKey={}, serverKey={}", sessionKey, clientKey, serverKey); | ||
|
|
||
} | } | ||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.