X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fping%2FPingerDaemon.java;h=0e4e3bd39df9c4ba216d729c84c066668d5a8645;hp=ceb88e9a68b80447207749c4797a3599cd8049c6;hb=1473ab8375d4fea142ec06facee786c598a8de68;hpb=a1a980dd0cc65f33a6189eb81a164fe79abb647c diff --git a/src/org/cacert/gigi/ping/PingerDaemon.java b/src/org/cacert/gigi/ping/PingerDaemon.java index ceb88e9a..0e4e3bd3 100644 --- a/src/org/cacert/gigi/ping/PingerDaemon.java +++ b/src/org/cacert/gigi/ping/PingerDaemon.java @@ -6,21 +6,20 @@ import java.util.LinkedList; import java.util.Queue; import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.database.DatabaseConnection.Link; import org.cacert.gigi.database.GigiPreparedStatement; import org.cacert.gigi.database.GigiResultSet; import org.cacert.gigi.dbObjects.Domain; import org.cacert.gigi.dbObjects.DomainPingConfiguration; -import org.cacert.gigi.dbObjects.DomainPingConfiguration.PingType; +import org.cacert.gigi.dbObjects.DomainPingType; import org.cacert.gigi.util.RandomToken; public class PingerDaemon extends Thread { - HashMap pingers = new HashMap<>(); + HashMap pingers = new HashMap<>(); private GigiPreparedStatement searchNeededPings; - private GigiPreparedStatement enterPingResult; - private KeyStore truststore; private Queue toExecute = new LinkedList<>(); @@ -31,51 +30,77 @@ public class PingerDaemon extends Thread { @Override public void run() { - 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"); - enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO domainPinglog SET configId=?, state=?, result=?, challenge=?"); - pingers.put(PingType.EMAIL, new EmailPinger()); - pingers.put(PingType.SSL, new SSLPinger(truststore)); - pingers.put(PingType.HTTP, new HTTPFetch()); - pingers.put(PingType.DNS, new DNSPinger()); + try (Link l = DatabaseConnection.newLink(false)) { + runWithConnection(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void runWithConnection() { + searchNeededPings = new GigiPreparedStatement("SELECT `pc`.`id`" // + + " FROM `pingconfig` AS `pc`" // + + " INNER JOIN `domains` AS `d` ON `d`.`id` = `pc`.`domainid`" // + + " WHERE `d`.`deleted` IS NULL" // + + " AND `pc`.`deleted` IS NULL" // + + " AND NOT EXISTS (" // + + " SELECT 1 FROM `domainPinglog` AS `dpl`" // + + " WHERE `dpl`.`configId` = `pc`.`id`" // + + " AND `dpl`.`when` >= CURRENT_TIMESTAMP - interval '6 mons')"); + pingers.put(DomainPingType.EMAIL, new EmailPinger()); + pingers.put(DomainPingType.SSL, new SSLPinger(truststore)); + pingers.put(DomainPingType.HTTP, new HTTPFetch()); + pingers.put(DomainPingType.DNS, new DNSPinger()); while (true) { - synchronized (this) { - DomainPingConfiguration conf; - while ((conf = toExecute.peek()) != null) { - handle(conf); - toExecute.remove(); + try { + boolean worked = false; + synchronized (this) { + DomainPingConfiguration conf; + while ((conf = toExecute.peek()) != null) { + worked = true; + handle(conf); + toExecute.remove(); + } + notifyAll(); } - notifyAll(); - } - GigiResultSet rs = searchNeededPings.executeQuery(); - while (rs.next()) { - handle(DomainPingConfiguration.getById(rs.getInt("id"))); - } - try { - Thread.sleep(5000); - } catch (InterruptedException e) { + GigiResultSet rs = searchNeededPings.executeQuery(); + while (rs.next()) { + worked = true; + handle(DomainPingConfiguration.getById(rs.getInt("id"))); + } + try { + if ( !worked) { + Thread.sleep(5000); + } + } catch (InterruptedException e) { + } + } catch (Throwable t) { + t.printStackTrace(); } } } private void handle(DomainPingConfiguration conf) { - PingType type = conf.getType(); + DomainPingType type = conf.getType(); String config = conf.getInfo(); DomainPinger dp = pingers.get(type); if (dp != null) { - String token = null; if (dp instanceof EmailPinger) { + String token = null; token = RandomToken.generateToken(16); config = config + ":" + token; } - enterPingResult.setInt(1, conf.getId()); Domain target = conf.getTarget(); - String resp = dp.ping(target, config, target.getOwner()); - enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == resp ? "open" : DomainPinger.PING_SUCCEDED.equals(resp) ? "success" : "failed"); - enterPingResult.setString(3, resp); - enterPingResult.setString(4, token); - enterPingResult.execute(); + System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")"); + try { + dp.ping(target, config, target.getOwner(), conf.getId()); + } catch (Throwable t) { + t.printStackTrace(); + DomainPinger.enterPingResult(conf.getId(), "error", "exception", null); + } + System.err.println("done (" + System.currentTimeMillis() + ")"); } }