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.DomainPingType;
14 import org.cacert.gigi.util.RandomToken;
16 public class PingerDaemon extends Thread {
18 HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
20 private GigiPreparedStatement searchNeededPings;
22 private KeyStore truststore;
24 private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
26 public PingerDaemon(KeyStore truststore) {
27 this.truststore = truststore;
32 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`");
33 pingers.put(DomainPingType.EMAIL, new EmailPinger());
34 pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
35 pingers.put(DomainPingType.HTTP, new HTTPFetch());
36 pingers.put(DomainPingType.DNS, new DNSPinger());
40 boolean worked = false;
42 DomainPingConfiguration conf;
43 while ((conf = toExecute.peek()) != null) {
51 GigiResultSet rs = searchNeededPings.executeQuery();
54 handle(DomainPingConfiguration.getById(rs.getInt("id")));
60 } catch (InterruptedException e) {
62 } catch (Throwable t) {
68 private void handle(DomainPingConfiguration conf) {
69 DomainPingType type = conf.getType();
70 String config = conf.getInfo();
71 DomainPinger dp = pingers.get(type);
73 if (dp instanceof EmailPinger) {
75 token = RandomToken.generateToken(16);
76 config = config + ":" + token;
78 Domain target = conf.getTarget();
79 System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
81 dp.ping(target, config, target.getOwner(), conf.getId());
82 } catch (Throwable t) {
84 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
86 System.err.println("done (" + System.currentTimeMillis() + ")");
90 public synchronized void queue(DomainPingConfiguration toReping) {
92 toExecute.add(toReping);
93 while (toExecute.size() > 0) {
96 } catch (InterruptedException e) {