summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/sketch/entity/DomainWhois.java
blob: 55679a8063c931f40b161e5d1bca6dc8dd0c0d69 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package com.mesasoft.cn.sketch.entity;

import com.alibaba.fastjson.JSONObject;
import com.mesasoft.cn.sketch.config.ApplicationConfig;
import com.mesasoft.cn.util.ConfigUtils;
import com.mesasoft.cn.util.MariaDbBase;
import com.mesasoft.cn.util.ValidationUtils;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: joy
 * Date: 2021/12/29
 * Time: 9:27 AM
 * Description: No Description
 */
public class DomainWhois {
    private static final String dataBase = ApplicationConfig.DATABASE;
    private static final String tableName = ApplicationConfig.DOMAIN_WHOIS_TABLENAME;

    private String fqdn;
    private String source;
    private Boolean query_success;
    private Integer match_pattern;
    private String whois_domain;
    private Timestamp whois_update_date;
    private Timestamp whois_create_date;
    private Timestamp whois_expire_date;
    private String whois_email;
    private String whois_ns;
    private String whois_registrar_name;
    private String whois_registrant_org;
    private String whois_registrant_name;
    private String whois_registrant_street;
    private String whois_registrant_city;
    private String whois_registrant_state;
    private String whois_registrant_postcode;
    private String whois_registrant_country;
    private String whois_registrant_phone;


    // category schema
    public DomainWhois(String fqdn,
                       String source,
                       Integer match_pattern,
                       Boolean query_success,
                       String whoisDomain,
                       Date whoisUpdateDate,
                       Date whoisCreateDate,
                       Date whoisExpireDate,
                       String whoisEmail,
                       String whoisNs,
                       String whoisRegistrarName,
                       String whoisRegistrantOrg,
                       String whoisRegistrantName,
                       String whoisRegistrantStreet,
                       String whoisRegistrantCity,
                       String whoisRegistrantState,
                       String whoisRegistrantPostcode,
                       String whoisRegistrantCountry,
                       String whoisRegistrantPhone
    ) {

        this.fqdn = fqdn;
        this.source = source;

        // 没有设置match_pattern,则二级域名为右匹配,其余为全匹配
        if (match_pattern == null) {
            this.match_pattern = ValidationUtils.getMatchPattern(fqdn);
        } else {
            this.match_pattern = match_pattern;
        }

        this.query_success = query_success;
        this.whois_domain = ConfigUtils.getEffectiveString(whoisDomain);
        this.whois_update_date = whoisUpdateDate == null ? null : new Timestamp(whoisUpdateDate.getTime());
        this.whois_create_date = whoisCreateDate == null ? null : new Timestamp(whoisCreateDate.getTime());
        this.whois_expire_date = whoisExpireDate == null ? null : new Timestamp(whoisExpireDate.getTime());
        this.whois_email = ConfigUtils.getEffectiveString(whoisEmail);
        this.whois_ns = ConfigUtils.getEffectiveString(whoisNs);
        this.whois_registrar_name = ConfigUtils.getEffectiveString(whoisRegistrarName);
        this.whois_registrant_org = ConfigUtils.getEffectiveString(whoisRegistrantOrg);
        this.whois_registrant_name = ConfigUtils.getEffectiveString(whoisRegistrantName);
        this.whois_registrant_street = ConfigUtils.getEffectiveString(whoisRegistrantStreet);
        this.whois_registrant_city = ConfigUtils.getEffectiveString(whoisRegistrantCity);
        this.whois_registrant_state = ConfigUtils.getEffectiveString(whoisRegistrantState);
        this.whois_registrant_postcode = ConfigUtils.getEffectiveString(whoisRegistrantPostcode);
        this.whois_registrant_country = ConfigUtils.getEffectiveString(whoisRegistrantCountry);
        this.whois_registrant_phone = ConfigUtils.getEffectiveString(whoisRegistrantPhone);
    }

    public static void insertRecords(List<DomainWhois> whoisFiles, MariaDbBase mariaDbBase) {
        for (DomainWhois whoisFile : whoisFiles) {
            // 生成sql
            String resSql = "INSERT INTO " + dataBase + "." + tableName + ' ' +
                    " (" + whoisFile.getKeys() + ") values" +
                    '(' + whoisFile.getValues() + ')';
            resSql = resSql.replace("'null'", "null");

            mariaDbBase.writeSqlExecute(resSql);
        }
    }

    public static String insertSql(List<DomainWhois> whoisFiles) {
        DomainWhois whoisFile = whoisFiles.get(0);
        // 生成sql
        String resSql = "INSERT INTO " + dataBase + "." + tableName + ' ' +
                " (" + whoisFile.getKeys() + ") values" +
                '(' + whoisFile.getValues() + ')';
        resSql = resSql.replace("'null'", "null");

        return resSql;
    }

    public static void updateRecords(List<DomainWhois> categoryFiles, MariaDbBase mariaDbBase) {
        for (DomainWhois categoryFile : categoryFiles) {

            String resSql = "UPDATE " + dataBase + "." +
                    tableName + ' ' +
                    "SET " + categoryFile.getKeyValues() +
                    ", update_time = current_time() " +
                    " WHERE fqdn = '" + categoryFile.getFqdn() + '\'';
            resSql = resSql.replace("'null'", "null");

            mariaDbBase.writeSqlExecute(resSql);
        }
    }

    public static List<DomainWhois> getDbRecord(List<String> fqdns, MariaDbBase mariaDbBase, String source) throws SQLException {
        String queryFqdns = fqdns.stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
        String sql = "SELECT * FROM " + dataBase + "." +
                tableName + ' ' +
                " WHERE fqdn in (" + queryFqdns + ") ";

        return rs2schema(mariaDbBase.querySqlExecute(sql));
    }

    public String getValues() {
        String resString = "'" + fqdn + '\'' +
                ", '" + source + '\'' +
                ", " + query_success +
                ", " + match_pattern +
                ", '" + whois_domain + '\'' +
                ", '" + whois_update_date + '\'' +
                ", '" + whois_create_date + '\'' +
                ", '" + whois_expire_date + '\'' +
                ", '" + whois_email + '\'' +
                ", '" + whois_ns + '\'' +
                ", '" + whois_registrar_name + '\'' +
                ", '" + whois_registrant_org + '\'' +
                ", '" + whois_registrant_name + '\'' +
                ", '" + whois_registrant_street + '\'' +
                ", '" + whois_registrant_city + '\'' +
                ", '" + whois_registrant_state + '\'' +
                ", '" + whois_registrant_postcode + '\'' +
                ", '" + whois_registrant_country + '\'' +
                ", '" + whois_registrant_phone + '\'';

        return resString.replace("'null'", "null");
    }

    public static List<DomainWhois> rs2schema(ResultSet rs) throws SQLException {
        List<DomainWhois> schemaFiles = new ArrayList<>();
        while (rs.next()) {
            schemaFiles.add(
                    new DomainWhois(
                            rs.getString("fqdn"),
                            rs.getString("source"),
                            rs.getInt("match_pattern"),
                            rs.getBoolean("query_success"),
                            rs.getString("whois_domain"),
                            (Date) rs.getDate("whois_update_date"),
                            (Date) rs.getDate("whois_create_date"),
                            (Date) rs.getDate("whois_expire_date"),
                            rs.getString("whois_email"),
                            rs.getString("whois_ns"),
                            rs.getString("whois_registrar_name"),
                            rs.getString("whois_registrant_org"),
                            rs.getString("whois_registrant_name"),
                            rs.getString("whois_registrant_street"),
                            rs.getString("whois_registrant_city"),
                            rs.getString("whois_registrant_state"),
                            rs.getString("whois_registrant_postcode"),
                            rs.getString("whois_registrant_country"),
                            rs.getString("whois_registrant_phone")
                    ));
        }
        return schemaFiles;
    }

    public static JSONObject schema2json(DomainWhois schema) throws SQLException {
        JSONObject jsonObject = new JSONObject(true);
        jsonObject.put("fqdn", schema.getFqdn());
        jsonObject.put("source", schema.getSource());
        jsonObject.put("match_pattern", schema.getMatch_pattern());
        jsonObject.put("query_success", schema.getQuery_success());
        jsonObject.put("whois_domain", schema.getWhois_domain());
        jsonObject.put("whois_update_date", schema.getWhois_update_date());
        jsonObject.put("whois_create_date", schema.getWhois_create_date());
        jsonObject.put("whois_expire_date", schema.getWhois_expire_date());
        jsonObject.put("whois_email", schema.getWhois_email());
        jsonObject.put("whois_ns", schema.getWhois_ns());
        jsonObject.put("whois_registrar_name", schema.getWhois_registrar_name());
        jsonObject.put("whois_registrant_org", schema.getWhois_registrant_org());
        jsonObject.put("whois_registrant_name", schema.getWhois_registrant_name());
        jsonObject.put("whois_registrant_street", schema.getWhois_registrant_street());
        jsonObject.put("whois_registrant_city", schema.getWhois_registrant_city());
        jsonObject.put("whois_registrant_state", schema.getWhois_registrant_state());
        jsonObject.put("whois_registrant_postcode", schema.getWhois_registrant_postcode());
        jsonObject.put("whois_registrant_country", schema.getWhois_registrant_country());
        jsonObject.put("whois_registrant_phone", schema.getWhois_registrant_phone());

        return jsonObject;
    }

    public String getKeys() {
        String resString;
        resString = "fqdn" +
                ", source" +
                ", query_success" +
                ", match_pattern" +
                ", whois_domain" +
                ", whois_update_date" +
                ", whois_create_date" +
                ", whois_expire_date" +
                ", whois_email" +
                ", whois_ns" +
                ", whois_registrar_name" +
                ", whois_registrant_org" +
                ", whois_registrant_name" +
                ", whois_registrant_street" +
                ", whois_registrant_city" +
                ", whois_registrant_state" +
                ", whois_registrant_postcode" +
                ", whois_registrant_country" +
                ", whois_registrant_phone";
        return resString;
    }

    public String getKeyValues() {
        String resString = "query_success=" + query_success +
                ", source='" + source + '\'' +
                ", match_pattern=" + match_pattern +
                ", whois_domain='" + whois_domain + '\'' +
                ", whois_update_date='" + whois_update_date + '\'' +
                ", whois_create_date='" + whois_create_date + '\'' +
                ", whois_expire_date='" + whois_expire_date + '\'' +
                ", whois_email='" + whois_email + '\'' +
                ", whois_ns='" + whois_ns + '\'' +
                ", whois_registrar_name='" + whois_registrar_name + '\'' +
                ", whois_registrant_org='" + whois_registrant_org + '\'' +
                ", whois_registrant_name='" + whois_registrant_name + '\'' +
                ", whois_registrant_street='" + whois_registrant_street + '\'' +
                ", whois_registrant_city='" + whois_registrant_city + '\'' +
                ", whois_registrant_state='" + whois_registrant_state + '\'' +
                ", whois_registrant_postcode='" + whois_registrant_postcode + '\'' +
                ", whois_registrant_country='" + whois_registrant_country + '\'' +
                ", whois_registrant_phone='" + whois_registrant_phone + '\'';

        return resString.replace("'null'", "null");
    }


    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getFqdn() {
        return fqdn;
    }

    public void setFqdn(String fqdn) {
        this.fqdn = fqdn;
    }

    public Boolean getQuery_success() {
        return query_success;
    }

    public void setQuery_success(Boolean query_success) {
        this.query_success = query_success;
    }

    public Integer getMatch_pattern() {
        return match_pattern;
    }

    public void setMatch_pattern(Integer match_pattern) {
        this.match_pattern = match_pattern;
    }

    public String getWhois_domain() {
        return whois_domain;
    }

    public void setWhois_domain(String whois_domain) {
        this.whois_domain = whois_domain;
    }

    public Timestamp getWhois_update_date() {
        return whois_update_date;
    }

    public void setWhois_update_date(Timestamp whois_update_date) {
        this.whois_update_date = whois_update_date;
    }

    public Timestamp getWhois_create_date() {
        return whois_create_date;
    }

    public void setWhois_create_date(Timestamp whois_create_date) {
        this.whois_create_date = whois_create_date;
    }

    public Timestamp getWhois_expire_date() {
        return whois_expire_date;
    }

    public void setWhois_expire_date(Timestamp whois_expire_date) {
        this.whois_expire_date = whois_expire_date;
    }

    public String getWhois_email() {
        return whois_email;
    }

    public void setWhois_email(String whois_email) {
        this.whois_email = whois_email;
    }

    public String getWhois_ns() {
        return whois_ns;
    }

    public void setWhois_ns(String whois_ns) {
        this.whois_ns = whois_ns;
    }

    public String getWhois_registrar_name() {
        return whois_registrar_name;
    }

    public void setWhois_registrar_name(String whois_registrar_name) {
        this.whois_registrar_name = whois_registrar_name;
    }

    public String getWhois_registrant_org() {
        return whois_registrant_org;
    }

    public void setWhois_registrant_org(String whois_registrant_org) {
        this.whois_registrant_org = whois_registrant_org;
    }

    public String getWhois_registrant_name() {
        return whois_registrant_name;
    }

    public void setWhois_registrant_name(String whois_registrant_name) {
        this.whois_registrant_name = whois_registrant_name;
    }

    public String getWhois_registrant_street() {
        return whois_registrant_street;
    }

    public void setWhois_registrant_street(String whois_registrant_street) {
        this.whois_registrant_street = whois_registrant_street;
    }

    public String getWhois_registrant_city() {
        return whois_registrant_city;
    }

    public void setWhois_registrant_city(String whois_registrant_city) {
        this.whois_registrant_city = whois_registrant_city;
    }

    public String getWhois_registrant_state() {
        return whois_registrant_state;
    }

    public void setWhois_registrant_state(String whois_registrant_state) {
        this.whois_registrant_state = whois_registrant_state;
    }

    public String getWhois_registrant_postcode() {
        return whois_registrant_postcode;
    }

    public void setWhois_registrant_postcode(String whois_registrant_postcode) {
        this.whois_registrant_postcode = whois_registrant_postcode;
    }

    public String getWhois_registrant_country() {
        return whois_registrant_country;
    }

    public void setWhois_registrant_country(String whois_registrant_country) {
        this.whois_registrant_country = whois_registrant_country;
    }

    public String getWhois_registrant_phone() {
        return whois_registrant_phone;
    }

    public void setWhois_registrant_phone(String whois_registrant_phone) {
        this.whois_registrant_phone = whois_registrant_phone;
    }
}