Skip to content

Commit

Permalink
1、websocket鉴权失败时响应ReplyBody,告知客户端鉴权失败
Browse files Browse the repository at this point in the history
  • Loading branch information
farsunset-com committed Jun 23, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 3fd6a69 commit 4eaa6c1
Showing 36 changed files with 476 additions and 144 deletions.
26 changes: 21 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@

## 1、[在线文档](https://www.yuque.com/yuanfangxiyang/ma4ytb)
## 1、[在线文档](https://www.yuque.com/yuanfangxiyang/ma4ytb)(https://www.yuque.com/yuanfangxiyang/ma4ytb)

## 2、[产品官网](http://farsunset.com)(http://farsunset.com)

---

## 联系方式

微信:farbluesky

<img src="https://staticres.oss-cn-hangzhou.aliyuncs.com/qcode/wechat_add.png" width="30%" />


邮箱:

[email protected]

## 2、[产品官网](http://farsunset.com)

---

@@ -13,19 +27,21 @@ CIM采用业内主流开源技术构建,易于扩展和使用,并完美支

用时7年 基于CIM的项目已经运行在全国各个地方,包括上市公司,各地政务系统,警务系统等服务于上百家客户,希望CIM也能为您带来价值,如果您也希望加入项目成为贡献者,请联系我。如果觉得有用欢迎打赏。

如果对您有价值,请送一个star和Fork喔~

<div align="center">
<img src="http://staticres.oss-cn-hangzhou.aliyuncs.com/qcode/ali_pay.jpg" width="30%" />
<img src="http://staticres.oss-cn-hangzhou.aliyuncs.com/qcode/wechat_pay.jpg" width="30%" />
</div>


---
## [收费产品介绍](http://farsunset.com)
## 收费产品介绍

#### 和信
#### 和信(http://farsunset.com)
和信是基于CIM组件开发的一整套完整的产品,面向所有人开放注册的试用场景。具有丰富的功能,聊天、群组、好友列表、黑名单、公众号、朋友圈等功能。不依赖任何第三方服务,可以私有化部署。

#### 侣信
#### 侣信(http://farsunset.com/lvxin)
侣信是基于CIM组件开发的一整套完整的产品,面向中小企业和者各类团队组织内部交流使用工具。具有丰富的功能,聊天、群组、部门组织、公众号、内部朋友圈等功能。不依赖任何第三方服务,可以私有化部署。


Binary file modified cim-boot-server/libs/cim-server-sdk-netty-4.2.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@
public class HandshakePredicate implements Predicate<HandshakeEvent> {

/**
*
* 验证身份信息,本方法切勿进行耗时操作!!!
* @param event
* @return true验证通过 false验证失败
*/
7 changes: 4 additions & 3 deletions cim-boot-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ spring.profiles.active=dev
# JDBC Config #
##################################################################
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/cim?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username = cim
spring.datasource.password = cimv587!
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver

@@ -20,6 +20,7 @@ spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1
spring.datasource.hikari.validation-timeout=600000


##################################################################
# JPA Config #
##################################################################
@@ -67,7 +68,7 @@ cim.websocket.enable=true
cim.websocket.port=34567
cim.websocket.path=/
## json or protobuf
cim.websocket.protocol=json
cim.websocket.protocol=protobuf

#please setting your p12 info and appId.
cim.apns.p12.file=/apns/app.p12
Original file line number Diff line number Diff line change
@@ -29,18 +29,25 @@
function onReplyReceived(reply)
{
console.log(reply);
if(reply.key==='client_bind' && reply.code === "200" )
{
hideProcess();

$('#LoginDialog').fadeOut();


$('#MessageDialog').fadeIn();
$('#MessageDialog').addClass("in");
$("#current_account").text($('#account').val());

}
if (reply.key === KEY_CLIENT_BIND && reply.code === CODE_OK) {
hideProcess();

$('#LoginDialog').fadeOut();

$('#MessageDialog').fadeIn();
$('#MessageDialog').addClass("in");
$("#current_account").text($('#account').val());

}

/**
* 链接鉴权失败
*/
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
hideProcess();
showETip("鉴权失败");
}

}

/** 当收到消息时候回调 **/
25 changes: 22 additions & 3 deletions cim-boot-server/src/main/resources/static/js/cim/cim.web.sdk.js
Original file line number Diff line number Diff line change
@@ -21,6 +21,16 @@ const REPLY_BODY = 4;
const SENT_BODY = 3;
const PING = 1;
const PONG = 0;

/*
* 握手鉴权常量
*/
const KEY_HANDSHAKE = "client_handshake";
const CODE_UNAUTHORIZED = "401";

const CODE_OK = "200";
const KEY_CLIENT_BIND = "client_bind";

/**
* PONG字符串转换后
* @type {Uint8Array}
@@ -54,7 +64,7 @@ CIMPushManager.bind = function (account) {

let browser = getBrowser();
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
body.setKey("client_bind");
body.setKey(KEY_CLIENT_BIND);
body.setTimestamp(new Date().getTime());
body.getDataMap().set("uid", account);
body.getDataMap().set("channel", APP_CHANNEL);
@@ -106,7 +116,8 @@ CIMPushManager.innerOnMessageReceived = function (e) {

if (type === REPLY_BODY) {
let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body);
/**

/*
* 将proto对象转换成json对象,去除无用信息
*/
let reply = {};
@@ -116,13 +127,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
reply.timestamp = message.getTimestamp();
reply.data = {};

/**
/*
* 注意,遍历map这里的参数 value在前key在后
*/
message.getDataMap().forEach(function (v, k) {
reply.data[k] = v;
});

/*
* 判断是否是握手鉴权失败
* 终止后续自动重连
*/
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
manualStop = true;
}

onReplyReceived(reply);
}
};
Binary file modified cim-client-sdk-libs/android/cim-android-sdk-4.2.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ public void onAvailable(Network network) {
}

@Override
public void onUnavailable() {
public void onLost(Network network) {
Intent intent = new Intent();
intent.setPackage(getPackageName());
intent.setAction(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED);
74 changes: 58 additions & 16 deletions cim-client-sdk/cim-web-sdk/cim.web.sdk.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*CIM服务器IP*/
const CIM_HOST = "127.0.0.1";
const CIM_HOST = window.location.hostname;
/*
*服务端 websocket端口
*/
const CIM_PORT = 34567;
const CIM_URI = "ws://" + CIM_HOST + ":" + CIM_PORT;

const APP_VERSION = "1.0.0";
const APP_CHANNEL = "browser";
const APP_CHANNEL = "web";
const APP_PACKAGE = "com.farsunset.cim";

/*
@@ -18,6 +18,25 @@ const DATA_HEADER_LENGTH = 1;

const MESSAGE = 2;
const REPLY_BODY = 4;
const SENT_BODY = 3;
const PING = 1;
const PONG = 0;

/*
* 握手鉴权常量
*/
const KEY_HANDSHAKE = "client_handshake";
const CODE_UNAUTHORIZED = "401";

const CODE_OK = "200";
const KEY_CLIENT_BIND = "client_bind";

/**
* PONG字符串转换后
* @type {Uint8Array}
*/
const PONG_BODY = new Uint8Array([80,79,78,71]);


let socket;
let manualStop = false;
@@ -33,27 +52,28 @@ CIMPushManager.connect = function () {
socket.onclose = CIMPushManager.innerOnConnectionClosed;
};

CIMPushManager.bindAccount = function (account) {
CIMPushManager.bind = function (account) {

window.localStorage.account = account;

let deviceId = window.localStorage.deviceIddeviceId;
if (deviceId == '' || deviceId == undefined) {
let deviceId = window.localStorage.deviceId;
if (deviceId === '' || deviceId === undefined) {
deviceId = generateUUID();
window.localStorage.deviceId = deviceId;
}

let browser = getBrowser();
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
body.setKey("client_bind");
body.setKey(KEY_CLIENT_BIND);
body.setTimestamp(new Date().getTime());
body.getDataMap().set("account", account);
body.getDataMap().set("uid", account);
body.getDataMap().set("channel", APP_CHANNEL);
body.getDataMap().set("appVersion", APP_VERSION);
body.getDataMap().set("osVersion", browser.version);
body.getDataMap().set("packageName", APP_PACKAGE);
body.getDataMap().set("deviceId", deviceId);
body.getDataMap().set("device", browser.name);
body.getDataMap().set("deviceName", browser.name);
body.getDataMap().set("language", navigator.language);
CIMPushManager.sendRequest(body);
};

@@ -83,15 +103,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
let type = data[0];
let body = data.subarray(DATA_HEADER_LENGTH, data.length);

if (type == MESSAGE) {
if (type === PING) {
CIMPushManager.pong();
return;
}

if (type === MESSAGE) {
let message = proto.com.farsunset.cim.sdk.web.model.Message.deserializeBinary(body);
onInterceptMessageReceived(message.toObject(false));
return;
}

if (type == REPLY_BODY) {
if (type === REPLY_BODY) {
let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body);
/**

/*
* 将proto对象转换成json对象,去除无用信息
*/
let reply = {};
@@ -101,13 +127,21 @@ CIMPushManager.innerOnMessageReceived = function (e) {
reply.timestamp = message.getTimestamp();
reply.data = {};

/**
/*
* 注意,遍历map这里的参数 value在前key在后
*/
message.getDataMap().forEach(function (v, k) {
reply.data[k] = v;
});

/*
* 判断是否是握手鉴权失败
* 终止后续自动重连
*/
if(reply.key === KEY_HANDSHAKE && reply.code === CODE_UNAUTHORIZED){
manualStop = true;
}

onReplyReceived(reply);
}
};
@@ -123,16 +157,24 @@ CIMPushManager.innerOnConnectionClosed = function (e) {

CIMPushManager.sendRequest = function (body) {
let data = body.serializeBinary();
let protobuf = new Uint8Array(data.length);
protobuf.set(data, 0);
let protobuf = new Uint8Array(data.length + 1);
protobuf[0] = SENT_BODY;
protobuf.set(data, 1);
socket.send(protobuf);
};

CIMPushManager.pong = function () {
let pong = new Uint8Array(PONG_BODY.byteLength + 1);
pong[0] = PONG;
pong.set(PONG_BODY,1);
socket.send(pong);
};

function onInterceptMessageReceived(message) {
/*
*被强制下线之后,不再继续连接服务端
*/
if (message.action == ACTION_999) {
if (message.action === ACTION_999) {
manualStop = true;
}
/*
@@ -174,7 +216,7 @@ function generateUUID() {
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
let r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid.replace(/-/g, '');
}
Original file line number Diff line number Diff line change
@@ -71,13 +71,13 @@ protected NioSocketAcceptor(CIMRequestHandler outerRequestHandler){

ThreadFactory bossThreadFactory = r -> {
Thread thread = new Thread(r);
thread.setName("nio-boss-");
thread.setName("nio-boss-" + thread.getId());
return thread;
};

ThreadFactory workerThreadFactory = r -> {
Thread thread = new Thread(r);
thread.setName("nio-worker-");
thread.setName("nio-worker-" + thread.getId());
return thread;
};

@@ -174,7 +174,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt){
}

/*
* 如果心跳请求发出30秒内没收到响应,则关闭连接
* 如果心跳请求发出(readIdle-writeIdle)秒内没收到响应,则关闭连接
*/
Integer pingCount = ctx.channel().attr(ChannelAttr.PING_COUNT).get();
if (idleEvent.state() == IdleState.READER_IDLE && pingCount != null && pingCount >= PONG_TIME_OUT_COUNT) {
Original file line number Diff line number Diff line change
@@ -106,7 +106,6 @@ public void initChannel(SocketChannel ch){
ch.pipeline().addLast(loggingHandler);
ch.pipeline().addLast(WebsocketAcceptor.this);
ch.pipeline().addLast(illegalRequestHandler);

}

});
Loading

0 comments on commit 4eaa6c1

Please sign in to comment.