]> WPIA git - gigi.git/blob - src/org/cacert/gigi/ping/PingerDaemon.java
fix: ssl-pinging stabelize + timeout
[gigi.git] / src / org / cacert / gigi / ping / PingerDaemon.java
1 package org.cacert.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 org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.database.GigiResultSet;
11 import org.cacert.gigi.dbObjects.Domain;
12 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
13 import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType;
14 import org.cacert.gigi.util.RandomToken;
15
16 public class PingerDaemon extends Thread {
17
18     HashMap<PingType, DomainPinger> pingers = new HashMap<>();
19
20     private GigiPreparedStatement searchNeededPings;
21
22     private GigiPreparedStatement enterPingResult;
23
24     private KeyStore truststore;
25
26     private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
27
28     public PingerDaemon(KeyStore truststore) {
29         this.truststore = truststore;
30     }
31
32     @Override
33     public void run() {
34         searchNeededPings = DatabaseConnection.getInstance().prepare("SELECT `pingconfig`.`id` FROM `pingconfig` LEFT JOIN `domainPinglog` ON `domainPinglog`.`configId` = `pingconfig`.`id` INNER JOIN `domains` ON `domains`.`id` = `pingconfig`.`domainid` WHERE ( `domainPinglog`.`configId` IS NULL) AND `domains`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
35         enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO `domainPinglog` SET `configId`=?, `state`=?::`pingState`, `result`=?, `challenge`=?");
36         pingers.put(PingType.EMAIL, new EmailPinger());
37         pingers.put(PingType.SSL, new SSLPinger(truststore));
38         pingers.put(PingType.HTTP, new HTTPFetch());
39         pingers.put(PingType.DNS, new DNSPinger());
40
41         while (true) {
42             try {
43                 boolean worked = false;
44                 synchronized (this) {
45                     DomainPingConfiguration conf;
46                     while ((conf = toExecute.peek()) != null) {
47                         worked = true;
48                         handle(conf);
49                         toExecute.remove();
50                     }
51                     notifyAll();
52                 }
53
54                 GigiResultSet rs = searchNeededPings.executeQuery();
55                 while (rs.next()) {
56                     worked = true;
57                     handle(DomainPingConfiguration.getById(rs.getInt("id")));
58                 }
59                 try {
60                     if ( !worked) {
61                         Thread.sleep(5000);
62                     }
63                 } catch (InterruptedException e) {
64                 }
65             } catch (Throwable t) {
66                 t.printStackTrace();
67             }
68         }
69     }
70
71     private void handle(DomainPingConfiguration conf) {
72         PingType type = conf.getType();
73         String config = conf.getInfo();
74         DomainPinger dp = pingers.get(type);
75         if (dp != null) {
76             String token = null;
77             if (dp instanceof EmailPinger) {
78                 token = RandomToken.generateToken(16);
79                 config = config + ":" + token;
80             }
81             enterPingResult.setInt(1, conf.getId());
82             Domain target = conf.getTarget();
83             String resp = dp.ping(target, config, target.getOwner());
84             enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == resp ? "open" : DomainPinger.PING_SUCCEDED.equals(resp) ? "success" : "failed");
85             enterPingResult.setString(3, resp);
86             enterPingResult.setString(4, token);
87             enterPingResult.execute();
88         }
89     }
90
91     public synchronized void queue(DomainPingConfiguration toReping) {
92         interrupt();
93         toExecute.add(toReping);
94         while (toExecute.size() > 0) {
95             try {
96                 wait();
97             } catch (InterruptedException e) {
98                 e.printStackTrace();
99             }
100         }
101     }
102 }