Skip to content

Commit

Permalink
添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Jan 8, 2016
1 parent fb286bd commit de64383
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
@@ -1,5 +1,6 @@
package com.shinemo.mpush.common.security; package com.shinemo.mpush.common.security;


import com.shinemo.mpush.api.Constants;
import com.shinemo.mpush.api.connection.Cipher; import com.shinemo.mpush.api.connection.Cipher;
import com.shinemo.mpush.tools.crypto.AESUtils; import com.shinemo.mpush.tools.crypto.AESUtils;


Expand Down Expand Up @@ -44,7 +45,7 @@ public String toString(byte[] a) {
public static byte[] toArray(String str) { public static byte[] toArray(String str) {
String[] a = str.split("\\|"); String[] a = str.split("\\|");
if (a.length != CipherBox.INSTANCE.getAesKeyLength()) { if (a.length != CipherBox.INSTANCE.getAesKeyLength()) {
throw new RuntimeException("decode cipher ex key length invalid"); return null;
} }
byte[] bytes = new byte[a.length]; byte[] bytes = new byte[a.length];
for (int i = 0; i < a.length; i++) { for (int i = 0; i < a.length; i++) {
Expand Down
Expand Up @@ -27,22 +27,33 @@ public FastConnectMessage decode(Packet packet, Connection connection) {
@Override @Override
public void handle(FastConnectMessage message) { public void handle(FastConnectMessage message) {
ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId); ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId);

if (session == null) { if (session == null) {
ErrorMessage.from(message).setReason("session expire").send();
ErrorMessage.from(message).setReason("session expired").send();

LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId); LOGGER.warn("fast connect failure, session is expired, sessionId={}, deviceId={}", message.sessionId, message.deviceId);

} else if (!session.context.deviceId.equals(message.deviceId)) { } else if (!session.context.deviceId.equals(message.deviceId)) {
ErrorMessage.from(message).setReason("error device").send();
LOGGER.warn("fast connect failure, not same device, deviceId={}, session={}", message.deviceId, session.context); ErrorMessage.from(message).setReason("invalid device").send();

LOGGER.warn("fast connect failure, not the same device, deviceId={}, session={}", message.deviceId, session.context);

} else { } else {

int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat); int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat);

session.context.setHeartbeat(heartbeat); session.context.setHeartbeat(heartbeat);
message.getConnection().setSessionContext(session.context); message.getConnection().setSessionContext(session.context);

FastConnectOkMessage FastConnectOkMessage
.from(message) .from(message)
.setServerHost(MPushUtil.getLocalIp()) .setServerHost(MPushUtil.getLocalIp())
.setServerTime(System.currentTimeMillis()) .setServerTime(System.currentTimeMillis())
.setHeartbeat(heartbeat) .setHeartbeat(heartbeat)
.send(); .send();

LOGGER.warn("fast connect success, session={}", message.deviceId, session.context); LOGGER.warn("fast connect success, session={}", message.deviceId, session.context);
} }
} }
Expand Down
Expand Up @@ -13,7 +13,7 @@ public final class ReusableSession {
public long expireTime; public long expireTime;
public SessionContext context; public SessionContext context;


public String encode() { public static String encode(SessionContext context) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append(context.osName).append(','); sb.append(context.osName).append(',');
sb.append(context.osVersion).append(','); sb.append(context.osVersion).append(',');
Expand All @@ -23,17 +23,20 @@ public String encode() {
return sb.toString(); return sb.toString();
} }


public void decode(String value) throws Exception { public static ReusableSession decode(String value) {
String[] array = value.split(","); String[] array = value.split(",");
if (array.length != 6) throw new RuntimeException("decode session exception"); if (array.length != 6) return null;
SessionContext context = new SessionContext(); SessionContext context = new SessionContext();
context.osName = array[0]; context.osName = array[0];
context.osVersion = array[1]; context.osVersion = array[1];
context.clientVersion = array[2]; context.clientVersion = array[2];
context.deviceId = array[3]; context.deviceId = array[3];
byte[] key = AesCipher.toArray(array[4]); byte[] key = AesCipher.toArray(array[4]);
byte[] iv = AesCipher.toArray(array[5]); byte[] iv = AesCipher.toArray(array[5]);
if (key == null || iv == null) return null;
context.cipher = new AesCipher(key, iv); context.cipher = new AesCipher(key, iv);
this.context = context; ReusableSession session = new ReusableSession();
session.context = context;
return session;
} }
} }
Expand Up @@ -14,20 +14,14 @@ public final class ReusableSessionManager {
private int expiredTime = ConfigCenter.INSTANCE.getSessionExpiredTime(); private int expiredTime = ConfigCenter.INSTANCE.getSessionExpiredTime();


public boolean cacheSession(ReusableSession session) { public boolean cacheSession(ReusableSession session) {
RedisManage.set(session.sessionId, session.encode(), expiredTime); RedisManage.set(session.sessionId, ReusableSession.encode(session.context), expiredTime);
return true; return true;
} }


public ReusableSession getSession(String sessionId) { public ReusableSession getSession(String sessionId) {
String value = RedisManage.get(sessionId, String.class); String value = RedisManage.get(sessionId, String.class);
if (Strings.isBlank(value)) return null; if (Strings.isBlank(value)) return null;
ReusableSession session = new ReusableSession(); return ReusableSession.decode(value);
try {
session.decode(value);
} catch (Exception e) {
return null;
}
return session;
} }


public ReusableSession genSession(SessionContext context) { public ReusableSession genSession(SessionContext context) {
Expand Down

0 comments on commit de64383

Please sign in to comment.