6: Create Analyser
This commit is contained in:
@@ -2,11 +2,13 @@ package com.bartek.esa;
|
|||||||
|
|
||||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||||
import com.bartek.esa.cli.parser.CliArgsParser;
|
import com.bartek.esa.cli.parser.CliArgsParser;
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
import com.bartek.esa.di.DaggerDependencyInjector;
|
import com.bartek.esa.di.DaggerDependencyInjector;
|
||||||
import com.bartek.esa.dispatcher.dispatcher.MethodDispatcher;
|
import com.bartek.esa.dispatcher.dispatcher.MethodDispatcher;
|
||||||
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class EsaMain {
|
public class EsaMain {
|
||||||
private final CliArgsParser cliArgsParser;
|
private final CliArgsParser cliArgsParser;
|
||||||
@@ -20,13 +22,13 @@ public class EsaMain {
|
|||||||
|
|
||||||
private void run(String[] args) {
|
private void run(String[] args) {
|
||||||
DispatcherActions dispatcherActions = DispatcherActions.builder()
|
DispatcherActions dispatcherActions = DispatcherActions.builder()
|
||||||
.sourceAnalysis(source -> {})
|
.sourceAnalysis((source, plugins, excludes) -> null)
|
||||||
.apkAudit(source -> {})
|
.apkAudit((source, plugins, excludes) -> null)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
CliArgsOptions options = cliArgsParser.parse(args);
|
CliArgsOptions options = cliArgsParser.parse(args);
|
||||||
|
|
||||||
methodDispatcher.dispatchMethod(options, dispatcherActions);
|
List<Issue> issues = methodDispatcher.dispatchMethod(options, dispatcherActions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|||||||
83
src/main/java/com/bartek/esa/core/analyser/Analyser.java
Normal file
83
src/main/java/com/bartek/esa/core/analyser/Analyser.java
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package com.bartek.esa.core.analyser;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.archetype.Plugin;
|
||||||
|
import com.bartek.esa.core.executor.PluginExecutor;
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class Analyser {
|
||||||
|
private final FileProvider fileProvider;
|
||||||
|
private final PluginExecutor pluginExecutor;
|
||||||
|
private final Set<Plugin> plugins;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public Analyser(FileProvider fileProvider, PluginExecutor pluginExecutor, Set<Plugin> plugins) {
|
||||||
|
this.fileProvider = fileProvider;
|
||||||
|
|
||||||
|
this.pluginExecutor = pluginExecutor;
|
||||||
|
this.plugins = plugins;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Issue> analyse(String source, Set<String> pluginCodes, Set<String> excludeCodes) {
|
||||||
|
File manifest = getManifest(source);
|
||||||
|
Set<File> files = getFiles(source);
|
||||||
|
Set<Plugin> selectedPlugins = getPlugins(pluginCodes, excludeCodes);
|
||||||
|
|
||||||
|
return pluginExecutor.executeForFiles(manifest, files, selectedPlugins);
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getManifest(String source) {
|
||||||
|
Set<File> manifests = fileProvider.getGlobMatchedFiles(source, "**/AndroidManifest.xml");
|
||||||
|
if (manifests.isEmpty()) {
|
||||||
|
throw new EsaException("No AndroidManifest.xml file found. Interrupting...");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (manifests.size() > 1) {
|
||||||
|
throw new EsaException("Found multiple AndroidManifest.xml files. Interrupting...");
|
||||||
|
}
|
||||||
|
|
||||||
|
return (File) (manifests.toArray())[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<File> getFiles(String source) {
|
||||||
|
Set<File> javaFiles = fileProvider.getGlobMatchedFiles(source, "**/*.java");
|
||||||
|
Set<File> androidManifest = fileProvider.getGlobMatchedFiles(source, "**/AndroidManifest.xml");
|
||||||
|
Set<File> layoutFiles = fileProvider.getGlobMatchedFiles(source, "**/res/layout*/**.xml");
|
||||||
|
|
||||||
|
return Stream.of(javaFiles, androidManifest, layoutFiles)
|
||||||
|
.flatMap(Set::stream)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Plugin> getPlugins(Set<String> pluginCodes, Set<String> excludeCodes) {
|
||||||
|
Set<Plugin> outputPlugins = plugins;
|
||||||
|
|
||||||
|
if (!pluginCodes.isEmpty()) {
|
||||||
|
outputPlugins = plugins.stream()
|
||||||
|
.filter(plugin -> pluginCodes
|
||||||
|
.stream()
|
||||||
|
.anyMatch(pluginCode -> plugin.getClass().getCanonicalName().equals(pluginCode))
|
||||||
|
)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!excludeCodes.isEmpty()) {
|
||||||
|
outputPlugins = outputPlugins.stream()
|
||||||
|
.filter(plugin -> excludeCodes
|
||||||
|
.stream()
|
||||||
|
.noneMatch(pluginCode -> plugin.getClass().getCanonicalName().equals(pluginCode))
|
||||||
|
)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputPlugins;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,17 @@
|
|||||||
package com.bartek.esa.core.di;
|
package com.bartek.esa.core.di;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.analyser.Analyser;
|
||||||
|
import com.bartek.esa.core.archetype.Plugin;
|
||||||
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||||
import com.bartek.esa.core.executor.PluginExecutor;
|
import com.bartek.esa.core.executor.PluginExecutor;
|
||||||
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
|
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
|
||||||
import com.bartek.esa.core.xml.XmlHelper;
|
import com.bartek.esa.core.xml.XmlHelper;
|
||||||
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@Module
|
@Module
|
||||||
public class CoreModule {
|
public class CoreModule {
|
||||||
|
|
||||||
@@ -29,4 +34,9 @@ public class CoreModule {
|
|||||||
public XmlHelper xmlHelper() {
|
public XmlHelper xmlHelper() {
|
||||||
return new XmlHelper();
|
return new XmlHelper();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public Analyser analyser(FileProvider fileProvider, PluginExecutor pluginExecutor, Set<Plugin> plugins) {
|
||||||
|
return new Analyser(fileProvider, pluginExecutor, plugins);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/main/java/com/bartek/esa/core/di/PluginModule.java
Normal file
19
src/main/java/com/bartek/esa/core/di/PluginModule.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.bartek.esa.core.di;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.archetype.Plugin;
|
||||||
|
import dagger.Module;
|
||||||
|
import dagger.Provides;
|
||||||
|
import dagger.multibindings.ElementsIntoSet;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@Module
|
||||||
|
public class PluginModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@ElementsIntoSet
|
||||||
|
public Set<Plugin> plugins() {
|
||||||
|
return new HashSet<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import org.w3c.dom.Document;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
@@ -19,14 +20,14 @@ public class PluginExecutor {
|
|||||||
this.xmlHelper = xmlHelper;
|
this.xmlHelper = xmlHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Issue> executeForFiles(File manifest, List<File> files, List<Plugin> plugins) {
|
public List<Issue> executeForFiles(File manifest, Set<File> files, Set<Plugin> plugins) {
|
||||||
return files.stream()
|
return files.stream()
|
||||||
.map(file -> executeForFile(manifest, file, plugins))
|
.map(file -> executeForFile(manifest, file, plugins))
|
||||||
.flatMap(List::stream)
|
.flatMap(List::stream)
|
||||||
.collect(toList());
|
.collect(toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Issue> executeForFile(File manifest, File file, List<Plugin> plugins) {
|
private List<Issue> executeForFile(File manifest, File file, Set<Plugin> plugins) {
|
||||||
Document xmlManifest = xmlHelper.parseXml(manifest);
|
Document xmlManifest = xmlHelper.parseXml(manifest);
|
||||||
return plugins.stream()
|
return plugins.stream()
|
||||||
.filter(plugin -> plugin.supports(file))
|
.filter(plugin -> plugin.supports(file))
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.bartek.esa.di;
|
|||||||
import com.bartek.esa.EsaMain;
|
import com.bartek.esa.EsaMain;
|
||||||
import com.bartek.esa.cli.di.CliModule;
|
import com.bartek.esa.cli.di.CliModule;
|
||||||
import com.bartek.esa.core.di.CoreModule;
|
import com.bartek.esa.core.di.CoreModule;
|
||||||
|
import com.bartek.esa.core.di.PluginModule;
|
||||||
import com.bartek.esa.decompiler.di.DecompilerModule;
|
import com.bartek.esa.decompiler.di.DecompilerModule;
|
||||||
import com.bartek.esa.dispatcher.di.DispatcherModule;
|
import com.bartek.esa.dispatcher.di.DispatcherModule;
|
||||||
import com.bartek.esa.file.di.FileModule;
|
import com.bartek.esa.file.di.FileModule;
|
||||||
@@ -13,7 +14,8 @@ import dagger.Component;
|
|||||||
DispatcherModule.class,
|
DispatcherModule.class,
|
||||||
FileModule.class,
|
FileModule.class,
|
||||||
DecompilerModule.class,
|
DecompilerModule.class,
|
||||||
CoreModule.class
|
CoreModule.class,
|
||||||
|
PluginModule.class
|
||||||
})
|
})
|
||||||
public interface DependencyInjector {
|
public interface DependencyInjector {
|
||||||
EsaMain esa();
|
EsaMain esa();
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package com.bartek.esa.dispatcher.dispatcher;
|
package com.bartek.esa.dispatcher.dispatcher;
|
||||||
|
|
||||||
import com.bartek.esa.cli.model.CliArgsOptions;
|
import com.bartek.esa.cli.model.CliArgsOptions;
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
import com.bartek.esa.dispatcher.model.DispatcherActions;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class MethodDispatcher {
|
public class MethodDispatcher {
|
||||||
|
|
||||||
@@ -12,14 +15,22 @@ public class MethodDispatcher {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dispatchMethod(CliArgsOptions options, DispatcherActions actions) {
|
public List<Issue> dispatchMethod(CliArgsOptions options, DispatcherActions actions) {
|
||||||
if(options.isSourceAnalysis()) {
|
if(options.isSourceAnalysis()) {
|
||||||
actions.getSourceAnalysis().accept(options.getSourceAnalysisDirectory());
|
return actions.getSourceAnalysis().perform(
|
||||||
return;
|
options.getSourceAnalysisDirectory(),
|
||||||
|
options.getPlugins(),
|
||||||
|
options.getExcludes()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(options.isApkAudit()) {
|
if(options.isApkAudit()) {
|
||||||
actions.getApkAudit().accept(options.getApkAuditFile());
|
return actions.getApkAudit().perform(options.getApkAuditFile(),
|
||||||
|
options.getPlugins(),
|
||||||
|
options.getExcludes()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/main/java/com/bartek/esa/dispatcher/model/Action.java
Normal file
12
src/main/java/com/bartek/esa/dispatcher/model/Action.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.bartek.esa.dispatcher.model;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.model.object.Issue;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Action {
|
||||||
|
|
||||||
|
List<Issue> perform(String source, Set<String> plugins, Set<String> excludes);
|
||||||
|
}
|
||||||
@@ -3,11 +3,9 @@ package com.bartek.esa.dispatcher.model;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
public class DispatcherActions {
|
public class DispatcherActions {
|
||||||
private Consumer<String> sourceAnalysis;
|
private Action sourceAnalysis;
|
||||||
private Consumer<String> apkAudit;
|
private Action apkAudit;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user