-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleEmails.java
135 lines (114 loc) · 4.42 KB
/
ExampleEmails.java
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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.stomp.Frame;
import io.vertx.ext.stomp.StompClient;
import io.vertx.ext.stomp.StompClientConnection;
import io.vertx.ext.stomp.StompClientOptions;
public class ExampleEmails {
public static final String SERVER = "stream.abusix.net:61612";
public static final boolean SSL = true;
public static final String CREDENTIALS = "<user>:<pass>";
public static final String TOPIC = "<topic>";
public static final boolean WRITE_RAW_EMAILS = false;
public static final SimpleDateFormat OUTPUT_DIR_FORMAT = new SimpleDateFormat("'filtered'/yyyy-MM-dd/HH/");
public static final Map<String, List<String>> ALLOWLIST_FILTERS = new HashMap<>();
public static final Map<String, List<String>> BLOCKLIST_FILTERS = new HashMap<>();
public static final Set<String> NON_EMPTY_FILTERS = new HashSet<>();
static {
/*
Possible fields:
- data_colorcode
- data_origin
- detected_text_language
- source_ip
- source_ip_country_iso
- source_ip_rir
- source_port
*/
// ALLOWLIST_FILTERS.put("data_colorcode", Arrays.asList("black"));
// BLOCKLIST_FILTERS.put("detected_text_language", Arrays.asList("jp", "kr"));
/*
Possible fields:
- email_urls
- data_origin
- email_attachment_content_types
- email_attachment_file_names
- email_attachment_tags
*/
// NON_EMPTY_FILTERS.add("email_urls");
}
public static void onMessage(Frame frame) {
JsonObject json = frame.getBody().toJsonObject();
for (Map.Entry<String, List<String>> entry : ALLOWLIST_FILTERS.entrySet()) {
String field = entry.getKey();
List<String> allowed = entry.getValue();
if (!allowed.contains(json.getString(field))) {
return;
}
}
for (Map.Entry<String, List<String>> entry : BLOCKLIST_FILTERS.entrySet()) {
String field = entry.getKey();
List<String> forbidden = entry.getValue();
if (forbidden.contains(json.getString(field))) {
return;
}
}
for (String field : NON_EMPTY_FILTERS) {
if (json.getString(field).isEmpty()) {
return;
}
}
Path target = Path.of(OUTPUT_DIR_FORMAT.format(new Date()));
if (!target.toFile().exists()) {
target.toFile().mkdirs();
}
try {
if (WRITE_RAW_EMAILS) {
Files.write(target.resolve(json.hashCode() + ".eml"),
Base64.getDecoder().decode(json.getString("original_message_base64_encoded")));
} else {
Files.write(target.resolve(json.hashCode() + ".json"),
json.toString().getBytes());
}
} catch (IOException e) {
System.err.println("Could not write file.");
e.printStackTrace();
}
}
public static void listen() {
StompClientOptions options = new StompClientOptions();
options.setHeartbeat(new JsonObject().put("x", 10000).put("y", 10000));
options.setHost(SERVER.split(":")[0]);
options.setPort(Integer.parseInt(SERVER.split(":")[1]));
options.setSsl(SSL);
options.setLogin(CREDENTIALS.split(":")[0]);
options.setPasscode(CREDENTIALS.split(":")[1]);
StompClient client = StompClient.create(Vertx.vertx(), options);
client.connect(ar -> {
if (ar.succeeded()) {
StompClientConnection conn = ar.result();
Map<String, String> headers = new HashMap<>();
headers.put("id", "1234"); // something unique
// To disable load-balancing (shared subscriptions):
//headers.put("channel", CREDENTIALS.split(":")[0] + "something_unique");
// Subscribe to topic
conn.subscribe(TOPIC, headers, ExampleEmails::onMessage);
}
});
client.close();
}
public static void main(String[] args) {
listen();
}
}