merge: merge old web-client

This commit is contained in:
Frank.R.Wu 2021-06-16 16:30:14 +08:00
parent 0ede1c3919
commit 1ead2613a9
3 changed files with 239 additions and 249 deletions

View File

@ -3,30 +3,30 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Cache-Control" content="no-cache"/> <meta http-equiv="Cache-Control" content="no-cache"/>
<title>数瑞客户端</title> <title>数瑞客户端</title>
<script src="./js/createWS.js"></script> <script src="./js/createWS.js"></script>
<script src="./js/cryptico.js"></script> <script src="./js/cryptico.js"></script>
<script src="./js/jquery-2.1.4.js"></script> <script src="./js/jquery-2.1.4.js"></script>
<script src="./js/bootstrap.min.js"></script> <script src="./js/bootstrap.min.js"></script>
<script src="./js/commonutil.js"></script> <script src="./js/commonutil.js"></script>
<link href="./css/common.css" rel="stylesheet">
<link href="./css/bootstrap.min.css" rel="stylesheet">
<script src="./js/vue.js"></script> <script src="./js/vue.js"></script>
<script src="./js/bdwareclient.js"></script> <script src="./js/bdwareclient.js"></script>
<script src="./js/sm2.js"></script> <script src="./js/sm2.js"></script>
<link href="./css/common.css" rel="stylesheet">
<link href="./css/bootstrap.min.css" rel="stylesheet">
</head> </head>
<body> <body>
<header <header class="headerFooter navbar navbar-default navbar-fixed-top navColor ">
class="headerFooter navbar navbar-default navbar-fixed-top navColor ">
<div class="col-10"> <div class="col-10">
<div class="input-group"> <div class="input-group">
<a class="navbar-brand" href="#"> <img alt="Brand" <a class="navbar-brand" href="#">
src="./img/logo-1.png" style="height: 30px"> <img alt="Brand"
</a><a class="navbar-brand">数瑞浏览器&nbsp;&nbsp; </a> src="./img/logo-1.png" style="height: 30px">
<div class="input-group-prepend" > </a>
<a class="navbar-brand">数瑞浏览器&nbsp;&nbsp; </a>
<div class="input-group-prepend">
<input id="urlInput" style="border-top-left-radius: 5px; border-bottom-left-radius: 5px" <input id="urlInput" style="border-top-left-radius: 5px; border-bottom-left-radius: 5px"
type="text" placeholder="输入地址"> type="text" placeholder="输入地址">
</div> </div>
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" <button class="btn btn-outline-secondary" type="button"
@ -34,12 +34,13 @@
</button> </button>
</div> </div>
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-outline-secondary" style="border-top-right-radius: 5px; border-bottom-right-radius: 5px" id="switchRenderBtn" type="button" <button class="btn btn-outline-secondary"
style="border-top-right-radius: 5px; border-bottom-right-radius: 5px" id="switchRenderBtn"
type="button"
onclick="changeRender()">不加载界面 onclick="changeRender()">不加载界面
</button> </button>
</div> </div>
<span>&nbsp;&nbsp;&nbsp;</span> <span>&nbsp;&nbsp;&nbsp;</span>
<div class="input-group-append"> <div class="input-group-append">
<select id="selectContract" class="custom-select" <select id="selectContract" class="custom-select"
style="-webkit-appearance: none" onchange="changeApp()"> style="-webkit-appearance: none" onchange="changeApp()">
@ -71,12 +72,10 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<div class="row" style="padding-left: 16px; padding-right: 16px"> <div class="row" style="padding-left: 16px; padding-right: 16px">
<div class="input-group mb-3"> <div class="input-group mb-3">
<div class="input-group-prepend"> <div class="input-group-prepend">
<label class="input-group-text" for="localKeyList">本地公钥</label> <label class="input-group-text" for="localKeyList">本地公钥</label>
</div> </div>
<select class="custom-select" <select class="custom-select"
aria-label="Example select with button addon" aria-label="Example select with button addon"
style="appearance: none" v-model="selectedSM2Key" style="appearance: none" v-model="selectedSM2Key"
@ -98,7 +97,6 @@
</div> </div>
</div> </div>
<div class="row" style="padding-left: 16px; padding-right: 16px"> <div class="row" style="padding-left: 16px; padding-right: 16px">
<div class="input-group mb-1"> <div class="input-group mb-1">
<div class="input-group-prepend"> <div class="input-group-prepend">
<label class="input-group-text" for="localKeyList">当前SM2密钥</label> <label class="input-group-text" for="localKeyList">当前SM2密钥</label>
@ -124,7 +122,6 @@
style="width: 100%; min-height: 150px" rows="" cols=""> style="width: 100%; min-height: 150px" rows="" cols="">
</textarea> </textarea>
<div class="row" style="padding-left: 16px; padding-right: 16px;"> <div class="row" style="padding-left: 16px; padding-right: 16px;">
</div> </div>
<div class="row" style="padding-left: 16px; padding-right: 16px"> <div class="row" style="padding-left: 16px; padding-right: 16px">
<span>我的权限:{{myRole}}</span> <span>我的权限:{{myRole}}</span>
@ -143,16 +140,10 @@
</button> </button>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div class="modal fade" id="dialog" tabindex="-1" role="dialog" <div class="modal fade" id="dialog" tabindex="-1" role="dialog"
aria-labelledby="exampleModalLabel" aria-hidden="true"> aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
@ -176,16 +167,12 @@
</div> </div>
</div> </div>
</div> </div>
<script> <script>
var h = document.body.clientHeight const h = document.body.clientHeight
- $("header").css("height").replace("px", "") - $("header").css("height").replace("px", "")
$("#main").css("height", h + "px");
$("#main").css("height", h + "px"); init();
init();
//initWSocket();
</script> </script>
</body> </body>

View File

@ -243,6 +243,11 @@ function onExecuteResult(obj) {
onExecuteResultInternal(obj); onExecuteResultInternal(obj);
} }
function onExecuteContractTrustfully(obj) {
console.log(obj);
$("#consistencyResult").html(global.executeResult.size + "个结果一致");
}
function showContractResult(requestID) { function showContractResult(requestID) {
var data = global.responseCollector[requestID][0]; var data = global.responseCollector[requestID][0];
result = data; result = data;
@ -601,7 +606,7 @@ function renewList() {
for (let i = 0; i < global.contracts.length; ++i) { for (let i = 0; i < global.contracts.length; ++i) {
const c = global.contracts[i]; const c = global.contracts[i];
console.log(c); console.log(c);
if (c.contractStatus=="KILLED") continue; if (c.contractStatus == "KILLED") continue;
if (!c.id) { if (!c.id) {
c.id = c.contractID; c.id = c.contractID;
} }

View File

@ -1,243 +1,241 @@
var createWssocket = function(wsurl, onopen, handler) { var createWssocket = function (wsurl, onopen, handler) {
console.log("[createWS.js] createWssocket : " + wsurl); console.log("[createWS.js] createWssocket : " + wsurl);
var retsocket = {}; var retsocket = {};
retsocket.handlerList = []; retsocket.handlerList = [];
if (handler != undefined) if (handler)
retsocket.handlerList.push(handler); retsocket.handlerList.push(handler);
var wssocket = new WebSocket(wsurl); var wssocket = new WebSocket(wsurl);
wssocket.onerror = function(error) { wssocket.onerror = function (error) {
console.log(error); console.log(error);
}; };
wssocket.onopen = onopen; wssocket.onopen = onopen;
var onmessage = function(event) { var onmessage = function (event) {
var obj = JSON.parse(event.data); var obj = JSON.parse(event.data);
switch (obj.action) { switch (obj.action) {
case 'sendNextSegment': case 'sendNextSegment':
retsocket.sendNextSegment(); retsocket.sendNextSegment();
break; break;
case 'sendSeg': case 'sendSeg':
retsocket.receiveSeg(obj); retsocket.receiveSeg(obj);
break; break;
default: default:
for (var i = 0; i < retsocket.handlerList.length; i++) { for (var i = 0; i < retsocket.handlerList.length; i++) {
var h = retsocket.handlerList[i]; var h = retsocket.handlerList[i];
h(event, wssocket); h(event, wssocket);
} }
} }
}; };
var reconnect = function(error) { var reconnect = function (error) {
setTimeout(function() { setTimeout(function () {
console.log("[createWS.js] try to reconnect"); console.log("[createWS.js] try to reconnect");
wssocket = new WebSocket(wsurl); wssocket = new WebSocket(wsurl);
wssocket.onclose = reconnect; wssocket.onclose = reconnect;
wssocket.onmessage = onmessage; wssocket.onmessage = onmessage;
wssocket.onopen = onopen; wssocket.onopen = onopen;
}, 1000); }, 1000);
}; };
wssocket.onclose = reconnect; wssocket.onclose = reconnect;
retsocket.receiveSeg = function(obj) { retsocket.receiveSeg = function (obj) {
if (obj.cid == 'start') { if (obj.cid === 'start') {
retsocket.toReceive = ""; retsocket.toReceive = "";
} }
retsocket.toReceive += obj.data; retsocket.toReceive += obj.data;
if (obj.cid == 'done') { if (obj.cid === 'done') {
console.log("[receiveSeg] Received AllData:" + retsocket.toReceive); console.log("[receiveSeg] Received AllData:" + retsocket.toReceive);
var event = {}; var event = {};
event.data = retsocket.toReceive; event.data = retsocket.toReceive;
retsocket.toReceive = ""; retsocket.toReceive = "";
handler(event); handler(event);
} }
}; };
wssocket.onmessage = onmessage; wssocket.onmessage = onmessage;
retsocket.isSending = false; retsocket.isSending = false;
retsocket.sendList = []; retsocket.sendList = [];
retsocket.monitor = function() { retsocket.monitor = function () {
if (!retsocket.isSending) { if (!retsocket.isSending) {
if (retsocket.sendList.length > 0) { if (retsocket.sendList.length > 0) {
retsocket.send(retsocket.sendList.pop()); retsocket.send(retsocket.sendList.pop());
} }
} }
setTimeout(retsocket.monitor, 1000); setTimeout(retsocket.monitor, 1000);
}; };
// TODO: we don't need monitor at all? // TODO: we don't need monitor at all?
retsocket.monitor(); retsocket.monitor();
retsocket.send = function(str) { retsocket.send = function (str) {
if (retsocket.isSending) { if (retsocket.isSending) {
retsocket.sendList.push(str); retsocket.sendList.push(str);
return; return;
} }
if (str.length > 1024) { if (str.length > 1024) {
retsocket.isSending = true; retsocket.isSending = true;
retsocket.toSend = str.substr(1024); retsocket.toSend = str.substr(1024);
var obj = {}; var obj = {};
obj.isSegment = true; obj.isSegment = true;
obj.data = str.substr(0, 1024); obj.data = str.substr(0, 1024);
wssocket.send(JSON.stringify(obj)); wssocket.send(JSON.stringify(obj));
} else } else
wssocket.send(str); wssocket.send(str);
}; };
retsocket.sendNextSegment = function() { retsocket.sendNextSegment = function () {
var str = retsocket.toSend; var str = retsocket.toSend;
if (str.length > 1024) { const obj = {};
retsocket.toSend = str.substr(1024); if (str.length > 1024) {
var obj = {}; retsocket.toSend = str.substr(1024);
obj.isSegment = true; obj.isSegment = true;
obj.data = str.substr(0, 1024); obj.data = str.substr(0, 1024);
wssocket.send(JSON.stringify(obj)); wssocket.send(JSON.stringify(obj));
} else { } else {
retsocket.toSend = ""; retsocket.toSend = "";
var obj = {}; obj.isSegment = false;
obj.isSegment = false; obj.data = str;
obj.data = str; wssocket.send(JSON.stringify(obj));
wssocket.send(JSON.stringify(obj)); retsocket.isSending = false;
retsocket.isSending = false; if (retsocket.sendList.length > 0) {
if (retsocket.sendList.length > 0) { retsocket.send(retsocket.sendList.pop());
retsocket.send(retsocket.sendList.pop()); }
} }
} };
}; retsocket.isOpen = function () {
retsocket.isOpen = function() { return wssocket.readyState;
return wssocket.readyState; }
} retsocket.addHandler = function (handler) {
retsocket.addHandler = function(handler) { retsocket.handlerList.push(handler);
retsocket.handlerList.push(handler); }
} return retsocket;
return retsocket;
}; };
var aesDecrypt = function(data) { var aesDecrypt = function (data) {
data = cryptico.b64to256(data); data = cryptico.b64to256(data);
var encryptedBlocks = cryptico.string2bytes(data); var encryptedBlocks = cryptico.string2bytes(data);
var exkey = global.aesKey.slice(0); var exkey = global.aesKey.slice(0);
aes.ExpandKey(exkey); aes.ExpandKey(exkey);
aes.Decrypt(encryptedBlocks, exkey); aes.Decrypt(encryptedBlocks, exkey);
return cryptico.bytes2string(encryptedBlocks); return cryptico.bytes2string(encryptedBlocks);
}; };
var aesEncrypt = function(data, aesKey) { var aesEncrypt = function (data, aesKey) {
var key = aesKey; var exkey = aesKey.slice(0);
var exkey = key.slice(0); aes.ExpandKey(exkey);
aes.ExpandKey(exkey); var blocks = my.string2bytes(data);
var blocks = my.string2bytes(data); blocks = my.pad16(blocks);
blocks = my.pad16(blocks); aes.Encrypt(blocks, exkey);
aes.Encrypt(blocks, exkey); ciphertext = cryptico.bytes2string(blocks);
ciphertext = cryptico.bytes2string(blocks); ciphertext = cryptico.b256to64(ciphertext);
ciphertext = cryptico.b256to64(ciphertext); return ciphertext;
return ciphertext;
}; };
var rsaEncrypt = function(data, rsaKey) { var rsaEncrypt = function (data, rsaKey) {
var rsa = new RSAKey(); var rsa = new RSAKey();
rsa.setPublic(rsaKey.n, rsaKey.e1); rsa.setPublic(rsaKey.n, rsaKey.e1);
var result = rsa.encrypt(data); return rsa.encrypt(data);
return result;
}; };
var loadRSAKey = function(rsaKey) { var loadRSAKey = function (rsaKey) {
var str = cryptico.b64to256(rsaKey); var str = cryptico.b64to256(rsaKey);
str = str.split(","); str = str.split(",");
var ret = {}; var ret = {};
ret.n = str[0]; ret.n = str[0];
ret.e1 = str[1]; ret.e1 = str[1];
ret.e2 = str[2]; ret.e2 = str[2];
return ret; return ret;
}; };
var testRSA = function() { var testRSA = function () {
pubKey = loadRSAKey(global.privKey); pubKey = loadRSAKey(global.privKey);
reqContent = {}; reqContent = {};
reqContent.action = "main"; reqContent.action = "main";
reqContent.arg = "[{\"score\":20},{\"score\":20}]"; reqContent.arg = "[{\"score\":20},{\"score\":20}]";
reqContent.contractID = "abc"; reqContent.contractID = "abc";
eReq = encryptReq(reqContent, pubKey); eReq = encryptReq(reqContent, pubKey);
url = "http://localhost:8080/SCIDE/SCManager?action=executeContractEncrypted&contractRequest=" url = "http://localhost:8080/SCIDE/SCManager?action=executeContractEncrypted&contractRequest="
+ encodeURIComponent(JSON.stringify(eReq)); + encodeURIComponent(JSON.stringify(eReq));
}; };
var encryptReq = function(reqContent, pubKey) { var encryptReq = function (reqContent, pubKey) {
// global.pubKey = loadRSAKey(global.privKey); // global.pubKey = loadRSAKey(global.privKey);
var aes = {}; var aes = {};
aes.key = cryptico.generateAESKey(); aes.key = cryptico.generateAESKey();
var aesObj = JSON.stringify(aes); var aesObj = JSON.stringify(aes);
var rsa = new RSAKey(); var rsa = new RSAKey();
rsa.setPrivate(pubKey.n, pubKey.e1, pubKey.e2); rsa.setPrivate(pubKey.n, pubKey.e1, pubKey.e2);
var encrypedReq = {}; var encrypedReq = {};
encrypedReq.action = rsa.decrypt(aesObj); encrypedReq.action = rsa.decrypt(aesObj);
encrypedReq.contractID = reqContent.contractID; encrypedReq.contractID = reqContent.contractID;
reqContent.contractID = undefined; reqContent.contractID = undefined;
encrypedReq.arg = JSON.stringify(reqContent); encrypedReq.arg = JSON.stringify(reqContent);
encrypedReq.arg = aesEncrypt(encrypedReq.arg, aes.key); encrypedReq.arg = aesEncrypt(encrypedReq.arg, aes.key);
encrypedReq.requester = pubKey.n + "," + pubKey.e1 + "," + "0"; encrypedReq.requester = pubKey.n + "," + pubKey.e1 + "," + "0";
encrypedReq.requester = cryptico.b256to64(encrypedReq.requester); encrypedReq.requester = cryptico.b256to64(encrypedReq.requester);
return encrypedReq; return encrypedReq;
}; };
var onExecuteResultInternal = function(data) { var onExecuteResultInternal = function (data) {
console.log(data); console.log(data);
global.executeResult = data; global.executeResult = data;
var reqId = data.responseID; var reqId = data.responseID;
var callback = global.cbs[data.responseID]; var callback = global.cbs[data.responseID];
if (callback != undefined) { if (callback) {
callback(global.executeResult, data); callback(global.executeResult, data);
} }
}; };
window.executeContract = function(contractID, method, strarg, cb) { window.executeContract = function (contractID, method, strarg, cb) {
var sm2Key = global.sm2Key; var sm2Key = global.sm2Key;
var request = {}; var request = {};
request.action = "executeContract"; request.action = "executeContract";
request.requestID = new Date().getTime() + "_" request.requestID = new Date().getTime() + "_"
+ Math.floor(Math.random() * 10000); + Math.floor(Math.random() * 10000);
global.cbs[request.requestID] = cb; global.cbs[request.requestID] = cb;
request.contractID = contractID; request.contractID = contractID;
request.operation=method; request.operation = method;
request.arg = strarg; request.arg = strarg;
if (sm2Key != undefined) { if (sm2Key) {
request.pubkey = sm2Key.publicKey; request.pubkey = sm2Key.publicKey;
request.signature = sm2.doSignature(request.contractID + "|" request.signature = sm2.doSignature(request.contractID + "|"
+ method + "|" + strarg + "|" + sm2Key.publicKey, + method + "|" + strarg + "|" + sm2Key.publicKey,
sm2Key.privateKey,{hash:true,der:true}); sm2Key.privateKey, {hash: true, der: true});
} }
global.wssocket.send(JSON.stringify(request)); global.wssocket.send(JSON.stringify(request));
}; };
window.executeCurrentContract = function(method, strarg, cb) { window.executeCurrentContract = function (method, strarg, cb) {
console.log("executeCurrentContract"); console.log("executeCurrentContract");
var contract = global.currentContract; var contract = global.currentContract;
executeContract(contract.id, method, strarg, cb); executeContract(contract.id, method, strarg, cb);
}; };
window.loadBDWareWebSDK = function(url, onopen, handler) { window.loadBDWareWebSDK = function (url, onopen, handler) {
if (window.global == undefined) { if (!window.global) {
window.global = {}; window.global = {};
} }
if (global.sm2Key == undefined) { if (!global.sm2Key) {
if (sm2 != undefined) if (sm2)
global.sm2Key = sm2.generateKeyPairHex(); global.sm2Key = sm2.generateKeyPairHex();
} }
if (global.cbs == undefined) { if (!global.cbs) {
global.cbs = {}; global.cbs = {};
} }
if (global.wssocket == undefined) { if (!global.wssocket) {
global.wssocket = createWssocket(url, onopen, function(event, wss) { global.wssocket = createWssocket(url, onopen, function (event, wss) {
var data = event.data; var data = event.data;
var obj = JSON.parse(data); var obj = JSON.parse(data);
switch (obj.action) { switch (obj.action) {
case 'onExecuteResult': case 'onExecuteResult':
onExecuteResultInternal(obj); onExecuteResultInternal(obj);
default: default:
} break;
handler(event, global.wssocket); }
}); handler(event, global.wssocket);
} else { });
if (onopen != undefined) } else {
onopen(); if (onopen)
global.wssocket.addHandler(handler); onopen();
} global.wssocket.addHandler(handler);
}
}; };
// Usage: // Usage:
// Approach 1: // Approach 1: