Skip to content

Commit

Permalink
add hand shake
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Dec 25, 2015
1 parent 29179b4 commit a625922
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/**
* Created by ohun on 2015/12/22.
*/
public class ConnectionInfo {
public class ClientInfo {
public final String osName;
public final String osVersion;
public final String clientVersion;
public final String deviceId;
public final String desKey;

public ConnectionInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) {
public ClientInfo(String osName, String osVersion, String clientVersion, String deviceId, String desKey) {
this.osName = osName;
this.osVersion = osVersion;
this.clientVersion = clientVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
*/
public interface Connection {

void setConnectionInfo(ConnectionInfo info);
void setClientInfo(ClientInfo info);

ClientInfo getClientInfo();

String getId();

Expand Down
4 changes: 3 additions & 1 deletion mpush-api/src/main/java/com/shinemo/mpush/api/Receiver.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.shinemo.mpush.api;

import com.shinemo.mpush.api.protocol.Packet;

/**
* Created by ohun on 2015/12/22.
*/
public interface Receiver {
void onMessage(Request request);
void onMessage(Packet packet, Connection connection);
}
31 changes: 5 additions & 26 deletions mpush-api/src/main/java/com/shinemo/mpush/api/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,13 @@
/**
* Created by ohun on 2015/12/22.
*/
public class Request {
private final Command command;
private final Packet message;
private final Connection connection;
public interface Request {

public Request(Packet message, Connection connection) {
this.message = message;
this.connection = connection;
this.command = Command.toCMD(message.command);
}
Command getCommand();

public Command getCommand() {
return command;
}
Packet getMessage();

public Packet getMessage() {
return message;
}
Connection getConnection();

public Connection getConnection() {
return connection;
}

public Response getResponse() {
Packet packet = new Packet();
packet.command = message.command;
packet.msgId = message.msgId;
packet.version = message.version;
return new Response(packet, connection);
}
Response getResponse();
}
21 changes: 4 additions & 17 deletions mpush-api/src/main/java/com/shinemo/mpush/api/Response.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
package com.shinemo.mpush.api;

import com.shinemo.mpush.api.protocol.Packet;

/**
* Created by ohun on 2015/12/22.
*/
public class Response {
private final Packet packet;
private final Connection connection;

public Response(Packet packet, Connection connection) {
this.packet = packet;
this.connection = connection;
}

public void send(byte[] body) {
packet.body = body;
connection.send(packet);
}
public interface Response {

void send(byte[] body);

public void sendError(byte[] reson) {
void sendRaw(byte[] body);

}
void sendError(byte[] reason);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ConnectionHandler extends ChannelHandlerAdapter {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("channelRead msg=" + msg);
logger.debug("channelRead msg=" + msg);
receiver.onMessage(new Request((Packet) msg, connection));
receiver.onMessage((Packet) msg, connection);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.shinemo.mpush.core;

import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.MessageHandler;
import com.shinemo.mpush.api.Receiver;
import com.shinemo.mpush.api.Request;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.core.handler.BindHandler;
import com.shinemo.mpush.core.handler.HeartBeatHandler;
import com.shinemo.mpush.core.handler.LoginHandler;
import com.shinemo.mpush.core.message.NettyRequest;

/**
* Created by ohun on 2015/12/22.
Expand All @@ -16,10 +19,11 @@ public class MessageReceiver implements Receiver {
public static final HeartBeatHandler HEART_HANDLER = new HeartBeatHandler();

@Override
public void onMessage(Request request) {
public void onMessage(Packet packet, Connection connection) {
Request request = new NettyRequest(packet, connection);
switch (request.getCommand()) {
case Heartbeat:
HEART_HANDLER.handle(request);
HEART_HANDLER.handle(request);
break;
case Handshake:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.netty.channel.ChannelFutureListener;

import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.ConnectionInfo;
import com.shinemo.mpush.api.ClientInfo;
import com.shinemo.mpush.api.protocol.Packet;

/**
Expand All @@ -18,7 +18,7 @@ public class NettyConnection implements Connection {

private static final Logger log = LoggerFactory.getLogger(NettyConnection.class);

private ConnectionInfo info;
private ClientInfo info;
private Channel channel;
private int status = 0;

Expand All @@ -30,7 +30,7 @@ public void init(Channel channel) {
}

@Override
public void setConnectionInfo(ConnectionInfo info) {
public void setClientInfo(ClientInfo info) {
this.info = info;
}

Expand Down Expand Up @@ -75,14 +75,10 @@ public boolean isOpen() {
return false;
}

public ConnectionInfo getInfo() {
public ClientInfo getClientInfo() {
return info;
}

public void setInfo(ConnectionInfo info) {
this.info = info;
}

public Channel getChannel() {
return channel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shinemo.mpush.core.handler;

import com.shinemo.mpush.api.ConnectionInfo;
import com.shinemo.mpush.api.ClientInfo;
import com.shinemo.mpush.api.Constants;
import com.shinemo.mpush.api.Request;
import com.shinemo.mpush.api.protocol.Packet;
Expand Down Expand Up @@ -29,16 +29,17 @@ public class HandShakeHandler extends BaseMessageHandler<HandShakeMessage> {
public HandShakeMessage decodeBody(Packet packet) {
RSAPrivateKey privateKey = CredentialManager.INSTANCE.getPrivateKey();
byte[] unwarp = RSAUtils.decryptByPrivateKey(packet.body, privateKey);

return new HandShakeMessage();
}

@Override
public void handle(HandShakeMessage body, Request request) {
String serverKey = RandomStringUtils.randomAscii(CryptoUtils.DES_KEY_SIZE);
String clientKey = CryptoUtils.fill2Length(body.clientKey, CryptoUtils.DES_KEY_SIZE);
String clientKey = body.clientKey;
String desKey = CryptoUtils.mixString(clientKey, serverKey);//生成混淆密钥
ConnectionInfo info = new ConnectionInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey);
request.getConnection().setConnectionInfo(info);
ClientInfo info = new ClientInfo(body.osName, body.osVersion, body.clientVersion, body.deviceId, desKey);
request.getConnection().setInfo(info);
ReusableToken token = ReusableTokenManager.INSTANCE.genToken(info);
ReusableTokenManager.INSTANCE.saveToken(token);
Map<String, Serializable> resp = new HashMap<String, Serializable>();
Expand All @@ -48,7 +49,7 @@ public void handle(HandShakeMessage body, Request request) {
resp.put("heartbeat", Constants.HEARTBEAT_TIME);
resp.put("tokenId", token.tokenId);
resp.put("tokenExpire", token.expireTime);
byte[] responseData = DESUtils.decryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), desKey);
request.getResponse().send(responseData);
byte[] responseData = DESUtils.decryptDES(Jsons.toJson(resp).getBytes(Constants.UTF_8), clientKey);
request.getResponse().sendRaw(responseData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.shinemo.mpush.core.message;

import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.Request;
import com.shinemo.mpush.api.Response;
import com.shinemo.mpush.api.protocol.Command;
import com.shinemo.mpush.api.protocol.Packet;

/**
* Created by ohun on 2015/12/22.
*/
public class NettyRequest implements Request{
private final Command command;
private final Packet message;
private final Connection connection;

public NettyRequest(Packet message, Connection connection) {
this.message = message;
this.connection = connection;
this.command = Command.toCMD(message.command);
}

public Command getCommand() {
return command;
}

public Packet getMessage() {
return message;
}

public Connection getConnection() {
return connection;
}

public Response getResponse() {
Packet packet = new Packet();
packet.command = message.command;
packet.msgId = message.msgId;
packet.version = message.version;
return new NettyResponse(packet, connection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.shinemo.mpush.core.message;

import com.shinemo.mpush.api.Connection;
import com.shinemo.mpush.api.Response;
import com.shinemo.mpush.api.protocol.Packet;
import com.shinemo.mpush.tools.crypto.DESUtils;

/**
* Created by ohun on 2015/12/22.
*/
public class NettyResponse implements Response {
private final Packet packet;
private final Connection connection;

public NettyResponse(Packet packet, Connection connection) {
this.packet = packet;
this.connection = connection;
}

public void send(byte[] body) {
packet.body = DESUtils.decryptDES(body, connection.getInfo().desKey);
connection.send(packet);
}

public void sendRaw(byte[] body) {
packet.body = body;
connection.send(packet);
}


public void sendError(byte[] reason) {

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.shinemo.mpush.core.security;

import com.shinemo.mpush.api.ConnectionInfo;
import com.shinemo.mpush.api.ClientInfo;
import com.shinemo.mpush.tools.crypto.MD5Utils;

/**
Expand All @@ -19,7 +19,7 @@ public ReusableToken getToken() {
return new ReusableToken();
}

public ReusableToken genToken(ConnectionInfo info) {
public ReusableToken genToken(ClientInfo info) {
/**
* 先生成key,需要保证半个周期内同一个设备生成的key是相同的
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;

/**
* Created by ohun on 2015/12/25.
Expand Down
Loading

0 comments on commit a625922

Please sign in to comment.