-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
300 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package entity; | ||
|
||
/** | ||
* @author fate | ||
* @date 2019-11-12 下午6:30 | ||
* 依赖信息实体 | ||
*/ | ||
public class DependencyInfo { | ||
|
||
private String version; | ||
|
||
private String groupId; | ||
|
||
private String artifactId; | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(String version) { | ||
this.version = version; | ||
} | ||
|
||
public String getGroupId() { | ||
return groupId; | ||
} | ||
|
||
public void setGroupId(String groupId) { | ||
this.groupId = groupId; | ||
} | ||
|
||
public String getArtifactId() { | ||
return artifactId; | ||
} | ||
|
||
public void setArtifactId(String artifactId) { | ||
this.artifactId = artifactId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package maven; | ||
|
||
import common.CommonUtil; | ||
import entity.DependencyInfo; | ||
|
||
import java.io.File; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
/** | ||
* @author fate | ||
* @date 2019-11-22 下午12:05 | ||
*/ | ||
public class EasyJarHandle { | ||
|
||
/** | ||
* 获取依赖信息 | ||
* @param jarpath jar文件路径 | ||
* @param dependencyInfoList 存放依赖包数据的list | ||
* @return | ||
*/ | ||
public static List<DependencyInfo> getDependencyInfo(String jarpath, List<DependencyInfo> dependencyInfoList) { | ||
|
||
try { | ||
|
||
File jarDict = new File(jarpath.replace("file:","").replace("WEB-INF/classes/", "WEB-INF/lib/")); | ||
|
||
for (File file : jarDict.listFiles()) { | ||
|
||
if (file.isFile() && file.getName().endsWith(".jar")) { | ||
|
||
JarFile jarFile = new JarFile(file); | ||
|
||
Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries(); | ||
|
||
while (jarEntryEnumeration.hasMoreElements()) { | ||
|
||
JarEntry jarEntry= jarEntryEnumeration.nextElement(); | ||
|
||
if (jarEntry.getName().endsWith("/pom.properties")) { | ||
|
||
Properties prop = new Properties(); | ||
prop.load(jarFile.getInputStream(jarEntry)); | ||
|
||
DependencyInfo dependencyInfo = new DependencyInfo(); // 存放依赖信息 | ||
dependencyInfo.setArtifactId(prop.getProperty("artifactId")); | ||
dependencyInfo.setGroupId(prop.getProperty("groupId")); | ||
dependencyInfo.setVersion(prop.getProperty("version")); | ||
|
||
dependencyInfoList.add(dependencyInfo); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception e) { | ||
|
||
CommonUtil.writeStr("/tmp/jvm_error.txt","getDependencyInfo_byeasy:\t" + e.getMessage()); | ||
} | ||
|
||
return dependencyInfoList; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package maven; | ||
import common.CommonUtil; | ||
import entity.DependencyInfo; | ||
import org.springframework.boot.loader.jar.JarFile; // 偷懒 直接使用springboot的 | ||
import java.io.File; | ||
import java.util.Enumeration; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.jar.JarEntry; | ||
|
||
/** | ||
* @author fate | ||
* @date 2019-11-22 上午11:38 | ||
* 用于处理fat jar资源的获取 | ||
*/ | ||
public class FatJarHandle { | ||
|
||
/** | ||
* fat jar 依赖文件的获取,多用于处理springboot打包的jar 传入的path是这样的 jar:file:/home/q/system/java/live/build/libs/live-33541.a12ed7cc.jar!/BOOT-INF/classes!/ | ||
* @param jarpath | ||
* @param dependencyInfoList | ||
* @return | ||
*/ | ||
public static List<DependencyInfo> getDependencyInfo(String jarpath, List<DependencyInfo> dependencyInfoList) { | ||
|
||
try { | ||
|
||
JarFile jarFile = new JarFile(new File(getROOTJar(jarpath))); | ||
|
||
Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries(); | ||
|
||
while (jarEntryEnumeration.hasMoreElements()) { | ||
|
||
JarEntry jarEntry = jarEntryEnumeration.nextElement(); | ||
|
||
if (jarEntry.getName().endsWith(".jar")) { // 这里就暂时不匹配BOOT-INF/lib,考虑通用性 | ||
|
||
JarFile inJarFile = jarFile.getNestedJarFile(jarEntry); | ||
DependencyInfo dependencyInfo = getJarInJardependcyInfo(inJarFile); // 获取资源 | ||
|
||
if (dependencyInfo != null) dependencyInfoList.add(dependencyInfo); | ||
|
||
} | ||
} | ||
|
||
} | ||
catch (Exception e) { | ||
|
||
CommonUtil.writeStr("/tmp/jvm_error.txt","getDependencyInfo:\t" + e.getMessage()); | ||
} | ||
|
||
return dependencyInfoList; | ||
} | ||
|
||
/** | ||
* 获取Jarinjar中的资源 | ||
* @param jarFile | ||
* @return | ||
*/ | ||
public static DependencyInfo getJarInJardependcyInfo(JarFile jarFile) { | ||
|
||
try { | ||
|
||
Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries(); | ||
|
||
while (jarEntryEnumeration.hasMoreElements()) { | ||
|
||
JarEntry jarEntry= jarEntryEnumeration.nextElement(); | ||
|
||
if (jarEntry.getName().endsWith("/pom.properties")) { | ||
|
||
Properties prop = new Properties(); | ||
prop.load(jarFile.getInputStream(jarEntry)); | ||
|
||
DependencyInfo dependencyInfo = new DependencyInfo(); // 存放依赖信息 | ||
dependencyInfo.setArtifactId(prop.getProperty("artifactId")); | ||
dependencyInfo.setGroupId(prop.getProperty("groupId")); | ||
dependencyInfo.setVersion(prop.getProperty("version")); | ||
|
||
return dependencyInfo; | ||
} | ||
} | ||
|
||
} | ||
catch (Exception e) { | ||
|
||
CommonUtil.writeStr("/tmp/jvm_error.txt","getJarInJardependcyInfo:\t" + e.getMessage()); | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
/** | ||
* 获取rootjar资源路径 | ||
* @param jarPath | ||
* @return | ||
*/ | ||
public static String getROOTJar(String jarPath) { | ||
|
||
jarPath = jarPath.split(".jar!/")[0].replace("jar:file:",""); | ||
|
||
return jarPath + ".jar"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package maven; | ||
|
||
import common.CommonUtil; | ||
import entity.DependencyInfo; | ||
import entity.JvmInfo; | ||
import org.springframework.boot.loader.jar.Handler; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author fate | ||
* @date 2019-11-08 上午12:31 | ||
* 从jvm实例中构建pom.xml | ||
*/ | ||
public class MavenHandle extends Handler { | ||
|
||
/** | ||
* 获取jar读取到的依赖 用于针对于应用的漏洞(风险)管理 | ||
* @param jvmInfo | ||
* @return | ||
*/ | ||
public JvmInfo getMavenResult(JvmInfo jvmInfo) { | ||
|
||
try { | ||
|
||
List<DependencyInfo> dependencyInfos = new ArrayList<DependencyInfo>(); | ||
|
||
for(Map.Entry<String, String> entry : jvmInfo.getJarPathMap().entrySet()){ | ||
|
||
String targetJar = entry.getKey().trim(); | ||
|
||
if (targetJar.endsWith("!/")) { | ||
|
||
FatJarHandle.getDependencyInfo(targetJar,dependencyInfos); | ||
//System.out.println("胖头鱼走起"); | ||
} | ||
else { | ||
|
||
EasyJarHandle.getDependencyInfo(targetJar,dependencyInfos); | ||
//System.out.println("easyjar 走起"); | ||
} | ||
} | ||
|
||
jvmInfo.setDependencyInfoList(dependencyInfos); | ||
} | ||
catch (Exception e) { | ||
|
||
CommonUtil.writeStr("/tmp/jvm_error.txt","getMavenResult:\t" + e.getMessage()); | ||
} | ||
|
||
return jvmInfo; | ||
} | ||
} | ||
|
Oops, something went wrong.