|
| 1 | +package com.r4v3zn.vulfocus.core.client; |
| 2 | +import com.fasterxml.jackson.databind.JsonNode; |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.r4v3zn.vulfocus.core.constants.OperationConstants; |
| 5 | +import com.r4v3zn.vulfocus.core.entity.HostEntity; |
| 6 | +import com.r4v3zn.vulfocus.core.entity.ImageEntity; |
| 7 | +import com.r4v3zn.vulfocus.core.entity.VulfocusException; |
| 8 | +import com.r4v3zn.vulfocus.core.util.HttpUtils; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.LinkedHashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +/** |
| 16 | + * Title: VulfocusClinet |
| 17 | + * Desc: Vulfocus for Client |
| 18 | + * Date:2021/11/25 20:30 |
| 19 | + * Email:woo0nise@gmail.com |
| 20 | + * Company:www.r4v3zn.com |
| 21 | + * |
| 22 | + * @author R4v3zn |
| 23 | + * @version 1.0.0 |
| 24 | + */ |
| 25 | +public class VulfocusClinet { |
| 26 | + |
| 27 | + /** |
| 28 | + * vulfocus url |
| 29 | + */ |
| 30 | + public static String VULFOCUS_URL = "http://vulfocus.io"; |
| 31 | + |
| 32 | + /** |
| 33 | + * api uri |
| 34 | + */ |
| 35 | + private static final String VULFOCUS_API_URI = "/api/imgs/operation"; |
| 36 | + |
| 37 | + /** |
| 38 | + * api url |
| 39 | + */ |
| 40 | + private static final String VULFOCUS_API_URL = VULFOCUS_URL + VULFOCUS_API_URI; |
| 41 | + |
| 42 | + /** |
| 43 | + * Jackson mapper |
| 44 | + */ |
| 45 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 46 | + |
| 47 | + /** |
| 48 | + * username |
| 49 | + */ |
| 50 | + private final String username; |
| 51 | + |
| 52 | + /** |
| 53 | + * licence |
| 54 | + */ |
| 55 | + private final String licence; |
| 56 | + |
| 57 | + /** |
| 58 | + * Vulfocus Clinet |
| 59 | + * @param username username |
| 60 | + * @param licence licence |
| 61 | + */ |
| 62 | + public VulfocusClinet(String username, String licence){ |
| 63 | + this.username = username; |
| 64 | + this.licence = licence; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Image list |
| 69 | + * @return images |
| 70 | + * @throws Exception exception |
| 71 | + */ |
| 72 | + public List<ImageEntity> imageList() throws Exception { |
| 73 | + Map<String,Object> params = new HashMap<>(16); |
| 74 | + params.put("username", this.username); |
| 75 | + params.put("licence", this.licence); |
| 76 | + String response = HttpUtils.doGet(VULFOCUS_API_URL,params); |
| 77 | + JsonNode jsonNode = mapper.readTree(response); |
| 78 | + // check response |
| 79 | + checkResponse(jsonNode); |
| 80 | + String data = jsonNode.get("data").toString(); |
| 81 | + List<LinkedHashMap<String,String>> imageEntities = mapper.readValue(data,List.class); |
| 82 | + return imageEntities.stream().map(tmp -> { |
| 83 | + ImageEntity imageEntity = new ImageEntity(); |
| 84 | + imageEntity.setImageName(tmp.get("image_name")); |
| 85 | + imageEntity.setImageDesc(tmp.get("image_desc")); |
| 86 | + imageEntity.setImageVulName(tmp.get("image_vul_name")); |
| 87 | + return imageEntity; |
| 88 | + }).collect(Collectors.toList()); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Start |
| 93 | + * @param imageName image name |
| 94 | + * @return HostEntity |
| 95 | + * @throws Exception exception |
| 96 | + */ |
| 97 | + public HostEntity start(String imageName) throws Exception { |
| 98 | + JsonNode dataNode = operation(imageName,OperationConstants.START).get("data"); |
| 99 | + HostEntity entity = new HostEntity(); |
| 100 | + entity.setHost(dataNode.get("host").asText()); |
| 101 | + entity.setPort(dataNode.get("port").asText()); |
| 102 | + return entity; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * delete |
| 107 | + * @param imageName image name |
| 108 | + * @return response |
| 109 | + * @throws Exception exception |
| 110 | + */ |
| 111 | + public String delete(String imageName) throws Exception{ |
| 112 | + return operation(imageName,OperationConstants.DELETE).get("msg").asText(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * stop |
| 117 | + * @param imageName image name |
| 118 | + * @return response |
| 119 | + * @throws Exception exception |
| 120 | + */ |
| 121 | + public String stop(String imageName) throws Exception{ |
| 122 | + return operation(imageName,OperationConstants.START).get("msg").asText(); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + /** |
| 127 | + * check param |
| 128 | + * @param imageName image name |
| 129 | + * @throws VulfocusException exception |
| 130 | + */ |
| 131 | + private void checkParam(String imageName)throws VulfocusException { |
| 132 | + if (imageName == null || "".equals(imageName)){ |
| 133 | + throw new VulfocusException("image name cannot be empty"); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * image operation |
| 139 | + * @param imageName image name |
| 140 | + * @param operation start stop delete |
| 141 | + * @return JsonNode |
| 142 | + * @throws Exception exception |
| 143 | + */ |
| 144 | + private JsonNode operation(String imageName, String operation) throws Exception { |
| 145 | + // check param |
| 146 | + checkParam(imageName); |
| 147 | + Map<String,Object> params = new HashMap<>(16); |
| 148 | + params.put("username", this.username); |
| 149 | + params.put("licence", this.licence); |
| 150 | + params.put("image_name", imageName); |
| 151 | + params.put("requisition", operation); |
| 152 | + String response = HttpUtils.doPost(VULFOCUS_API_URL,params); |
| 153 | + JsonNode jsonNode = mapper.readTree(response); |
| 154 | + // check response |
| 155 | + checkResponse(jsonNode); |
| 156 | + return jsonNode; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Check response |
| 161 | + * @param jsonNode json node |
| 162 | + * @throws VulfocusException exception |
| 163 | + */ |
| 164 | + private void checkResponse(JsonNode jsonNode)throws VulfocusException{ |
| 165 | + String msg =jsonNode.get("msg").asText(); |
| 166 | + int status = jsonNode.get("status").asInt(); |
| 167 | + if (status != 200){ |
| 168 | + throw new VulfocusException(msg); |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments