mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
First commit
This commit is contained in:
commit
28e192c519
18 changed files with 3272 additions and 0 deletions
2135
src/main/java/cli/Base64.java
Normal file
2135
src/main/java/cli/Base64.java
Normal file
File diff suppressed because it is too large
Load diff
135
src/main/java/cli/JsonAxolotlStore.java
Normal file
135
src/main/java/cli/JsonAxolotlStore.java
Normal file
|
@ -0,0 +1,135 @@
|
|||
package cli;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.whispersystems.libaxolotl.*;
|
||||
import org.whispersystems.libaxolotl.state.AxolotlStore;
|
||||
import org.whispersystems.libaxolotl.state.PreKeyRecord;
|
||||
import org.whispersystems.libaxolotl.state.SessionRecord;
|
||||
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class JsonAxolotlStore implements AxolotlStore {
|
||||
private final JsonPreKeyStore preKeyStore;
|
||||
private final JsonSessionStore sessionStore;
|
||||
private final JsonSignedPreKeyStore signedPreKeyStore;
|
||||
|
||||
private final JsonIdentityKeyStore identityKeyStore;
|
||||
|
||||
public JsonAxolotlStore(JSONObject jsonAxolotl) throws IOException, InvalidKeyException {
|
||||
this.preKeyStore = new JsonPreKeyStore(jsonAxolotl.getJSONArray("preKeys"));
|
||||
this.sessionStore = new JsonSessionStore(jsonAxolotl.getJSONArray("sessionStore"));
|
||||
this.signedPreKeyStore = new JsonSignedPreKeyStore(jsonAxolotl.getJSONArray("signedPreKeyStore"));
|
||||
this.identityKeyStore = new JsonIdentityKeyStore(jsonAxolotl.getJSONObject("identityKeyStore"));
|
||||
}
|
||||
|
||||
public JsonAxolotlStore(IdentityKeyPair identityKeyPair, int registrationId) {
|
||||
preKeyStore = new JsonPreKeyStore();
|
||||
sessionStore = new JsonSessionStore();
|
||||
signedPreKeyStore = new JsonSignedPreKeyStore();
|
||||
this.identityKeyStore = new JsonIdentityKeyStore(identityKeyPair, registrationId);
|
||||
}
|
||||
|
||||
public JSONObject getJson() {
|
||||
return new JSONObject().put("preKeys", preKeyStore.getJson())
|
||||
.put("sessionStore", sessionStore.getJson())
|
||||
.put("signedPreKeyStore", signedPreKeyStore.getJson())
|
||||
.put("identityKeyStore", identityKeyStore.getJson());
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKeyPair getIdentityKeyPair() {
|
||||
return identityKeyStore.getIdentityKeyPair();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalRegistrationId() {
|
||||
return identityKeyStore.getLocalRegistrationId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIdentity(String name, IdentityKey identityKey) {
|
||||
identityKeyStore.saveIdentity(name, identityKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrustedIdentity(String name, IdentityKey identityKey) {
|
||||
return identityKeyStore.isTrustedIdentity(name, identityKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException {
|
||||
return preKeyStore.loadPreKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storePreKey(int preKeyId, PreKeyRecord record) {
|
||||
preKeyStore.storePreKey(preKeyId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPreKey(int preKeyId) {
|
||||
return preKeyStore.containsPreKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePreKey(int preKeyId) {
|
||||
preKeyStore.removePreKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SessionRecord loadSession(AxolotlAddress address) {
|
||||
return sessionStore.loadSession(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getSubDeviceSessions(String name) {
|
||||
return sessionStore.getSubDeviceSessions(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSession(AxolotlAddress address, SessionRecord record) {
|
||||
sessionStore.storeSession(address, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsSession(AxolotlAddress address) {
|
||||
return sessionStore.containsSession(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteSession(AxolotlAddress address) {
|
||||
sessionStore.deleteSession(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAllSessions(String name) {
|
||||
sessionStore.deleteAllSessions(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
|
||||
return signedPreKeyStore.loadSignedPreKey(signedPreKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignedPreKeyRecord> loadSignedPreKeys() {
|
||||
return signedPreKeyStore.loadSignedPreKeys();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
|
||||
signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsSignedPreKey(int signedPreKeyId) {
|
||||
return signedPreKeyStore.containsSignedPreKey(signedPreKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSignedPreKey(int signedPreKeyId) {
|
||||
signedPreKeyStore.removeSignedPreKey(signedPreKeyId);
|
||||
}
|
||||
}
|
74
src/main/java/cli/JsonIdentityKeyStore.java
Normal file
74
src/main/java/cli/JsonIdentityKeyStore.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
package cli;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.whispersystems.libaxolotl.IdentityKey;
|
||||
import org.whispersystems.libaxolotl.IdentityKeyPair;
|
||||
import org.whispersystems.libaxolotl.InvalidKeyException;
|
||||
import org.whispersystems.libaxolotl.state.IdentityKeyStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonIdentityKeyStore implements IdentityKeyStore {
|
||||
|
||||
private final Map<String, IdentityKey> trustedKeys = new HashMap<>();
|
||||
|
||||
private final IdentityKeyPair identityKeyPair;
|
||||
private final int localRegistrationId;
|
||||
|
||||
public JsonIdentityKeyStore(JSONObject jsonAxolotl) throws IOException, InvalidKeyException {
|
||||
localRegistrationId = jsonAxolotl.getInt("registrationId");
|
||||
identityKeyPair = new IdentityKeyPair(Base64.decode(jsonAxolotl.getString("identityKey")));
|
||||
|
||||
JSONArray list = jsonAxolotl.getJSONArray("trustedKeys");
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
JSONObject k = list.getJSONObject(i);
|
||||
try {
|
||||
trustedKeys.put(k.getString("name"), new IdentityKey(Base64.decode(k.getString("identityKey")), 0));
|
||||
} catch (InvalidKeyException | IOException e) {
|
||||
System.out.println("Error while decoding key for: " + k.getString("name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JsonIdentityKeyStore(IdentityKeyPair identityKeyPair, int localRegistrationId) {
|
||||
this.identityKeyPair = identityKeyPair;
|
||||
this.localRegistrationId = localRegistrationId;
|
||||
}
|
||||
|
||||
public JSONObject getJson() {
|
||||
JSONArray list = new JSONArray();
|
||||
for (String name : trustedKeys.keySet()) {
|
||||
list.put(new JSONObject().put("name", name).put("identityKey", Base64.encodeBytes(trustedKeys.get(name).serialize())));
|
||||
}
|
||||
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("registrationId", localRegistrationId);
|
||||
result.put("identityKey", Base64.encodeBytes(identityKeyPair.serialize()));
|
||||
result.put("trustedKeys", list);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKeyPair getIdentityKeyPair() {
|
||||
return identityKeyPair;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLocalRegistrationId() {
|
||||
return localRegistrationId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveIdentity(String name, IdentityKey identityKey) {
|
||||
trustedKeys.put(name, identityKey);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTrustedIdentity(String name, IdentityKey identityKey) {
|
||||
IdentityKey trusted = trustedKeys.get(name);
|
||||
return (trusted == null || trusted.equals(identityKey));
|
||||
}
|
||||
}
|
67
src/main/java/cli/JsonPreKeyStore.java
Normal file
67
src/main/java/cli/JsonPreKeyStore.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
package cli;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.whispersystems.libaxolotl.InvalidKeyIdException;
|
||||
import org.whispersystems.libaxolotl.state.PreKeyRecord;
|
||||
import org.whispersystems.libaxolotl.state.PreKeyStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonPreKeyStore implements PreKeyStore {
|
||||
|
||||
private final Map<Integer, byte[]> store = new HashMap<>();
|
||||
|
||||
public JsonPreKeyStore() {
|
||||
|
||||
}
|
||||
|
||||
public JsonPreKeyStore(JSONArray list) throws IOException {
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
JSONObject k = list.getJSONObject(i);
|
||||
try {
|
||||
store.put(k.getInt("id"), Base64.decode(k.getString("record")));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error while decoding prekey for: " + k.getString("name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJson() {
|
||||
JSONArray result = new JSONArray();
|
||||
for (Integer id : store.keySet()) {
|
||||
result.put(new JSONObject().put("id", id.toString()).put("record", Base64.encodeBytes(store.get(id))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PreKeyRecord loadPreKey(int preKeyId) throws InvalidKeyIdException {
|
||||
try {
|
||||
if (!store.containsKey(preKeyId)) {
|
||||
throw new InvalidKeyIdException("No such prekeyrecord!");
|
||||
}
|
||||
|
||||
return new PreKeyRecord(store.get(preKeyId));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storePreKey(int preKeyId, PreKeyRecord record) {
|
||||
store.put(preKeyId, record.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsPreKey(int preKeyId) {
|
||||
return store.containsKey(preKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePreKey(int preKeyId) {
|
||||
store.remove(preKeyId);
|
||||
}
|
||||
}
|
94
src/main/java/cli/JsonSessionStore.java
Normal file
94
src/main/java/cli/JsonSessionStore.java
Normal file
|
@ -0,0 +1,94 @@
|
|||
package cli;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.whispersystems.libaxolotl.AxolotlAddress;
|
||||
import org.whispersystems.libaxolotl.state.SessionRecord;
|
||||
import org.whispersystems.libaxolotl.state.SessionStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonSessionStore implements SessionStore {
|
||||
|
||||
private Map<AxolotlAddress, byte[]> sessions = new HashMap<>();
|
||||
|
||||
public JsonSessionStore() {
|
||||
|
||||
}
|
||||
|
||||
public JsonSessionStore(JSONArray list) throws IOException {
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
JSONObject k = list.getJSONObject(i);
|
||||
try {
|
||||
sessions.put(new AxolotlAddress(k.getString("name"), k.getInt("deviceId")), Base64.decode(k.getString("record")));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error while decoding prekey for: " + k.getString("name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJson() {
|
||||
JSONArray result = new JSONArray();
|
||||
for (AxolotlAddress address : sessions.keySet()) {
|
||||
result.put(new JSONObject().put("name", address.getName()).
|
||||
put("deviceId", address.getDeviceId()).
|
||||
put("record", Base64.encodeBytes(sessions.get(address))));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized SessionRecord loadSession(AxolotlAddress remoteAddress) {
|
||||
try {
|
||||
if (containsSession(remoteAddress)) {
|
||||
return new SessionRecord(sessions.get(remoteAddress));
|
||||
} else {
|
||||
return new SessionRecord();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized List<Integer> getSubDeviceSessions(String name) {
|
||||
List<Integer> deviceIds = new LinkedList<>();
|
||||
|
||||
for (AxolotlAddress key : sessions.keySet()) {
|
||||
if (key.getName().equals(name) &&
|
||||
key.getDeviceId() != 1) {
|
||||
deviceIds.add(key.getDeviceId());
|
||||
}
|
||||
}
|
||||
|
||||
return deviceIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void storeSession(AxolotlAddress address, SessionRecord record) {
|
||||
sessions.put(address, record.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized boolean containsSession(AxolotlAddress address) {
|
||||
return sessions.containsKey(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void deleteSession(AxolotlAddress address) {
|
||||
sessions.remove(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void deleteAllSessions(String name) {
|
||||
for (AxolotlAddress key : sessions.keySet()) {
|
||||
if (key.getName().equals(name)) {
|
||||
sessions.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
84
src/main/java/cli/JsonSignedPreKeyStore.java
Normal file
84
src/main/java/cli/JsonSignedPreKeyStore.java
Normal file
|
@ -0,0 +1,84 @@
|
|||
package cli;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.whispersystems.libaxolotl.InvalidKeyIdException;
|
||||
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonSignedPreKeyStore implements SignedPreKeyStore {
|
||||
|
||||
private final Map<Integer, byte[]> store = new HashMap<>();
|
||||
|
||||
public JsonSignedPreKeyStore() {
|
||||
|
||||
}
|
||||
|
||||
public JsonSignedPreKeyStore(JSONArray list) throws IOException {
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
JSONObject k = list.getJSONObject(i);
|
||||
try {
|
||||
store.put(k.getInt("id"), Base64.decode(k.getString("record")));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error while decoding prekey for: " + k.getString("name"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JSONArray getJson() {
|
||||
JSONArray result = new JSONArray();
|
||||
for (Integer id : store.keySet()) {
|
||||
result.put(new JSONObject().put("id", id.toString()).put("record", store.get(id)));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
|
||||
try {
|
||||
if (!store.containsKey(signedPreKeyId)) {
|
||||
throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId);
|
||||
}
|
||||
|
||||
return new SignedPreKeyRecord(store.get(signedPreKeyId));
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SignedPreKeyRecord> loadSignedPreKeys() {
|
||||
try {
|
||||
List<SignedPreKeyRecord> results = new LinkedList<>();
|
||||
|
||||
for (byte[] serialized : store.values()) {
|
||||
results.add(new SignedPreKeyRecord(serialized));
|
||||
}
|
||||
|
||||
return results;
|
||||
} catch (IOException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeSignedPreKey(int signedPreKeyId, SignedPreKeyRecord record) {
|
||||
store.put(signedPreKeyId, record.serialize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsSignedPreKey(int signedPreKeyId) {
|
||||
return store.containsKey(signedPreKeyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeSignedPreKey(int signedPreKeyId) {
|
||||
store.remove(signedPreKeyId);
|
||||
}
|
||||
}
|
148
src/main/java/cli/Main.java
Normal file
148
src/main/java/cli/Main.java
Normal file
|
@ -0,0 +1,148 @@
|
|||
/**
|
||||
* Copyright (C) 2015 AsamK
|
||||
* <p>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* This program 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 General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cli;
|
||||
|
||||
import net.sourceforge.argparse4j.ArgumentParsers;
|
||||
import net.sourceforge.argparse4j.inf.*;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.whispersystems.libaxolotl.InvalidVersionException;
|
||||
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
||||
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
|
||||
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.Security;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Workaround for BKS truststore
|
||||
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
|
||||
|
||||
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
|
||||
.defaultHelp(true)
|
||||
.description("Commandline interface for TextSecure.");
|
||||
Subparsers subparsers = parser.addSubparsers()
|
||||
.title("subcommands")
|
||||
.dest("command")
|
||||
.description("valid subcommands")
|
||||
.help("additional help");
|
||||
Subparser parserRegister = subparsers.addParser("register");
|
||||
Subparser parserVerify = subparsers.addParser("verify");
|
||||
parserVerify.addArgument("verificationCode")
|
||||
.help("The verification code you received via sms.");
|
||||
Subparser parserSend = subparsers.addParser("send");
|
||||
parserSend.addArgument("recipient")
|
||||
.help("Specify the recipients' phone number.")
|
||||
.nargs("*");
|
||||
parserSend.addArgument("-m", "--message")
|
||||
.help("Specify the message, if missing standard input is used.");
|
||||
Subparser parserReceive = subparsers.addParser("receive");
|
||||
parser.addArgument("-u", "--username")
|
||||
.required(true)
|
||||
.help("Specify your phone number, that will be used for verification.");
|
||||
Namespace ns = null;
|
||||
try {
|
||||
ns = parser.parseArgs(args);
|
||||
} catch (ArgumentParserException e) {
|
||||
parser.handleError(e);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
String username = ns.getString("username");
|
||||
Manager m = new Manager(username);
|
||||
if (m.userExists()) {
|
||||
try {
|
||||
m.load();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Loading file error: " + e.getMessage());
|
||||
System.exit(2);
|
||||
}
|
||||
}
|
||||
switch (ns.getString("command")) {
|
||||
case "register":
|
||||
if (!m.userHasKeys()) {
|
||||
m.createNewIdentity();
|
||||
}
|
||||
try {
|
||||
m.register();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Request verify error: " + e.getMessage());
|
||||
System.exit(3);
|
||||
}
|
||||
break;
|
||||
case "verify":
|
||||
if (!m.userHasKeys()) {
|
||||
System.out.println("User has no keys, first call register.");
|
||||
System.exit(1);
|
||||
}
|
||||
if (m.isRegistered()) {
|
||||
System.out.println("User registration is already verified");
|
||||
System.exit(1);
|
||||
}
|
||||
try {
|
||||
m.verifyAccount(ns.getString("verificationCode"));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Verify error: " + e.getMessage());
|
||||
System.exit(3);
|
||||
}
|
||||
break;
|
||||
case "send":
|
||||
if (!m.isRegistered()) {
|
||||
System.out.println("User is not registered.");
|
||||
System.exit(1);
|
||||
}
|
||||
TextSecureMessageSender messageSender = m.getMessageSender();
|
||||
String messageText = ns.getString("message");
|
||||
if (messageText == null) {
|
||||
try {
|
||||
messageText = IOUtils.toString(System.in);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Failed to read message from stdin: " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
TextSecureMessage message = TextSecureMessage.newBuilder().withBody(messageText).build();
|
||||
for (String recipient : ns.<String>getList("recipient")) {
|
||||
try {
|
||||
messageSender.sendMessage(new TextSecureAddress(recipient), message);
|
||||
} catch (UntrustedIdentityException | IOException e) {
|
||||
System.out.println("Send message: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "receive":
|
||||
if (!m.isRegistered()) {
|
||||
System.out.println("User is not registered.");
|
||||
System.exit(1);
|
||||
}
|
||||
try {
|
||||
message = m.receiveMessage();
|
||||
if (message == null) {
|
||||
System.exit(0);
|
||||
} else {
|
||||
System.out.println("Received message: " + message.getBody().get());
|
||||
}
|
||||
} catch (IOException | InvalidVersionException e) {
|
||||
System.out.println("Receive message: " + e.getMessage());
|
||||
}
|
||||
break;
|
||||
}
|
||||
m.save();
|
||||
}
|
||||
}
|
181
src/main/java/cli/Manager.java
Normal file
181
src/main/java/cli/Manager.java
Normal file
|
@ -0,0 +1,181 @@
|
|||
/**
|
||||
* Copyright (C) 2015 AsamK
|
||||
* <p>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* This program 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 General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cli;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.json.JSONObject;
|
||||
import org.whispersystems.libaxolotl.IdentityKeyPair;
|
||||
import org.whispersystems.libaxolotl.InvalidKeyException;
|
||||
import org.whispersystems.libaxolotl.InvalidVersionException;
|
||||
import org.whispersystems.libaxolotl.state.PreKeyRecord;
|
||||
import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
|
||||
import org.whispersystems.libaxolotl.util.KeyHelper;
|
||||
import org.whispersystems.libaxolotl.util.guava.Optional;
|
||||
import org.whispersystems.textsecure.api.TextSecureAccountManager;
|
||||
import org.whispersystems.textsecure.api.TextSecureMessagePipe;
|
||||
import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
|
||||
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
||||
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
|
||||
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
|
||||
import org.whispersystems.textsecure.api.push.TrustStore;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
public class Manager {
|
||||
private final static String URL = "https://textsecure-service.whispersystems.org";
|
||||
private final static TrustStore TRUST_STORE = new WhisperTrustStore();
|
||||
|
||||
private final static String settingsPath = System.getProperty("user.home") + "/.config/textsecure";
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
private String signalingKey;
|
||||
|
||||
private boolean registered = false;
|
||||
|
||||
private JsonAxolotlStore axolotlStore;
|
||||
TextSecureAccountManager accountManager;
|
||||
|
||||
public Manager(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
private String getFileName() {
|
||||
String path = settingsPath + "/data";
|
||||
new File(path).mkdirs();
|
||||
return path + "/" + username;
|
||||
}
|
||||
|
||||
public boolean userExists() {
|
||||
File f = new File(getFileName());
|
||||
if (!f.exists() || f.isDirectory()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean userHasKeys() {
|
||||
return axolotlStore != null;
|
||||
}
|
||||
|
||||
public void load() throws IOException, InvalidKeyException {
|
||||
JSONObject in = new JSONObject(IOUtils.toString(new FileInputStream(getFileName())));
|
||||
username = in.getString("username");
|
||||
password = in.getString("password");
|
||||
signalingKey = in.getString("signalingKey");
|
||||
axolotlStore = new JsonAxolotlStore(in.getJSONObject("axolotlStore"));
|
||||
registered = in.getBoolean("registered");
|
||||
accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password);
|
||||
}
|
||||
|
||||
public void save() {
|
||||
String out = new JSONObject().put("username", username)
|
||||
.put("password", password)
|
||||
.put("signalingKey", signalingKey)
|
||||
.put("axolotlStore", axolotlStore.getJson())
|
||||
.put("registered", registered).toString();
|
||||
try {
|
||||
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getFileName()));
|
||||
writer.write(out);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Saving file error: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void createNewIdentity() {
|
||||
IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();
|
||||
int registrationId = KeyHelper.generateRegistrationId(false);
|
||||
axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
|
||||
registered = false;
|
||||
}
|
||||
|
||||
public boolean isRegistered() {
|
||||
return registered;
|
||||
}
|
||||
|
||||
public void register() throws IOException {
|
||||
password = Util.getSecret(18);
|
||||
|
||||
accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password);
|
||||
|
||||
accountManager.requestSmsVerificationCode();
|
||||
registered = false;
|
||||
}
|
||||
|
||||
public void verifyAccount(String verificationCode) throws IOException {
|
||||
verificationCode = verificationCode.replace("-", "");
|
||||
signalingKey = Util.getSecret(52);
|
||||
accountManager.verifyAccount(verificationCode, signalingKey, false, axolotlStore.getLocalRegistrationId());
|
||||
|
||||
//accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));
|
||||
registered = true;
|
||||
int start = 0;
|
||||
List<PreKeyRecord> oneTimePreKeys = KeyHelper.generatePreKeys(start, 100);
|
||||
PreKeyRecord lastResortKey = KeyHelper.generateLastResortPreKey();
|
||||
int signedPreKeyId = 0;
|
||||
SignedPreKeyRecord signedPreKeyRecord;
|
||||
try {
|
||||
signedPreKeyRecord = KeyHelper.generateSignedPreKey(axolotlStore.getIdentityKeyPair(), signedPreKeyId);
|
||||
} catch (InvalidKeyException e) {
|
||||
// Should really not happen
|
||||
System.out.println("invalid key");
|
||||
return;
|
||||
}
|
||||
accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
|
||||
}
|
||||
|
||||
public TextSecureMessageSender getMessageSender() {
|
||||
return new TextSecureMessageSender(URL, TRUST_STORE, username, password,
|
||||
axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
|
||||
}
|
||||
|
||||
public TextSecureMessage receiveMessage() throws IOException, InvalidVersionException {
|
||||
TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey);
|
||||
TextSecureMessagePipe messagePipe = null;
|
||||
|
||||
try {
|
||||
messagePipe = messageReceiver.createMessagePipe();
|
||||
|
||||
TextSecureEnvelope envelope;
|
||||
try {
|
||||
envelope = messagePipe.read(5, TimeUnit.SECONDS);
|
||||
} catch (TimeoutException e) {
|
||||
return null;
|
||||
}
|
||||
TextSecureCipher cipher = new TextSecureCipher(axolotlStore);
|
||||
TextSecureMessage message = null;
|
||||
try {
|
||||
message = cipher.decrypt(envelope);
|
||||
} catch (Exception e) {
|
||||
// TODO handle all exceptions
|
||||
e.printStackTrace();
|
||||
}
|
||||
return message;
|
||||
} finally {
|
||||
if (messagePipe != null)
|
||||
messagePipe.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
25
src/main/java/cli/Util.java
Normal file
25
src/main/java/cli/Util.java
Normal file
|
@ -0,0 +1,25 @@
|
|||
package cli;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class Util {
|
||||
public static String getSecret(int size) {
|
||||
byte[] secret = getSecretBytes(size);
|
||||
return Base64.encodeBytes(secret);
|
||||
}
|
||||
|
||||
public static byte[] getSecretBytes(int size) {
|
||||
byte[] secret = new byte[size];
|
||||
getSecureRandom().nextBytes(secret);
|
||||
return secret;
|
||||
}
|
||||
|
||||
public static SecureRandom getSecureRandom() {
|
||||
try {
|
||||
return SecureRandom.getInstance("SHA1PRNG");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
}
|
20
src/main/java/cli/WhisperTrustStore.java
Normal file
20
src/main/java/cli/WhisperTrustStore.java
Normal file
|
@ -0,0 +1,20 @@
|
|||
package cli;
|
||||
|
||||
import org.whispersystems.textsecure.api.push.TrustStore;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class WhisperTrustStore implements TrustStore {
|
||||
|
||||
@Override
|
||||
public InputStream getKeyStoreInputStream() {
|
||||
return cli.WhisperTrustStore.class.getResourceAsStream("whisper.store");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKeyStorePassword() {
|
||||
return "whisper";
|
||||
}
|
||||
}
|
BIN
src/main/resources/cli/whisper.store
Normal file
BIN
src/main/resources/cli/whisper.store
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue