auto prune

This commit is contained in:
CaiHQ 2023-12-15 16:41:27 +08:00
parent 22532666dd
commit 540a4232d0
9 changed files with 547 additions and 648 deletions

View File

@ -4,6 +4,7 @@ plugins {
}
mainClassName = 'org.bdware.server.CMHttpServer'
apply from: '../spotless.gradle'
application {

View File

@ -57,8 +57,7 @@ public class CMHttpServer {
private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class);
private static final String CONFIG_PATH = "cmconfig.json";
public static EventLoopGroup workerGroup = new NioEventLoopGroup();
public static MultiIndexTimeRocksDBUtil nodeLogDB =
new MultiIndexTimeRocksDBUtil(
public static MultiIndexTimeRocksDBUtil nodeLogDB = new MultiIndexTimeRocksDBUtil(
"./ContractManagerDB", CMTables.LocalNodeLogDB.toString());
public static URLClassLoader pluginLoader;
private static SslContext sslContext = null;
@ -89,23 +88,21 @@ public class CMHttpServer {
GlobalConf.initDOAConfig(doaConf);
if (cmdConf.withBdledgerServer) {
ContractManager.threadPool.execute(
() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4));
ContractManager.threadPool
.execute(() -> NetworkManager.instance.initP2P(cmdConf.servicePort + 4));
}
// 可自动运行bdledger可执行文件也可在shell脚步中运行和停止
if (!cmdConf.withBdledgerClient.isEmpty()) {
ContractManager.scheduledThreadPool.schedule(
() -> {
ContractManager.scheduledThreadPool.schedule(() -> {
File ledgerClient = new File(cmdConf.withBdledgerClient);
LOGGER.debug("canRead=" + ledgerClient.canRead() +
" path=" + ledgerClient.getAbsolutePath());
LOGGER.debug("canRead=" + ledgerClient.canRead() + " path="
+ ledgerClient.getAbsolutePath());
try {
Runtime.getRuntime().exec(ledgerClient.getAbsolutePath());
} catch (IOException e) {
LOGGER.warn("start bdledger client failed: " + e.getMessage());
}
},
1, TimeUnit.SECONDS);
}, 1, TimeUnit.SECONDS);
}
if (cmdConf.enableEventPersistence) {
ContractManager.eventPersistenceEnabled = true;
@ -115,13 +112,10 @@ public class CMHttpServer {
String[] filePaths = cmdConf.enableSsl.split(":");
File chainedFile = new File(filePaths[0]), keyFile = new File(filePaths[1]);
if (chainedFile.exists() && keyFile.exists()) {
sslContext =
SslContextBuilder.forServer(chainedFile, keyFile)
.ciphers(
null,
(ciphers, defaultCiphers, supportedCiphers) ->
defaultCiphers.stream()
.filter(x -> null != x && !x.contains("RC4"))
sslContext = SslContextBuilder.forServer(chainedFile, keyFile)
.ciphers(null,
(ciphers, defaultCiphers, supportedCiphers) -> defaultCiphers
.stream().filter(x -> null != x && !x.contains("RC4"))
.toArray(String[]::new))
.build();
LOGGER.info("openssl isAvailable:" + OpenSsl.isAvailable());
@ -143,7 +137,8 @@ public class CMHttpServer {
// plugins
CMHttpHandler.wsPluginActions = parseStrAsList(cmdConf.wsPluginActions);
TCPClientFrameHandler.clientToAgentPlugins = parseStrAsList(cmdConf.clientToAgentPlugins);
NodeCenterClientHandler.clientToClusterPlugins = parseStrAsList(cmdConf.clientToClusterPlugins);
NodeCenterClientHandler.clientToClusterPlugins =
parseStrAsList(cmdConf.clientToClusterPlugins);
org.bdware.units.tcp.TCPClientFrameHandler.tcpPlugins = parseStrAsList(cmdConf.tcpPlugins);
if (!StringUtil.isNullOrEmpty(cmdConf.debug)) {
try {
@ -246,19 +241,15 @@ public class CMHttpServer {
try {
BufferedReader br = new BufferedReader(new FileReader(keyFile));
String pubKey = br.readLine();
String nowManager =
KeyValueDBUtil.instance.getValue(
CMTables.ConfigDB.toString(), "__NodeManager__");
String nowManager = KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(),
"__NodeManager__");
// manager.key is used when node manager isn' set
if (null == nowManager || nowManager.isEmpty()) {
KeyValueDBUtil.instance.setValue(
CMTables.ConfigDB.toString(), "__NodeManager__", pubKey);
KeyValueDBUtil.instance.setValue(
CMTables.NodeRole.toString(), pubKey,
KeyValueDBUtil.instance.setValue(CMTables.ConfigDB.toString(),
"__NodeManager__", pubKey);
KeyValueDBUtil.instance.setValue(CMTables.NodeRole.toString(), pubKey,
"NodeManager,ContractProvider,ContractUser,ContractInstanceManager");
KeyValueDBUtil.instance.setValue(
CMTables.NodeTime.toString(),
pubKey,
KeyValueDBUtil.instance.setValue(CMTables.NodeTime.toString(), pubKey,
Long.toString(new Date().getTime()));
LOGGER.info("set node manager from manager.key");
}
@ -286,8 +277,8 @@ public class CMHttpServer {
// EpollServerSocketChannel
// ContractManager.reconnectPort = (port - 18000) * 30 + 1630;
// if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630;
File[] pluginJar = new File("./pluginLib/")
.listFiles(pathname -> pathname.getName().endsWith(".jar"));
File[] pluginJar =
new File("./pluginLib/").listFiles(pathname -> pathname.getName().endsWith(".jar"));
URL[] urls;
if (pluginJar != null && pluginJar.length > 0) {
urls = new URL[pluginJar.length];
@ -316,25 +307,17 @@ public class CMHttpServer {
try {
ServerBootstrap b1 = new ServerBootstrap();
b1.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b1.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.localAddress(port)
.childHandler(
new ChannelInitializer<SocketChannel>() {
b1.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.localAddress(port).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel arg0) {
if (sslContext != null) {
arg0.pipeline().addLast(new OptionalSslHandler(sslContext));
}
arg0.pipeline()
.addLast(trafficSharp)
.addLast(new HttpServerCodec())
arg0.pipeline().addLast(trafficSharp).addLast(new HttpServerCodec())
.addLast(new HttpObjectAggregator(10 * 1024 * 1024))
.addLast(
new WebSocketServerProtocolHandler(
PATH, null, true))
.addLast(new ChunkedWriteHandler())
.addLast(serverHandler)
.addLast(new WebSocketServerProtocolHandler(PATH, null, true))
.addLast(new ChunkedWriteHandler()).addLast(serverHandler)
.addLast(new ContractManagerFrameHandler());
}
});
@ -358,8 +341,7 @@ public class CMHttpServer {
private void loadStartContractConfiguration() {
if (cmdConf.startContract != null && cmdConf.startContract.size() > 0) {
ContractManager.scheduledThreadPool.schedule(
() -> {
ContractManager.scheduledThreadPool.schedule(() -> {
for (JsonElement je : cmdConf.startContract) {
try {
JsonObject jo = je.getAsJsonObject();
@ -368,7 +350,8 @@ public class CMHttpServer {
if (!jo.has("owner"))
jo.addProperty("owner", UserManagerAction.getNodeManager());
if (jo.has("killBeforeStart")) {
ContractManager.instance.stopContract(jo.get("killBeforeStart").getAsString());
ContractManager.instance
.stopContract(jo.get("killBeforeStart").getAsString());
}
if (jo.get("path").getAsString().startsWith("@")) {
jo.addProperty("bcoId", jo.get("path").getAsString().substring(1));
@ -380,15 +363,13 @@ public class CMHttpServer {
e.printStackTrace();
}
}
},
10, TimeUnit.SECONDS);
}, 10, TimeUnit.SECONDS);
}
}
@Sharable
public static class NettyConnectServerHandler extends ChannelInboundHandlerAdapter {
public NettyConnectServerHandler(AtomicInteger connectNum) {
}
public NettyConnectServerHandler(AtomicInteger connectNum) {}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

View File

@ -17,7 +17,6 @@ import org.zz.gmhelper.SM2KeyPair;
import org.zz.gmhelper.SM2Util;
import java.io.File;
import java.net.URL;
import java.util.logging.Level;
public class GlobalConf {
@ -64,14 +63,11 @@ public class GlobalConf {
private static GlobalConf init() {
java.util.logging.Logger.getLogger(org.bdware.bdledger.api.grpc.Client.class.getName())
.setLevel(Level.OFF);
Configurator.setLevel(
"io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder",
org.apache.logging.log4j.Level.OFF);
Configurator.setLevel(
"io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder",
org.apache.logging.log4j.Level.OFF);
Configurator.setLevel(
"io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
Configurator.setLevel("io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker",
org.apache.logging.log4j.Level.OFF);
KeyValueDBUtil.setupCM();
@ -83,19 +79,17 @@ public class GlobalConf {
String dbName = CMTables.ConfigDB.toString();
if (!KeyValueDBUtil.instance.getKeys(dbName).contains("hasInited")) {
KeyValueDBUtil.instance.setValue(dbName, "hasInited", "true");
KeyValueDBUtil.instance.setValue(
dbName, "projectDir", new File("./BDWareProjectDir/").getAbsolutePath());
KeyValueDBUtil.instance.setValue(
dbName,
"ADSPDir",
KeyValueDBUtil.instance.setValue(dbName, "projectDir",
new File("./BDWareProjectDir/").getAbsolutePath());
KeyValueDBUtil.instance.setValue(dbName, "ADSPDir",
new File("./BDWareProjectDir/ADSPDir/").getAbsolutePath());
File f = new File("./yjs.jar");
if (f.exists()) {
KeyValueDBUtil.instance.setValue(
dbName, "yjsPath", new File("./yjs.jar").getAbsolutePath());
KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
new File("./yjs.jar").getAbsolutePath());
} else {
KeyValueDBUtil.instance.setValue(
dbName, "yjsPath", new File("./cp/yjs.jar").getAbsolutePath());
KeyValueDBUtil.instance.setValue(dbName, "yjsPath",
new File("./cp/yjs.jar").getAbsolutePath());
}
conf.keyPairStr = SM2Util.generateSM2KeyPair().toJson();
@ -111,36 +105,23 @@ public class GlobalConf {
// long time = System.currentTimeMillis()+15811200000;
String licence =
ByteHexUtil.encode(
SM2Util.encrypt(
conf.keyPair.getPublicKey(),
(HardwareInfo.getCPUID()
+ "=="
+ (System.currentTimeMillis()
+ 15811200000L))
ByteHexUtil
.encode(SM2Util.encrypt(conf.keyPair.getPublicKey(),
(HardwareInfo.getCPUID() + "=="
+ (System.currentTimeMillis() + 15811200000L))
.getBytes()));
LOGGER.debug("licence:" + licence);
LOGGER.debug(
String.format(
"deviceName %s license %s %s==%d",
deviceName,
licence,
HardwareInfo.getCPUID(),
(System.currentTimeMillis() + 15811200000L)));
LOGGER.debug(String.format("deviceName %s license %s %s==%d", deviceName, licence,
HardwareInfo.getCPUID(), (System.currentTimeMillis() + 15811200000L)));
KeyValueDBUtil.instance.setValue(dbName, "licence", licence);
KeyValueDBUtil.instance.setValue(dbName, "nodeName", deviceName.substring(0, 10));
KeyValueDBUtil.instance.setValue(dbName, "masterAddress", "null");
KeyValueDBUtil.instance.setValue(dbName, "ipPort", "null");
KeyValueDBUtil.instance.setValue(dbName, "nodeCenter", "ws://127.0.0.1:18005");
KeyValueDBUtil.instance.setValue(
dbName,
"datachainConf",
"39.104.70.160:18091\n"
+ "47.98.247.70:18091\n"
+ "47.98.248.208:18091\n"
+ "39.104.77.165:18091\n"
+ "47.98.249.131:18091");
KeyValueDBUtil.instance.setValue(dbName, "datachainConf",
"39.104.70.160:18091\n" + "47.98.247.70:18091\n" + "47.98.248.208:18091\n"
+ "39.104.77.165:18091\n" + "47.98.249.131:18091");
}
conf.projectDir = KeyValueDBUtil.instance.getValue(dbName, "projectDir");
@ -211,8 +192,7 @@ public class GlobalConf {
LOGGER.debug("[GlobalConf] ProjectDir Path:" + new File(conf.projectDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.publicDir).getAbsolutePath());
LOGGER.debug("[GlobalConf] publicDir Path:" + new File(conf.ADSPDir).getAbsolutePath());
LOGGER.debug(
"[GlobalConf] publicDirCompiled Path:"
LOGGER.debug("[GlobalConf] publicDirCompiled Path:"
+ new File(conf.publicCompiledDir).getAbsolutePath());
LOGGER.debug(
"[GlobalConf] PersonalDir Path:" + new File(conf.privateDir).getAbsolutePath());
@ -251,7 +231,8 @@ public class GlobalConf {
private static void verifyLicence(GlobalConf conf) {
try {
// String pubkey =
// "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi" +
// "OTIzNmUzMGNmOGI1ZjFkMDBjZjEyMWY4OThmM2ZmYTIwNjE2ODYxOWNiMDNhMTVlM2FiZTA0OThhNTlkZDg1MmRi"
// +
// "MjA5Njc1NmM3ZDBhOWM3YTNkOTg2NWVlYzk2YzM1MmY0MDdkMGMyOTA4M2NkNDI4YmY1YjM5M2U5OTA1" +
// "NWE0MzM0MTJhM2Y2ZDhkZWVmZDk4MmI4NmZiZTMyYjhlMGE3ZWFmZmE5ODM3M2E4ZTRmNTYyNDgxNTY0" +
// "Yjk2ZjFkMTZiODk2MGRhZDAwMTNjZDYwOGZmOTcxNjdiOWI1MDU1MjJlMzk0ODhmODczNDJjNWUwOGRj" +
@ -267,10 +248,7 @@ public class GlobalConf {
try {
// byte[] arr = key.encode(new BASE64Decoder().decodeBuffer(conf.licence));
// String actualKey = new String(arr);
String arr =
new String(
SM2Util.decrypt(
conf.keyPair.getPrivateKeyParameter(),
String arr = new String(SM2Util.decrypt(conf.keyPair.getPrivateKeyParameter(),
ByteHexUtil.decode(conf.licence)));
LOGGER.debug("[GlobalConf] actualKey:" + arr);
@ -326,7 +304,8 @@ public class GlobalConf {
}
// public static void initMasterAddress(String val) {
// if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1")) {
// if ("null".equals(instance.masterAddress) || instance.masterAddress.startsWith("127.0.0.1"))
// {
// resetMasterAddress(val);
// }
// }

View File

@ -59,8 +59,7 @@ public class FileActions {
ContractManagerFrameHandler handler;
static {
clearUploadFailed =
new TimerTask() {
clearUploadFailed = new TimerTask() {
@Override
public void run(Timeout arg0) {
clearUploadFailed();
@ -85,10 +84,7 @@ public class FileActions {
String fileName = compileInternal(parentPath, projectName, isPrivate, true);
File dir;
if (isPrivate) {
dir =
new File(
parentPath.replace(
GlobalConf.instance.privateDir,
dir = new File(parentPath.replace(GlobalConf.instance.privateDir,
GlobalConf.instance.privateCompiledDir));
} else {
dir = new File(GlobalConf.instance.publicCompiledDir);
@ -107,9 +103,7 @@ public class FileActions {
return ret != null && ret.equals("locked");
}
@URIPath(
method = org.bdware.server.http.HttpMethod.POST,
value = {"/SCIDE/Upload", "/Upload"})
@URIPath(method = org.bdware.server.http.HttpMethod.POST, value = {"/SCIDE/Upload", "/Upload"})
public static void handleUploadRequest(ChannelHandlerContext ctx, FullHttpRequest request) {
// logger.info("[CMHttpHandler] handleUploadRequest : ");
// Upload method is POST
@ -124,17 +118,13 @@ public class FileActions {
}
}
if (!transformedParam.containsKey("path")
|| !transformedParam.containsKey("fileName")
if (!transformedParam.containsKey("path") || !transformedParam.containsKey("fileName")
|| !transformedParam.containsKey("isPrivate")
|| !transformedParam.containsKey("order")
|| !transformedParam.containsKey("count")
|| !transformedParam.containsKey("order") || !transformedParam.containsKey("count")
|| !transformedParam.containsKey("pubKey")
|| !transformedParam.containsKey("sign")) {
DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse(
request.protocolVersion(),
OK,
DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
request.protocolVersion(), OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"Missing argument, please check: path, fileName, isPrivate, order, count, pubKey, sign!\"}"
.getBytes()));
@ -147,9 +137,7 @@ public class FileActions {
// 验签
// HttpMethod method = request.method();
String uri =
URLDecoder.decode(request.uri())
.split("\\?")[1]; // http请求中规定签名必须是最后一个且公钥名必须为pubKey否则验签失败
String uri = URLDecoder.decode(request.uri()).split("\\?")[1]; // http请求中规定签名必须是最后一个且公钥名必须为pubKey否则验签失败
int index = uri.lastIndexOf('&');
String str = uri.substring(0, index);
// logger.info("uri=" + uri);
@ -183,9 +171,8 @@ public class FileActions {
Method mm;
Action a = null;
try {
mm =
FileActions.class.getDeclaredMethod(
"uploadFile", JsonObject.class, ResultCallback.class);
mm = FileActions.class.getDeclaredMethod("uploadFile", JsonObject.class,
ResultCallback.class);
a = mm.getAnnotation(Action.class);
} catch (SecurityException | NoSuchMethodException e1) {
// TODO Auto-generated catch block
@ -215,28 +202,17 @@ public class FileActions {
status = "accept";
}
TimeDBUtil.instance.put(
CMTables.LocalNodeLogDB.toString(),
"{\"action\":\""
+ action
+ "\",\"pubKey\":\""
+ transformedParam.get("pubKey")
+ "\",\"status\":\""
+ status
+ "\",\"date\":"
+ System.currentTimeMillis()
TimeDBUtil.instance.put(CMTables.LocalNodeLogDB.toString(),
"{\"action\":\"" + action + "\",\"pubKey\":\"" + transformedParam.get("pubKey")
+ "\",\"status\":\"" + status + "\",\"date\":" + System.currentTimeMillis()
+ "}");
// logger.info("[CMHttpHandler] flag = " + flag + " flag2 = " + flag2);
if (!flag || !flag2) {
DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse(
request.protocolVersion(),
OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"Permission denied!\"}"
.getBytes()));
DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
request.protocolVersion(), OK, Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"Permission denied!\"}".getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE);
@ -256,9 +232,7 @@ public class FileActions {
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(contractID);
if (meta == null) {
DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse(
request.protocolVersion(),
OK,
new DefaultFullHttpResponse(request.protocolVersion(), OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"no such contract!\"}"
.getBytes()));
@ -267,23 +241,19 @@ public class FileActions {
return;
}
if (!meta.contract.getOwner().equals(pubkey)) {
DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse(
request.protocolVersion(),
OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"not owner!\"}"
.getBytes()));
DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
request.protocolVersion(), OK, Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"not owner!\"}".getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE);
return;
}
dir = new File(GlobalConf.instance.getDBPath(), meta.getName());
dir = new File(dir, dirName);
if (!dir.exists()) dir.mkdirs();
if (!dir.exists())
dir.mkdirs();
} else {
boolean isPrivate =
transformedParam.containsKey("isPrivate")
boolean isPrivate = transformedParam.containsKey("isPrivate")
&& Boolean.parseBoolean(transformedParam.get("isPrivate"));
// TODO verify signature and
if (isPrivate) {
@ -298,13 +268,9 @@ public class FileActions {
}
File target = new File(dir, fileName);
if (fileName.contains("..")) {
DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse(
request.protocolVersion(),
OK,
Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"FileName illegal!\"}"
.getBytes()));
DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
request.protocolVersion(), OK, Unpooled.wrappedBuffer(
"{\"status\":\"false\",\"data\":\"FileName illegal!\"}".getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE);
@ -318,7 +284,8 @@ public class FileActions {
if (order == 0) {
try {
LOGGER.debug("Path:" + target.getAbsolutePath());
if (!target.getParentFile().exists()) target.getParentFile().mkdirs();
if (!target.getParentFile().exists())
target.getParentFile().mkdirs();
fout = new FileOutputStream(target, false);
fileMap.put(target.getAbsolutePath(), fout);
} catch (FileNotFoundException e) {
@ -340,8 +307,7 @@ public class FileActions {
}
fileMap.remove(target.getAbsolutePath());
updateTime.remove(target.getAbsolutePath());
boolean isDebug =
transformedParam.containsKey("isDebug")
boolean isDebug = transformedParam.containsKey("isDebug")
&& Boolean.parseBoolean(transformedParam.get("isDebug"));
String doi = null;
String path = target.getAbsolutePath();
@ -359,14 +325,14 @@ public class FileActions {
retStr = retStr.replaceFirst("null", doi);
}
}
DefaultFullHttpResponse fullResponse =
new DefaultFullHttpResponse(
DefaultFullHttpResponse fullResponse = new DefaultFullHttpResponse(
request.protocolVersion(), OK, Unpooled.wrappedBuffer(retStr.getBytes()));
ChannelFuture f = addCrossOriginHeaderAndWrite(ctx, fullResponse);
f.addListener(ChannelFutureListener.CLOSE);
}
private static ChannelFuture addCrossOriginHeaderAndWrite(ChannelHandlerContext ctx, DefaultFullHttpResponse fullResponse) {
private static ChannelFuture addCrossOriginHeaderAndWrite(ChannelHandlerContext ctx,
DefaultFullHttpResponse fullResponse) {
fullResponse.headers().add("Access-Control-Allow-Origin", "*");
fullResponse.headers().add("Access-Control-Allow-Methods", "*");
return ctx.write(fullResponse);
@ -385,14 +351,11 @@ public class FileActions {
private static String unzipIfYpk(File target, File dir, boolean isDebug) {
// [FileAction] unzipIfYpk,
// target:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown.ypk targetDir:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown
LOGGER.info(
"[FileAction] unzipIfYpk, date:"
+ new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date())
+ " -> target:"
+ target.getAbsolutePath()
+ " dir:"
+ dir.getAbsolutePath());
// target:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown.ypk
// targetDir:/data/bdwaas/bdcontract/./BDWareProjectDir/private/045eeda3a001faad9d636ab1e973599ea87338a9576756eb10ceeca6083c1f76aac5cd201eab21c41342eb8aac40e9b283f0b6eae019644cdcc0a9f9aeb73de8fc/ContractUNknown
LOGGER.info("[FileAction] unzipIfYpk, date:"
+ new SimpleDateFormat("MM-dd hh:mm:ss").format(new Date()) + " -> target:"
+ target.getAbsolutePath() + " dir:" + dir.getAbsolutePath());
if (target.getParentFile().equals(dir)) {
if (target.getName().endsWith(".ypk")) {
String dirName = target.getName().substring(0, target.getName().length() - 4);
@ -406,11 +369,8 @@ public class FileActions {
}
}
targetDir.mkdirs();
LOGGER.info(
"[FileAction] unzipIfYpk, target:"
+ target.getAbsolutePath()
+ " targetDir:"
+ targetDir.getAbsolutePath());
LOGGER.info("[FileAction] unzipIfYpk, target:" + target.getAbsolutePath()
+ " targetDir:" + targetDir.getAbsolutePath());
YJSPacker.unpack(target.getAbsolutePath(), targetDir.getAbsolutePath());
final File tempDir = targetDir;
ContractManager.threadPool.execute(() -> startIfManifestDetected(tempDir, isDebug));
@ -428,8 +388,7 @@ public class FileActions {
String doi = "86.5000.470/do." + geneRandomID();
jo.addProperty("doi", doi);
}
ContractManager.threadPool.execute(
() -> {
ContractManager.threadPool.execute(() -> {
try {
GRPCPool.instance.reRegister(jo.get("doi").getAsString());
} catch (Exception e) {
@ -460,9 +419,8 @@ public class FileActions {
LOGGER.info("startContractProcess:" + targetDir);
Contract c = new Contract();
c.setType(ContractExecType.Sole);
String ypkPath =
FileActions.autoCompile(
targetDir.getParentFile().getAbsolutePath(), contractName);
String ypkPath = FileActions
.autoCompile(targetDir.getParentFile().getAbsolutePath(), contractName);
c.setScript(ypkPath);
c.setOwner(owner);
c.setDebug(isDebug);
@ -497,8 +455,8 @@ public class FileActions {
}
}
private static void writeChunk(
ChannelHandlerContext ctx, HttpPostRequestDecoder httpDecoder, File file) {
private static void writeChunk(ChannelHandlerContext ctx, HttpPostRequestDecoder httpDecoder,
File file) {
try {
if (!file.exists()) {
file.createNewFile();
@ -531,8 +489,8 @@ public class FileActions {
}
}
private static String compileInternal(
String parPath, String projectName, boolean isPrivate, boolean isAuto) {
private static String compileInternal(String parPath, String projectName, boolean isPrivate,
boolean isAuto) {
File project = new File(parPath, projectName);
try {
if (project.isDirectory()) {
@ -544,10 +502,7 @@ public class FileActions {
File dir;
if (isPrivate) {
dir =
new File(
parPath.replace(
GlobalConf.instance.privateDir,
dir = new File(parPath.replace(GlobalConf.instance.privateDir,
GlobalConf.instance.privateCompiledDir));
} else {
@ -555,13 +510,10 @@ public class FileActions {
}
tempZip = new File(dir, projectName + "_" + time + isAutoStr + ".ypk");
if (isAuto) {
File[] files =
dir.listFiles(
pathname ->
pathname.getName().startsWith(projectName + "_")
File[] files = dir
.listFiles(pathname -> pathname.getName().startsWith(projectName + "_")
&& pathname.getName().endsWith("_Auto.ypk"));
if (files != null
&& files.length == 1
if (files != null && files.length == 1
&& !changeSet.contains(project.getAbsolutePath())) {
return files[0].getName();
} else if (files != null) {
@ -586,8 +538,7 @@ public class FileActions {
Map<String, String> header = new HashMap<>();
header.put("accept", "*/*");
header.put("connection", "Keep-Alive");
header.put(
"user-agent",
header.put("user-agent",
" Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36)");
// header.put("Content-Type", "application/json");
return header;
@ -662,7 +613,8 @@ public class FileActions {
}
private static String toProjectName(String path) {
if (path.startsWith("/")) path = path.substring(1);
if (path.startsWith("/"))
path = path.substring(1);
return path.replaceAll("/.*$", "");
}
@ -781,9 +733,7 @@ public class FileActions {
int counter = 0;
while ((s = br.readLine()) != null) {
if (s.equals("{")
|| s.equals("}")
|| s.equals("")
if (s.equals("{") || s.equals("}") || s.equals("")
|| s.trim().startsWith("\"doi\":\"")
|| s.trim().startsWith("\"doipFlag\":\"")) {
continue;
@ -862,8 +812,8 @@ public class FileActions {
returnFileListResponse(resultCallback, response, dirs, f);
}
private void returnFileListResponse(
ResultCallback resultCallback, Response response, List<String> dirs, File f) {
private void returnFileListResponse(ResultCallback resultCallback, Response response,
List<String> dirs, File f) {
if (f.exists() && f.isDirectory()) {
for (File subFile : f.listFiles()) {
dirs.add(subFile.getName());
@ -905,7 +855,8 @@ public class FileActions {
String fileName = subFile.getName();
dirs.add(fileName);
}
} else dirs.add(project);
} else
dirs.add(project);
}
response.data = JsonUtil.toJson(dirs);
}
@ -1205,7 +1156,8 @@ public class FileActions {
// e.setData("{}".getBytes());
// contractDO.addElements(e);
// //
// DOAClient.getGlobalInstance().create(DOIPMainServer.repoIdentifier, contractDO);
// DOAClient.getGlobalInstance().create(DOIPMainServer.repoIdentifier,
// contractDO);
// DoipClient doipClient =
//
// DoipClient.createByRepoUrlAndMsgFmt(
@ -1225,52 +1177,34 @@ public class FileActions {
// }
if (args.has("projectDOI"))
manifestFout.write(
("{\n \"main\":\""
+ fileName
+ ".yjs\",\n"
+ " \"doipFlag\":true,\n"
+ " \"doi\":\""
+ args.get("projectDOI").getAsString()
+ "\",\n"
+ " \"authInfoPersistDOI\":\""
+ authInfoPersistDOI
+ "\"\n}")
.getBytes());
manifestFout.write(("{\n \"main\":\"" + fileName + ".yjs\",\n"
+ " \"doipFlag\":true,\n" + " \"doi\":\""
+ args.get("projectDOI").getAsString() + "\",\n"
+ " \"authInfoPersistDOI\":\"" + authInfoPersistDOI
+ "\"\n}").getBytes());
else
manifestFout.write(
("{\n \"main\":\""
+ fileName
+ ".yjs\",\n"
+ " \"authInfoPersistDOI\":\""
+ authInfoPersistDOI
+ "\"\n}")
.getBytes());
manifestFout.write(("{\n \"main\":\"" + fileName + ".yjs\",\n"
+ " \"authInfoPersistDOI\":\"" + authInfoPersistDOI
+ "\"\n}").getBytes());
YJSPacker.unpack(
new File("./WebContent/ProjectTemplate/naiveDAC.zip")
.getAbsolutePath(),
newFile.getPath() + "/naiveDAC");
mainyjsFout.write(
("import \"naiveDAC/naiveDAC.yjs\"\n\n").getBytes());
mainyjsFout.write(
("contract "
+ fileName
mainyjsFout
.write(("import \"naiveDAC/naiveDAC.yjs\"\n\n").getBytes());
mainyjsFout.write(("contract " + fileName
+ "{\n function onCreate(){\n initDAC(requester);\n }\n}")
.getBytes());
break;
default:
if (args.has("projectDOI"))
manifestFout.write(
("{\n \"main\":\""
+ fileName
manifestFout.write(("{\n \"main\":\"" + fileName
+ ".yjs\",\n \"doipFlag\":true,\n \"doi\":\""
+ args.get("projectDOI").getAsString()
+ "\"\n}")
+ args.get("projectDOI").getAsString() + "\"\n}")
.getBytes());
else
manifestFout.write(
("{\n \"main\":\"" + fileName + ".yjs\"\n}")
manifestFout.write(("{\n \"main\":\"" + fileName + ".yjs\"\n}")
.getBytes());
mainyjsFout.write(("contract " + fileName + "{\n\n}").getBytes());
}
@ -1333,10 +1267,7 @@ public class FileActions {
File f = new File(parPath + "/" + oldFile);
if (!oldFile.contains("..") && f.exists()) {
LOGGER.debug(
"[FileController] delete:"
+ f.getAbsolutePath()
+ " exists:"
LOGGER.debug("[FileController] delete:" + f.getAbsolutePath() + " exists:"
+ f.exists());
if (f.isDirectory()) {
deleteDir(f);
@ -1475,10 +1406,8 @@ public class FileActions {
// 文本文件
if (!path.contains("..") && isTextFile(path)) {
LOGGER.debug("[FileActions] 上传文本文件类型 : ");
BufferedWriter bw =
new BufferedWriter(
new FileWriter(
target.getAbsolutePath() + "/" + fileName, isAppend));
BufferedWriter bw = new BufferedWriter(
new FileWriter(target.getAbsolutePath() + "/" + fileName, isAppend));
bw.write(content);
bw.close();
} else { // 其他类型文件
@ -1515,7 +1444,8 @@ public class FileActions {
private String getPubkey(JsonObject args) {
try {
if (handler != null) return handler.getPubKey();
if (handler != null)
return handler.getPubKey();
return args.get("pubKey").getAsString();
} catch (Exception e) {
e.printStackTrace();
@ -1658,9 +1588,8 @@ public class FileActions {
if (null != ypkPath) {
try {
ret.put("ypk", new File(ypkPath).getName());
ret.put(
"permissions",
JsonUtil.parseStringAsJsonObject(CMActions.manager.parseYpkPermissions(ypkPath)));
ret.put("permissions", JsonUtil
.parseStringAsJsonObject(CMActions.manager.parseYpkPermissions(ypkPath)));
} catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));

View File

@ -39,7 +39,8 @@ import java.util.List;
import static org.bdware.server.doip.ContractRepositoryMain.currentIrpClient;
@ChannelHandler.Sharable
public class ContractRepositoryHandler extends RepositoryHandlerBase implements DefaultRepoHandlerInjector {
public class ContractRepositoryHandler extends RepositoryHandlerBase
implements DefaultRepoHandlerInjector {
BCOManager bcoManager;
static Logger LOGGER = LogManager.getLogger(ContractRepositoryHandler.class);
@ -47,7 +48,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
super(info);
GlobalConf.DOAConf doaConf = GlobalConf.instance.doaConf;
String bcoDir = GlobalConf.instance.bcoDir;
bcoManager = new BCOManager(doaConf.repoDoid, new File(bcoDir), doaConf.lhsAddress, GlobalConf.instance.keyPair);
bcoManager = new BCOManager(doaConf.repoDoid, new File(bcoDir), doaConf.lhsAddress,
GlobalConf.instance.keyPair);
}
@Override
@ -58,7 +60,9 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Override
public DoipMessage handleListOps(DoipMessage doipMessage) {
if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) {
return replyStringWithStatus(doipMessage, "unsupported doid:" + doipMessage.header.parameters.id, DoipResponseCode.DoNotFound);
return replyStringWithStatus(doipMessage,
"unsupported doid:" + doipMessage.header.parameters.id,
DoipResponseCode.DoNotFound);
}
JsonArray ops = new JsonArray();
ops.add(BasicOperations.Hello.getName());
@ -69,13 +73,17 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Override
public DoipMessage handleCreate(DoipMessage doipMessage) {
if (!GlobalConf.instance.doaConf.repoDoid.equals(doipMessage.header.parameters.id)) {
return replyStringWithStatus(doipMessage, "unsupported doid:" + doipMessage.header.parameters.id, DoipResponseCode.DoNotFound);
return replyStringWithStatus(doipMessage,
"unsupported doid:" + doipMessage.header.parameters.id,
DoipResponseCode.DoNotFound);
}
// TODD 验证签名完事后进入
if (doipMessage.credential != null && doipMessage.credential.getSigner() != null) {
String permissions = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(), doipMessage.credential.getSigner());
String permissions = KeyValueDBUtil.instance.getValue(CMTables.NodeRole.toString(),
doipMessage.credential.getSigner());
if (!permissions.contains("ContractInstanceManager")) {
return replyStringWithStatus(doipMessage, "permission denied, role:" + permissions, DoipResponseCode.Declined);
return replyStringWithStatus(doipMessage, "permission denied, role:" + permissions,
DoipResponseCode.Declined);
}
}
DigitalObject digitalObject = null;
@ -85,7 +93,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
} catch (Exception e) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
e.printStackTrace(new PrintStream(bo));
return replyStringWithStatus(doipMessage, "exception:" + new String(bo.toByteArray()), DoipResponseCode.Invalid);
return replyStringWithStatus(doipMessage, "exception:" + new String(bo.toByteArray()),
DoipResponseCode.Invalid);
}
digitalObject.attributes.addProperty("owner", doipMessage.credential.getSigner());
try {
@ -93,7 +102,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
// Please note startContractByYPK is invoked in sync mode method.
return replyStringWithStatus(doipMessage, result, DoipResponseCode.Success);
} catch (Exception e) {
return replyStringWithStatus(doipMessage, e.getMessage(), DoipResponseCode.MoreThanOneErrors);
return replyStringWithStatus(doipMessage, e.getMessage(),
DoipResponseCode.MoreThanOneErrors);
}
@ -105,16 +115,19 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
if (attributes.has("bcoId"))
path = bcoManager.download(attributes.get("bcoId").getAsString());
if (path == null) {
throw new IllegalArgumentException("exception, failed to locate bcoId:" + attributes.get("bcoId").getAsString());
throw new IllegalArgumentException(
"exception, failed to locate bcoId:" + attributes.get("bcoId").getAsString());
}
attributes.addProperty("path", path);
Contract c = new Contract();
if (attributes.has("contractExecType"))
c.setType(ContractExecType.valueOf(attributes.get("contractExecType").getAsString()));
else c.setType(ContractExecType.Sole);
else
c.setType(ContractExecType.Sole);
if (attributes.has("shardingId"))
c.setShardingId(Integer.valueOf(attributes.get("shardingId").getAsString()));
else c.setShardingId(-1);
else
c.setShardingId(-1);
c.setScript(path);
c.setOwner(attributes.get("owner").getAsString());
if (attributes.has("createParam")) {
@ -137,7 +150,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
return result;
}
private void registerBDOID(String contractID, String contractPubKey, String bcoId, String startResult) {
private void registerBDOID(String contractID, String contractPubKey, String bcoId,
String startResult) {
try {
JsonObject startResultJO = JsonParser.parseString(startResult).getAsJsonObject();
@ -171,7 +185,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
registerResult = currentIrpClient.register(base);
else
registerResult = currentIrpClient.reRegister(base);
// LOGGER.info("TORegister:" + new GsonBuilder().setPrettyPrinting().create().toJson(base));
// LOGGER.info("TORegister:" + new
// GsonBuilder().setPrettyPrinting().create().toJson(base));
LOGGER.info(base.identifier + " register result:" + registerResult);
} catch (Exception e) {
e.printStackTrace();
@ -192,10 +207,12 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
// TODO judge whethere the do exists
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
if (meta == null) {
return replyStringWithStatus(doipMessage, "can't locate do", DoipResponseCode.DoNotFound);
return replyStringWithStatus(doipMessage, "can't locate do",
DoipResponseCode.DoNotFound);
}
if (meta.getStatus() == ContractStatusEnum.KILLED) {
return replyStringWithStatus(doipMessage, "already deleted!", DoipResponseCode.Declined);
return replyStringWithStatus(doipMessage, "already deleted!",
DoipResponseCode.Declined);
}
DoipMessage[] content = new DoipMessage[1];
JsonObject jo = new JsonObject();
@ -207,11 +224,14 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Override
public DoipMessage handleRetrieve(DoipMessage doipMessage) {
if (doipMessage.header.parameters.id.equals(GlobalConf.instance.doaConf.repoDoid)) {
if (doipMessage.header.parameters.attributes == null || !doipMessage.header.parameters.attributes.has("element"))
if (doipMessage.header.parameters.attributes == null
|| !doipMessage.header.parameters.attributes.has("element"))
return retrieveList(doipMessage);
else
return retrieveBDO(doipMessage, doipMessage.header.parameters.attributes.get("element").getAsString());
} else return replyStringWithStatus(doipMessage, "no such do", DoipResponseCode.Declined);
return retrieveBDO(doipMessage,
doipMessage.header.parameters.attributes.get("element").getAsString());
} else
return replyStringWithStatus(doipMessage, "no such do", DoipResponseCode.Declined);
}
@ -224,11 +244,13 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
if (client == null) {
String doid = idOrName;
int off = doid.lastIndexOf("/");
if (off > 0) doid = doid.substring(off + 1);
if (off > 0)
doid = doid.substring(off + 1);
client = CMActions.manager.getContractClientByDoi(doid);
}
if (client == null) {
return replyStringWithStatus(doipMessage, "BDO instance not exist!", DoipResponseCode.DoNotFound);
return replyStringWithStatus(doipMessage, "BDO instance not exist!",
DoipResponseCode.DoNotFound);
}
byte[] data = doipMessage.body.getEncodedData();
if (data == null || data.length == 0) {
@ -264,7 +286,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
result.add(new JsonPrimitive("Timeout!"));
retStatus = DoipResponseCode.MoreThanOneErrors;
}
DigitalObject digitalObject = new DigitalObject(doipMessage.header.parameters.id, DoType.DO);
DigitalObject digitalObject =
new DigitalObject(doipMessage.header.parameters.id, DoType.DO);
digitalObject.addAttribute("status", retStatus.getName());
digitalObject.addAttribute("result", result.get(0));
@ -305,29 +328,35 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
@Op(op = BasicOperations.Extension, name = "call")
public DoipMessage handleCall(DoipMessage doipMessage) {
ContractClient cc = CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id);
ContractClient cc =
CMActions.manager.getContractClientByDoi(doipMessage.header.parameters.id);
if (cc == null)
return replyStringWithStatus(doipMessage, "Contract instance not exist!", DoipResponseCode.DoNotFound);
return replyStringWithStatus(doipMessage, "Contract instance not exist!",
DoipResponseCode.DoNotFound);
// todo transform doipMessage to args
JsonObject args = doipMessage.header.parameters.attributes;
ContractRequest cr = new ContractRequest();
cr.setContractID(cc.getContractID());
if (args.has("withDynamicAnalysis")) cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
if (args.has("withDynamicAnalysis"))
cr.withDynamicAnalysis = args.get("withDynamicAnalysis").getAsBoolean();
if (args.has("withEvaluatesAnalysis"))
cr.withEvaluatesAnalysis = args.get("withEvaluatesAnalysis").getAsBoolean();
if (args.get("elementID") == null)
return replyStringWithStatus(doipMessage, "missing elementID", DoipResponseCode.Invalid);
return replyStringWithStatus(doipMessage, "missing elementID",
DoipResponseCode.Invalid);
cr.setAction(args.get("elementID").getAsString());
if (doipMessage.body.getEncodedData() != null) cr.setArg(new String(doipMessage.body.getEncodedData()));
if (doipMessage.body.getEncodedData() != null)
cr.setArg(new String(doipMessage.body.getEncodedData()));
if (doipMessage.credential != null && doipMessage.credential.getSigner() != null)
cr.setRequesterDOI(doipMessage.credential.getSigner());
String reqID;
if (args.has("requestID")) reqID = args.get("requestID").getAsString();
if (args.has("requestID"))
reqID = args.get("requestID").getAsString();
else {
reqID = System.currentTimeMillis() + "_" + (int) (Math.random() * 1000);
args.addProperty("requestID", reqID);
@ -354,7 +383,8 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
}
@Override
public void injectListOps(DoipMessage doipMessage, DoipMessageFactory.DoipMessageBuilder builder) {
public void injectListOps(DoipMessage doipMessage,
DoipMessageFactory.DoipMessageBuilder builder) {
builder.setDoipMessage(handleListOps(doipMessage));
}
@ -364,12 +394,14 @@ public class ContractRepositoryHandler extends RepositoryHandlerBase implements
}
@Override
public void injectRetrieve(DoipMessage doipMessage, DoipMessageFactory.DoipMessageBuilder builder) {
public void injectRetrieve(DoipMessage doipMessage,
DoipMessageFactory.DoipMessageBuilder builder) {
builder.setDoipMessage(handleRetrieve(doipMessage));
}
@Override
public void injectDelete(DoipMessage doipMessage, DoipMessageFactory.DoipMessageBuilder builder) {
public void injectDelete(DoipMessage doipMessage,
DoipMessageFactory.DoipMessageBuilder builder) {
builder.setDoipMessage(handleDelete(doipMessage));
}
}

View File

@ -37,7 +37,8 @@ public class ContractRepositoryMain {
String url = GlobalConf.instance.doaConf.doipAddress;
forwarder = new LocalDoipFrowarder();
if (url == null || GlobalConf.instance.doaConf.repoDoid.isEmpty()) {
LOGGER.warn("missing args, failed to start! url:" + url + " doid:" + GlobalConf.instance.doaConf.repoDoid);
LOGGER.warn("missing args, failed to start! url:" + url + " doid:"
+ GlobalConf.instance.doaConf.repoDoid);
return;
}
if (currentServer != null) {
@ -45,7 +46,8 @@ public class ContractRepositoryMain {
}
List<DoipListenerConfig> infos = new ArrayList<>();
infos.add(new DoipListenerConfig(url, "2.1"));
DoipServiceInfo info = new DoipServiceInfo(GlobalConf.instance.doaConf.repoDoid, GlobalConf.instance.keyPair.getPublicKeyStr(), repoType, infos);
DoipServiceInfo info = new DoipServiceInfo(GlobalConf.instance.doaConf.repoDoid,
GlobalConf.instance.keyPair.getPublicKeyStr(), repoType, infos);
EndpointConfig config = new EndpointConfig();
config.publicKey = GlobalConf.instance.keyPair.getPublicKeyStr();
config.privateKey = GlobalConf.instance.keyPair.getPrivateKeyStr();
@ -60,14 +62,17 @@ public class ContractRepositoryMain {
SM2Signer signer = new SM2Signer(SM2KeyPair.fromJson(new Gson().toJson(config)));
DelegateDoipHandler delegateHandler = new DelegateDoipHandler(requestCallback) {
@Override
protected DoipMessage delegateMessage(ChannelHandlerContext context, DoipMessage message) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
protected DoipMessage delegateMessage(ChannelHandlerContext context,
DoipMessage message) {
DoipMessageFactory.DoipMessageBuilder builder =
new DoipMessageFactory.DoipMessageBuilder();
builder.createResponse(DoipResponseCode.DelegateRequired, message);
String id = message.header.parameters.id;
id = id.replaceAll(".*/", "");
ContractMeta meta = CMActions.manager.statusRecorder.getContractMeta(id);
if (enableDelegate(meta)) {
LOGGER.info("delegate:" + message.requestID + " --> doipPort:" + meta.contract.getDoipPort());
LOGGER.info("delegate:" + message.requestID + " --> doipPort:"
+ meta.contract.getDoipPort());
// if port is near cmhttp server port
builder.addAttributes("port", meta.contract.getDoipPort());
DoipMessage ret;
@ -77,7 +82,8 @@ public class ContractRepositoryMain {
}
return ret;
} else {
LOGGER.info("forward:" + message.requestID + " --> doipPort:" + meta.contract.getDoipPort());
LOGGER.info("forward:" + message.requestID + " --> doipPort:"
+ meta.contract.getDoipPort());
return forwarder.forward(meta, message);
}

View File

@ -123,11 +123,8 @@ public class NodeCenterClientController implements NodeCenterConn {
setNodeID.put("id", keyPair.getPublicKeyStr());
String signature = "no signature";
try {
byte[] sig =
SM2Util.sign(
keyPair.getPrivateKeyParameter(),
(keyPair.getPublicKeyStr() + json.get("session").getAsString())
.getBytes());
byte[] sig = SM2Util.sign(keyPair.getPrivateKeyParameter(),
(keyPair.getPublicKeyStr() + json.get("session").getAsString()).getBytes());
signature = ByteUtils.toHexString(sig);
} catch (Exception e) {
e.printStackTrace();
@ -149,8 +146,8 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action
public void syncPong(JsonObject json, ResultCallback resultCallback) {
sync.wakeUp(
json.get("requestID").getAsString(), "{\"status\":\"Success\",\"result\":\"a\"}");
sync.wakeUp(json.get("requestID").getAsString(),
"{\"status\":\"Success\",\"result\":\"a\"}");
}
public boolean syncPing() {
@ -173,12 +170,9 @@ public class NodeCenterClientController implements NodeCenterConn {
}
public void listCMInfo() {
sendMsg(
"{\"action\":\"listCMInfo\",\"pubKey\":\""
sendMsg("{\"action\":\"listCMInfo\",\"pubKey\":\""
+ KeyValueDBUtil.instance.getValue(CMTables.ConfigDB.toString(), "pubKey")
+ "\",\"requestID\":\""
+ System.currentTimeMillis()
+ "\"}");
+ "\",\"requestID\":\"" + System.currentTimeMillis() + "\"}");
}
@Action
@ -230,7 +224,8 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action(async = true)
public void publishEventFromCenter(JsonObject jo, ResultCallback rcb) {
if (jo.has("event")) {
CMActions.manager.deliverEvent(JsonUtil.fromJson(jo.get("event").getAsString(), REvent.class));
CMActions.manager
.deliverEvent(JsonUtil.fromJson(jo.get("event").getAsString(), REvent.class));
}
}
@ -278,7 +273,8 @@ public class NodeCenterClientController implements NodeCenterConn {
String requestID = jo.get("requestID").getAsString();
String requesterNodeID = jo.get("requesterNodeID").getAsString();
String crStr = jo.get("contractRequest").getAsString();
CMActions.manager.executeLocallyAsync(JsonUtil.fromJson(crStr, ContractRequest.class), new ResultCallback() {
CMActions.manager.executeLocallyAsync(JsonUtil.fromJson(crStr, ContractRequest.class),
new ResultCallback() {
@Override
public void onResult(String str) {
JsonObject ret = new JsonObject();
@ -365,8 +361,8 @@ public class NodeCenterClientController implements NodeCenterConn {
if (!cr.result.equals(JsonNull.INSTANCE)) {
try {
JsonObject jo = cr.result.getAsJsonObject();
NetworkManager.instance.updateAgentRouter(
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString());
NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
jo.get("masterAddress").getAsString());
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
return "success";
} catch (Exception e) {
@ -390,18 +386,17 @@ public class NodeCenterClientController implements NodeCenterConn {
if (!cr.result.equals(JsonNull.INSTANCE)) {
try {
JsonObject jo = cr.result.getAsJsonObject();
NetworkManager.instance.updateAgentRouter(
jo.get("pubKey").getAsString(), jo.get("masterAddress").getAsString());
NetworkManager.instance.updateAgentRouter(jo.get("pubKey").getAsString(),
jo.get("masterAddress").getAsString());
NetworkManager.instance.connectToAgent(jo.get("pubKey").getAsString(), null);
LOGGER.info(
String.format("the master of contract %s: pubKey=%s address=%s",
contractID,
jo.get("pubKey").getAsString(),
LOGGER.info(String.format("the master of contract %s: pubKey=%s address=%s",
contractID, jo.get("pubKey").getAsString(),
jo.get("masterAddress").getAsString()));
contractID2PubKey.put(contractID, jo.get("pubKey").getAsString());
return jo.get("pubKey").getAsString();
} catch (Exception e) {
LOGGER.warn("the master of contract " + contractID + " is null! " + e.getMessage());
LOGGER.warn(
"the master of contract " + contractID + " is null! " + e.getMessage());
}
}
} catch (Exception e) {
@ -420,8 +415,8 @@ public class NodeCenterClientController implements NodeCenterConn {
return;
}
startCheck = true;
for (MultiContractMeta meta :
CMActions.manager.multiContractRecorder.getStatus().values()) {
for (MultiContractMeta meta : CMActions.manager.multiContractRecorder.getStatus()
.values()) {
String contractID = meta.getContractID();
LOGGER.info("check master of contract " + contractID);
ContractMeta cmeta = CMActions.manager.statusRecorder.getContractMeta(meta.getID());
@ -435,20 +430,21 @@ public class NodeCenterClientController implements NodeCenterConn {
// 该合约可能在这个节点崩溃期间已经被终止不存在了这个节点不用恢复这个合约了直接从数据库中删除
LOGGER.info("该合约在集群已经不存在!");
KeyValueDBUtil.instance.delete(CMTables.UnitContracts.toString(), contractID);
if (KeyValueDBUtil.instance.containsKey(
CMTables.CheckPointLastHash.toString(), contractID)) {
KeyValueDBUtil.instance.delete(
CMTables.CheckPointLastHash.toString(), contractID);
if (KeyValueDBUtil.instance.containsKey(CMTables.CheckPointLastHash.toString(),
contractID)) {
KeyValueDBUtil.instance.delete(CMTables.CheckPointLastHash.toString(),
contractID);
}
if (KeyValueDBUtil.instance.containsKey(
CMTables.LastExeSeq.toString(), contractID)) {
if (KeyValueDBUtil.instance.containsKey(CMTables.LastExeSeq.toString(),
contractID)) {
KeyValueDBUtil.instance.delete(CMTables.LastExeSeq.toString(), contractID);
}
continue;
}
MasterClientRecoverMechAction.recoverSet.add(contractID);
LOGGER.info("queryUnitContractsID master为" + master.substring(0, 5) + " -> 合约id:" + contractID);
LOGGER.info("queryUnitContractsID master为" + master.substring(0, 5) + " -> 合约id:"
+ contractID);
RecoverMechTimeRecorder.queryMasterFinish = System.currentTimeMillis();
queryUnitContractsID2(contractID, master);
}
@ -489,9 +485,7 @@ public class NodeCenterClientController implements NodeCenterConn {
@Action(async = true)
public void requestLog(JsonObject json, ResultCallback rc) {
if (!json.has("requestID")
|| !json.has("contractID")
|| !json.has("offset")
if (!json.has("requestID") || !json.has("contractID") || !json.has("offset")
|| !json.has("count")) {
LOGGER.debug(
"[CMClientController] missing arguments, requestID / contractID / offset / count");
@ -631,8 +625,8 @@ public class NodeCenterClientController implements NodeCenterConn {
MasterElectTimeRecorder.slaveConnectFinish = System.currentTimeMillis();
// 开启master恢复
MasterServerRecoverMechAction.newMasterRecover(
contractID, json.get("members").getAsString(), onlineMembers);
MasterServerRecoverMechAction.newMasterRecover(contractID,
json.get("members").getAsString(), onlineMembers);
}
private boolean waitForConnection(List<String> nodeNames) {
@ -652,7 +646,8 @@ public class NodeCenterClientController implements NodeCenterConn {
e.printStackTrace();
}
}
} else return true;
} else
return true;
}
return false;
}
@ -668,7 +663,8 @@ public class NodeCenterClientController implements NodeCenterConn {
String distributeID = null;
if (json.has("distributeID"))
distributeID = json.get("distributeID").getAsString();
else distributeID = json.get("responseID").getAsString();
else
distributeID = json.get("responseID").getAsString();
ResultCallback to = distributeReqMap.get(distributeID);
distributeReqMap.remove(distributeID);
to.onResult(json.get("content").getAsString());
@ -720,12 +716,10 @@ public class NodeCenterClientController implements NodeCenterConn {
public void NCStartElect(JsonObject jo, ResultCallback result) {
String conID = jo.get("contractID").getAsString();
String uniNumber = null;
if (jo.has("nuiNumber")) uniNumber = jo.get("nuiNumber").getAsString();
if (jo.has("nuiNumber"))
uniNumber = jo.get("nuiNumber").getAsString();
ContractClient cc = CMActions.manager.getClient(conID);
LOGGER.info(
"[CMClientController] NCStartElect : contractID="
+ conID
+ " client==null:"
LOGGER.info("[CMClientController] NCStartElect : contractID=" + conID + " client==null:"
+ (null == cc));
// 不是自己本地的合约
@ -800,11 +794,12 @@ public class NodeCenterClientController implements NodeCenterConn {
boolean isAppend = args.get("isAppend").getAsBoolean();
boolean isDone = args.get("isDone").getAsBoolean();
boolean isPrivate = args.get("isPrivate").getAsBoolean();
LOGGER.debug(
String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone, isPrivate));
LOGGER.debug(String.format("isAppend=%b isDone=%b isPrivate=%b", isAppend, isDone,
isPrivate));
String path = GlobalConf.instance.publicCompiledDir;
if (isPrivate && args.has("pubKey")) {
path = GlobalConf.instance.privateCompiledDir + "/" + args.get("pubKey").getAsString();
path = GlobalConf.instance.privateCompiledDir + "/"
+ args.get("pubKey").getAsString();
}
File dir = new File(path);
if (!dir.exists()) {

View File

@ -44,7 +44,8 @@ public class NetworkManager {
// Manage server->client connection;
public static final Map<String, AgentConnector> CONNECTORS = new ConcurrentHashMap<>();
// Manage client->server connection;
public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS = new ConcurrentHashMap<>();
public static final Map<String, TCPServerFrameHandler> SERVER_CONNECTORS =
new ConcurrentHashMap<>();
public static final String NODE_CENTER_CLIENT = "NODE_CENTER_CLIENT";
public static final String P2P_GRPC_CLIENT = "P2P_GRPC_CLIENT";
private static final Map<String, String> slaverRouter = new HashMap<>();
@ -59,18 +60,13 @@ public class NetworkManager {
}
public static void reconnectAgent(String master) {
LOGGER.debug(
String.format("master=%s\t%s",
master,
JsonUtil.toJson(slaverRouter)));
LOGGER.debug(String.format("master=%s\t%s", master, JsonUtil.toJson(slaverRouter)));
try {
NetworkManager.AgentConnector conn;
synchronized (conn = NetworkManager.CONNECTORS.get(master)) {
if (!conn.handler.isOpen()) {
String[] ipAndPort = slaverRouter.get(master).split(":");
conn.bootstrap
.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1]))
.sync()
conn.bootstrap.connect(ipAndPort[0], Integer.parseInt(ipAndPort[1])).sync()
.channel();
}
}
@ -94,17 +90,14 @@ public class NetworkManager {
nodeCenterClientHandler = ControllerManager.createNodeCenterClientHandler();
EventLoopGroup group = new NioEventLoopGroup();
b.group(group);
b.channel(NioSocketChannel.class)
.handler(
new ChannelInitializer<SocketChannel>() {
b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new DelimiterCodec()).addLast(nodeCenterClientHandler);
}
});
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(
() -> {
ContractManager.scheduledThreadPool.scheduleWithFixedDelay(() -> {
try {
// manager.clearCache();
String URL = GlobalConf.getNodeCenterUrl();
@ -120,20 +113,13 @@ public class NetworkManager {
nodeCenterClientHandler.close();
assert null != uri;
b.connect(uri.getHost(), uri.getPort()).sync().channel();
LOGGER.info(
"connect to node center: "
+ uri.getHost()
+ ":"
+ uri.getPort());
LOGGER.info("connect to node center: " + uri.getHost() + ":" + uri.getPort());
}
} catch (Exception e) {
// e.printStackTrace();
LOGGER.warn("connecting to node center failed! " + e.getMessage());
}
},
4,
30 + (int) (20 * Math.random()),
TimeUnit.SECONDS);
}, 4, 30 + (int) (20 * Math.random()), TimeUnit.SECONDS);
}
@ -142,17 +128,13 @@ public class NetworkManager {
ServerBootstrap b = new ServerBootstrap();
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.localAddress(port)
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100).localAddress(port)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(
new ChannelInitializer<SocketChannel>() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel arg0) {
arg0.pipeline()
.addLast(new DelimiterCodec())
arg0.pipeline().addLast(new DelimiterCodec())
.addLast(new TCPServerFrameHandler());
}
});
@ -174,9 +156,8 @@ public class NetworkManager {
}
public void waitForNodeCenterConnected() {
for (int i = 0;
i < 10 && null != nodeCenterClientHandler && !nodeCenterClientHandler.isConnected();
i++) {
for (int i = 0; i < 10 && null != nodeCenterClientHandler
&& !nodeCenterClientHandler.isConnected(); i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
@ -233,9 +214,7 @@ public class NetworkManager {
connector.handler = handler;
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
b.group(CMHttpServer.workerGroup);
b.channel(NioSocketChannel.class)
.handler(
new ChannelInitializer<SocketChannel>() {
b.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
@ -307,12 +286,10 @@ public class NetworkManager {
Bootstrap b = new Bootstrap();
final TCPClientFrameHandler handler = new TCPClientFrameHandler(peer);
// tcpClientMap.put(peer, handler);
b.group(group)
.channel(NioSocketChannel.class)
b.group(group).channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.option(ChannelOption.TCP_NODELAY, true)
.handler(
new ChannelInitializer<SocketChannel>() {
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
@ -368,17 +345,16 @@ public class NetworkManager {
LOGGER.info("send msg to itself " + msg);
continue;
}
// tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer, null);
// tcpClientFrameHandler = NetworkManager.instance.tcpClientMap.getOrDefault(peer,
// null);
if (peerID2TCPAddress.containsKey(peer)) {
// recreateTCPClient(peer);
// instance.tcpClientMap.put(peer, tcpClientFrameHandler);
UnitMessage unitMessage =
msg.toBuilder().clearReceiver().addReceiver(peer).build();
UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
LOGGER.info("send msg by p2p to " + peer);
JavaContractServiceGrpcServer.sendMsg(unitMessage);
} else {
UnitMessage unitMessage =
msg.toBuilder().clearReceiver().addReceiver(peer).build();
UnitMessage unitMessage = msg.toBuilder().clearReceiver().addReceiver(peer).build();
LOGGER.info("send msg by p2p to " + peer);
JavaContractServiceGrpcServer.sendMsg(unitMessage);
}