Sekiro开源版使用指南

一些资料

【我们已经构建好的包】
开源版地址:https://oss.iinti.cn/sekiro/sekiro-demo
一般建议下载最新的包

【开源代码仓库】
github开源版地址:https://github.com/yint-tech/sekiro-open

【我们的文档】
https://sekiro.iinti.cn/sekiro-doc/

安装流程

安装环境需要有Java环境(经过测试jdk8, jdk11, jdk17等都是可以正常使用的

以下载我们构建好的包为例,先进行解压
img.png

在项目目录下,执行启动脚本:

  • windows: bin\sekiro.bat
  • linux: bin/sekiro.sh

功能验证

这里使用一个js注入的场景,选取了一个http网址 (如果是https的小伙伴记得把ws改成wss,具体可以参考 https://iinti.cn/sekiro-article/sekiro-wss/

需要注意: 开源版本在实现上没有支持wss,只能通过证书签名的方式。请参考:https://sekiro.iinti.cn/sekiro-doc/02_advance/03_sslForWebsocket.html#%E5%AE%89%E8%A3%85ca%E8%AF%81%E4%B9%A6 商业版可以直接使用443端口

在网页控制台下贴入

function SekiroClient(e){if(this.wsURL=e,this.handlers={},this.socket={},!e)throw new Error("wsURL can not be empty!!");this.webSocketFactory=this.resolveWebSocketFactory(),this.connect()}SekiroClient.prototype.resolveWebSocketFactory=function(){if("object"==typeof window){var e=window.WebSocket?window.WebSocket:window.MozWebSocket;return function(o){function t(o){this.mSocket=new e(o)}return t.prototype.close=function(){this.mSocket.close()},t.prototype.onmessage=function(e){this.mSocket.onmessage=e},t.prototype.onopen=function(e){this.mSocket.onopen=e},t.prototype.onclose=function(e){this.mSocket.onclose=e},t.prototype.send=function(e){this.mSocket.send(e)},new t(o)}}if("object"==typeof weex)try{console.log("test webSocket for weex");var o=weex.requireModule("webSocket");return console.log("find webSocket for weex:"+o),function(e){try{o.close()}catch(e){}return o.WebSocket(e,""),o}}catch(e){console.log(e)}if("object"==typeof WebSocket)return function(o){return new e(o)};throw new Error("the js environment do not support websocket")},SekiroClient.prototype.connect=function(){console.log("sekiro: begin of connect to wsURL: "+this.wsURL);var e=this;try{this.socket=this.webSocketFactory(this.wsURL)}catch(o){return console.log("sekiro: create connection failed,reconnect after 2s:"+o),void setTimeout(function(){e.connect()},2e3)}this.socket.onmessage(function(o){e.handleSekiroRequest(o.data)}),this.socket.onopen(function(e){console.log("sekiro: open a sekiro client connection")}),this.socket.onclose(function(o){console.log("sekiro: disconnected ,reconnection after 2s"),setTimeout(function(){e.connect()},2e3)})},SekiroClient.prototype.handleSekiroRequest=function(e){console.log("receive sekiro request: "+e);var o=JSON.parse(e),t=o.__sekiro_seq__;if(o.action){var n=o.action;if(this.handlers[n]){var s=this.handlers[n],i=this;try{s(o,function(e){try{i.sendSuccess(t,e)}catch(e){i.sendFailed(t,"e:"+e)}},function(e){i.sendFailed(t,e)})}catch(e){console.log("error: "+e),i.sendFailed(t,":"+e)}}else this.sendFailed(t,"no action handler: "+n+" defined")}else this.sendFailed(t,"need request param {action}")},SekiroClient.prototype.sendSuccess=function(e,o){var t;if("string"==typeof o)try{t=JSON.parse(o)}catch(e){(t={}).data=o}else"object"==typeof o?t=o:(t={}).data=o;(Array.isArray(t)||"string"==typeof t)&&(t={data:t,code:0}),t.code?t.code=0:(t.status,t.status=0),t.__sekiro_seq__=e;var n=JSON.stringify(t);console.log("response :"+n),this.socket.send(n)},SekiroClient.prototype.sendFailed=function(e,o){"string"!=typeof o&&(o=JSON.stringify(o));var t={};t.message=o,t.status=-1,t.__sekiro_seq__=e;var n=JSON.stringify(t);console.log("sekiro: response :"+n),this.socket.send(n)},SekiroClient.prototype.registerAction=function(e,o){if("string"!=typeof e)throw new Error("an action must be string");if("function"!=typeof o)throw new Error("a handler must be function");return console.log("sekiro: register action: "+e),this.handlers[e]=o,this};
var client = new SekiroClient("ws://127.0.0.1:5612/business/register?group=test_web&clientId=" + Math.random());
client.registerAction("testAction", function (request, resolve, reject) {
    resolve("ok");
});

回车看到建立起了一个Sekiro连接

img_14.png

然后我们去浏览器调用一下:http://127.0.0.1:5612/business/invoke?group=test_web&action=testAction&param=testparm

img_15.png

可以看到返回的data符合我们在注入代码里写的resolve("ok")

常见问答

Q1:我需要手动在后台创建分组吗?
A1:demo版本是自动的。开源版group之类的信息都存在内存中。


Q2:我想使用demo版本注入https的站点,有哪些办法?
A2:

  • 方法一:自签名或者用域名签名
    可以自己去网上找相关的教程
  • 方法二:部署在本地127.0.0.1的服务端不需要签名吧?(答案是否定的)
    理论上我们不需要对发送到127.0.0.1(即本地)的消息进行签名,因为对于发送到本地的消息我们通常认为它们是可信的。
    但是【开源版在实现上没有支持wss】,会直接关掉连接。
  • 方法三:最省事的就是使用我们的测试服务器了~
    测试服务器是有CA签名的。

Q3:一个group下面有action数量限制吗,action不用会自动回收吗
A3:一个group下面没有action数量限制;action不会自动回收 你用开源版本的话,关掉就没有了~都是记录在内存里的~ 如果自己有需求需要控制创建和删除的话,可以自己在源码上修改~


Q4:Sekiro的并发在多少?
A4:一般是用不到上限的~
最后的瓶颈在设备和网络。我们100M裸机,24核16G,支撑了5W+QPS。
因为设备被打死过、路由器也被打死过,所以我们给设备也做了QPS限流。甚至于nginx也影响性能,所以我们才做的端口直连。