Scanner serverdata now tries to get a non-localhost IPV4 address if possible
parent
6b47387920
commit
291f7d93c0
|
@ -13,8 +13,8 @@ apply plugin: 'maven-publish'
|
||||||
// Uncomment the lines below to define an application
|
// Uncomment the lines below to define an application
|
||||||
// (this will also allow you to build a "fatCapsule" which includes
|
// (this will also allow you to build a "fatCapsule" which includes
|
||||||
// the entire application, including all dependencies in a single jar)
|
// the entire application, including all dependencies in a single jar)
|
||||||
//apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
//mainClassName='your.main.ApplicationClass'
|
mainClassName='meerkat.pollingstation.PollingStationToyRun'
|
||||||
|
|
||||||
|
|
||||||
// Is this a snapshot version?
|
// Is this a snapshot version?
|
||||||
|
@ -32,7 +32,7 @@ ext {
|
||||||
description = "Meerkat polling-station application"
|
description = "Meerkat polling-station application"
|
||||||
|
|
||||||
// Your project version
|
// Your project version
|
||||||
version = "0.0"
|
version = "0.1"
|
||||||
|
|
||||||
version += "${isSnapshot ? '-SNAPSHOT' : ''}"
|
version += "${isSnapshot ? '-SNAPSHOT' : ''}"
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ public class PollingStationToyRun {
|
||||||
scanner = new PollingStationWebScanner(0, CONTEXT_PATH);
|
scanner = new PollingStationWebScanner(0, CONTEXT_PATH);
|
||||||
PollingStation.ConnectionServerData serverData = scanner.start(true);
|
PollingStation.ConnectionServerData serverData = scanner.start(true);
|
||||||
|
|
||||||
|
logger.info("Started polling station web scanner on {}", serverData.getServerUrl());
|
||||||
PollingStationMainController controller = new PollingStationMainController();
|
PollingStationMainController controller = new PollingStationMainController();
|
||||||
controller.init(scanner);
|
controller.init(scanner);
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,13 @@
|
||||||
package meerkat.pollingstation;
|
package meerkat.pollingstation;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.channels.ServerSocketChannel;
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.security.SignatureException;
|
import java.security.SignatureException;
|
||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
@ -13,6 +17,7 @@ import meerkat.crypto.DigitalSignature;
|
||||||
import meerkat.crypto.concrete.ECDSASignature;
|
import meerkat.crypto.concrete.ECDSASignature;
|
||||||
import meerkat.protobuf.Crypto;
|
import meerkat.protobuf.Crypto;
|
||||||
import meerkat.protobuf.PollingStation;
|
import meerkat.protobuf.PollingStation;
|
||||||
|
import org.eclipse.jetty.server.Connector;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.servlet.*;
|
import org.eclipse.jetty.servlet.*;
|
||||||
|
|
||||||
|
@ -38,6 +43,12 @@ public class PollingStationWebScanner implements PollingStationScanner.PollingSt
|
||||||
*/
|
*/
|
||||||
long expectedSerial;
|
long expectedSerial;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context path for servlet
|
||||||
|
*/
|
||||||
|
|
||||||
|
String contextPath;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should a newly-connected scanner be verified using the nonce?
|
* Should a newly-connected scanner be verified using the nonce?
|
||||||
|
@ -108,6 +119,7 @@ public class PollingStationWebScanner implements PollingStationScanner.PollingSt
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
expectedSerial = 0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,7 +128,7 @@ public class PollingStationWebScanner implements PollingStationScanner.PollingSt
|
||||||
if (verifySignatures) {
|
if (verifySignatures) {
|
||||||
ByteString scannerID = scannedData.getScannerId();
|
ByteString scannerID = scannedData.getScannerId();
|
||||||
if (!scannerID.equals(connectedScannerID)) {
|
if (!scannerID.equals(connectedScannerID)) {
|
||||||
logger.warn("Scanner ID doesn't match connection public key");
|
logger.error("Scanner ID doesn't match connection public key");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,22 +136,28 @@ public class PollingStationWebScanner implements PollingStationScanner.PollingSt
|
||||||
verifier.initVerify(data.getScannerSig());
|
verifier.initVerify(data.getScannerSig());
|
||||||
verifier.updateContent(scannedData);
|
verifier.updateContent(scannedData);
|
||||||
if (!verifier.verify()) {
|
if (!verifier.verify()) {
|
||||||
logger.warn("Bad Signature");
|
logger.error("Bad Signature");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (CertificateException e) {
|
} catch (CertificateException e) {
|
||||||
logger.warn("Certificate Exception: {}", e);
|
logger.error("Certificate Exception: {}", e);
|
||||||
return false;
|
return false;
|
||||||
} catch (InvalidKeyException e) {
|
} catch (InvalidKeyException e) {
|
||||||
logger.warn("InvalidKey Exception: {}", e);
|
logger.error("InvalidKey Exception: {}", e);
|
||||||
return false;
|
return false;
|
||||||
} catch (SignatureException e) {
|
} catch (SignatureException e) {
|
||||||
logger.warn("Signature Exception: {}", e);
|
logger.error("Signature Exception: {}", e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scannedData.getSerial() != expectedSerial) {
|
||||||
|
logger.warn("Got scan with serial {}, expecting {}", scannedData.getSerial(), expectedSerial);
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedSerial = scannedData.getSerial() + 1;
|
||||||
|
|
||||||
for (FutureCallback<ScannedData> callback : callbacks) {
|
for (FutureCallback<ScannedData> callback : callbacks) {
|
||||||
callback.onSuccess(scannedData);
|
callback.onSuccess(scannedData);
|
||||||
}
|
}
|
||||||
|
@ -153,7 +171,7 @@ public class PollingStationWebScanner implements PollingStationScanner.PollingSt
|
||||||
|
|
||||||
|
|
||||||
public PollingStationWebScanner(int port, String contextPath) {
|
public PollingStationWebScanner(int port, String contextPath) {
|
||||||
|
this.contextPath = contextPath;
|
||||||
scanRequestHandler = new ScanRequestHandler();
|
scanRequestHandler = new ScanRequestHandler();
|
||||||
callbacks = new LinkedList<>();
|
callbacks = new LinkedList<>();
|
||||||
verifier = new ECDSASignature();
|
verifier = new ECDSASignature();
|
||||||
|
@ -173,15 +191,64 @@ public class PollingStationWebScanner implements PollingStationScanner.PollingSt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
String getServerURL(Server server) {
|
||||||
|
|
||||||
|
String serverURL = server.getURI().toASCIIString();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Connector connector = server.getConnectors()[0];
|
||||||
|
|
||||||
|
Object transport = connector.getTransport();
|
||||||
|
|
||||||
|
if (transport instanceof ServerSocketChannel) {
|
||||||
|
// We can get better info about the local endpoint
|
||||||
|
ServerSocketChannel sock = (ServerSocketChannel) transport;
|
||||||
|
SocketAddress localAddr = null;
|
||||||
|
localAddr = sock.getLocalAddress();
|
||||||
|
if (localAddr instanceof InetSocketAddress) {
|
||||||
|
InetSocketAddress localInet = (InetSocketAddress) localAddr;
|
||||||
|
InetAddress hostAddr = localInet.getAddress();
|
||||||
|
if (hostAddr.isAnyLocalAddress()) {
|
||||||
|
Enumeration<NetworkInterface> n = null;
|
||||||
|
n = NetworkInterface.getNetworkInterfaces();
|
||||||
|
while (n.hasMoreElements()) {
|
||||||
|
NetworkInterface e = n.nextElement();
|
||||||
|
if (e.isLoopback() || e.isPointToPoint() || !e.isUp())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Enumeration<InetAddress> aList = e.getInetAddresses();
|
||||||
|
while (aList.hasMoreElements()) {
|
||||||
|
InetAddress a = aList.nextElement();
|
||||||
|
if (a instanceof Inet4Address) {
|
||||||
|
serverURL = "http://" + a.getHostAddress() + ":" + localInet.getPort()
|
||||||
|
+ contextPath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SocketException e) {
|
||||||
|
// Ignore
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverURL;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PollingStation.ConnectionServerData start(boolean verifyScanner) throws Exception {
|
public PollingStation.ConnectionServerData start(boolean verifyScanner) throws Exception {
|
||||||
this.verifyNonce = this.verifySignatures = verifyScanner;
|
this.verifyNonce = this.verifySignatures = verifyScanner;
|
||||||
nonce = new SecureRandom().nextLong();
|
nonce = new SecureRandom().nextLong();
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
return PollingStation.ConnectionServerData.newBuilder()
|
return PollingStation.ConnectionServerData.newBuilder()
|
||||||
.setNonce(nonce)
|
.setNonce(nonce)
|
||||||
.setServerUrl(server.getURI().toString())
|
.setServerUrl(getServerURL(server))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue