1 package club.wpia.gigi.ping;
3 import java.security.KeyStore;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.Queue;
8 import club.wpia.gigi.database.DatabaseConnection;
9 import club.wpia.gigi.database.DatabaseConnection.Link;
10 import club.wpia.gigi.database.GigiPreparedStatement;
11 import club.wpia.gigi.database.GigiResultSet;
12 import club.wpia.gigi.dbObjects.Certificate.RevocationType;
13 import club.wpia.gigi.dbObjects.Domain;
14 import club.wpia.gigi.dbObjects.DomainPingConfiguration;
15 import club.wpia.gigi.dbObjects.DomainPingType;
16 import club.wpia.gigi.util.RandomToken;
20 * This thread is executing pings for domains, if the last ping is older than a
21 * specific amount of time.
24 * When a ping fails the corresponding domain becomes unverified. If the ping
25 * succeeds the domains becomes verified and all other domain with the same name
29 * When a domain becomes unverified and has still certificated attached to it
30 * the certificates will become "pending for revocation". This state is removed
31 * when the domain is successfully verified again. When this state prolongs for
32 * more than 2 weeks, such certificates are revoked with type
33 * {@link RevocationType#PING_TIMEOUT}.
36 public class PingerDaemon extends Thread {
38 HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
40 private KeyStore truststore;
42 private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
44 public PingerDaemon(KeyStore truststore) {
45 this.truststore = truststore;
50 try (Link l = DatabaseConnection.newLink(false)) {
52 } catch (InterruptedException e) {
57 public void runWithConnection() {
59 pingers.put(DomainPingType.EMAIL, new EmailPinger());
60 pingers.put(DomainPingType.SSL, new SSLPinger(truststore));
61 pingers.put(DomainPingType.HTTP, new HTTPFetch());
62 pingers.put(DomainPingType.DNS, new DNSPinger());
66 boolean worked = false;
68 DomainPingConfiguration conf;
69 while ((conf = toExecute.peek()) != null) {
76 try (GigiPreparedStatement searchNeededPings = new GigiPreparedStatement("SELECT `pc`.`id`" //
77 + " FROM `pingconfig` AS `pc`" //
78 + " INNER JOIN `domains` AS `d` ON `d`.`id` = `pc`.`domainid`" //
79 + " WHERE `d`.`deleted` IS NULL" //
80 + " AND `pc`.`deleted` IS NULL" //
81 + " AND NOT EXISTS (" //
82 + " SELECT 1 FROM `domainPinglog` AS `dpl`" //
83 + " WHERE `dpl`.`configId` = `pc`.`id`" //
84 + " AND `dpl`.`when` >= CURRENT_TIMESTAMP - interval '6 mons')")) {
86 GigiResultSet rs = searchNeededPings.executeQuery();
89 handle(DomainPingConfiguration.getById(rs.getInt("id")));
96 } catch (InterruptedException e) {
98 } catch (Throwable t) {
102 } catch (InterruptedException e) {
108 private void handle(DomainPingConfiguration conf) {
109 DomainPingType type = conf.getType();
110 String config = conf.getInfo();
111 DomainPinger dp = pingers.get(type);
113 if (dp instanceof EmailPinger) {
115 token = RandomToken.generateToken(16);
116 config = config + ":" + token;
118 Domain target = conf.getTarget();
119 System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
121 dp.ping(target, config, target.getOwner(), conf.getId());
122 } catch (Throwable t) {
124 DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
126 System.err.println("done (" + System.currentTimeMillis() + ")");
130 public synchronized void queue(DomainPingConfiguration toReping) {
132 toExecute.add(toReping);
133 while (toExecute.size() > 0) {
136 } catch (InterruptedException e) {