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=?, 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 DomainPingConfiguration conf;
44 while ((conf = toExecute.peek()) != null) {
51 GigiResultSet rs = searchNeededPings.executeQuery();
53 handle(DomainPingConfiguration.getById(rs.getInt("id")));
57 } catch (InterruptedException e) {
62 private void handle(DomainPingConfiguration conf) {
63 PingType type = conf.getType();
64 String config = conf.getInfo();
65 DomainPinger dp = pingers.get(type);
68 if (dp instanceof EmailPinger) {
69 token = RandomToken.generateToken(16);
70 config = config + ":" + token;
72 enterPingResult.setInt(1, conf.getId());
73 Domain target = conf.getTarget();
74 String resp = dp.ping(target, config, target.getOwner());
75 enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == resp ? "open" : DomainPinger.PING_SUCCEDED.equals(resp) ? "success" : "failed");
76 enterPingResult.setString(3, resp);
77 enterPingResult.setString(4, token);
78 enterPingResult.execute();
82 public synchronized void queue(DomainPingConfiguration toReping) {
84 toExecute.add(toReping);
85 while (toExecute.size() > 0) {
88 } catch (InterruptedException e) {