Search in sources :

Example 1 with ValidatorItem

use of cn.jiangzeyin.common.validator.ValidatorItem in project Jpom by dromara.

the class OutGivingProjectController method getItemData.

@RequestMapping(value = "getItemData.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String getItemData(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "id error") String id) {
    HttpServletRequest request = getRequest();
    String workspaceId = outGivingServer.getCheckUserWorkspace(request);
    OutGivingModel outGivingServerItem = outGivingServer.getByKey(id, request);
    Objects.requireNonNull(outGivingServerItem, "没有数据");
    List<OutGivingNodeProject> outGivingNodeProjectList = outGivingServerItem.outGivingNodeProjectList();
    List<JSONObject> collect = outGivingNodeProjectList.stream().map(outGivingNodeProject -> {
        NodeModel nodeModel = nodeService.getByKey(outGivingNodeProject.getNodeId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("nodeId", outGivingNodeProject.getNodeId());
        jsonObject.put("projectId", outGivingNodeProject.getProjectId());
        jsonObject.put("nodeName", nodeModel.getName());
        jsonObject.put("id", BaseNodeModel.fullId(workspaceId, outGivingNodeProject.getNodeId(), outGivingNodeProject.getProjectId()));
        // set projectStatus property
        // NodeModel node = nodeService.getItem(outGivingNodeProject.getNodeId());
        // Project Status: data.pid > 0 means running
        JSONObject projectStatus = JsonMessage.toJson(200, "success");
        if (nodeModel.isOpenStatus()) {
            JSONObject projectInfo = null;
            try {
                projectInfo = projectInfoCacheService.getItem(nodeModel, outGivingNodeProject.getProjectId());
                projectStatus = NodeForward.requestBySys(nodeModel, NodeUrl.Manage_GetProjectStatus, "id", outGivingNodeProject.getProjectId()).toJson();
            } catch (Exception e) {
                jsonObject.put("errorMsg", "error " + e.getMessage());
            }
            if (projectInfo != null) {
                jsonObject.put("projectName", projectInfo.getString("name"));
            }
        } else {
            jsonObject.put("errorMsg", "节点未启用");
        }
        JSONObject data = projectStatus.getJSONObject("data");
        if (data != null && data.getInteger("pId") != null) {
            jsonObject.put("projectStatus", data.getIntValue("pId") > 0);
        } else {
            jsonObject.put("projectStatus", false);
        }
        jsonObject.put("outGivingStatus", outGivingNodeProject.getStatusMsg());
        jsonObject.put("outGivingResult", outGivingNodeProject.getResult());
        jsonObject.put("lastTime", outGivingNodeProject.getLastOutGivingTime());
        return jsonObject;
    }).collect(Collectors.toList());
    return JsonMessage.getString(200, "", collect);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServerWhitelist(io.jpom.model.data.ServerWhitelist) ServerConfigBean(io.jpom.system.ServerConfigBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Feature(io.jpom.permission.Feature) ProjectInfoCacheService(io.jpom.service.node.ProjectInfoCacheService) JsonMessage(cn.jiangzeyin.common.JsonMessage) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpUtil(cn.hutool.http.HttpUtil) BaseEnum(io.jpom.model.BaseEnum) OutGivingRun(io.jpom.outgiving.OutGivingRun) BaseNodeModel(io.jpom.model.BaseNodeModel) OutGivingServer(io.jpom.service.node.OutGivingServer) MethodFeature(io.jpom.permission.MethodFeature) NodeForward(io.jpom.common.forward.NodeForward) ClassFeature(io.jpom.permission.ClassFeature) MediaType(org.springframework.http.MediaType) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IOException(java.io.IOException) AfterOpt(io.jpom.model.AfterOpt) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) BooleanUtil(cn.hutool.core.util.BooleanUtil) OutGivingModel(io.jpom.model.data.OutGivingModel) NodeUrl(io.jpom.common.forward.NodeUrl) Objects(java.util.Objects) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) ValidatorRule(cn.jiangzeyin.common.validator.ValidatorRule) List(java.util.List) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) StringUtil(io.jpom.util.StringUtil) Convert(cn.hutool.core.convert.Convert) ConfigBean(io.jpom.system.ConfigBean) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) BaseServerController(io.jpom.common.BaseServerController) Assert(org.springframework.util.Assert) BaseNodeModel(io.jpom.model.BaseNodeModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) OutGivingNodeProject(io.jpom.model.data.OutGivingNodeProject) OutGivingModel(io.jpom.model.data.OutGivingModel) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ValidatorItem

use of cn.jiangzeyin.common.validator.ValidatorItem in project Jpom by dromara.

the class NodeUpdateController method confirmFastInstall.

@GetMapping(value = "confirm_fast_install.json", produces = MediaType.APPLICATION_JSON_VALUE)
public String confirmFastInstall(@ValidatorItem String id, @ValidatorItem String ip, int port) {
    JSONObject receiveCache = NodeInfoController.getReceiveCache(id);
    Assert.notNull(receiveCache, "没有对应的缓存信息");
    JSONArray jsonArray = receiveCache.getJSONArray("canUseNode");
    Assert.notEmpty(jsonArray, "没有对应的缓存信息:-1");
    Optional<NodeModel> any = jsonArray.stream().map(o -> {
        if (o instanceof NodeModel) {
            return (NodeModel) o;
        }
        JSONObject jsonObject = (JSONObject) o;
        return jsonObject.toJavaObject(NodeModel.class);
    }).filter(nodeModel -> StrUtil.equals(nodeModel.getUrl(), StrUtil.format("{}:{}", ip, port))).findAny();
    Assert.state(any.isPresent(), "ip 地址信息不正确");
    NodeModel nodeModel = any.get();
    try {
        nodeService.testNode(nodeModel);
    } catch (Exception e) {
        DefaultSystemLog.getLog().warn("测试结果:{} {}", nodeModel.getUrl(), e.getMessage());
        return JsonMessage.getString(500, "节点连接失败:" + e.getMessage());
    }
    // 插入
    boolean exists = nodeService.existsByUrl(nodeModel.getUrl(), nodeModel.getWorkspaceId(), null);
    Assert.state(!exists, "对应的节点已经存在拉:" + nodeModel.getUrl());
    nodeService.insert(nodeModel);
    // 更新结果
    receiveCache.put("type", "success");
    return JsonMessage.getString(200, "安装成功", NodeInfoController.listReceiveCache(null));
}
Also used : MultipartFileBuilder(cn.jiangzeyin.controller.multipart.MultipartFileBuilder) ResourceUtil(cn.hutool.core.io.resource.ResourceUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServerConfigBean(io.jpom.system.ServerConfigBean) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Feature(io.jpom.permission.Feature) BaseJpomInterceptor(io.jpom.common.interceptor.BaseJpomInterceptor) NodeInfoController(io.jpom.controller.openapi.NodeInfoController) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) SystemParametersServer(io.jpom.service.system.SystemParametersServer) GetMapping(org.springframework.web.bind.annotation.GetMapping) io.jpom.common(io.jpom.common) MethodFeature(io.jpom.permission.MethodFeature) SystemPermission(io.jpom.permission.SystemPermission) IoUtil(cn.hutool.core.io.IoUtil) ClassFeature(io.jpom.permission.ClassFeature) MediaType(org.springframework.http.MediaType) Collection(java.util.Collection) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) Tuple(cn.hutool.core.lang.Tuple) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) CharsetUtil(cn.hutool.core.util.CharsetUtil) Optional(java.util.Optional) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) AgentFileModel(io.jpom.model.AgentFileModel) HttpStatus(cn.hutool.http.HttpStatus) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) IOException(java.io.IOException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with ValidatorItem

use of cn.jiangzeyin.common.validator.ValidatorItem in project Jpom by dromara.

the class NodeInfoController method receivePush.

/**
 * 接收节点推送的信息
 * <p>
 * yum install -y wget && wget -O install.sh https://dromara.gitee.io/jpom/docs/install.sh && bash install.sh Agent jdk
 * --auto-push-to-server http://127.0.0.1:3000/api/node/receive_push?token=462a47b8fba8da1f824370bb9fcdc01aa1a0fe20&workspaceId=DEFAULT
 *
 * @return json
 */
@RequestMapping(value = ServerOpenApi.RECEIVE_PUSH, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@NotLogin
public String receivePush(@ValidatorItem(msg = "token empty") String token, @ValidatorItem(msg = "ips empty") String ips, @ValidatorItem(msg = "loginName empty") String loginName, @ValidatorItem(msg = "loginPwd empty") String loginPwd, @ValidatorItem(msg = "workspaceId empty") String workspaceId, @ValidatorItem(value = ValidatorRule.NUMBERS, msg = "port error") int port, String ping) {
    Assert.state(StrUtil.equals(token, JpomManifest.getInstance().randomIdSign()), "token error");
    boolean exists = workspaceService.exists(new WorkspaceModel(workspaceId));
    Assert.state(exists, "workspaceId error");
    String sha1Id = SecureUtil.sha1(ips);
    // 
    List<String> ipsList = StrUtil.split(ips, StrUtil.COMMA);
    String clientIp = getClientIP();
    if (!ipsList.contains(clientIp)) {
        ipsList.add(clientIp);
    }
    List<String> canUseIps = ipsList.stream().filter(s -> this.testIpProt(s, ping)).collect(Collectors.toList());
    List<NodeModel> canUseNode = canUseIps.stream().map(s -> {
        NodeModel model = NodeInfoController.this.createModel(s, loginName, loginPwd, port, workspaceId);
        try {
            nodeService.testNode(model);
        } catch (Exception e) {
            DefaultSystemLog.getLog().warn("测试结果:{} {}", model.getUrl(), e.getMessage());
            return null;
        }
        return model;
    }).filter(Objects::nonNull).collect(Collectors.toList());
    // 只返回能通的 IP
    canUseIps = canUseNode.stream().map(NodeModel::getName).collect(Collectors.toList());
    int size1 = CollUtil.size(canUseNode);
    // 
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("allIp", ipsList);
    jsonObject.put("canUseIp", canUseIps);
    jsonObject.put("port", port);
    jsonObject.put("id", sha1Id);
    jsonObject.put("canUseNode", canUseNode);
    // 
    exists = false;
    for (NodeModel nodeModel : canUseNode) {
        if (nodeService.existsByUrl(nodeModel.getUrl(), nodeModel.getWorkspaceId(), null)) {
            // 存在
            jsonObject.put("type", "exists");
            exists = true;
            break;
        }
    }
    if (!exists) {
        if (size1 == 1) {
            // 只有一个 ip 可以使用
            // 添加插件端
            NodeModel first = CollUtil.getFirst(canUseNode);
            nodeService.insertNotFill(first);
            jsonObject.put("type", "success");
        } else {
            jsonObject.put("type", size1 == 0 ? "canUseIpEmpty" : "multiIp");
        }
    }
    CACHE_RECEIVE_PUSH.put(sha1Id, jsonObject);
    return JsonMessage.getString(200, "done", jsonObject);
}
Also used : AbstractController(cn.jiangzeyin.controller.base.AbstractController) SecureUtil(cn.hutool.crypto.SecureUtil) java.util(java.util) ServerOpenApi(io.jpom.common.ServerOpenApi) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) JsonMessage(cn.jiangzeyin.common.JsonMessage) NetUtil(cn.hutool.core.net.NetUtil) NodeService(io.jpom.service.node.NodeService) WorkspaceModel(io.jpom.model.data.WorkspaceModel) WorkspaceService(io.jpom.service.system.WorkspaceService) MediaType(org.springframework.http.MediaType) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) ValidatorRule(cn.jiangzeyin.common.validator.ValidatorRule) JpomManifest(io.jpom.common.JpomManifest) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) Convert(cn.hutool.core.convert.Convert) JSONObject(com.alibaba.fastjson.JSONObject) NotLogin(io.jpom.common.interceptor.NotLogin) Assert(org.springframework.util.Assert) WorkspaceModel(io.jpom.model.data.WorkspaceModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) NotLogin(io.jpom.common.interceptor.NotLogin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

StrUtil (cn.hutool.core.util.StrUtil)3 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)3 JsonMessage (cn.jiangzeyin.common.JsonMessage)3 ValidatorItem (cn.jiangzeyin.common.validator.ValidatorItem)3 JSONObject (com.alibaba.fastjson.JSONObject)3 NodeModel (io.jpom.model.data.NodeModel)3 Collectors (java.util.stream.Collectors)3 MediaType (org.springframework.http.MediaType)3 Assert (org.springframework.util.Assert)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)3 RestController (org.springframework.web.bind.annotation.RestController)3 CollUtil (cn.hutool.core.collection.CollUtil)2 Convert (cn.hutool.core.convert.Convert)2 FileUtil (cn.hutool.core.io.FileUtil)2 ValidatorRule (cn.jiangzeyin.common.validator.ValidatorRule)2 MultipartFileBuilder (cn.jiangzeyin.controller.multipart.MultipartFileBuilder)2 ClassFeature (io.jpom.permission.ClassFeature)2 Feature (io.jpom.permission.Feature)2 MethodFeature (io.jpom.permission.MethodFeature)2