-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogViewer.java
More file actions
203 lines (180 loc) · 6.35 KB
/
LogViewer.java
File metadata and controls
203 lines (180 loc) · 6.35 KB
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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LogViewer {
private static final String MESSAGE_LOG_FILE = "encrypted_messages.txt";
public static void main(String[] args) {
System.out.println("\n\t\tEncrypted Message Log Viewer");
System.out.println("\t\t============================\n");
try {
if (!Files.exists(Paths.get(MESSAGE_LOG_FILE))) {
System.out.println("No log file found. Start the server and send some messages first.");
return;
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Options:");
System.out.println("1. View all logs");
System.out.println("2. View logs by sender");
System.out.println("3. View logs by receiver");
System.out.println("4. View recent logs (last 10)");
System.out.println("5. Search logs");
System.out.println("6. View encryption type statistics");
System.out.println("7. Exit");
System.out.print("Choose option: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
viewAllLogs();
break;
case "2":
System.out.print("Enter sender username: ");
String sender = scanner.nextLine();
viewLogsBySender(sender);
break;
case "3":
System.out.print("Enter receiver username: ");
String receiver = scanner.nextLine();
viewLogsByReceiver(receiver);
break;
case "4":
viewRecentLogs(10);
break;
case "5":
System.out.print("Enter search term: ");
String searchTerm = scanner.nextLine();
searchLogs(searchTerm);
break;
case "6":
viewEncryptionStats();
break;
case "7":
System.out.println("Goodbye!");
return;
default:
System.out.println("Invalid option. Please try again.\n");
}
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
System.out.println();
}
} catch (Exception e) {
System.err.println("Error reading log file: " + e.getMessage());
}
}
private static void viewAllLogs() throws IOException {
List<String> lines = Files.readAllLines(Paths.get(MESSAGE_LOG_FILE));
System.out.println("\n=== All Encrypted Message Logs ===");
for (String line : lines) {
System.out.println(line);
}
}
private static void viewLogsBySender(String sender) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(MESSAGE_LOG_FILE));
System.out.println("\n=== Messages from " + sender + " ===");
boolean inRelevantEntry = false;
for (String line : lines) {
if (line.contains("FROM: " + sender)) {
inRelevantEntry = true;
}
if (inRelevantEntry) {
System.out.println(line);
if (line.contains("----------------------------------------")) {
inRelevantEntry = false;
}
}
}
}
private static void viewLogsByReceiver(String receiver) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(MESSAGE_LOG_FILE));
System.out.println("\n=== Messages to " + receiver + " ===");
boolean inRelevantEntry = false;
for (String line : lines) {
if (line.contains("TO: " + receiver)) {
inRelevantEntry = true;
}
if (inRelevantEntry) {
System.out.println(line);
if (line.contains("----------------------------------------")) {
inRelevantEntry = false;
}
}
}
}
private static void viewRecentLogs(int count) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(MESSAGE_LOG_FILE));
System.out.println("\n=== Recent " + count + " Messages ===");
// Find message entries (look for timestamp lines)
List<String> messageEntries = new ArrayList<>();
List<String> currentEntry = new ArrayList<>();
for (String line : lines) {
if (line.matches("\\[.*\\] FROM:.*")) {
// Start of new message entry
if (!currentEntry.isEmpty()) {
messageEntries.add(String.join("\n", currentEntry));
}
currentEntry.clear();
}
currentEntry.add(line);
}
// Add the last entry
if (!currentEntry.isEmpty()) {
messageEntries.add(String.join("\n", currentEntry));
}
// Show last 'count' entries
int start = Math.max(0, messageEntries.size() - count);
for (int i = start; i < messageEntries.size(); i++) {
System.out.println(messageEntries.get(i));
System.out.println();
}
}
private static void searchLogs(String searchTerm) throws IOException {
List<String> lines = Files.readAllLines(Paths.get(MESSAGE_LOG_FILE));
System.out.println("\n=== Search Results for '" + searchTerm + "' ===");
boolean inRelevantEntry = false;
List<String> currentEntry = new ArrayList<>();
for (String line : lines) {
if (line.matches("\\[.*\\] FROM:.*")) {
// Check if previous entry contained search term
if (inRelevantEntry) {
for (String entryLine : currentEntry) {
System.out.println(entryLine);
}
System.out.println();
}
// Start new entry
currentEntry.clear();
inRelevantEntry = false;
}
currentEntry.add(line);
if (line.toLowerCase().contains(searchTerm.toLowerCase())) {
inRelevantEntry = true;
}
}
// Check last entry
if (inRelevantEntry) {
for (String entryLine : currentEntry) {
System.out.println(entryLine);
}
}
}
private static void viewEncryptionStats() throws IOException {
List<String> lines = Files.readAllLines(Paths.get(MESSAGE_LOG_FILE));
int rsaCount = 0;
int aesCount = 0;
for (String line : lines) {
if (line.contains("SIGNATURE:")) {
rsaCount++;
} else if (line.contains("AES-ENCRYPTED")) {
aesCount++;
}
}
System.out.println("\n=== Encryption Statistics ===");
System.out.println("RSA encrypted messages: " + rsaCount);
System.out.println("AES encrypted messages (PFS): " + aesCount);
System.out.println("Total messages: " + (rsaCount + aesCount));
}
}