-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
222 lines (193 loc) · 8.32 KB
/
app.js
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
const { createApp, ref, computed, onMounted, nextTick } = Vue
createApp({
setup() {
const matrixEnabled = ref(false)
const searchQuery = ref('')
const allMalware = ref([])
const malware = ref([])
const selectedCategory = ref('')
const errorMessage = ref('')
const selectedMalware = ref(null)
const isLoading = ref(true)
const toggleMatrix = () => {
matrixEnabled.value = !matrixEnabled.value
const canvas = document.getElementById('matrixCanvas')
canvas.style.display = matrixEnabled.value ? 'block' : 'none'
if (matrixEnabled.value) {
startMatrix()
}
};
const categories = computed(() => {
const categoryCounts = allMalware.value.reduce((acc, malware) => {
acc[malware.category] = (acc[malware.category] || 0) + 1;
return acc;
}, {});
return Object.keys(categoryCounts).map(category => ({
name: category,
count: categoryCounts[category]
}));
});
const loadMalware = async () => {
try {
// Check if cached data exists and is less than 1 hour old
const cachedData = localStorage.getItem('cachedMalwareData');
const cachedTimestamp = localStorage.getItem('cachedMalwareTimestamp');
if (cachedData && cachedTimestamp) {
const currentTime = new Date().getTime();
const cacheAge = currentTime - parseInt(cachedTimestamp);
if (cacheAge < 3600000) { // 1 hour in milliseconds
allMalware.value = JSON.parse(cachedData);
malware.value = allMalware.value;
isLoading.value = false;
return;
}
}
// If no valid cache, fetch from GitHub
const repoContentsResponse = await axios.get('https://api.github.com/repos/0x4F776C/Malware/contents', {
headers: { 'Accept': 'application/vnd.github.v3+json' }
});
const directories = repoContentsResponse.data.filter(item => item.type === 'dir');
const malwareData = await Promise.all(directories.map(async (dir) => {
try {
const dirContentsResponse = await axios.get(dir.url);
const infoFile = dirContentsResponse.data.find(file => file.name === 'info.json');
if (!infoFile) {
console.warn(`No info.json found in ${dir.name}`);
return null;
}
const infoResponse = await axios.get(infoFile.download_url);
const info = infoResponse.data;
// Proceed to fetch files only if code directory exists
const codeDir = dirContentsResponse.data.find(item => item.name === 'code' && item.type === 'dir');
let files = [];
if (codeDir) {
const codeContentsResponse = await axios.get(codeDir.url);
const codeFiles = codeContentsResponse.data.filter(file => /\.(go|py|js|c|cpp|java|rb|php|cs|ts|rs|swift)$/i.test(file.name));
files = await Promise.all(codeFiles.map(async (file) => {
const content = await axios.get(file.download_url);
return { name: file.name, content: content.data };
}));
}
return {
name: info.name || dir.name,
category: info.category,
description: info.description,
references: info.references,
files: files
};
} catch (error) {
console.error(`Error processing ${dir.name}:`, error);
return null;
}
}));
allMalware.value = malwareData.filter(item => item !== null);
malware.value = allMalware.value;
// Cache the fetched data
localStorage.setItem('cachedMalwareData', JSON.stringify(allMalware.value));
localStorage.setItem('cachedMalwareTimestamp', new Date().getTime().toString());
} catch (error) {
console.error('Error fetching malware:', error);
errorMessage.value = 'Failed to load malware data. Please try again later.';
} finally {
isLoading.value = false;
}
};
const visibleMalware = computed(() => {
if (searchQuery.value || selectedCategory.value) {
return malware.value;
} else {
return malware.value.slice(0, 5); // Show only first 5 malware
}
});
const searchMalware = () => {
malware.value = allMalware.value.filter(malware =>
(malware.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
malware.description.toLowerCase().includes(searchQuery.value.toLowerCase())) &&
(selectedCategory.value === '' || malware.category === selectedCategory.value)
);
};
const escapeHtml = (unsafe) => {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
};
const openModal = (malware) => {
selectedMalware.value = malware
};
const closeModal = () => {
selectedMalware.value = null
};
const getLanguage = (fileName) => {
const extension = fileName.split('.').pop().toLowerCase();
const languageMap = {
'go': 'language-go',
'py': 'language-python',
'js': 'language-javascript',
'c': 'language-c',
'cpp': 'language-cpp',
'java': 'language-java',
'rb': 'language-ruby',
'php': 'language-php',
'cs': 'language-csharp',
'ts': 'language-typescript',
'rs': 'language-rust',
'swift': 'language-swift'
};
return languageMap[extension] || 'language-plaintext';
};
const highlightAll = () => {
nextTick(() => {
document.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
});
};
onMounted(() => {
loadMalware()
document.getElementById('matrixToggle').addEventListener('click', toggleMatrix)
highlightAll()
});
return {
searchQuery,
malware,
visibleMalware,
searchMalware,
categories,
selectedCategory,
errorMessage,
selectedMalware,
escapeHtml,
openModal,
closeModal,
matrixEnabled,
toggleMatrix,
getLanguage,
highlightAll,
isLoading
};
},
methods: {
clearSearch() {
this.searchQuery = ''
this.searchMalware()
}
},
updated() {
this.highlightAll()
}
}).mount('#app')
const backToTopButton = document.getElementById("backToTop")
window.onscroll = function() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
backToTopButton.style.display = "block"
} else {
backToTopButton.style.display = "none"
}
}
backToTopButton.onclick = function() {
document.body.scrollTop = 0 // For Safari
document.documentElement.scrollTop = 0 // For Chrome, Firefox, IE and Opera
}