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
|
package com.geedge.scheduler;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import com.geedge.common.constant.TsgObject;
import com.geedge.common.util.TsgUtil;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Counter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
/**
* TODO
*
* @Classname Scheduler
* @Date 2024/1/2 11:31
* @Author wWei
*/
@Slf4j
@Component
public class FqdnScheduler {
@Value("${tsg.object.fqdn.cyberghostvpn_servername.id}")
private Integer cyberghostvpnServernameId;
@Value("${tsg.object.fqdn.cyberghostvpn_servername.name}")
private String cyberghostvpnServernameName;
@Value("${tsg.object.fqdn.cyberghostvpn_servername.update.enable}")
private Boolean cyberghostvpnServernameUpdateEnable;
@Value("${tsg.object.fqdn.cyberghostvpn_servername.update.sql}")
private String cyberghostvpnServernameSql;
@Value("${tsg.object.fqdn.cyberghostvpn_servername.delete.enable}")
private Boolean cyberghostvpnServernameDeleteEnable;
@Value("${tsg.object.fqdn.cyberghostvpn_servername.delete.offsetSecond}")
private Integer cyberghostvpnServernameOffsetSecond;
public Counter cyberghostvpnServernameCounter = Counter.build("cyberghostvpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry);
@Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.update.cron}")
public void updateCyberghostvpn() {
executeUpdate(cyberghostvpnServernameId, cyberghostvpnServernameName, cyberghostvpnServernameUpdateEnable, cyberghostvpnServernameSql, cyberghostvpnServernameCounter);
}
@Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.delete.cron}")
public void deleteCyberghostvpn() {
executeDelete(cyberghostvpnServernameId, cyberghostvpnServernameDeleteEnable, cyberghostvpnServernameOffsetSecond);
}
@Value("${tsg.object.fqdn.ipvanishvpn_servername.id}")
private Integer ipvanishvpnServernameId;
@Value("${tsg.object.fqdn.ipvanishvpn_servername.name}")
private String ipvanishvpnServernameName;
@Value("${tsg.object.fqdn.ipvanishvpn_servername.update.enable}")
private Boolean ipvanishvpnServernameUpdateEnable;
@Value("${tsg.object.fqdn.ipvanishvpn_servername.update.sql}")
private String ipvanishvpnServernameSql;
@Value("${tsg.object.fqdn.ipvanishvpn_servername.delete.enable}")
private Boolean ipvanishvpnServernameDeleteEnable;
@Value("${tsg.object.fqdn.ipvanishvpn_servername.delete.offsetSecond}")
private Integer ipvanishvpnServernameOffsetSecond;
public Counter ipvanishvpnServernameCounter = Counter.build("ipvanishvpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry);
@Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.update.cron}")
public void updateIpvanishvpn() {
executeUpdate(ipvanishvpnServernameId, ipvanishvpnServernameName, ipvanishvpnServernameUpdateEnable, ipvanishvpnServernameSql, ipvanishvpnServernameCounter);
}
@Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.delete.cron}")
public void deleteIpvanishvpn() {
executeDelete(ipvanishvpnServernameId, ipvanishvpnServernameDeleteEnable, ipvanishvpnServernameOffsetSecond);
}
@Value("${tsg.object.fqdn.ivacyvpn_servername.id}")
private Integer ivacyvpnServernameId;
@Value("${tsg.object.fqdn.ivacyvpn_servername.name}")
private String ivacyvpnServernameName;
@Value("${tsg.object.fqdn.ivacyvpn_servername.update.enable}")
private Boolean ivacyvpnServernameUpdateEnable;
@Value("${tsg.object.fqdn.ivacyvpn_servername.update.sql}")
private String ivacyvpnServernameSql;
@Value("${tsg.object.fqdn.ivacyvpn_servername.delete.enable}")
private Boolean ivacyvpnServernameDeleteEnable;
@Value("${tsg.object.fqdn.ivacyvpn_servername.delete.offsetSecond}")
private Integer ivacyvpnServernameOffsetSecond;
public Counter ivacyvpnServernameCounter = Counter.build("ivacyvpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry);
@Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.update.cron}")
public void updateIvacyvpn() {
executeUpdate(ivacyvpnServernameId, ivacyvpnServernameName, ivacyvpnServernameUpdateEnable, ivacyvpnServernameSql, ivacyvpnServernameCounter);
}
@Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.delete.cron}")
public void deleteIvacyvpn() {
executeDelete(ivacyvpnServernameId, ivacyvpnServernameDeleteEnable, ivacyvpnServernameOffsetSecond);
}
@Value("${tsg.object.fqdn.vpnunlimited_servername.id}")
private Integer vpnunlimitedServernameId;
@Value("${tsg.object.fqdn.vpnunlimited_servername.name}")
private String vpnunlimitedServernameName;
@Value("${tsg.object.fqdn.vpnunlimited_servername.update.enable}")
private Boolean vpnunlimitedServernameUpdateEnable;
@Value("${tsg.object.fqdn.vpnunlimited_servername.update.sql}")
private String vpnunlimitedServernameSql;
@Value("${tsg.object.fqdn.vpnunlimited_servername.delete.enable}")
private Boolean vpnunlimitedServernameDeleteEnable;
@Value("${tsg.object.fqdn.vpnunlimited_servername.delete.offsetSecond}")
private Integer vpnunlimitedServernameOffsetSecond;
public Counter vpnunlimitedServernameCounter = Counter.build("vpnunlimited_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry);
@Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.update.cron}")
public void updateVpnunlimited() {
executeUpdate(vpnunlimitedServernameId, vpnunlimitedServernameName, vpnunlimitedServernameUpdateEnable, vpnunlimitedServernameSql, vpnunlimitedServernameCounter);
}
@Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.delete.cron}")
public void deleteVpnunlimited() {
executeDelete(vpnunlimitedServernameId, vpnunlimitedServernameDeleteEnable, vpnunlimitedServernameOffsetSecond);
}
@Value("${tsg.object.fqdn.vpn4fame_servername.id}")
private Integer vpn4fameServernameId;
@Value("${tsg.object.fqdn.vpn4fame_servername.name}")
private String vpn4fameServernameName;
@Value("${tsg.object.fqdn.vpn4fame_servername.update.enable}")
private Boolean vpn4fameServernameUpdateEnable;
@Value("${tsg.object.fqdn.vpn4fame_servername.update.sql}")
private String vpn4fameServernameSql;
@Value("${tsg.object.fqdn.vpn4fame_servername.delete.enable}")
private Boolean vpn4fameServernameDeleteEnable;
@Value("${tsg.object.fqdn.vpn4fame_servername.delete.offsetSecond}")
private Integer vpn4fameServernameOffsetSecond;
public Counter vpn4fameServernameCounter = Counter.build("vpn4fame_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry);
@Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.update.cron}")
public void updateVpn4fame() {
executeUpdate(vpn4fameServernameId, vpn4fameServernameName, vpn4fameServernameUpdateEnable, vpn4fameServernameSql, vpn4fameServernameCounter);
}
@Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.delete.cron}")
public void deleteVpn4fame() {
executeDelete(vpn4fameServernameId, vpn4fameServernameDeleteEnable, vpn4fameServernameOffsetSecond);
}
private static void executeUpdate(Integer id, String name, Boolean enable, String sql, Counter counter) {
log.info("{}: started update task.", id);
if (BooleanUtil.isFalse(enable)) {
log.warn("{}: interrupted update task. enable: {}", id, enable);
return;
}
try {
Stopwatch watch = Stopwatch.createStarted();
List<Record> data = Db.find(sql);
log.info("{}: query knowledge base content, cost {} seconds", id, watch.elapsed(TimeUnit.SECONDS));
watch.reset().start();
List<Map<String, Object>> items = Lists.newArrayList();
for (Record record : data) {
Map<String, Object> item = Maps.newHashMap();
String domain = record.get("domain");
List<Map<String, Object>> patterns = Lists.newArrayList();
Map<String, Object> pattern = Maps.newHashMap();
pattern.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS_KEYWORDS, domain);
patterns.add(pattern);
Map<String, Object> str = Maps.newHashMap();
str.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_PATTERNS, patterns);
str.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_EXPR_TYPE, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_STRING_EXPR_TYPE_0);
str.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING_IS_HEXBIN, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_STRING_IS_HEXBIN_0);
item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_OP, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_OP_ADD);
item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_STRING, str);
items.add(item);
}
Map<String, Object> member = Maps.newHashMap();
member.put(TsgObject.KEY_OBJECT_MEMBER_TYPE, TsgObject.VALUE_OBJECT_MEMBER_TYPE_1);
member.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS, items);
Map<String, Object> obj = Maps.newHashMap();
obj.put(TsgObject.KEY_OBJECT_NAME, name);
obj.put(TsgObject.KEY_OBJECT_TYPE, TsgObject.VALUE_OBJECT_TYPE_FQDN);
obj.put(TsgObject.KEY_OBJECT_MEMBER, member);
Map<String, Object> body = Maps.newHashMap();
body.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1);
body.put(TsgObject.KEY_OBJECT, obj);
log.info("{}: build api params, items size: {}, cost {} seconds", id, items.size(), watch.elapsed(TimeUnit.SECONDS));
if (items.isEmpty()) {
return;
}
TsgUtil.updateObjectById(id, body);
counter.inc(items.size());
} catch (Exception e) {
log.error("{}: failed to execute update task. message: {}", id, e.getMessage());
throw new RuntimeException(e);
}
}
private static void executeDelete(Integer id, Boolean enable, Integer offsetSecond) {
log.info("{}: started delete task.", id);
if (BooleanUtil.isFalse(enable)) {
log.warn("{}: interrupted delete task. enable: {}", id, enable);
return;
}
try {
DateTime dateTime = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC"));
String datetimeZ = DateUtil.format(dateTime, DatePattern.UTC_PATTERN);
Map<String, Object> form = Maps.newHashMap();
form.put(TsgObject.KEY_OBJECT_TYPE, TsgObject.VALUE_OBJECT_TYPE_FQDN);
form.put(TsgObject.KEY_VSYS_ID, TsgObject.VALUE_VSYS_ID_1);
form.put(TsgObject.KEY_CREATED_BEFORE, datetimeZ);
TsgUtil.deleteItemOfObjectById(id, form);
} catch (Exception e) {
log.error("{}: failed to execute delete task. message: {}", id, e.getMessage());
throw new RuntimeException(e);
}
}
}
|