1 package org.cacert.gigi.ping;
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
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;
16 public class PingerDaemon extends Thread {
18 HashMap<PingType, DomainPinger> pingers = new HashMap<>();
20 private GigiPreparedStatement searchNeededPings;
22 private GigiPreparedStatement enterPingResult;
24 private KeyStore truststore;
26 private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
28 public PingerDaemon(KeyStore truststore) {
29 this.truststore = truststore;
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());
43 boolean worked = false;
45 DomainPingConfiguration conf;
46 while ((conf = toExecute.peek()) != null) {
54 GigiResultSet rs = searchNeededPings.executeQuery();
57 handle(DomainPingConfiguration.getById(rs.getInt("id")));
63 } catch (InterruptedException e) {
65 } catch (Throwable t) {
71 private void handle(DomainPingConfiguration conf) {
72 PingType type = conf.getType();
73 String config = conf.getInfo();
74 DomainPinger dp = pingers.get(type);
77 if (dp instanceof EmailPinger) {
78 token = RandomToken.generateToken(16);
79 config = config + ":" + token;
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();
91 public synchronized void queue(DomainPingConfiguration toReping) {
93 toExecute.add(toReping);
94 while (toExecute.size() > 0) {
97 } catch (InterruptedException e) {