Ready for first message/download test

This commit is contained in:
SimonTuberlin 2017-03-13 22:42:56 +01:00 committed by Mike Rosseel
parent ad96e1f494
commit 289a018be8
4 changed files with 159 additions and 6 deletions

View file

@ -57,7 +57,7 @@ public class DownloadUtil extends Task<File> {
String disposition = httpConn.getHeaderField("Content-Disposition"); String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType(); String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength(); int contentLength = httpConn.getContentLength();
if (! contentLength > 0) if (!(contentLength > 0))
contentLength = -1; contentLength = -1;
if (disposition != null) { if (disposition != null) {
@ -81,9 +81,10 @@ public class DownloadUtil extends Task<File> {
String saveFilePath = saveDir + File.separator + fileName; String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file // opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath); File outputFile = new File(saveFilePath);
FileOutputStream outputStream = new FileOutputStream(outputFile);
int bytesRead = -1; int bytesRead;
int totalRead = 0; int totalRead = 0;
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) { while ((bytesRead = inputStream.read(buffer)) != -1) {
@ -100,7 +101,7 @@ public class DownloadUtil extends Task<File> {
} }
System.out.println("File downloaded"); System.out.println("File downloaded");
return saveFilePath; return outputFile.getParentFile();
} else { } else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode); System.out.println("No file to download. Server replied HTTP code: " + responseCode);
} }

View file

@ -221,6 +221,7 @@ public class Utilities {
} }
Thread th = new Thread(task); Thread th = new Thread(task);
th.start(); th.start();
return null; // TODO !
} }
public static void printSystemLoad() { public static void printSystemLoad() {

View file

@ -0,0 +1,152 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.alert;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import io.bitsquare.app.AppOptionKeys;
import io.bitsquare.common.crypto.KeyRing;
import io.bitsquare.p2p.P2PService;
import io.bitsquare.p2p.storage.HashMapChangedListener;
import io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry;
import io.bitsquare.user.User;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.security.SignatureException;
import static org.bitcoinj.core.Utils.HEX;
public class AlertManager {
private static final Logger log = LoggerFactory.getLogger(AlertManager.class);
private P2PService p2PService;
private final KeyRing keyRing;
private final User user;
private final ObjectProperty<Alert> alertMessageProperty = new SimpleObjectProperty<>();
// Pub key for developer global alert message
private static final String pubKeyAsHex = "027a381b5333a56e1cc3d90d3a7d07f26509adf7029ed06fc997c656621f8da1ee";
private ECKey alertSigningKey;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, Initialization
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public AlertManager(P2PService p2PService, KeyRing keyRing, User user, @Named(AppOptionKeys.IGNORE_DEV_MSG_KEY) boolean ignoreDevMsg) {
this.p2PService = p2PService;
this.keyRing = keyRing;
this.user = user;
if (!ignoreDevMsg) {
p2PService.addHashSetChangedListener(new HashMapChangedListener() {
@Override
public void onAdded(ProtectedStorageEntry data) {
if (data.getStoragePayload() instanceof Alert) {
Alert alert = (Alert) data.getStoragePayload();
if (verifySignature(alert))
alertMessageProperty.set(alert);
}
}
@Override
public void onRemoved(ProtectedStorageEntry data) {
if (data.getStoragePayload() instanceof Alert) {
Alert alert = (Alert) data.getStoragePayload();
if (verifySignature(alert))
alertMessageProperty.set(null);
}
}
});
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// API
///////////////////////////////////////////////////////////////////////////////////////////
public ReadOnlyObjectProperty<Alert> alertMessageProperty() {
return alertMessageProperty;
}
public boolean addAlertMessageIfKeyIsValid(Alert alert, String privKeyString) {
// if there is a previous message we remove that first
if (user.getDevelopersAlert() != null)
removeAlertMessageIfKeyIsValid(privKeyString);
boolean isKeyValid = isKeyValid(privKeyString);
if (isKeyValid) {
signAndAddSignatureToAlertMessage(alert);
user.setDevelopersAlert(alert);
boolean result = p2PService.addData(alert, true);
if (result) {
log.trace("Add alertMessage to network was successful. AlertMessage = " + alert);
}
}
return isKeyValid;
}
public boolean removeAlertMessageIfKeyIsValid(String privKeyString) {
Alert alert = user.getDevelopersAlert();
if (isKeyValid(privKeyString) && alert != null) {
if (p2PService.removeData(alert, true))
log.trace("Remove alertMessage from network was successful. AlertMessage = " + alert);
user.setDevelopersAlert(null);
return true;
} else {
return false;
}
}
private boolean isKeyValid(String privKeyString) {
try {
alertSigningKey = ECKey.fromPrivate(new BigInteger(1, HEX.decode(privKeyString)));
return pubKeyAsHex.equals(Utils.HEX.encode(alertSigningKey.getPubKey()));
} catch (Throwable t) {
return false;
}
}
private void signAndAddSignatureToAlertMessage(Alert alert) {
String alertMessageAsHex = Utils.HEX.encode(alert.message.getBytes());
String signatureAsBase64 = alertSigningKey.signMessage(alertMessageAsHex);
alert.setSigAndPubKey(signatureAsBase64, keyRing.getSignatureKeyPair().getPublic());
}
private boolean verifySignature(Alert alert) {
String alertMessageAsHex = Utils.HEX.encode(alert.message.getBytes());
try {
ECKey.fromPublicOnly(HEX.decode(pubKeyAsHex)).verifyMessage(alertMessageAsHex, alert.getSignatureAsBase64());
return true;
} catch (SignatureException e) {
log.warn("verifySignature failed");
return false;
}
}
}

View file

@ -79,7 +79,6 @@ public class DisplayUpdateDownloadWindow extends Overlay<DisplayUpdateDownloadWi
switch (Utilities.get)
ProgressIndicator indicator = new ProgressIndicator(0L); ProgressIndicator indicator = new ProgressIndicator(0L);
indicator.setVisible(false); indicator.setVisible(false);
GridPane.setRowIndex(indicator, ++rowIndex); GridPane.setRowIndex(indicator, ++rowIndex);
@ -107,7 +106,7 @@ public class DisplayUpdateDownloadWindow extends Overlay<DisplayUpdateDownloadWi
indicator.setVisible(true); indicator.setVisible(true);
try { try {
Utilities.downloadFile(url, null, indicator); Utilities.downloadFile(url, null, indicator);
} catch (IOException e) { } catch (IOException exception) {
messageLabel.setText("Unable to download files.\n" + messageLabel.setText("Unable to download files.\n" +
"Please manually download and verify the file from https://bitsquare.io/downloads"); "Please manually download and verify the file from https://bitsquare.io/downloads");
downloadButton.setDisable(true); downloadButton.setDisable(true);