Skip to content

Commit

Permalink
add session decode encode
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Jan 5, 2016
1 parent 994e92a commit 84d86d2
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
@@ -1,7 +1,5 @@
package com.shinemo.mpush.api.connection;

import com.google.common.base.Strings;

/**
* Created by ohun on 2015/12/22.
*/
Expand Down Expand Up @@ -42,10 +40,9 @@ public void setHeartbeat(int heartbeat) {
}

public boolean handshakeOk() {
return !Strings.isNullOrEmpty(deviceId);
return deviceId != null && deviceId.length() > 0;
}


@Override
public String toString() {
return "SessionContext{" +
Expand Down
Expand Up @@ -3,12 +3,14 @@
import com.shinemo.mpush.api.connection.Cipher;
import com.shinemo.mpush.tools.crypto.AESUtils;

import java.util.Arrays;

/**
* Created by ohun on 2015/12/28.
*/
public final class AesCipher implements Cipher {
private final byte[] key;
private final byte[] iv;
public final byte[] key;
public final byte[] iv;

public AesCipher(byte[] key, byte[] iv) {
this.key = key;
Expand All @@ -24,4 +26,18 @@ public byte[] decrypt(byte[] data) {
public byte[] encrypt(byte[] data) {
return AESUtils.encrypt(data, key, iv);
}

@Override
public String toString() {
return toString(key) + ',' + toString(iv);
}

public String toString(byte[] a) {
StringBuilder b = new StringBuilder();
for (int i = 0; i < a.length; i++) {
if (i != 0) b.append('|');
b.append(a[i]);
}
return b.toString();
}
}
Expand Up @@ -25,12 +25,12 @@ public void handle(FastConnectMessage message) {
ReusableSession session = ReusableSessionManager.INSTANCE.getSession(message.sessionId);
if (session == null) {
ErrorMessage.from(message).setReason("session expire").close();
} else if (!session.sessionContext.deviceId.equals(message.deviceId)) {
} else if (!session.context.deviceId.equals(message.deviceId)) {
ErrorMessage.from(message).setReason("error device").close();
} else {
int heartbeat = MPushUtil.getHeartbeat(message.minHeartbeat, message.maxHeartbeat);
session.sessionContext.setHeartbeat(heartbeat);
message.getConnection().setSessionContext(session.sessionContext);
session.context.setHeartbeat(heartbeat);
message.getConnection().setSessionContext(session.context);
FastConnectOkMessage
.from(message)
.setServerHost(MPushUtil.getLocalIp())
Expand Down
@@ -1,12 +1,49 @@
package com.shinemo.mpush.core.session;

import com.shinemo.mpush.api.connection.SessionContext;
import com.shinemo.mpush.common.security.AesCipher;
import com.shinemo.mpush.common.security.CipherBox;

/**
* Created by ohun on 2015/12/25.
*/
public final class ReusableSession {
public String sessionId;
public long expireTime;
public SessionContext sessionContext;
public SessionContext context;

public String encode() {
StringBuffer sb = new StringBuffer();
sb.append(context.osName).append(',');
sb.append(context.osVersion).append(',');
sb.append(context.clientVersion).append(',');
sb.append(context.deviceId).append(',');
sb.append(context.cipher).append(',');
return sb.toString();
}

public void decode(String value) throws Exception {
String[] array = value.split(",");
if (array.length != 6) throw new RuntimeException("decode session exception");
SessionContext context = new SessionContext();
context.osName = array[0];
context.osVersion = array[1];
context.clientVersion = array[2];
context.deviceId = array[3];
byte[] key = toArray(array[4]);
byte[] iv = toArray(array[5]);
context.cipher = new AesCipher(key, iv);
}

private byte[] toArray(String str) {
String[] a = str.split("|");
if (a.length != CipherBox.INSTANCE.getAesKeyLength()) {
throw new RuntimeException("decode session cipher exception");
}
byte[] bytes = new byte[a.length];
for (int i = 0; i < a.length; i++) {
bytes[i] = Byte.parseByte(a[i]);
}
return bytes;
}
}
@@ -1,33 +1,41 @@
package com.shinemo.mpush.core.session;

import com.shinemo.mpush.api.connection.SessionContext;
import com.shinemo.mpush.tools.Strings;
import com.shinemo.mpush.tools.crypto.MD5Utils;
import io.netty.util.internal.chmv8.ConcurrentHashMapV8;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by ohun on 2015/12/25.
*/
public final class ReusableSessionManager {
public static final ReusableSessionManager INSTANCE = new ReusableSessionManager();
private static final int EXPIRE_TIME = 24 * 60 * 60 * 1000;
private final Map<String, ReusableSession> sessionCache = new ConcurrentHashMapV8<>();
private final Map<String, String> sessionCache = new ConcurrentHashMapV8<>();

public boolean cacheSession(ReusableSession session) {
sessionCache.put(session.sessionId, session);
sessionCache.put(session.sessionId, session.encode());
return true;
}

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

public ReusableSession genSession(SessionContext context) {
long now = System.currentTimeMillis();
ReusableSession session = new ReusableSession();
session.sessionContext = context;
session.context = context;
session.sessionId = MD5Utils.encrypt(context.deviceId + now);
session.expireTime = now + EXPIRE_TIME;
return session;
Expand Down

0 comments on commit 84d86d2

Please sign in to comment.