]> WPIA git - gigi.git/blob - src/club/wpia/gigi/ping/PingerDaemon.java
Merge "add: show more certificates on the "roots" page"
[gigi.git] / src / club / wpia / gigi / ping / PingerDaemon.java
1 package club.wpia.gigi.ping;
2
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
7
8 import club.wpia.gigi.database.DatabaseConnection;
9 import club.wpia.gigi.database.DatabaseConnection.Link;
10 import club.wpia.gigi.database.GigiPreparedStatement;
11 import club.wpia.gigi.database.GigiResultSet;
12 import club.wpia.gigi.dbObjects.Domain;
13 import club.wpia.gigi.dbObjects.DomainPingConfiguration;
14 import club.wpia.gigi.dbObjects.DomainPingType;
15
16 public class PingerDaemon extends Thread {
17
18     HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
19
20     private KeyStore truststore;
21
22     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
23
24     public PingerDaemon(KeyStore truststore) {
25         this.truststore = truststore;
26     }
27
28     @Override
29     public void run() {
30         try (Link l = DatabaseConnection.newLink(false)) {
31             runWithConnection();
32         } catch (InterruptedException e) {
33             e.printStackTrace();
34         }
35     }
36
37     public void runWithConnection() {
38
39         pingers.put(DomainPingType.EMAIL, new EmailPinger());
40         pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
41         pingers.put(DomainPingType.HTTP, new HTTPFetch());
42         pingers.put(DomainPingType.DNS, new DNSPinger());
43
44         while (true) {
45             try {
46                 boolean worked = false;
47                 synchronized (this) {
48                     DomainPingConfiguration conf;
49                     while ((conf = toExecute.peek()) != null) {
50                         worked = true;
51                         handle(conf);
52                         toExecute.remove();
53                     }
54                     notifyAll();
55                 }
56                 try (GigiPreparedStatement searchNeededPings = new GigiPreparedStatement("SELECT `pc`.`id`" //
57                         + " FROM `pingconfig` AS `pc`" //
58                         + " INNER JOIN `domains` AS `d` ON `d`.`id` = `pc`.`domainid`" //
59                         + " WHERE `d`.`deleted` IS NULL" //
60                         + "  AND `pc`.`deleted` IS NULL" //
61                         + "  AND NOT EXISTS (" //
62                         + "    SELECT 1 FROM `domainPinglog` AS `dpl`" //
63                         + "    WHERE `dpl`.`configId` = `pc`.`id`" //
64                         + "     AND `dpl`.`when` >= CURRENT_TIMESTAMP - interval '6 mons')")) {
65
66                     GigiResultSet rs = searchNeededPings.executeQuery();
67                     while (rs.next()) {
68                         worked = true;
69                         handle(DomainPingConfiguration.getById(rs.getInt("id")));
70                     }
71                 }
72                 try {
73                     if ( !worked) {
74                         Thread.sleep(5000);
75                     }
76                 } catch (InterruptedException e) {
77                 }
78             } catch (Throwable t) {
79                 t.printStackTrace();
80                 try {
81                     Thread.sleep(5000);
82                 } catch (InterruptedException e) {
83                 }
84             }
85         }
86     }
87
88     private void handle(DomainPingConfiguration conf) {
89         DomainPingType type = conf.getType();
90         String config = conf.getInfo();
91         DomainPinger dp = pingers.get(type);
92         if (dp != null) {
93             Domain target = conf.getTarget();
94             System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
95             try {
96                 dp.ping(target, config, target.getOwner(), conf.getId());
97             } catch (Throwable t) {
98                 t.printStackTrace();
99                 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
100             }
101             System.err.println("done (" + System.currentTimeMillis() + ")");
102         }
103     }
104
105     public synchronized void queue(DomainPingConfiguration toReping) {
106         interrupt();
107         toExecute.add(toReping);
108         while (toExecute.size() > 0) {
109             try {
110                 wait();
111             } catch (InterruptedException e) {
112                 e.printStackTrace();
113             }
114         }
115     }
116 }