summaryrefslogtreecommitdiff
path: root/src/main/java/com/geedge/scheduler/FqdnScheduler.java
blob: 386aff37a0228d60ff170b8c827dbd2098ffe080 (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
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 cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
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.*;
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.uuid}")
    private String cyberghostvpnServernameUUID;
    @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(cyberghostvpnServernameUUID, cyberghostvpnServernameName, cyberghostvpnServernameUpdateEnable, cyberghostvpnServernameSql, cyberghostvpnServernameCounter);
    }

    @Scheduled(cron = "${tsg.object.fqdn.cyberghostvpn_servername.delete.cron}")
    public void deleteCyberghostvpn() {
        executeDelete(cyberghostvpnServernameUUID, cyberghostvpnServernameDeleteEnable, cyberghostvpnServernameOffsetSecond);
    }

    @Value("${tsg.object.fqdn.ipvanishvpn_servername.uuid}")
    private String ipvanishvpnServernameUUID;
    @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(ipvanishvpnServernameUUID, ipvanishvpnServernameName, ipvanishvpnServernameUpdateEnable, ipvanishvpnServernameSql, ipvanishvpnServernameCounter);
    }

    @Scheduled(cron = "${tsg.object.fqdn.ipvanishvpn_servername.delete.cron}")
    public void deleteIpvanishvpn() {
        executeDelete(ipvanishvpnServernameUUID, ipvanishvpnServernameDeleteEnable, ipvanishvpnServernameOffsetSecond);
    }


    @Value("${tsg.object.fqdn.ivacyvpn_servername.uuid}")
    private String ivacyvpnServernameUUID;
    @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(ivacyvpnServernameUUID, ivacyvpnServernameName, ivacyvpnServernameUpdateEnable, ivacyvpnServernameSql, ivacyvpnServernameCounter);
    }

    @Scheduled(cron = "${tsg.object.fqdn.ivacyvpn_servername.delete.cron}")
    public void deleteIvacyvpn() {
        executeDelete(ivacyvpnServernameUUID, ivacyvpnServernameDeleteEnable, ivacyvpnServernameOffsetSecond);
    }

    @Value("${tsg.object.fqdn.vpnunlimited_servername.uuid}")
    private String vpnunlimitedServernameUUID;
    @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(vpnunlimitedServernameUUID, vpnunlimitedServernameName, vpnunlimitedServernameUpdateEnable, vpnunlimitedServernameSql, vpnunlimitedServernameCounter);
    }

    @Scheduled(cron = "${tsg.object.fqdn.vpnunlimited_servername.delete.cron}")
    public void deleteVpnunlimited() {
        executeDelete(vpnunlimitedServernameUUID, vpnunlimitedServernameDeleteEnable, vpnunlimitedServernameOffsetSecond);
    }

    @Value("${tsg.object.fqdn.vpn4fame_servername.uuid}")
    private String vpn4fameServernameUUID;
    @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(vpn4fameServernameUUID, vpn4fameServernameName, vpn4fameServernameUpdateEnable, vpn4fameServernameSql, vpn4fameServernameCounter);
    }

    @Scheduled(cron = "${tsg.object.fqdn.vpn4fame_servername.delete.cron}")
    public void deleteVpn4fame() {
        executeDelete(vpn4fameServernameUUID, vpn4fameServernameDeleteEnable, vpn4fameServernameOffsetSecond);
    }

    @Value("${tsg.object.fqdn.windscribevpn_servername.uuid}")
    private String windscribevpnServernameUUID;
    @Value("${tsg.object.fqdn.windscribevpn_servername.name}")
    private String windscribevpnServernameName;
    @Value("${tsg.object.fqdn.windscribevpn_servername.update.enable}")
    private Boolean windscribevpnServernameUpdateEnable;
    @Value("${tsg.object.fqdn.windscribevpn_servername.update.sql}")
    private String windscribevpnServernameSql;
    @Value("${tsg.object.fqdn.windscribevpn_servername.delete.enable}")
    private Boolean windscribevpnServernameDeleteEnable;
    @Value("${tsg.object.fqdn.windscribevpn_servername.delete.offsetSecond}")
    private Integer windscribevpnServernameOffsetSecond;
    public Counter windscribevpnServernameCounter = Counter.build("windscribevpn_servername_counter", "COUNTER HELP").register(CollectorRegistry.defaultRegistry);

    @Scheduled(cron = "${tsg.object.fqdn.windscribevpn_servername.update.cron}")
    public void updateWindscribevpn() {
        executeUpdate(windscribevpnServernameUUID, windscribevpnServernameName, windscribevpnServernameUpdateEnable, windscribevpnServernameSql, windscribevpnServernameCounter);
    }

    @Scheduled(cron = "${tsg.object.fqdn.windscribevpn_servername.delete.cron}")
    public void deleteWindscribevpn() {
        executeDelete(windscribevpnServernameUUID, windscribevpnServernameDeleteEnable, windscribevpnServernameOffsetSecond);
    }

    private static void executeUpdate(String uuid, String name, Boolean enable, String sql, Counter counter) {
        log.info("{}: started update task.", uuid);
        if (BooleanUtil.isFalse(enable)) {
            log.warn("{}: interrupted update task. enable: {}", uuid, enable);
            return;
        }
        try {
            Stopwatch watch = Stopwatch.createStarted();
            List<Record> data = Db.find(sql);
            log.info("{}: query knowledge base content, cost {} seconds", uuid, watch.elapsed(TimeUnit.SECONDS));
            watch.reset().start();
            List<Map<String, Object>> items = Lists.newArrayList();
            for (Record record : data) {
                String domain = record.get("domain");
                if (StrUtil.isEmptyIfStr(domain)) {
                    continue;
                }
                Map<String, Object> item = Maps.newHashMap();
                item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_OP, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_OP_ADD);
                item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_EXPR_TYPE, TsgObject.VALUE_OBJECT_MEMBER_ITEMS_EXPR_TYPE);
                if (domain.startsWith("$")) {
                    domain = domain.substring(1);
                    domain = "^" + domain + "$";
                } else if (domain.startsWith("*")) {
                    domain = domain.substring(1);
                    domain = domain + "$";
                } else if (domain.endsWith("*")) {
                    domain = domain.substring(0, domain.length() - 1);
                    domain = "^" + domain;
                } else {
                    domain = "^" + domain + "$";
                }
                item.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS_EXPRESSION, domain);
                items.add(item);
            }
            Map<String, Object> object = Maps.newHashMap();
            object.put(TsgObject.KEY_OBJECT_MEMBER_ITEMS, items);
            object.put(TsgObject.KEY_OBJECT_NAME, name);
            object.put("type", "fqdn");
            object.put("statistics_option", "none");
            object.put("member_type", "item");


            Map<String, Object> body = Maps.newHashMap();
            body.put(TsgObject.KEY_VSYS, TsgObject.VALUE_VSYS_1);
            body.put(TsgObject.KEY_OBJECT, object);
            body.put("op", "update");

            log.info("{}: build api params, items size: {}, cost {} seconds", uuid, items.size(), watch.elapsed(TimeUnit.SECONDS));
            if (items.isEmpty()) {
                return;
            }
            TsgUtil.updateObjectByUUID(TsgObject.VALUE_OBJECT_TYPE_FQDN, uuid, body);
            counter.inc(items.size());
        } catch (Exception e) {
            log.error("{}: failed to execute update task. message: {}", uuid, e.getMessage());
            throw new RuntimeException(e);
        }
    }


    private static void executeDelete(String uuid, Boolean enable, Integer offsetSecond) {
        log.info("{}: started delete task.", uuid);
        if (BooleanUtil.isFalse(enable)) {
            log.warn("{}: interrupted delete task. enable: {}", uuid, enable);
            return;
        }
        try {
            DateTime deleteCursor = DateUtil.offsetSecond(new Date(), offsetSecond).setTimeZone(TimeZone.getTimeZone("UTC"));
            String deleteCursorStr = DateUtil.format(deleteCursor, DatePattern.UTC_PATTERN);

            JSONObject objectItemList = TsgUtil.getObjectItemList(uuid, TsgObject.VALUE_OBJECT_TYPE_FQDN);
            Object data = objectItemList.get("data");
            if (StrUtil.isEmptyIfStr(data)) {
                log.warn("No item does not need to be deleted");
                return;
            }
            Map<String, Object> dataMap = (Map<String, Object>) data;
            Object itemsObj = dataMap.get("list");
            if (StrUtil.isEmptyIfStr(itemsObj)) {
                log.warn("No item does not need to be deleted");
                return;
            }
            List<Map<String, Object>> itemList = (List<Map<String, Object>>) itemsObj;
            if (itemList.isEmpty()) {
                log.warn("No item does not need to be deleted");
                return;
            }
            Map<String, Object> map = itemList.get(0);
            if (StrUtil.isEmptyIfStr(map)) {
                log.warn("No item does not need to be deleted");
                return;
            }
            Object createdTimeObj = map.get("created_time");
            DateTime latestCreatedDateTime = DateUtil.parse(createdTimeObj.toString());
            int compare = DateUtil.compare(deleteCursor, latestCreatedDateTime);
            if (compare > 0) {
                log.warn("Does not need to be deleted, avoid item to empty");
                return;
            }

            Map<String, Object> form = Maps.newHashMap();
            form.put(TsgObject.KEY_VSYS, TsgObject.VALUE_VSYS_1);
            form.put(TsgObject.KEY_CREATED_BEFORE, deleteCursorStr);
            TsgUtil.deleteItemOfObjectByUUID(TsgObject.VALUE_OBJECT_TYPE_FQDN, uuid, form);
        } catch (Exception e) {
            log.error("{}: failed to execute delete task. message: {}", uuid, e.getMessage());
            throw new RuntimeException(e);
        }
    }


}