package org.cacert.gigi.ping;
-import java.io.FileReader;
-import java.io.IOException;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
+import java.security.KeyStore;
import java.util.HashMap;
-import java.util.Properties;
+import java.util.LinkedList;
+import java.util.Queue;
import org.cacert.gigi.database.DatabaseConnection;
+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.util.RandomToken;
-public class PingerDaemon implements Runnable {
+public class PingerDaemon extends Thread {
- HashMap<String, DomainPinger> pingers = new HashMap<>();
+ HashMap<PingType, DomainPinger> pingers = new HashMap<>();
- public PingerDaemon() {
- // pingers.put("email",);
- pingers.put("ssl", new SSLPinger());
- pingers.put("http", new HTTPFetch());
- pingers.put("dns", new DNSPinger());
+ private GigiPreparedStatement searchNeededPings;
+ private GigiPreparedStatement enterPingResult;
+
+ private KeyStore truststore;
+
+ private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
+
+ public PingerDaemon(KeyStore truststore) {
+ this.truststore = truststore;
}
@Override
public void run() {
- try {
- PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT pingconfig.*, domains.domain FROM pingconfig LEFT JOIN domainPinglog ON domainPinglog.configId=pingconfig.id INNER JOIN domains ON domains.id=pingconfig.domainid WHERE domainPinglog.configId IS NULL ");
- PreparedStatement result = DatabaseConnection.getInstance().prepare("INSERT INTO domainPinglog SET configId=?, state=?, result=?");
+ 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());
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- String type = rs.getString("type");
- String config = rs.getString("info");
- System.out.println(type);
- System.out.println(config);
- DomainPinger dp = pingers.get(type);
- if (dp != null) {
- result.setInt(1, rs.getInt("id"));
- String resp = dp.ping(rs.getString("domain"), config);
- result.setString(2, resp == DomainPinger.PING_STILL_PENDING ? "open" : resp == DomainPinger.PING_SUCCEDED ? "success" : "failed");
- result.setString(3, resp);
- result.execute();
+ while (true) {
+ synchronized (this) {
+ DomainPingConfiguration conf;
+ while ((conf = toExecute.peek()) != null) {
+ handle(conf);
+ toExecute.remove();
}
+ notifyAll();
+ }
+
+ GigiResultSet rs = searchNeededPings.executeQuery();
+ while (rs.next()) {
+ handle(DomainPingConfiguration.getById(rs.getInt("id")));
+ }
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException e) {
}
- } catch (SQLException e) {
- e.printStackTrace();
}
}
- public static void main(String[] args) throws IOException {
- Properties conf = new Properties();
- conf.load(new FileReader("config/gigi.properties"));
- DatabaseConnection.init(conf);
- new PingerDaemon().run();
+ private void handle(DomainPingConfiguration conf) {
+ PingType type = conf.getType();
+ String config = conf.getInfo();
+ DomainPinger dp = pingers.get(type);
+ if (dp != null) {
+ String token = null;
+ if (dp instanceof EmailPinger) {
+ 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();
+ }
+ }
+ public synchronized void queue(DomainPingConfiguration toReping) {
+ interrupt();
+ toExecute.add(toReping);
+ while (toExecute.size() > 0) {
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
}
}