Download this file

showFileDetails.java    241 lines (208 with data), 0 Bytes

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-12T00:00:00Z
* LicenseName: NOASSERTION
* FileName: showFileDetails.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> This class displays the details about a given file that
* has been selected on the treeview or throught some similar manner </text>
*/
package basic;
import GUI.NodeType;
import GUI.TreeNodeSPDX;
import GUI.swingUtils;
import definitions.Messages;
import definitions.is;
import script.Plugin;
import script.log;
import spdxlib.FileInfo;
import main.core;
import utils.html;
/**
*
* @author Nuno Brito, 12th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class showFileDetails extends Plugin{
@Override
public void startup(){
// react whenever a tree node is changed
log.hooks.addAction(Messages.TreeNodeChanged, thisFile, "processFile");
}
/**
* Checks if the node of a file has been selected. If this is the case then
* show as much details as possible about the file
*/
void processFile(){
// ensure we get to know which node is selected
TreeNodeSPDX node = swingUtils.getSelectedNode();
// no need to continue if there is nothing selected
if(node == null){
return;
}
// process files
if(node.nodeType == NodeType.file){
showFileDetails(node);
}
}
/**
* Tries to discover the file extension based on the SPDX file name
* @return
*/
String getFileExtension(String filename){
// preflight checks
if(filename.contains(".")==false){
return "";
}
if(filename.contains("/")){
return "";
}
int pos = filename.lastIndexOf(".");
String extension = "filetype:" + filename.substring(pos+1);
return extension;
}
/**
* Do the actual part of showing the details for this file
* @param node
*/
private void showFileDetails(TreeNodeSPDX node) {
// where we store the end result
FileInfo file = (FileInfo) node.getUserObject();
String googleTerm = "";
String filename = file.tagFileName.toString();
// get the file extension if available
//String extension = getFileExtension(filename);
try{
// get the package name to narrow down the results
TreeNodeSPDX parent = (TreeNodeSPDX) node.getParent();
TreeNodeSPDX root = (TreeNodeSPDX) parent.getParent();
// we need to remove misleading terms from the filename
String filteredName = root.id.toLowerCase();
filteredName = filteredName.replace(".tar.gz", "");
filteredName = filteredName.replace(".zip", "");
filteredName = filteredName.replace(".jar", "");
filteredName = filteredName.replace(".7z", "");
filteredName = filteredName.replace(".tar", "");
filteredName = filteredName.replace(".bz2", "");
// build up our google query
googleTerm = filteredName
+ " %22"
+ filename
+ "%22 "
//+ extension
;
} catch (Exception e){
log.write(is.ERROR, "SFD01 - Error occurred when trying to build"
+ " a search keyword for google");
}
// do the introduction of this file with a breadcrumb
String[] fields = node.getUID().split(">>");
String breadCrumb =
fields[4]
+ ">"
+ fields[3]
+ ">"
+ fields[1]
;
// add a short summary about the file
String summary = "";
if(file.tagFileSize != null){
summary += "This file is sized in " + file.tagFileSize.toString();
}
if(file.tagFileLOC != null){
summary += " and has " + file.tagFileLOC + " lines of code"
+ html.br;
}
if(spdxlib.tools.hasLicense(file)){
summary += "Applicable license(s): " + spdxlib.tools.getLicense(file);
}
// do the date creation
//TODO We need to archive file date information
//summary += "Created in " + file.;
String resultIntroduction = ""
+ "<h2>"
+ breadCrumb
+ "</h2>"
+ html.div()
+ summary
+ html._div
// + html.br
;
// Add links to find more info on the Internet about the file name
String resultFilename =
html.div()
+ html.linkToSearchYandex("\"" + filename + "\"")
+ html.divider
+ html.linkToSearchGoogle(googleTerm)
// + html.divider
// + html.linkToSearchOhloh(file.tagFileName.toString())
+ html.divider
+ html.linkToSearchGitHub(file.tagFileName.toString())
+ html._div;
String resultSHA1 = "";
if(file.tagFileChecksumSHA1!= null){
resultSHA1 = file.tagFileChecksumSHA1.toString()
+ html.br
+ html.div()
+ html.linkSearch("Find duplicates", "SHA1: "
+ file.tagFileChecksumSHA1.toString())
+ html.divider
+ html.linkToSearchGoogle(
"%22"+ file.tagFileChecksumSHA1.toString() + "%22" )
+ html._div;
}
String resultSHA256 = "";
if(file.tagFileChecksumSHA256!= null){
resultSHA256 = file.tagFileChecksumSHA256.toString()
+ html.div()
// only possible when we have SHA256 hashes available
+ html.linkToSearchVirusTotal(file.tagFileChecksumSHA256.toString())
+ html._div;
}
String resultMD5 = "";
if(file.tagFileChecksumMD5!= null){
resultMD5 = file.tagFileChecksumMD5.toString()
+ html.div()
// We might be lucky and find an MD5 with info
+ html.linkToSearchGoogle(
"%22"+ file.tagFileChecksumMD5.toString() + "%22" )
+ html._div;
}
String resultSSDEEP = "";
if(file.tagFileChecksumSSDEEP!= null){
String text = file.tagFileChecksumSSDEEP.raw;
// remove the tag header
text = text.replace("FileChecksum: SSDEEP: ", "");
resultSSDEEP = text
+ html.div()
// only possible when we have SHA256 hashes available
+ html.linkSearch("Find similar files", "SSDEEP: "
+ text)
+ html._div;
}
String result = html.div()
+ resultIntroduction
+ html.br
+ swingUtils.addIfNotEmpty("SHA1", resultSHA1)
+ swingUtils.addIfNotEmpty("SHA256", resultSHA256)
+ swingUtils.addIfNotEmpty("MD5",resultMD5)
//+ html.br
+ swingUtils.addIfNotEmpty("SSDEEP", resultSSDEEP)
+ swingUtils.addIfNotEmpty("Look for \""
+filename
+"\" in search engines"
, resultFilename)
+ html._div
// + swingUtils.addSSDEEP("SSDEEP", file)
// + swingUtils.addIfNotEmpty("Copyright text",file.tagFileCopyrightText.toString())
;
/** missing to add:
* - File path
* - Applicable licenses (when available)
*/
core.studio.editorPane(is.contentHTML, Boolean.FALSE, 0, result);
}
}