Skip to content

Commit c3550ea

Browse files
committed
init
1 parent e78a1c8 commit c3550ea

File tree

11 files changed

+681
-2
lines changed

11 files changed

+681
-2
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* linguist-language=java

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# target
2+
target/
3+
# idea
4+
.idea/
15
# Compiled class file
26
*.class
7+
.DS_Store
38

49
# Log file
510
*.log
@@ -20,4 +25,4 @@
2025
*.rar
2126

2227
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
28+
hs_err_pid*

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# vulfocus-java
1+
# vulfocus-java
2+
3+
4+
5+
[![GitHub (pre-)release](https://img.shields.io/github/release/fofapro/vulfocus-java/all.svg)](https://github.com/fofapro/vulfocus-java/releases) [![stars](https://img.shields.io/github/stars/fofapro/vulfocus-java.svg)](https://github.com/fofapro/vulfocus-java/stargazers) [![license](https://img.shields.io/github/license/fofapro/vulfocus-java.svg)](https://github.com/fofapro/vulfocus-java/blob/master/LICENSE)
6+
7+
## Vulfocus API
8+
9+
10+
[`Vulfocus API`](https://fofapro.github.io/vulfocus/#/VULFOCUSAPI) is the `RESUFul API` interface provided by [`Vulfocus`](http://vulfocus.io/) for development, allowing Developers integrate [`Vulfocus`](http://vulfocus.io) in their own projects.
11+
12+
## Vulfocus SDK
13+
14+
The `Java` version of `SDK` written based on the [`Vulfocus API`](https://fofapro.github.io/vulfocus/#/VULFOCUSAPI) makes it easy for `Java` developers to quickly integrate [`Vulfocus`](http://vulfocus.io/) into their projects.

pom.xml

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.r4v3zn.vulfocus.core</groupId>
8+
<artifactId>vulfocus-java</artifactId>
9+
<version>0.0.1</version>
10+
<description>Thi is a Vulfocus SDK</description>
11+
<url>https://github.com/fofapro/vulfocus-java</url>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.apache.maven.plugins</groupId>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>8</source>
20+
<target>8</target>
21+
</configuration>
22+
</plugin>
23+
</plugins>
24+
</build>
25+
26+
<licenses>
27+
<license>
28+
<name>The Apache Software License, Version 2.0</name>
29+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
30+
</license>
31+
</licenses>
32+
<developers>
33+
<developer>
34+
<name>R4v3zn</name>
35+
<email>woo0nise@gmail.com</email>
36+
</developer>
37+
</developers>
38+
<dependencies>
39+
<!-- jackson -->
40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-core</artifactId>
43+
<version>2.13.0</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.fasterxml.jackson.core</groupId>
47+
<artifactId>jackson-databind</artifactId>
48+
<version>2.13.0</version>
49+
</dependency>
50+
</dependencies>
51+
<distributionManagement>
52+
<snapshotRepository>
53+
<id>oss</id>
54+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
55+
</snapshotRepository>
56+
<repository>
57+
<id>oss</id>
58+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
59+
</repository>
60+
</distributionManagement>
61+
<issueManagement>
62+
<system>Github Issue</system>
63+
<url>https://github.com/fofapro/vulfocus-java/issues</url>
64+
</issueManagement>
65+
<scm>
66+
<connection>scm:git:https://github.com/fofapro/vulfocus-java.git</connection>
67+
<developerConnection>scm:git:https://github.com/fofapro/vulfocus-java.git</developerConnection>
68+
<url>https://github.com/fofapro/vulfocus-java.git</url>
69+
</scm>
70+
<profiles>
71+
<profile>
72+
<id>release</id>
73+
<activation>
74+
<activeByDefault>true</activeByDefault>
75+
</activation>
76+
<distributionManagement>
77+
<snapshotRepository>
78+
<id>oss</id>
79+
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
80+
</snapshotRepository>
81+
<repository>
82+
<id>oss</id>
83+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
84+
</repository>
85+
</distributionManagement>
86+
<build>
87+
<plugins>
88+
<!-- Source -->
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-source-plugin</artifactId>
92+
<version>3.0.1</version>
93+
<executions>
94+
<execution>
95+
<phase>package</phase>
96+
<goals>
97+
<goal>jar-no-fork</goal>
98+
</goals>
99+
</execution>
100+
</executions>
101+
</plugin>
102+
<!-- Javadoc -->
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-javadoc-plugin</artifactId>
106+
<version>3.0.0</version>
107+
<executions>
108+
<execution>
109+
<phase>package</phase>
110+
<goals>
111+
<goal>jar</goal>
112+
</goals>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
<!-- Gpg Signature -->
117+
<plugin>
118+
<groupId>org.apache.maven.plugins</groupId>
119+
<artifactId>maven-gpg-plugin</artifactId>
120+
<version>1.6</version>
121+
<executions>
122+
<execution>
123+
<id>sign-artifacts</id>
124+
<phase>verify</phase>
125+
<goals>
126+
<goal>sign</goal>
127+
</goals>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
</plugins>
132+
</build>
133+
</profile>
134+
</profiles>
135+
136+
<properties>
137+
<maven.compiler.source>8</maven.compiler.source>
138+
<maven.compiler.target>8</maven.compiler.target>
139+
</properties>
140+
141+
</project>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.r4v3zn.vulfocus.core.constants;
2+
3+
/**
4+
* Title: OperationConstants
5+
* Desc: Operation Constants
6+
* Date:2021/11/25 22:06
7+
* Email:woo0nise@gmail.com
8+
* Company:www.r4v3zn.com
9+
*
10+
* @author R4v3zn
11+
* @version 1.0.0
12+
*/
13+
public class OperationConstants {
14+
15+
/**
16+
* start
17+
*/
18+
public static final String START = "start";
19+
20+
/**
21+
* stop
22+
*/
23+
public static final String STOP = "stop";
24+
25+
/**
26+
* delete
27+
*/
28+
public static final String DELETE = "delete";
29+
}

0 commit comments

Comments
 (0)