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.DatabaseConnection.Link;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
14 import org.cacert.gigi.dbObjects.DomainPingType;
15 import org.cacert.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) {
85 private void handle(DomainPingConfiguration conf) {
86 DomainPingType type = conf.getType();
87 String config = conf.getInfo();
88 DomainPinger dp = pingers.get(type);
90 if (dp instanceof EmailPinger) {
92 token = RandomToken.generateToken(16);
93 config = config + ":" + token;
95 Domain target = conf.getTarget();
96 System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
98 dp.ping(target, config, target.getOwner(), conf.getId());
99 } catch (Throwable t) {
101 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
103 System.err.println("done (" + System.currentTimeMillis() + ")");
107 public synchronized void queue(DomainPingConfiguration toReping) {
109 toExecute.add(toReping);
110 while (toExecute.size() > 0) {
113 } catch (InterruptedException e) {