1 package club.wpia.gigi.ping;
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
8 import club.wpia.gigi.database.DatabaseConnection;
9 import club.wpia.gigi.database.GigiPreparedStatement;
10 import club.wpia.gigi.database.GigiResultSet;
11 import club.wpia.gigi.database.DatabaseConnection.Link;
12 import club.wpia.gigi.dbObjects.Domain;
13 import club.wpia.gigi.dbObjects.DomainPingConfiguration;
14 import club.wpia.gigi.dbObjects.DomainPingType;
15 import club.wpia.gigi.util.RandomToken;
17 public class PingerDaemon extends Thread {
19 HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
21 private GigiPreparedStatement searchNeededPings;
23 private KeyStore truststore;
25 private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
27 public PingerDaemon(KeyStore truststore) {
28 this.truststore = truststore;
33 try (Link l = DatabaseConnection.newLink(false)) {
35 } catch (InterruptedException e) {
40 public void runWithConnection() {
41 searchNeededPings = new GigiPreparedStatement("SELECT `pc`.`id`" //
42 + " FROM `pingconfig` AS `pc`" //
43 + " INNER JOIN `domains` AS `d` ON `d`.`id` = `pc`.`domainid`" //
44 + " WHERE `d`.`deleted` IS NULL" //
45 + " AND `pc`.`deleted` IS NULL" //
46 + " AND NOT EXISTS (" //
47 + " SELECT 1 FROM `domainPinglog` AS `dpl`" //
48 + " WHERE `dpl`.`configId` = `pc`.`id`" //
49 + " AND `dpl`.`when` >= CURRENT_TIMESTAMP - interval '6 mons')");
50 pingers.put(DomainPingType.EMAIL, new EmailPinger());
51 pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
52 pingers.put(DomainPingType.HTTP, new HTTPFetch());
53 pingers.put(DomainPingType.DNS, new DNSPinger());
57 boolean worked = false;
59 DomainPingConfiguration conf;
60 while ((conf = toExecute.peek()) != null) {
68 GigiResultSet rs = searchNeededPings.executeQuery();
71 handle(DomainPingConfiguration.getById(rs.getInt("id")));
77 } catch (InterruptedException e) {
79 } catch (Throwable t) {
83 } catch (InterruptedException e) {
89 private void handle(DomainPingConfiguration conf) {
90 DomainPingType type = conf.getType();
91 String config = conf.getInfo();
92 DomainPinger dp = pingers.get(type);
94 if (dp instanceof EmailPinger) {
96 token = RandomToken.generateToken(16);
97 config = config + ":" + token;
99 Domain target = conf.getTarget();
100 System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
102 dp.ping(target, config, target.getOwner(), conf.getId());
103 } catch (Throwable t) {
105 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
107 System.err.println("done (" + System.currentTimeMillis() + ")");
111 public synchronized void queue(DomainPingConfiguration toReping) {
113 toExecute.add(toReping);
114 while (toExecute.size() > 0) {
117 } catch (InterruptedException e) {