Initial crappy commit

This commit is contained in:
Paul Schaub 2018-10-24 03:03:46 +02:00
commit f131259fb9
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
14 changed files with 699 additions and 0 deletions

View file

@ -0,0 +1,6 @@
package de.vanitasvitae.omemoqrgenerator;
public interface LoginCallback {
void login(String username, String password);
}

View file

@ -0,0 +1,27 @@
package de.vanitasvitae.omemoqrgenerator;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import javafx.fxml.FXML;
public class LoginController {
@FXML
private JFXButton button_login;
@FXML
private JFXTextField text_username;
@FXML
private JFXPasswordField text_password;
public void setLoginCallback(LoginCallback callback) {
button_login.setOnAction(actionEvent -> {
String username = text_username.getText();
String password = text_password.getText();
callback.login(username, password);
});
}
}

View file

@ -0,0 +1,115 @@
package de.vanitasvitae.omemoqrgenerator;
import java.util.Arrays;
import java.util.Map;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.jxmpp.jid.BareJid;
public class Main extends Application implements LoginCallback {
private Stage stage;
static {
SmackConfiguration.DEBUG = true;
SmackConfiguration.addDisabledSmackClasses("org.jivesoftware.smack.ReconnectionManager",
"org.jivesoftware.smack.sasl.javax",
"org.jivesoftware.smack.legacy",
"org.jivesoftware.smackx.gcm",
"org.jivesoftware.smackx.httpfileupload",
"org.jivesoftware.smackx.hoxt",
"org.jivesoftware.smackx.iot",
"org.jivesoftware.smackx.json",
"org.jivesoftware.smackx.muc",
"org.jivesoftware.smackx.xdata",
"org.jivesoftware.smackx.xdatalayout",
"org.jivesoftware.smackx.xdatavalidation",
"org.jivesoftware.smackx.admin",
"org.jivesoftware.smackx.amp",
"org.jivesoftware.smackx.attention",
"org.jivesoftware.smackx.blocking",
"org.jivesoftware.smackx.bob",
"org.jivesoftware.smackx.bookmark",
"org.jivesoftware.smackx.bytestreams",
"org.jivesoftware.smackx.chatstates",
"org.jivesoftware.smackx.commands",
"org.jivesoftware.smackx.filetransfer",
"org.jivesoftware.smackx.forward",
"org.jivesoftware.smackx.sid",
"org.jivesoftware.smackx.eme",
"org.jivesoftware.smackx.vcardtemp",
"org.jivesoftware.smackx.xhtmlim",
"org.jivesoftware.smackx.time",
"org.jivesoftware.smackx.privacy",
"org.jivesoftware.smackx.ping",
"org.jivesoftware.smackx.iqlast",
"org.jivesoftware.smackx.receipts",
"org.jivesoftware.smackx.iqversion"
);
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/login.fxml"));
Parent root = loader.load();
stage.setMinHeight(600);
stage.setMinWidth(400);
LoginController loginController = loader.getController();
loginController.setLoginCallback(this);
Scene scene = new Scene(root, 400, 600);
stage.setTitle("OMEMO QR-Code Generator - Login");
stage.setScene(scene);
stage.show();
}
@Override
public void login(String username, String password) {
try {
XMPPTCPConnection connection = new XMPPTCPConnection(username, password);
connection.connect().login();
BareJid jid = connection.getUser().asBareJid();
Map<OmemoDevice, OmemoFingerprint> fingerprints = Util.getFingerprints(connection);
connection.disconnect(new Presence(Presence.Type.unavailable));
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/qrdisplay.fxml"));
Parent root = loader.load();
QrDisplayController controller = loader.getController();
controller.setFingerprints(jid, fingerprints);
Scene scene = new Scene(root, 400, 600);
stage.setTitle("OMEMO QR-Code Generator");
stage.setScene(scene);
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,87 @@
package de.vanitasvitae.omemoqrgenerator;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Map;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.jfoenix.controls.JFXTextArea;
import javafx.embed.swing.SwingFXUtils;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import org.jxmpp.jid.BareJid;
public class QrDisplayController {
@FXML
private ImageView qr_view;
@FXML
private JFXTextArea content_text;
private BareJid jid;
private Map<OmemoDevice, OmemoFingerprint> fingerprintMap;
public void setFingerprints(BareJid jid, Map<OmemoDevice, OmemoFingerprint> fingerprints) {
this.jid = jid;
this.fingerprintMap = fingerprints;
drawQRCode(jid, fingerprints);
}
public void drawQRCode(BareJid jid, Map<OmemoDevice, OmemoFingerprint> fingerprints) {
int width = 300, height = 300;
QRCodeWriter qrCodeWriter = new QRCodeWriter();
String content = "xmpp:" + jid.toString();
Iterator<OmemoDevice> iterator = fingerprints.keySet().iterator();
if (iterator.hasNext()) {
OmemoDevice first = iterator.next();
content += "?omemo-sid-" + first.getDeviceId() + "=" + fingerprints.get(first);
}
while (iterator.hasNext()) {
OmemoDevice next = iterator.next();
content += ";omemo-sid-" + next.getDeviceId() + "=" + fingerprints.get(next);
}
content_text.setText(content);
BufferedImage image;
try {
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
graphics.setColor(Color.BLACK);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1);
}
}
}
System.out.println("Success...");
} catch (WriterException ex) {
ex.printStackTrace();
return;
}
qr_view.setImage(SwingFXUtils.toFXImage(image, null));
}
}

View file

@ -0,0 +1,97 @@
package de.vanitasvitae.omemoqrgenerator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.omemo.element.OmemoBundleElement;
import org.jivesoftware.smackx.omemo.element.OmemoDeviceListElement;
import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
import org.jivesoftware.smackx.omemo.internal.OmemoDevice;
import org.jivesoftware.smackx.omemo.signal.SignalOmemoKeyUtil;
import org.jivesoftware.smackx.omemo.trust.OmemoFingerprint;
import org.jivesoftware.smackx.omemo.util.OmemoConstants;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubException;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jxmpp.jid.BareJid;
public class Util {
private static final SignalOmemoKeyUtil KEYUTIL = new SignalOmemoKeyUtil();
public static Map<OmemoDevice, OmemoFingerprint> getFingerprints(XMPPTCPConnection connection)
throws InterruptedException, PubSubException.NotALeafNodeException,
SmackException.NoResponseException, SmackException.NotConnectedException,
XMPPException.XMPPErrorException, PubSubException.NotAPubSubNodeException,
CorruptedOmemoKeyException {
BareJid jid = connection.getUser().asBareJid();
Map<OmemoDevice, OmemoFingerprint> fingerprintMap = new HashMap<>();
OmemoDeviceListElement deviceList = fetchDeviceList(connection, jid);
if (deviceList == null) {
return fingerprintMap;
}
for (int id : deviceList.getDeviceIds()) {
OmemoDevice device = new OmemoDevice(jid, id);
OmemoBundleElement bundle = fetchBundle(connection, device);
if (bundle == null) {
continue;
}
OmemoFingerprint fingerprint = KEYUTIL.getFingerprintOfIdentityKey(KEYUTIL.BUNDLE.identityKey(bundle));
fingerprintMap.put(device, fingerprint);
}
return fingerprintMap;
}
public static OmemoBundleElement fetchBundle(XMPPConnection connection,
OmemoDevice contactsDevice)
throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException,
XMPPException.XMPPErrorException, PubSubException.NotALeafNodeException,
PubSubException.NotAPubSubNodeException {
PubSubManager pm = PubSubManager.getInstance(connection, contactsDevice.getJid());
LeafNode node = pm.getLeafNode(contactsDevice.getBundleNodeName());
if (node == null) {
return null;
}
List<PayloadItem<OmemoBundleElement>> bundleItems = node.getItems();
if (bundleItems.isEmpty()) {
return null;
}
return bundleItems.get(bundleItems.size() - 1).getPayload();
}
public static OmemoDeviceListElement fetchDeviceList(XMPPConnection connection, BareJid contact)
throws InterruptedException, PubSubException.NotALeafNodeException, SmackException.NoResponseException,
SmackException.NotConnectedException, XMPPException.XMPPErrorException,
PubSubException.NotAPubSubNodeException {
PubSubManager pm = PubSubManager.getInstance(connection, contact);
String nodeName = OmemoConstants.PEP_NODE_DEVICE_LIST;
LeafNode node = pm.getLeafNode(nodeName);
if (node == null) {
return null;
}
List<PayloadItem<OmemoDeviceListElement>> items = node.getItems();
if (items.isEmpty()) {
return null;
}
return items.get(items.size() - 1).getPayload();
}
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXPasswordField?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" minHeight="400.0" minWidth="600.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.vanitasvitae.omemoqrgenerator.LoginController">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<VBox alignment="CENTER" maxHeight="150.0" maxWidth="300.0" minHeight="150.0" minWidth="300.0" prefHeight="150.0" prefWidth="300.0" spacing="8.0">
<children>
<JFXTextField fx:id="text_username" promptText="Username" />
<JFXPasswordField fx:id="text_password" promptText="Password" />
<JFXButton fx:id="button_login" mnemonicParsing="false" text="Login" />
</children>
<padding>
<Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
</padding>
</VBox>
</HBox>
</children>
</VBox>

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.VBox?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import javafx.scene.layout.HBox?>
<VBox alignment="TOP_CENTER" minHeight="400.0" minWidth="600.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.162-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.vanitasvitae.omemoqrgenerator.QrDisplayController">
<children>
<ImageView fx:id="qr_view"/>
<HBox>
<JFXTextArea fx:id="content_text" />
</HBox>
</children>
</VBox>