mirror of
https://github.com/AsamK/signal-cli.git
synced 2026-10-29 09:19:29 +00:00
Merge branch 'master' into notification-profiles
This commit is contained in:
commit
0534854c6c
13
CHANGELOG.md
13
CHANGELOG.md
@ -7,7 +7,20 @@
|
||||
- Sync notification profiles via storage sync
|
||||
- New `listNotificationProfiles` command to show notification profiles and the current manual override
|
||||
- New `setNotificationProfile` command to manually turn a notification profile on for any duration or until any point in time, or off
|
||||
|
||||
## [0.14.9] - 2026-10-07
|
||||
|
||||
### Added
|
||||
|
||||
- `send --attachment-dimensions` and `--attachment-blurhash` to set the placeholder shown before an attachment is downloaded
|
||||
- Support linking phone-numberless Signal accounts
|
||||
- Support recovering accounts with a recovery key
|
||||
- Support replaying missed SSE events and configuring keep-alive intervals
|
||||
|
||||
### Improved
|
||||
|
||||
- Show the actual error for failed requests
|
||||
- Improve attachment upload and download failure handling
|
||||
|
||||
## [0.14.8] - 2026-09-10
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ It supports registering, verifying, sending and receiving messages.
|
||||
signal-cli uses a [patched libsignal-service-java](https://github.com/Turasa/libsignal-service-java),
|
||||
extracted from the [Signal-Android source code](https://github.com/signalapp/Signal-Android/tree/main/lib/libsignal-service).
|
||||
For registering you need a phone number where you can receive SMS or incoming calls.
|
||||
Existing accounts without a phone number can be linked from a compatible Signal mobile app.
|
||||
|
||||
signal-cli is primarily intended to be used on servers to notify admins of important events.
|
||||
For this use-case, it has a daemon mode with JSON-RPC interface ([man page](https://github.com/AsamK/signal-cli/blob/master/man/signal-cli-jsonrpc.5.adoc))
|
||||
@ -60,14 +61,16 @@ For a complete usage overview please read
|
||||
the [man page](https://github.com/AsamK/signal-cli/blob/master/man/signal-cli.1.adoc) and
|
||||
the [wiki](https://github.com/AsamK/signal-cli/wiki).
|
||||
|
||||
Important: The ACCOUNT is your phone number in international format and must include the country calling code. Hence it
|
||||
For a numbered account, ACCOUNT is your phone number in international format and must include the country calling code. Hence it
|
||||
should start with a "+" sign. (See [Wikipedia](https://en.wikipedia.org/wiki/List_of_country_calling_codes) for a list
|
||||
of all country codes.)
|
||||
For a linked account without a phone number, use its ACI (Account ID) instead.
|
||||
See the [wiki](https://github.com/AsamK/signal-cli/wiki) for further documentation.
|
||||
|
||||
* Link to an existing account
|
||||
|
||||
If you have an existing Signal account associated
|
||||
with a number, you can link signal-cli to it with:
|
||||
If you have an existing Signal account, with or without a phone number,
|
||||
you can link signal-cli to it with:
|
||||
|
||||
signal-cli link
|
||||
|
||||
|
||||
@ -5,12 +5,12 @@ plugins {
|
||||
application
|
||||
eclipse
|
||||
`check-lib-versions`
|
||||
id("org.graalvm.buildtools.native") version "1.1.12"
|
||||
id("org.graalvm.buildtools.native") version "1.1.14"
|
||||
}
|
||||
|
||||
allprojects {
|
||||
group = "org.asamk"
|
||||
version = "0.14.9-SNAPSHOT"
|
||||
version = "0.14.10-SNAPSHOT"
|
||||
}
|
||||
|
||||
java {
|
||||
|
||||
6
client/Cargo.lock
generated
6
client/Cargo.lock
generated
@ -850,9 +850,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.23.44"
|
||||
version = "0.23.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6725596c3f2c3a0aef021139e145d4eafe314a6623e4680ca83852b2c67ab2ba"
|
||||
checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
@ -1014,7 +1014,7 @@ checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "signal-cli-client"
|
||||
version = "0.14.8"
|
||||
version = "0.14.9"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bytes",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "signal-cli-client"
|
||||
version = "0.14.8"
|
||||
version = "0.14.9"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -45,6 +45,9 @@
|
||||
<content_attribute id="social-chat">intense</content_attribute>
|
||||
</content_rating>
|
||||
<releases>
|
||||
<release version="0.14.9" date="2026-10-07">
|
||||
<url type="details">https://github.com/AsamK/signal-cli/releases/tag/v0.14.9</url>
|
||||
</release>
|
||||
<release version="0.14.8" date="2026-09-10">
|
||||
<url type="details">https://github.com/AsamK/signal-cli/releases/tag/v0.14.8</url>
|
||||
</release>
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
[versions]
|
||||
slf4j = "2.0.19"
|
||||
slf4j = "2.0.20"
|
||||
coroutines = "1.11.0"
|
||||
junit = "6.1.3"
|
||||
micronaut-json-schema = "2.2.0"
|
||||
micronaut-core = "5.2.0"
|
||||
signal-service = "2.15.3_unofficial_153"
|
||||
micronaut-json-schema = "2.3.1"
|
||||
micronaut-core = "5.2.14"
|
||||
signal-service = "2.15.3_unofficial_155"
|
||||
|
||||
[libraries]
|
||||
bouncycastle = "org.bouncycastle:bcprov-jdk18on:1.85.2"
|
||||
jackson-databind = "com.fasterxml.jackson.core:jackson-databind:2.22.2"
|
||||
bouncycastle = "org.bouncycastle:bcprov-jdk18on:1.86"
|
||||
jackson-databind = "com.fasterxml.jackson.core:jackson-databind:2.22.3"
|
||||
argparse4j = "net.sourceforge.argparse4j:argparse4j:0.9.0"
|
||||
dbusjava = "com.github.hypfvieh:dbus-java-transport-native-unixsocket:5.0.0"
|
||||
zxing = "com.google.zxing:core:3.5.4"
|
||||
@ -18,7 +18,7 @@ micronaut-json-schema-generator = { module = "io.micronaut.jsonschema:micronaut-
|
||||
micronaut-inject-java = { module = "io.micronaut:micronaut-inject-java", version.ref = "micronaut-core" }
|
||||
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
|
||||
slf4j-jul = { module = "org.slf4j:jul-to-slf4j", version.ref = "slf4j" }
|
||||
logback = "ch.qos.logback:logback-classic:1.6.3"
|
||||
logback = "ch.qos.logback:logback-classic:1.6.5"
|
||||
|
||||
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
|
||||
signalnetwork = { module = "com.github.turasa:signal-network", version.ref = "signal-service" }
|
||||
|
||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-9.7.1-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-9.8.0-bin.zip
|
||||
networkTimeout=10000
|
||||
retries=0
|
||||
retryBackOffMs=500
|
||||
|
||||
52
gradlew.bat
vendored
52
gradlew.bat
vendored
@ -26,6 +26,33 @@
|
||||
@rem Set local scope for the variables, and ensure extensions are enabled
|
||||
setlocal EnableExtensions
|
||||
|
||||
@rem Catch executions from older scripts and ensure they exit cleanly.
|
||||
@rem This can be removed once we can be reasonably confident that few people
|
||||
@rem will be migrating directly to this new wrapper.
|
||||
goto afterSafetyNet
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
goto exitWithErrorLevel
|
||||
:afterSafetyNet
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%"=="" set DIRNAME=.
|
||||
@rem This is normally unused
|
||||
@ -45,13 +72,14 @@ set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
1>&2 echo.
|
||||
1>&2 echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
1>&2 echo.
|
||||
1>&2 echo Please set the JAVA_HOME variable in your environment to match the
|
||||
1>&2 echo location of your Java installation.
|
||||
|
||||
"%COMSPEC%" /c exit 1
|
||||
goto exitWithErrorLevel
|
||||
|
||||
:findJavaFromJavaHome
|
||||
set JAVA_HOME=%JAVA_HOME:"=%
|
||||
@ -59,13 +87,14 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||
|
||||
if exist "%JAVA_EXE%" goto execute
|
||||
|
||||
echo. 1>&2
|
||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||
echo. 1>&2
|
||||
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||
echo location of your Java installation. 1>&2
|
||||
1>&2 echo.
|
||||
1>&2 echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||
1>&2 echo.
|
||||
1>&2 echo Please set the JAVA_HOME variable in your environment to match the
|
||||
1>&2 echo location of your Java installation.
|
||||
|
||||
"%COMSPEC%" /c exit 1
|
||||
goto exitWithErrorLevel
|
||||
|
||||
:execute
|
||||
@rem Setup the command line
|
||||
@ -75,8 +104,9 @@ echo location of your Java installation. 1>&2
|
||||
@rem Execute gradlew
|
||||
@rem endlocal doesn't take effect until after the line is parsed and variables are expanded
|
||||
@rem which allows us to clear the local environment before executing the java command
|
||||
endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel
|
||||
endlocal & "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* & call :exitWithErrorLevel & goto exitWithErrorLevel
|
||||
|
||||
@rem This label must not be changed. We rely on old scripts being able to jump to this point.
|
||||
:exitWithErrorLevel
|
||||
@rem Use "%COMSPEC%" /c exit to allow operators to work properly in scripts
|
||||
"%COMSPEC%" /c exit %ERRORLEVEL%
|
||||
|
||||
@ -101,6 +101,14 @@ public interface Manager extends Closeable {
|
||||
|
||||
String getSelfACI();
|
||||
|
||||
/**
|
||||
* Returns the phone number for numbered accounts, or the ACI for numberless accounts.
|
||||
*/
|
||||
default String getSelfIdentifier() {
|
||||
final var number = getSelfNumber();
|
||||
return number != null ? number : getSelfACI();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is used for checking a set of phone numbers for registration on Signal
|
||||
*
|
||||
|
||||
@ -8,14 +8,16 @@ import java.util.function.Consumer;
|
||||
|
||||
public interface MultiAccountManager extends AutoCloseable {
|
||||
|
||||
List<String> getAccountNumbers();
|
||||
|
||||
List<Manager> getManagers();
|
||||
|
||||
void addOnManagerAddedHandler(Consumer<Manager> handler);
|
||||
|
||||
void removeOnManagerAddedHandler(Consumer<Manager> handler);
|
||||
|
||||
void addOnManagerRemovedHandler(Consumer<Manager> handler);
|
||||
|
||||
void removeOnManagerRemovedHandler(Consumer<Manager> handler);
|
||||
|
||||
Manager getManager(String phoneNumber);
|
||||
|
||||
URI getNewProvisioningDeviceLinkUri() throws TimeoutException, IOException;
|
||||
|
||||
@ -10,5 +10,8 @@ public interface ProvisioningManager {
|
||||
|
||||
URI getDeviceLinkUri() throws TimeoutException, IOException;
|
||||
|
||||
/**
|
||||
* Completes linking and returns the account's phone number, or ACI if it has no number.
|
||||
*/
|
||||
String finishDeviceLink(String deviceName) throws IOException, TimeoutException, UserAlreadyExistsException;
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
|
||||
import org.asamk.signal.manager.api.PinLockMissingException;
|
||||
import org.asamk.signal.manager.api.PinLockedException;
|
||||
import org.asamk.signal.manager.api.RateLimitException;
|
||||
import org.asamk.signal.manager.api.TwoFactorRequired;
|
||||
import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
|
||||
|
||||
import java.io.Closeable;
|
||||
@ -17,13 +18,19 @@ public interface RegistrationManager extends Closeable {
|
||||
boolean voiceVerification,
|
||||
String captcha,
|
||||
final boolean forceRegister
|
||||
) throws IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException, RateLimitException, VerificationMethodNotAvailableException;
|
||||
) throws IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException, RateLimitException, TwoFactorRequired, VerificationMethodNotAvailableException;
|
||||
|
||||
void verifyAccount(
|
||||
String verificationCode,
|
||||
String pin
|
||||
) throws IOException, PinLockedException, IncorrectPinException, PinLockMissingException;
|
||||
|
||||
void registerWithRecoveryKey(
|
||||
String recoveryKey,
|
||||
boolean forceRegister,
|
||||
Integer totp
|
||||
) throws IOException, RateLimitException, TwoFactorRequired;
|
||||
|
||||
void deleteLocalAccountData() throws IOException;
|
||||
|
||||
boolean isRegistered();
|
||||
|
||||
@ -25,6 +25,7 @@ import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SignalAccountFiles {
|
||||
|
||||
@ -65,15 +66,26 @@ public class SignalAccountFiles {
|
||||
return accountsStore.getAllNumbers();
|
||||
}
|
||||
|
||||
public Set<String> getAllLocalAccountIdentifiers() throws IOException {
|
||||
return accountsStore.getAllAccounts()
|
||||
.stream()
|
||||
.map(a -> a.number() != null ? a.number() : a.uuid())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public MultiAccountManager initMultiAccountManager() throws IOException {
|
||||
final var managerPairs = accountsStore.getAllAccounts().parallelStream().map(a -> {
|
||||
final var identifier = a.number() != null ? a.number() : a.uuid();
|
||||
try {
|
||||
return new Pair<Manager, Throwable>(initManagerByNumber(a.number(), a.path()), null);
|
||||
final var manager = a.number() != null
|
||||
? initManagerByNumber(a.number(), a.path())
|
||||
: initManagerByAci(ACI.parseOrThrow(a.uuid()), a.path());
|
||||
return new Pair<Manager, Throwable>(manager, null);
|
||||
} catch (NotRegisteredException e) {
|
||||
logger.warn("Ignoring {}: {} ({})", a.number(), e.getMessage(), e.getClass().getSimpleName());
|
||||
logger.warn("Ignoring {}: {} ({})", identifier, e.getMessage(), e.getClass().getSimpleName());
|
||||
return null;
|
||||
} catch (AccountCheckException | IOException e) {
|
||||
logger.error("Failed to load {}: {} ({})", a.number(), e.getMessage(), e.getClass().getSimpleName());
|
||||
logger.error("Failed to load {}: {} ({})", identifier, e.getMessage(), e.getClass().getSimpleName());
|
||||
return new Pair<Manager, Throwable>(null, e);
|
||||
}
|
||||
}).filter(Objects::nonNull).toList();
|
||||
@ -199,6 +211,11 @@ public class SignalAccountFiles {
|
||||
String number,
|
||||
Consumer<Manager> newManagerListener
|
||||
) throws IOException {
|
||||
final var aci = ACI.parseOrNull(number);
|
||||
if (aci != null) {
|
||||
return initRegistrationManager(aci, newManagerListener);
|
||||
}
|
||||
|
||||
final var accountPath = accountsStore.getPathByNumber(number);
|
||||
if (accountPath == null || !SignalAccount.accountFileExists(pathConfig.dataPath(), accountPath)) {
|
||||
final var newAccountPath = accountPath == null ? accountsStore.addAccount(number, null) : accountPath;
|
||||
@ -238,4 +255,43 @@ public class SignalAccountFiles {
|
||||
newManagerListener,
|
||||
new AccountFileUpdaterImpl(accountsStore, accountPath));
|
||||
}
|
||||
|
||||
private RegistrationManager initRegistrationManager(
|
||||
final ACI aci,
|
||||
final Consumer<Manager> newManagerListener
|
||||
) throws IOException {
|
||||
final var accountPath = accountsStore.getPathByAci(aci);
|
||||
if (accountPath == null || !SignalAccount.accountFileExists(pathConfig.dataPath(), accountPath)) {
|
||||
final var newAccountPath = accountPath == null ? accountsStore.addAccount(null, aci) : accountPath;
|
||||
final var account = SignalAccount.create(pathConfig.dataPath(),
|
||||
newAccountPath,
|
||||
null,
|
||||
aci,
|
||||
serviceEnvironment,
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.createProfileKey(),
|
||||
settings);
|
||||
account.initDatabase();
|
||||
return new RegistrationManagerImpl(account,
|
||||
pathConfig,
|
||||
serviceEnvironmentConfig,
|
||||
userAgent,
|
||||
newManagerListener,
|
||||
new AccountFileUpdaterImpl(accountsStore, newAccountPath));
|
||||
}
|
||||
|
||||
final var account = SignalAccount.load(pathConfig.dataPath(), accountPath, true, settings);
|
||||
if (!aci.equals(account.getAci())) {
|
||||
account.close();
|
||||
throw new IOException("ACI in account file doesn't match expected ACI: " + account.getAci());
|
||||
}
|
||||
account.initDatabase();
|
||||
return new RegistrationManagerImpl(account,
|
||||
pathConfig,
|
||||
serviceEnvironmentConfig,
|
||||
userAgent,
|
||||
newManagerListener,
|
||||
new AccountFileUpdaterImpl(accountsStore, accountPath));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
package org.asamk.signal.manager.api;
|
||||
|
||||
import org.signal.libsignal.net.BadRequestError;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class BadRequestException extends IOException {
|
||||
|
||||
private final BadRequestError error;
|
||||
|
||||
public BadRequestException(final BadRequestError error) {
|
||||
super(error.toString());
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public BadRequestError getError() {
|
||||
return error;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package org.asamk.signal.manager.api;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class TwoFactorRequired extends IOException {
|
||||
|
||||
public TwoFactorRequired() {
|
||||
super("A TOTP token is required or the supplied token is incorrect");
|
||||
}
|
||||
}
|
||||
@ -3,7 +3,6 @@ package org.asamk.signal.manager.config;
|
||||
import org.signal.libsignal.net.Network.Environment;
|
||||
import org.signal.libsignal.protocol.InvalidKeyException;
|
||||
import org.signal.libsignal.protocol.ecc.ECPublicKey;
|
||||
import org.signal.network.config.HttpProxy;
|
||||
import org.signal.network.config.SignalCdnUrl;
|
||||
import org.signal.network.config.SignalCdsiUrl;
|
||||
import org.signal.network.config.SignalProxy;
|
||||
@ -44,7 +43,6 @@ class LiveConfig {
|
||||
|
||||
private static final Optional<Dns> dns = Optional.empty();
|
||||
private static final Optional<SignalProxy> proxy = Optional.empty();
|
||||
private static final Optional<HttpProxy> systemProxy = Optional.empty();
|
||||
|
||||
private static final byte[] zkGroupServerPublicParams = Base64.getDecoder()
|
||||
.decode("AMhf5ywVwITZMsff/eCyudZx9JDmkkkbV6PInzG4p8x3VqVJSFiMvnvlEKWuRob/1eaIetR31IYeAbm0NdOuHH8Qi+Rexi1wLlpzIo1gstHWBfZzy1+qHRV5A4TqPp15YzBPm0WSggW6PbSn+F4lf57VCnHF7p8SvzAA2ZZJPYJURt8X7bbg+H3i+PEjH9DXItNEqs2sNcug37xZQDLm7X36nOoGPs54XsEGzPdEV+itQNGUFEjY6X9Uv+Acuks7NpyGvCoKxGwgKgE5XyJ+nNKlyHHOLb6N1NuHyBrZrgtY/JYJHRooo5CEqYKBqdFnmbTVGEkCvJKxLnjwKWf+fEPoWeQFj5ObDjcKMZf2Jm2Ae69x+ikU5gBXsRmoF94GXTLfN0/vLt98KDPnxwAQL9j5V1jGOY8jQl6MLxEs56cwXN0dqCnImzVH3TZT1cJ8SW1BRX6qIVxEzjsSGx3yxF3suAilPMqGRp4ffyopjMD1JXiKR2RwLKzizUe5e8XyGOy9fplzhw3jVzTRyUZTRSZKkMLWcQ/gv0E4aONNqs4P+NameAZYOD12qRkxosQQP5uux6B2nRyZ7sAV54DgFyLiRcq1FvwKw2EPQdk4HDoePrO/RNUbyNddnM/mMgj4FW65xCoT1LmjrIjsv/Ggdlx46ueczhMgtBunx1/w8k8V+l8LVZ8gAT6wkU5J+DPQalQguMg12Jzug3q4TbdHiGCmD9EunCwOmsLuLJkz6EcSYXtrlDEnAM+hicw7iergYLLlMXpfTdGxJCWJmP4zqUFeTTmsmhsjGBt7NiEB/9pFFEB3pSbf4iiUukw63Eo8Aqnf4iwob6X1QviCWuc8t0LUlT9vALgh/f2DPVOOmR0RW6bgRvc7DSF20V/omg+YBw==");
|
||||
@ -72,7 +70,6 @@ class LiveConfig {
|
||||
interceptors,
|
||||
dns,
|
||||
proxy,
|
||||
systemProxy,
|
||||
zkGroupServerPublicParams,
|
||||
genericServerPublicParams,
|
||||
backupServerPublicParams,
|
||||
|
||||
@ -28,11 +28,14 @@ public class ServiceConfig {
|
||||
public static final int MAXIMUM_ONE_OFF_REQUEST_SIZE = 3;
|
||||
public static final long UNREGISTERED_LIFESPAN = TimeUnit.DAYS.toMillis(30);
|
||||
|
||||
public static AccountAttributes.Capabilities getCapabilities(boolean isPrimaryDevice) {
|
||||
public static AccountAttributes.Capabilities getCapabilities(
|
||||
final boolean isPrimaryDevice,
|
||||
final boolean hasPhoneNumber
|
||||
) {
|
||||
final var attachmentBackfill = !isPrimaryDevice;
|
||||
final var spqr = true;
|
||||
final var usernameSyncChangeMessage = !isPrimaryDevice;
|
||||
final var optionalPhoneNumber = !isPrimaryDevice;
|
||||
final var optionalPhoneNumber = !isPrimaryDevice || !hasPhoneNumber;
|
||||
return new AccountAttributes.Capabilities(true,
|
||||
true,
|
||||
attachmentBackfill,
|
||||
|
||||
@ -3,7 +3,6 @@ package org.asamk.signal.manager.config;
|
||||
import org.signal.libsignal.net.Network;
|
||||
import org.signal.libsignal.protocol.InvalidKeyException;
|
||||
import org.signal.libsignal.protocol.ecc.ECPublicKey;
|
||||
import org.signal.network.config.HttpProxy;
|
||||
import org.signal.network.config.SignalCdnUrl;
|
||||
import org.signal.network.config.SignalCdsiUrl;
|
||||
import org.signal.network.config.SignalProxy;
|
||||
@ -44,7 +43,6 @@ class StagingConfig {
|
||||
|
||||
private static final Optional<Dns> dns = Optional.empty();
|
||||
private static final Optional<SignalProxy> proxy = Optional.empty();
|
||||
private static final Optional<HttpProxy> systemProxy = Optional.empty();
|
||||
|
||||
private static final byte[] zkGroupServerPublicParams = Base64.getDecoder()
|
||||
.decode("ABSY21VckQcbSXVNCGRYJcfWHiAMZmpTtTELcDmxgdFbtp/bWsSxZdMKzfCp8rvIs8ocCU3B37fT3r4Mi5qAemeGeR2X+/YmOGR5ofui7tD5mDQfstAI9i+4WpMtIe8KC3wU5w3Inq3uNWVmoGtpKndsNfwJrCg0Hd9zmObhypUnSkfYn2ooMOOnBpfdanRtrvetZUayDMSC5iSRcXKpdlukrpzzsCIvEwjwQlJYVPOQPj4V0F4UXXBdHSLK05uoPBCQG8G9rYIGedYsClJXnbrgGYG3eMTG5hnx4X4ntARBgELuMWWUEEfSK0mjXg+/2lPmWcTZWR9nkqgQQP0tbzuiPm74H2wMO4u1Wafe+UwyIlIT9L7KLS19Aw8r4sPrXZSSsOZ6s7M1+rTJN0bI5CKY2PX29y5Ok3jSWufIKcgKOnWoP67d5b2du2ZVJjpjfibNIHbT/cegy/sBLoFwtHogVYUewANUAXIaMPyCLRArsKhfJ5wBtTminG/PAvuBdJ70Z/bXVPf8TVsR292zQ65xwvWTejROW6AZX6aqucUjlENAErBme1YHmOSpU6tr6doJ66dPzVAWIanmO/5mgjNEDeK7DDqQdB1xd03HT2Qs2TxY3kCK8aAb/0iM0HQiXjxZ9HIgYhbtvGEnDKW5ILSUydqH/KBhW4Pb0jZWnqN/YgbWDKeJxnDbYcUob5ZY5Lt5ZCMKuaGUvCJRrCtuugSMaqjowCGRempsDdJEt+cMaalhZ6gczklJB/IbdwENW9KeVFPoFNFzhxWUIS5ML9riVYhAtE6JE5jX0xiHNVIIPthb458cfA8daR0nYfYAUKogQArm0iBezOO+mPk5vCNWI+wwkyFCqNDXz/qxl1gAntuCJtSfq9OC3NkdhQlgYQ==");
|
||||
@ -72,7 +70,6 @@ class StagingConfig {
|
||||
interceptors,
|
||||
dns,
|
||||
proxy,
|
||||
systemProxy,
|
||||
zkGroupServerPublicParams,
|
||||
genericServerPublicParams,
|
||||
backupServerPublicParams,
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.api.BadRequestException;
|
||||
import org.asamk.signal.manager.api.CaptchaRequiredException;
|
||||
import org.asamk.signal.manager.api.DeviceLinkUrl;
|
||||
import org.asamk.signal.manager.api.IncorrectPinException;
|
||||
@ -27,6 +28,7 @@ import org.signal.libsignal.protocol.state.SignedPreKeyRecord;
|
||||
import org.signal.libsignal.protocol.util.KeyHelper;
|
||||
import org.signal.libsignal.usernames.BaseUsernameException;
|
||||
import org.signal.libsignal.usernames.Username;
|
||||
import org.signal.network.api.AccountApiV2.SetAccountAttributesError;
|
||||
import org.signal.network.exceptions.NonSuccessfulResponseCodeException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -96,10 +98,10 @@ public class AccountHelper {
|
||||
} else {
|
||||
context.getPreKeyHelper().refreshPreKeysIfNecessary();
|
||||
}
|
||||
if (account.getPni() == null) {
|
||||
if (account.getPni() == null && account.getNumber() != null) {
|
||||
checkWhoAmiI();
|
||||
}
|
||||
if (!account.isPrimaryDevice() && account.getPniIdentityKeyPair() == null) {
|
||||
if (!account.isPrimaryDevice() && account.getPni() != null && account.getPniIdentityKeyPair() == null) {
|
||||
throw new IOException("Missing PNI identity key, relinking required");
|
||||
}
|
||||
if (account.getPreviousStorageVersion() < 10
|
||||
@ -138,8 +140,9 @@ public class AccountHelper {
|
||||
final var whoAmI = dependencies.getAccountManager().getWhoAmI();
|
||||
final var number = whoAmI.getNumber();
|
||||
final var aci = ACI.parseOrThrow(whoAmI.getAci());
|
||||
final var pni = PNI.parseOrThrow(whoAmI.getPni());
|
||||
if (number.equals(account.getNumber()) && aci.equals(account.getAci()) && pni.equals(account.getPni())) {
|
||||
final var pni = whoAmI.getPni() == null ? null : PNI.parseOrThrow(whoAmI.getPni());
|
||||
if (Objects.equals(number, account.getNumber()) && aci.equals(account.getAci()) && Objects.equals(pni,
|
||||
account.getPni())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -150,7 +153,7 @@ public class AccountHelper {
|
||||
account.setNumber(number);
|
||||
account.setAci(aci);
|
||||
account.setPni(pni);
|
||||
if (account.isPrimaryDevice() && account.getPniIdentityKeyPair() == null) {
|
||||
if (pni != null && account.isPrimaryDevice() && account.getPniIdentityKeyPair() == null) {
|
||||
account.setPniIdentityKeyPair(KeyUtils.generateIdentityKeyPair());
|
||||
}
|
||||
account.getRecipientTrustedResolver().resolveSelfRecipientTrusted(account.getSelfRecipientAddress());
|
||||
@ -557,14 +560,27 @@ public class AccountHelper {
|
||||
final List<DeviceInfo> devices = handleResponseExceptionSuspend(cont -> dependencies.getLinkDeviceApi()
|
||||
.getDevices(cont));
|
||||
final var deviceId = account.getDeviceId();
|
||||
final var device = devices.stream().filter(d -> d.id == deviceId).findFirst();
|
||||
final var device = devices.stream().filter(d -> d.getId() == deviceId).findFirst();
|
||||
if (device.isPresent()) {
|
||||
account.setEncryptedDeviceName(device.get().name);
|
||||
account.setEncryptedDeviceName(device.get().getName());
|
||||
}
|
||||
}
|
||||
|
||||
public void updateAccountAttributes() throws IOException {
|
||||
handleResponseException(dependencies.getAccountApi().setAccountAttributes(account.getAccountAttributes(null)));
|
||||
if (account.getNumber() != null) {
|
||||
handleResponseException(dependencies.getAccountApi()
|
||||
.setAccountAttributes(account.getAccountAttributes(null)));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
handleResponseExceptionSuspend(cont -> dependencies.getAccountApiV2()
|
||||
.setAccountAttributes(account.getAccountAttributesV2(), cont));
|
||||
} catch (BadRequestException e) {
|
||||
if (e.getError() instanceof SetAccountAttributesError.Unauthorized) {
|
||||
throw new AuthorizationFailedException(401, "Authorization failed!");
|
||||
}
|
||||
throw new IOException("Account attribute update rate limited; try again later");
|
||||
}
|
||||
}
|
||||
|
||||
public void addDevice(DeviceLinkUrl deviceLinkInfo) throws IOException, org.asamk.signal.manager.api.DeviceLimitExceededException {
|
||||
|
||||
@ -2,11 +2,13 @@ package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||
import org.asamk.signal.manager.api.Message.AttachmentDimensions;
|
||||
import org.asamk.signal.manager.api.Pair;
|
||||
import org.asamk.signal.manager.config.ServiceConfig;
|
||||
import org.asamk.signal.manager.internal.SignalDependencies;
|
||||
import org.asamk.signal.manager.storage.AttachmentStore;
|
||||
import org.asamk.signal.manager.util.AttachmentUtils;
|
||||
import org.asamk.signal.manager.util.IOUtils;
|
||||
import org.asamk.signal.manager.util.MimeUtils;
|
||||
import org.asamk.signal.manager.util.Utils;
|
||||
import org.signal.libsignal.protocol.InvalidMessageException;
|
||||
import org.slf4j.Logger;
|
||||
@ -21,6 +23,7 @@ import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
import org.whispersystems.signalservice.internal.crypto.PaddingInputStream;
|
||||
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -28,6 +31,8 @@ import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class AttachmentHelper {
|
||||
|
||||
@ -35,12 +40,21 @@ public class AttachmentHelper {
|
||||
|
||||
private final SignalDependencies dependencies;
|
||||
private final AttachmentStore attachmentStore;
|
||||
private final Context context;
|
||||
private final Uploader uploader;
|
||||
private final Supplier<File> dataPath;
|
||||
|
||||
public AttachmentHelper(final Context context) {
|
||||
this.context = context;
|
||||
this.dependencies = context.getDependencies();
|
||||
this.attachmentStore = context.getAttachmentStore();
|
||||
this.uploader = new DependenciesUploader(dependencies);
|
||||
this.dataPath = () -> context.getAccount().getDataPath();
|
||||
}
|
||||
|
||||
AttachmentHelper(final Uploader uploader, final Supplier<File> dataPath) {
|
||||
this.dependencies = null;
|
||||
this.attachmentStore = null;
|
||||
this.uploader = uploader;
|
||||
this.dataPath = dataPath;
|
||||
}
|
||||
|
||||
public File getAttachmentFile(SignalServiceAttachmentPointer pointer) {
|
||||
@ -51,29 +65,36 @@ public class AttachmentHelper {
|
||||
return attachmentStore.retrieveAttachment(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares and uploads all attachments. I/O failures are reported as {@link AttachmentInvalidException}, so
|
||||
* callers know that nothing has been sent yet. Attachments after the failing one are not uploaded.
|
||||
*/
|
||||
public List<SignalServiceAttachment> uploadAttachments(
|
||||
final List<String> attachments,
|
||||
final List<AttachmentDimensions> dimensions,
|
||||
final List<String> blurHashes,
|
||||
boolean voiceNote
|
||||
) throws AttachmentInvalidException, IOException {
|
||||
) throws AttachmentInvalidException {
|
||||
final var attachmentStreams = createAttachmentStreams(attachments, dimensions, blurHashes, voiceNote);
|
||||
|
||||
try {
|
||||
// Upload attachments here, so we only upload once even for multiple recipients
|
||||
final var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
|
||||
for (final var attachmentStream : attachmentStreams) {
|
||||
attachmentPointers.add(uploadAttachment(attachmentStream));
|
||||
for (var i = 0; i < attachmentStreams.size(); i++) {
|
||||
final var attachment = attachments.get(i);
|
||||
try {
|
||||
attachmentPointers.add(uploader.upload(attachmentStreams.get(i)));
|
||||
} catch (IOException e) {
|
||||
throw invalid(describe(attachment, inlineAttachmentLabel(i)), e);
|
||||
}
|
||||
}
|
||||
return attachmentPointers;
|
||||
} finally {
|
||||
for (final var attachmentStream : attachmentStreams) {
|
||||
attachmentStream.close();
|
||||
}
|
||||
closeAll(attachmentStreams);
|
||||
}
|
||||
}
|
||||
|
||||
public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
|
||||
public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException {
|
||||
return uploadAttachments(attachments, List.of(), List.of(), false);
|
||||
}
|
||||
|
||||
@ -82,45 +103,55 @@ public class AttachmentHelper {
|
||||
List<AttachmentDimensions> dimensions,
|
||||
List<String> blurHashes,
|
||||
boolean voiceNote
|
||||
) throws AttachmentInvalidException, IOException {
|
||||
) throws AttachmentInvalidException {
|
||||
if (attachments == null) {
|
||||
return null;
|
||||
return List.of();
|
||||
}
|
||||
final var signalServiceAttachments = new ArrayList<SignalServiceAttachmentStream>(attachments.size());
|
||||
for (var i = 0; i < attachments.size(); i++) {
|
||||
final var size = i < dimensions.size() ? dimensions.get(i) : null;
|
||||
final var blurHash = i < blurHashes.size() && !blurHashes.get(i).isEmpty() ? blurHashes.get(i) : null;
|
||||
signalServiceAttachments.add(getAttachmentStream(attachments.get(i), size, blurHash, voiceNote));
|
||||
signalServiceAttachments.add(getAttachmentStream(attachments.get(i),
|
||||
inlineAttachmentLabel(i),
|
||||
size,
|
||||
blurHash,
|
||||
voiceNote));
|
||||
}
|
||||
return signalServiceAttachments;
|
||||
}
|
||||
|
||||
private SignalServiceAttachmentStream getAttachmentStream(
|
||||
final String attachment,
|
||||
final String inlineLabel,
|
||||
final AttachmentDimensions dimensions,
|
||||
final String blurHash,
|
||||
final boolean voiceNote
|
||||
) throws AttachmentInvalidException {
|
||||
final var label = describe(attachment, inlineLabel);
|
||||
try {
|
||||
// Reject local files that point into the signal-cli data directory
|
||||
if (attachment != null && !attachment.startsWith("data:")) {
|
||||
try {
|
||||
final var file = new File(attachment);
|
||||
final var canonical = file.getCanonicalFile();
|
||||
final var dataPath = context.getAccount().getDataPath().getCanonicalFile();
|
||||
if (canonical.toPath().startsWith(dataPath.toPath())) {
|
||||
throw new AttachmentInvalidException(attachment,
|
||||
new IOException("Attaching files from the signal-cli data directory is not allowed"));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new AttachmentInvalidException(attachment, e);
|
||||
final Pair<StreamDetails, Optional<String>> streamDetailsAndFileName;
|
||||
if (isInline(attachment)) {
|
||||
// Never open inline data as a file, whose errors could echo its payload. Check for the comma
|
||||
// before parsing to avoid regex backtracking over long malformed values.
|
||||
if (attachment.indexOf(',') < 0) {
|
||||
throw new IOException("Invalid data URI");
|
||||
}
|
||||
try {
|
||||
streamDetailsAndFileName = Utils.createStreamDetailsFromDataURI(attachment);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new IOException("Invalid data URI");
|
||||
}
|
||||
} else {
|
||||
// Reject local files that point into the signal-cli data directory
|
||||
final var canonical = new File(attachment).getCanonicalFile();
|
||||
final var dataPath = this.dataPath.get().getCanonicalFile();
|
||||
if (canonical.toPath().startsWith(dataPath.toPath())) {
|
||||
throw new IOException("Attaching files from the signal-cli data directory is not allowed");
|
||||
}
|
||||
streamDetailsAndFileName = Utils.createStreamDetails(attachment);
|
||||
}
|
||||
|
||||
final var streamDetailsAndFileName = Utils.createStreamDetails(attachment);
|
||||
final var streamDetails = streamDetailsAndFileName.first();
|
||||
final var uploadSpec = getResumableUploadSpec(streamDetails);
|
||||
|
||||
return AttachmentUtils.createAttachmentStream(streamDetails,
|
||||
streamDetailsAndFileName.second(),
|
||||
voiceNote,
|
||||
@ -128,7 +159,7 @@ public class AttachmentHelper {
|
||||
blurHash,
|
||||
uploadSpec);
|
||||
} catch (IOException e) {
|
||||
throw new AttachmentInvalidException(attachment, e);
|
||||
throw invalid(label, e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,17 +167,89 @@ public class AttachmentHelper {
|
||||
final var streamLength = streamDetails.getLength();
|
||||
final var ciphertextLength = AttachmentCipherStreamUtil.getCiphertextLength(PaddingInputStream.getPaddedSize(
|
||||
streamLength));
|
||||
return dependencies.getCdnService().getResumableUploadSpecBlocking(ciphertextLength);
|
||||
return uploader.getResumableUploadSpec(ciphertextLength);
|
||||
}
|
||||
|
||||
public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws IOException, AttachmentInvalidException {
|
||||
final var attachmentStream = getAttachmentStream(attachment, null, null, false);
|
||||
return uploadAttachment(attachmentStream);
|
||||
public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws AttachmentInvalidException {
|
||||
return uploadAttachment(attachment, "inline attachment");
|
||||
}
|
||||
|
||||
public SignalServiceAttachmentPointer uploadAttachment(SignalServiceAttachmentStream attachment) throws IOException {
|
||||
var messageSender = dependencies.getMessageSender();
|
||||
return messageSender.uploadAttachment(attachment);
|
||||
return uploader.upload(attachment);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares and uploads a single attachment, such as a quote thumbnail or a link preview image. Any
|
||||
* failure is reported as {@link AttachmentInvalidException} with the given label for inline data.
|
||||
*/
|
||||
public SignalServiceAttachmentPointer uploadAttachment(
|
||||
final String attachment,
|
||||
final String inlineLabel
|
||||
) throws AttachmentInvalidException {
|
||||
try (final var attachmentStream = getAttachmentStream(attachment, inlineLabel, null, null, false)) {
|
||||
return uploader.upload(attachmentStream);
|
||||
} catch (IOException e) {
|
||||
throw invalid(describe(attachment, inlineLabel), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads the full text of a message that is too long to be sent inline. Failures never include the text.
|
||||
*/
|
||||
public SignalServiceAttachmentPointer uploadLongTextAttachment(final byte[] messageBytes) throws AttachmentInvalidException {
|
||||
final var streamDetails = new StreamDetails(new ByteArrayInputStream(messageBytes),
|
||||
MimeUtils.LONG_TEXT,
|
||||
messageBytes.length);
|
||||
try (final var textAttachment = createAttachmentStream(streamDetails)) {
|
||||
return uploader.upload(textAttachment);
|
||||
} catch (IOException e) {
|
||||
throw invalid("long text attachment", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares a sticker for sending. The sticker itself is uploaded later by the message sender.
|
||||
*/
|
||||
public SignalServiceAttachmentStream createStickerAttachmentStream(final StreamDetails streamDetails) throws AttachmentInvalidException {
|
||||
try {
|
||||
return createAttachmentStream(streamDetails);
|
||||
} catch (IOException e) {
|
||||
throw invalid("sticker", e);
|
||||
}
|
||||
}
|
||||
|
||||
private SignalServiceAttachmentStream createAttachmentStream(final StreamDetails streamDetails) throws IOException {
|
||||
final var uploadSpec = getResumableUploadSpec(streamDetails);
|
||||
return AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty(), uploadSpec);
|
||||
}
|
||||
|
||||
private static String describe(final String attachment, final String inlineLabel) {
|
||||
return isInline(attachment) ? inlineLabel : attachment;
|
||||
}
|
||||
|
||||
static AttachmentInvalidException invalid(final String label, final Exception e) {
|
||||
final var message = e.getMessage() != null ? e.getMessage() : e.getClass().getName();
|
||||
final var exception = new AttachmentInvalidException(label + ": " + message);
|
||||
exception.initCause(e);
|
||||
return exception;
|
||||
}
|
||||
|
||||
private static boolean isInline(final String attachment) {
|
||||
return attachment != null && attachment.regionMatches(true, 0, "data:", 0, 5);
|
||||
}
|
||||
|
||||
private static String inlineAttachmentLabel(final int index) {
|
||||
return "inline attachment #" + (index + 1);
|
||||
}
|
||||
|
||||
private static void closeAll(final List<SignalServiceAttachmentStream> streams) {
|
||||
for (final var stream : streams) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to close attachment stream, ignoring: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void downloadAttachment(final SignalServiceAttachment attachment) {
|
||||
@ -168,7 +271,7 @@ public class AttachmentHelper {
|
||||
try {
|
||||
attachmentStore.storeAttachment(pointer, outputStream -> this.retrieveAttachment(pointer, outputStream));
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to download attachment ({}), ignoring: {}", pointer.getRemoteId(), e.getMessage());
|
||||
logger.warn("Failed to download attachment ({}), ignoring", pointer.getRemoteId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,4 +326,24 @@ public class AttachmentHelper {
|
||||
|
||||
void handle(InputStream inputStream) throws IOException;
|
||||
}
|
||||
|
||||
interface Uploader {
|
||||
|
||||
ResumableUploadSpec getResumableUploadSpec(long ciphertextLength) throws IOException;
|
||||
|
||||
SignalServiceAttachmentPointer upload(SignalServiceAttachmentStream stream) throws IOException;
|
||||
}
|
||||
|
||||
private record DependenciesUploader(SignalDependencies dependencies) implements Uploader {
|
||||
|
||||
@Override
|
||||
public ResumableUploadSpec getResumableUploadSpec(final long ciphertextLength) throws IOException {
|
||||
return dependencies.getCdnService().getResumableUploadSpecBlocking(ciphertextLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignalServiceAttachmentPointer upload(final SignalServiceAttachmentStream stream) throws IOException {
|
||||
return dependencies.getMessageSender().uploadAttachment(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -458,10 +458,14 @@ public class CallManager implements AutoCloseable {
|
||||
}
|
||||
|
||||
private String buildConfig(CallState state) {
|
||||
return buildConfig(state, account.getDeviceId());
|
||||
}
|
||||
|
||||
static String buildConfig(CallState state, int localDeviceId) {
|
||||
var config = mapper.createObjectNode();
|
||||
config.put("call_id", Utils.callIdUnsigned(state.callId));
|
||||
config.put("is_outgoing", state.isOutgoing);
|
||||
config.put("local_device_id", 1);
|
||||
config.put("local_device_id", localDeviceId);
|
||||
return writeJson(config);
|
||||
}
|
||||
|
||||
|
||||
@ -136,7 +136,7 @@ class GroupV2Helper {
|
||||
|
||||
int findRevisionWeWereAdded(DecryptedGroup partialDecryptedGroup) {
|
||||
ByteString aciBytes = getSelfAci().toByteString();
|
||||
ByteString pniBytes = getSelfPni().toByteString();
|
||||
ByteString pniBytes = getSelfPni() == null ? null : getSelfPni().toByteString();
|
||||
for (DecryptedMember decryptedMember : partialDecryptedGroup.members) {
|
||||
if (decryptedMember.aciBytes.equals(aciBytes) || decryptedMember.pniBytes.equals(pniBytes)) {
|
||||
return decryptedMember.joinedAtRevision;
|
||||
@ -264,7 +264,7 @@ class GroupV2Helper {
|
||||
var pendingMembersList = groupInfoV2.getGroup().pendingMembers;
|
||||
final var selfAci = getSelfAci();
|
||||
var selfPendingMember = DecryptedGroupUtil.findPendingByServiceId(pendingMembersList, selfAci);
|
||||
if (selfPendingMember.isEmpty()) {
|
||||
if (selfPendingMember.isEmpty() && getSelfPni() != null) {
|
||||
selfPendingMember = DecryptedGroupUtil.findPendingByServiceId(pendingMembersList, getSelfPni());
|
||||
}
|
||||
|
||||
|
||||
@ -339,7 +339,7 @@ public final class IncomingMessageHandler {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
|
||||
if (account.getPni().equals(destination.serviceId)) {
|
||||
if (destination.serviceId.equals(account.getPni())) {
|
||||
account.getRecipientStore().markNeedsPniSignature(sender, true);
|
||||
} else if (account.getAci().equals(destination.serviceId)) {
|
||||
account.getRecipientStore().markNeedsPniSignature(sender, false);
|
||||
@ -585,6 +585,21 @@ public final class IncomingMessageHandler {
|
||||
actions.addAll(dataResults.first());
|
||||
longTexts.putAll(dataResults.second());
|
||||
}
|
||||
if (message.getEditMessage().isPresent()) {
|
||||
final var dataResults = handleSignalServiceDataMessage(message.getEditMessage()
|
||||
.get()
|
||||
.getDataMessage(),
|
||||
true,
|
||||
sender,
|
||||
destination == null
|
||||
? null
|
||||
: new DeviceAddress(account.getRecipientResolver().resolveRecipient(destination),
|
||||
destination.getServiceId(),
|
||||
0),
|
||||
receiveConfig);
|
||||
actions.addAll(dataResults.first());
|
||||
longTexts.putAll(dataResults.second());
|
||||
}
|
||||
if (message.getStoryMessage().isPresent()) {
|
||||
actions.addAll(handleSignalServiceStoryMessage(message.getStoryMessage().get(),
|
||||
sender.recipientId(),
|
||||
@ -1124,7 +1139,7 @@ public final class IncomingMessageHandler {
|
||||
if (destination == null || destination.isUnknown()) {
|
||||
throw new InvalidMessageException("Missing destination");
|
||||
}
|
||||
if (!account.getAci().equals(destination) && !account.getPni().equals(destination)) {
|
||||
if (!destination.equals(account.getAci()) && !destination.equals(account.getPni())) {
|
||||
throw new InvalidMessageException("Message not intended for this account");
|
||||
}
|
||||
return new DeviceAddress(account.getRecipientResolver().resolveRecipient(destination),
|
||||
|
||||
@ -365,7 +365,7 @@ public final class ProfileHelper {
|
||||
}
|
||||
|
||||
if (recipientId.equals(account.getSelfRecipientId())) {
|
||||
final var isUnrestricted = encryptedProfile.isUnrestrictedUnidentifiedAccess();
|
||||
final var isUnrestricted = encryptedProfile.getUnrestrictedUnidentifiedAccess();
|
||||
if (account.isUnrestrictedUnidentifiedAccess() != isUnrestricted) {
|
||||
account.setUnrestrictedUnidentifiedAccess(isUnrestricted);
|
||||
}
|
||||
|
||||
@ -42,8 +42,8 @@ public class ReceiveHelper {
|
||||
|
||||
private ReceiveConfig receiveConfig = new ReceiveConfig(false, false, false, false, false);
|
||||
private boolean hasCaughtUpWithOldMessages = false;
|
||||
private boolean isWaitingForMessage = false;
|
||||
private boolean shouldStop = false;
|
||||
private volatile boolean isWaitingForMessage = false;
|
||||
private volatile boolean shouldStop = false;
|
||||
private Callable authenticationFailureListener;
|
||||
private Callable caughtUpWithOldMessagesListener;
|
||||
|
||||
@ -71,6 +71,15 @@ public class ReceiveHelper {
|
||||
return isWaitingForMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Must only be called right before a new receive loop is started, while no other receive loop is running.
|
||||
* Ending a receive loop doesn't clear the stop request, so a loop that was asked to stop can't continue
|
||||
* receiving, e.g. by retrying in receiveMessagesContinuously after an IOException.
|
||||
*/
|
||||
public void clearStopRequest() {
|
||||
this.shouldStop = false;
|
||||
}
|
||||
|
||||
public void receiveMessagesContinuously(Manager.ReceiveMessageHandler handler) {
|
||||
while (!shouldStop) {
|
||||
try {
|
||||
@ -111,7 +120,6 @@ public class ReceiveHelper {
|
||||
signalWebSocket.removeKeepAliveToken("receive");
|
||||
signalWebSocket.disconnect();
|
||||
webSocketStateDisposable.dispose();
|
||||
shouldStop = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -555,7 +555,8 @@ public class SendHelper {
|
||||
recipients,
|
||||
unidentifiedAccess,
|
||||
groupSendEndorsements,
|
||||
message),
|
||||
message,
|
||||
null),
|
||||
recipientIds,
|
||||
groupInfo,
|
||||
false);
|
||||
@ -886,7 +887,8 @@ public class SendHelper {
|
||||
|
||||
var address = context.getRecipientHelper().resolveSignalServiceAddress(recipientId);
|
||||
try {
|
||||
final boolean includePniSignature = account.getRecipientStore().needsPniSignature(recipientId);
|
||||
final boolean includePniSignature = account.getPni() != null && account.getRecipientStore()
|
||||
.needsPniSignature(recipientId);
|
||||
try {
|
||||
return s.send(messageSender,
|
||||
address,
|
||||
|
||||
@ -93,7 +93,6 @@ import org.asamk.signal.manager.storage.recipients.RecipientId;
|
||||
import org.asamk.signal.manager.storage.stickerPacks.JsonStickerPack;
|
||||
import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
|
||||
import org.asamk.signal.manager.storage.stickers.StickerPack;
|
||||
import org.asamk.signal.manager.util.AttachmentUtils;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.asamk.signal.manager.util.MimeUtils;
|
||||
import org.asamk.signal.manager.util.NotificationProfileUtils;
|
||||
@ -130,11 +129,9 @@ import org.whispersystems.signalservice.api.push.ServiceIdType;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.CdsiResourceExhaustedException;
|
||||
import org.whispersystems.signalservice.api.storage.SignalAccountRecord;
|
||||
import org.whispersystems.signalservice.api.storage.StorageId;
|
||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
import org.whispersystems.signalservice.internal.storage.protos.AccountRecord;
|
||||
import org.whispersystems.signalservice.internal.util.Util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
@ -1028,15 +1025,8 @@ public class ManagerImpl implements Manager {
|
||||
final var remainder = result.getSecond();
|
||||
if (remainder != null) {
|
||||
final var messageBytes = message.messageText().getBytes(StandardCharsets.UTF_8);
|
||||
final var streamDetails = new StreamDetails(new ByteArrayInputStream(messageBytes),
|
||||
MimeUtils.LONG_TEXT,
|
||||
messageBytes.length);
|
||||
final var uploadSpec = context.getAttachmentHelper().getResumableUploadSpec(streamDetails);
|
||||
final var textAttachment = AttachmentUtils.createAttachmentStream(streamDetails,
|
||||
Optional.empty(),
|
||||
uploadSpec);
|
||||
messageBuilder.withBody(trimmed);
|
||||
additionalAttachments.add(context.getAttachmentHelper().uploadAttachment(textAttachment));
|
||||
additionalAttachments.add(context.getAttachmentHelper().uploadLongTextAttachment(messageBytes));
|
||||
} else {
|
||||
messageBuilder.withBody(message.messageText());
|
||||
}
|
||||
@ -1069,9 +1059,12 @@ public class ManagerImpl implements Manager {
|
||||
final var quote = message.quote().get();
|
||||
final var quotedAttachments = new ArrayList<SignalServiceDataMessage.Quote.QuotedAttachment>();
|
||||
for (final var a : quote.attachments()) {
|
||||
final var label = "quote thumbnail #" + (quotedAttachments.size() + 1);
|
||||
final var quotedAttachment = new SignalServiceDataMessage.Quote.QuotedAttachment(a.contentType(),
|
||||
a.filename(),
|
||||
a.preview() == null ? null : context.getAttachmentHelper().uploadAttachment(a.preview()));
|
||||
a.preview() == null
|
||||
? null
|
||||
: context.getAttachmentHelper().uploadAttachment(a.preview(), label));
|
||||
quotedAttachments.add(quotedAttachment);
|
||||
}
|
||||
messageBuilder.withQuote(new SignalServiceDataMessage.Quote(quote.timestamp(),
|
||||
@ -1102,10 +1095,7 @@ public class ManagerImpl implements Manager {
|
||||
if (streamDetails == null) {
|
||||
throw new InvalidStickerException("Missing local sticker file");
|
||||
}
|
||||
final var uploadSpec = context.getAttachmentHelper().getResumableUploadSpec(streamDetails);
|
||||
final var stickerAttachment = AttachmentUtils.createAttachmentStream(streamDetails,
|
||||
Optional.empty(),
|
||||
uploadSpec);
|
||||
final var stickerAttachment = context.getAttachmentHelper().createStickerAttachmentStream(streamDetails);
|
||||
messageBuilder.withSticker(new SignalServiceDataMessage.Sticker(packId.serialize(),
|
||||
stickerPack.packKey(),
|
||||
stickerId,
|
||||
@ -1115,8 +1105,9 @@ public class ManagerImpl implements Manager {
|
||||
if (!message.previews().isEmpty()) {
|
||||
final var previews = new ArrayList<SignalServicePreview>(message.previews().size());
|
||||
for (final var p : message.previews()) {
|
||||
final var label = "link preview image #" + (previews.size() + 1);
|
||||
final var image = p.image().isPresent() ? context.getAttachmentHelper()
|
||||
.uploadAttachment(p.image().get()) : null;
|
||||
.uploadAttachment(p.image().get(), label) : null;
|
||||
previews.add(new SignalServicePreview(p.url(),
|
||||
p.title(),
|
||||
p.description(),
|
||||
@ -1594,15 +1585,18 @@ public class ManagerImpl implements Manager {
|
||||
private static final AtomicInteger threadNumber = new AtomicInteger(0);
|
||||
|
||||
private void startReceiveThreadIfRequired() {
|
||||
if (receiveThread != null || isReceivingSynchronous) {
|
||||
if (isReceiveThreadAlive() || isReceivingSynchronous) {
|
||||
return;
|
||||
}
|
||||
context.getReceiveHelper().clearStopRequest();
|
||||
receiveThread = Thread.ofPlatform().name("receive-" + threadNumber.getAndIncrement()).start(() -> {
|
||||
logger.debug("Starting receiving messages");
|
||||
context.getReceiveHelper().receiveMessagesContinuously(this::passReceivedMessageToHandlers);
|
||||
logger.debug("Finished receiving messages");
|
||||
synchronized (messageHandlers) {
|
||||
receiveThread = null;
|
||||
if (receiveThread == Thread.currentThread()) {
|
||||
receiveThread = null;
|
||||
}
|
||||
|
||||
// Check if in the meantime another handler has been registered
|
||||
if (!messageHandlers.isEmpty()) {
|
||||
@ -1613,6 +1607,13 @@ public class ManagerImpl implements Manager {
|
||||
});
|
||||
}
|
||||
|
||||
private boolean isReceiveThreadAlive() {
|
||||
// The receive thread clears receiveThread itself when it has finished, so it stays set while the thread is
|
||||
// still stopping and no second receive thread can be started in the meantime.
|
||||
// A receive thread that died from an uncaught exception never clears it.
|
||||
return receiveThread != null && receiveThread.isAlive();
|
||||
}
|
||||
|
||||
private void passReceivedMessageToHandlers(MessageEnvelope envelope, Throwable e) {
|
||||
synchronized (messageHandlers) {
|
||||
Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> {
|
||||
@ -1635,7 +1636,6 @@ public class ManagerImpl implements Manager {
|
||||
return;
|
||||
}
|
||||
thread = receiveThread;
|
||||
receiveThread = null;
|
||||
}
|
||||
|
||||
stopReceiveThread(thread);
|
||||
@ -1691,11 +1691,12 @@ public class ManagerImpl implements Manager {
|
||||
ReceiveMessageHandler handler
|
||||
) throws IOException, AlreadyReceivingException {
|
||||
synchronized (messageHandlers) {
|
||||
if (isReceiving()) {
|
||||
if (isReceiving() || isReceiveThreadAlive()) {
|
||||
throw new AlreadyReceivingException("Already receiving message.");
|
||||
}
|
||||
isReceivingSynchronous = true;
|
||||
receiveThread = Thread.currentThread();
|
||||
context.getReceiveHelper().clearStopRequest();
|
||||
}
|
||||
try {
|
||||
context.getReceiveHelper().receiveMessages(timeout, maxMessages, (envelope, e) -> {
|
||||
@ -2120,7 +2121,6 @@ public class ManagerImpl implements Manager {
|
||||
weakHandlers.clear();
|
||||
messageHandlers.clear();
|
||||
thread = receiveThread;
|
||||
receiveThread = null;
|
||||
}
|
||||
if (thread != null) {
|
||||
stopReceiveThread(thread);
|
||||
|
||||
@ -39,13 +39,6 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||
managers.forEach(m -> m.addClosedListener(() -> this.removeManager(m)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAccountNumbers() {
|
||||
synchronized (managers) {
|
||||
return managers.stream().map(Manager::getSelfNumber).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Manager> getManagers() {
|
||||
synchronized (managers) {
|
||||
@ -75,6 +68,13 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeOnManagerAddedHandler(final Consumer<Manager> handler) {
|
||||
synchronized (onManagerAddedHandlers) {
|
||||
onManagerAddedHandlers.remove(handler);
|
||||
}
|
||||
}
|
||||
|
||||
void removeManager(final Manager m) {
|
||||
synchronized (managers) {
|
||||
if (!managers.remove(m)) {
|
||||
@ -95,6 +95,13 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeOnManagerRemovedHandler(final Consumer<Manager> handler) {
|
||||
synchronized (onManagerRemovedHandlers) {
|
||||
onManagerRemovedHandlers.remove(handler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Manager getManager(final String identifier) {
|
||||
synchronized (managers) {
|
||||
@ -121,7 +128,7 @@ public class MultiAccountManagerImpl implements MultiAccountManager {
|
||||
} else {
|
||||
// Phone number — check already loaded managers
|
||||
var existing = managers.stream()
|
||||
.filter(m -> m.getSelfNumber().equals(identifier))
|
||||
.filter(m -> identifier.equals(m.getSelfNumber()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (existing != null) {
|
||||
|
||||
@ -19,8 +19,8 @@ package org.asamk.signal.manager.internal;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.ProvisioningManager;
|
||||
import org.asamk.signal.manager.Settings;
|
||||
import org.asamk.signal.manager.api.BadRequestException;
|
||||
import org.asamk.signal.manager.api.UserAlreadyExistsException;
|
||||
import org.asamk.signal.manager.config.ServiceConfig;
|
||||
import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.storage.accounts.AccountsStore;
|
||||
@ -34,13 +34,15 @@ import org.signal.libsignal.protocol.IdentityKey;
|
||||
import org.signal.libsignal.protocol.IdentityKeyPair;
|
||||
import org.signal.libsignal.protocol.ecc.ECPrivateKey;
|
||||
import org.signal.libsignal.zkgroup.profiles.ProfileKey;
|
||||
import org.signal.network.api.RegistrationApiV2;
|
||||
import org.signal.network.api.RegistrationApiV2.LinkDeviceResponse;
|
||||
import org.signal.network.api.RegistrationApiV2.RegisterAsLinkedDeviceError;
|
||||
import org.signal.network.rest.SignalRestClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||
import org.whispersystems.signalservice.api.account.DeviceAttributes;
|
||||
import org.whispersystems.signalservice.api.account.PreKeyCollection;
|
||||
import org.whispersystems.signalservice.api.provisioning.ProvisioningSocket;
|
||||
import org.whispersystems.signalservice.api.push.ServiceIdType;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
|
||||
import org.whispersystems.signalservice.internal.crypto.SecondaryProvisioningCipher;
|
||||
import org.whispersystems.signalservice.internal.push.ProvisionMessage;
|
||||
@ -67,7 +69,7 @@ import kotlinx.coroutines.BuildersKt;
|
||||
import kotlinx.coroutines.CoroutineScope;
|
||||
|
||||
import static org.asamk.signal.manager.util.KeyUtils.generatePreKeysForType;
|
||||
import static org.asamk.signal.manager.util.Utils.handleResponseException;
|
||||
import static org.asamk.signal.manager.util.Utils.handleResponseExceptionSuspend;
|
||||
|
||||
public class ProvisioningManagerImpl implements ProvisioningManager, Closeable {
|
||||
|
||||
@ -148,18 +150,20 @@ public class ProvisioningManagerImpl implements ProvisioningManager, Closeable {
|
||||
|
||||
var number = msg.number;
|
||||
var aci = ACI.parseOrThrow(msg.aci, msg.aciBinary);
|
||||
var pni = PNI.parseOrThrow(msg.pni, msg.pniBinary);
|
||||
var pni = parsePni(msg);
|
||||
var identifier = number != null ? number : aci.toString();
|
||||
|
||||
logger.info("Received link information from {}, linking in progress ...", number);
|
||||
logger.info("Received link information from {}, linking in progress ...", identifier);
|
||||
|
||||
var accountPath = accountsStore.getPathByAci(aci);
|
||||
if (accountPath == null) {
|
||||
if (accountPath == null && number != null) {
|
||||
accountPath = accountsStore.getPathByNumber(number);
|
||||
}
|
||||
final var accountExists = accountPath != null && SignalAccount.accountFileExists(pathConfig.dataPath(),
|
||||
accountPath);
|
||||
if (accountExists && !canRelinkExistingAccount(accountPath)) {
|
||||
throw new UserAlreadyExistsException(number, SignalAccount.getFileName(pathConfig.dataPath(), accountPath));
|
||||
throw new UserAlreadyExistsException(identifier,
|
||||
SignalAccount.getFileName(pathConfig.dataPath(), accountPath));
|
||||
}
|
||||
if (accountPath == null) {
|
||||
accountPath = accountsStore.addAccount(number, aci);
|
||||
@ -173,8 +177,10 @@ public class ProvisioningManagerImpl implements ProvisioningManager, Closeable {
|
||||
try {
|
||||
aciIdentity = new IdentityKeyPair(new IdentityKey(msg.aciIdentityKeyPublic.toByteArray()),
|
||||
new ECPrivateKey(msg.aciIdentityKeyPrivate.toByteArray()));
|
||||
pniIdentity = new IdentityKeyPair(new IdentityKey(msg.pniIdentityKeyPublic.toByteArray()),
|
||||
new ECPrivateKey(msg.pniIdentityKeyPrivate.toByteArray()));
|
||||
pniIdentity = pni == null
|
||||
? null
|
||||
: new IdentityKeyPair(new IdentityKey(msg.pniIdentityKeyPublic.toByteArray()),
|
||||
new ECPrivateKey(msg.pniIdentityKeyPrivate.toByteArray()));
|
||||
profileKey = msg.profileKey == null
|
||||
? KeyUtils.createProfileKey()
|
||||
: new ProfileKey(msg.profileKey.toByteArray());
|
||||
@ -222,28 +228,19 @@ public class ProvisioningManagerImpl implements ProvisioningManager, Closeable {
|
||||
}
|
||||
|
||||
final var aciPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.ACI));
|
||||
final var pniPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));
|
||||
final var pniPreKeys = pni == null
|
||||
? null
|
||||
: generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));
|
||||
|
||||
logger.debug("Finishing new device registration");
|
||||
final var attrs = account.getAccountAttributes(null);
|
||||
final var deviceAttributes = new DeviceAttributes(attrs.getFetchesMessages(),
|
||||
attrs.getRegistrationId(),
|
||||
attrs.getPniRegistrationId(),
|
||||
attrs.getName(),
|
||||
attrs.getCapabilities());
|
||||
final var unauthAccountManager = SignalServiceAccountManager.createWithStaticCredentials(
|
||||
serviceEnvironmentConfig.signalServiceConfiguration(),
|
||||
null,
|
||||
null,
|
||||
number,
|
||||
SignalServiceAddress.DEFAULT_DEVICE_ID,
|
||||
password,
|
||||
userAgent,
|
||||
ServiceConfig.AUTOMATIC_NETWORK_RETRY,
|
||||
ServiceConfig.GROUP_MAX_SIZE);
|
||||
final var registerResponse = handleResponseException(unauthAccountManager.getRegistrationApi()
|
||||
.registerAsSecondaryDevice(msg.provisioningCode, deviceAttributes, aciPreKeys, pniPreKeys, null));
|
||||
final var deviceId = Integer.parseInt(registerResponse.getDeviceId());
|
||||
final var restClient = new SignalRestClient(serviceEnvironmentConfig.signalServiceConfiguration(),
|
||||
userAgent);
|
||||
final var registrationApi = new RegistrationApiV2(restClient, false);
|
||||
final var deviceId = registerLinkedDevice(registrationApi,
|
||||
account,
|
||||
msg.provisioningCode,
|
||||
aciPreKeys,
|
||||
pniPreKeys);
|
||||
|
||||
account.finishLinking(deviceId, aciPreKeys, pniPreKeys);
|
||||
linkingFinished = true;
|
||||
@ -277,7 +274,7 @@ public class ProvisioningManagerImpl implements ProvisioningManager, Closeable {
|
||||
newManagerListener.accept(m);
|
||||
m = null;
|
||||
}
|
||||
return number;
|
||||
return identifier;
|
||||
} finally {
|
||||
if (m != null) {
|
||||
m.close();
|
||||
@ -296,6 +293,71 @@ public class ProvisioningManagerImpl implements ProvisioningManager, Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
static PNI parsePni(final ProvisionMessage message) throws IOException {
|
||||
if (message.number != null) {
|
||||
return PNI.parseOrThrow(message.pni, message.pniBinary);
|
||||
}
|
||||
if (message.pni != null
|
||||
|| message.pniBinary != null
|
||||
|| message.pniIdentityKeyPublic != null
|
||||
|| message.pniIdentityKeyPrivate != null) {
|
||||
throw new IOException("Provisioning message has PNI material without a phone number");
|
||||
}
|
||||
if (message.authCredentialSalt == null || message.authCredentialSalt.size() == 0) {
|
||||
throw new IOException("Numberless provisioning message is missing the group auth credential salt");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static int registerLinkedDevice(
|
||||
final RegistrationApiV2 registrationApi,
|
||||
final SignalAccount account,
|
||||
final String provisioningCode,
|
||||
final PreKeyCollection aciPreKeys,
|
||||
final PreKeyCollection pniPreKeys
|
||||
) throws IOException {
|
||||
final var attrs = account.getAccountAttributesV2();
|
||||
final var deviceAttributes = new RegistrationApiV2.DeviceAttributes(attrs.getFetchesMessages(),
|
||||
attrs.getRegistrationId(),
|
||||
attrs.getPniRegistrationId(),
|
||||
attrs.getName(),
|
||||
attrs.getCapabilities());
|
||||
try {
|
||||
final LinkDeviceResponse result = handleResponseExceptionSuspend(cont -> registrationApi.registerAsSecondaryDevice(
|
||||
account.getAci(),
|
||||
account.getPassword(),
|
||||
provisioningCode,
|
||||
deviceAttributes,
|
||||
toRegistrationPreKeys(aciPreKeys),
|
||||
toRegistrationPreKeys(pniPreKeys),
|
||||
null,
|
||||
cont));
|
||||
return result.getDeviceId();
|
||||
} catch (BadRequestException e) {
|
||||
throw switch (e.getError()) {
|
||||
case RegisterAsLinkedDeviceError.IncorrectVerification ignored ->
|
||||
new AuthorizationFailedException(403, "Device verification failed");
|
||||
case RegisterAsLinkedDeviceError.MissingCapability ignored ->
|
||||
new IOException("Linked device is missing a required account capability");
|
||||
case RegisterAsLinkedDeviceError.MaxLinkedDevices ignored ->
|
||||
new IOException("Account has reached its linked device limit");
|
||||
case RegisterAsLinkedDeviceError.InvalidRequest ignored ->
|
||||
new IOException("Signal rejected the device linking request");
|
||||
case RegisterAsLinkedDeviceError.RateLimited ignored ->
|
||||
new IOException("Device linking rate limited; try again later");
|
||||
default -> new IOException("Unexpected device linking response");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static RegistrationApiV2.PreKeyCollection toRegistrationPreKeys(final PreKeyCollection preKeys) {
|
||||
return preKeys == null
|
||||
? null
|
||||
: new RegistrationApiV2.PreKeyCollection(preKeys.getIdentityKey(),
|
||||
preKeys.getSignedPreKey(),
|
||||
preKeys.getLastResortKyberPreKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
socketHandle.close();
|
||||
|
||||
@ -18,12 +18,14 @@ package org.asamk.signal.manager.internal;
|
||||
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.RegistrationManager;
|
||||
import org.asamk.signal.manager.api.BadRequestException;
|
||||
import org.asamk.signal.manager.api.CaptchaRequiredException;
|
||||
import org.asamk.signal.manager.api.IncorrectPinException;
|
||||
import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
|
||||
import org.asamk.signal.manager.api.PinLockMissingException;
|
||||
import org.asamk.signal.manager.api.PinLockedException;
|
||||
import org.asamk.signal.manager.api.RateLimitException;
|
||||
import org.asamk.signal.manager.api.TwoFactorRequired;
|
||||
import org.asamk.signal.manager.api.UpdateProfile;
|
||||
import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
|
||||
import org.asamk.signal.manager.config.ServiceConfig;
|
||||
@ -33,10 +35,14 @@ import org.asamk.signal.manager.helper.PinHelper;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.asamk.signal.manager.util.NumberVerificationUtils;
|
||||
import org.signal.core.models.AccountEntropyPool;
|
||||
import org.signal.core.models.MasterKey;
|
||||
import org.signal.core.models.ServiceId.ACI;
|
||||
import org.signal.core.models.ServiceId.PNI;
|
||||
import org.signal.libsignal.usernames.BaseUsernameException;
|
||||
import org.signal.network.api.RegistrationApiV2;
|
||||
import org.signal.network.api.RegistrationApiV2.RegisterAccountError;
|
||||
import org.signal.network.rest.SignalRestClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
||||
@ -50,15 +56,25 @@ import org.whispersystems.signalservice.api.svr.SecureValueRecovery;
|
||||
import org.whispersystems.signalservice.internal.push.VerifyAccountResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static org.asamk.signal.manager.internal.ProvisioningManagerImpl.toRegistrationPreKeys;
|
||||
import static org.asamk.signal.manager.util.KeyUtils.generatePreKeysForType;
|
||||
import static org.asamk.signal.manager.util.Utils.handleResponseException;
|
||||
import static org.asamk.signal.manager.util.Utils.handleResponseExceptionSuspend;
|
||||
|
||||
public class RegistrationManagerImpl implements RegistrationManager {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RegistrationManagerImpl.class);
|
||||
|
||||
private static final class RecoveryRequestFailedException extends IOException {
|
||||
|
||||
private RecoveryRequestFailedException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
private SignalAccount account;
|
||||
private final PathConfig pathConfig;
|
||||
private final ServiceEnvironmentConfig serviceEnvironmentConfig;
|
||||
@ -107,7 +123,7 @@ public class RegistrationManagerImpl implements RegistrationManager {
|
||||
boolean voiceVerification,
|
||||
String captcha,
|
||||
final boolean forceRegister
|
||||
) throws IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException, RateLimitException, VerificationMethodNotAvailableException {
|
||||
) throws IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException, RateLimitException, TwoFactorRequired, VerificationMethodNotAvailableException {
|
||||
if (account.isRegistered()
|
||||
&& account.getServiceEnvironment() != null
|
||||
&& account.getServiceEnvironment() != serviceEnvironmentConfig.type()) {
|
||||
@ -129,6 +145,13 @@ public class RegistrationManagerImpl implements RegistrationManager {
|
||||
if (recoveryPassword != null && account.isPrimaryDevice() && attemptReregisterAccount(recoveryPassword)) {
|
||||
return;
|
||||
}
|
||||
if (account.getAci() != null && account.getAccountEntropyPool() != null && attemptRecoverAccount(null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (account.getNumber() == null) {
|
||||
throw new IOException("Failed to recover account using its ACI and Account Entropy Pool");
|
||||
}
|
||||
|
||||
final var registrationApi = unauthenticatedAccountManager.getRegistrationApi();
|
||||
logger.trace("Creating verification session");
|
||||
@ -184,6 +207,109 @@ public class RegistrationManagerImpl implements RegistrationManager {
|
||||
finishAccountRegistration(response, pin, masterKey, aciPreKeys, pniPreKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerWithRecoveryKey(
|
||||
final String recoveryKey,
|
||||
final boolean forceRegister,
|
||||
final Integer totp
|
||||
) throws IOException, RateLimitException, TwoFactorRequired {
|
||||
if (account.getAci() == null) {
|
||||
throw new IOException("Recovery-key registration requires an ACI account identifier");
|
||||
}
|
||||
if (account.isRegistered() && !forceRegister) {
|
||||
throw new IOException("Account is already registered; use --reregister to register it again");
|
||||
}
|
||||
final var accountEntropyPool = AccountEntropyPool.Companion.parseOrNull(recoveryKey);
|
||||
if (accountEntropyPool == null || !AccountEntropyPool.Companion.isFullyValid(accountEntropyPool.getValue())) {
|
||||
throw new IOException("Invalid recovery key");
|
||||
}
|
||||
recoverAccount(totp, false, accountEntropyPool);
|
||||
}
|
||||
|
||||
private boolean attemptRecoverAccount(
|
||||
final Integer totp
|
||||
) throws IOException, RateLimitException, TwoFactorRequired {
|
||||
final var accountEntropyPool = account.getAccountEntropyPool();
|
||||
try {
|
||||
recoverAccount(totp, false, accountEntropyPool);
|
||||
logger.info("Reregistered existing account using its ACI and Account Entropy Pool.");
|
||||
return true;
|
||||
} catch (TwoFactorRequired | RateLimitException e) {
|
||||
throw e;
|
||||
} catch (RecoveryRequestFailedException e) {
|
||||
logger.debug("Failed to reregister account using its ACI and Account Entropy Pool", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void recoverAccount(
|
||||
final Integer totp,
|
||||
final boolean includeRegistrationLock,
|
||||
final AccountEntropyPool accountEntropyPool
|
||||
) throws IOException, RateLimitException, TwoFactorRequired {
|
||||
if (account.getPniIdentityKeyPair() == null) {
|
||||
account.setPniIdentityKeyPair(KeyUtils.generateIdentityKeyPair());
|
||||
}
|
||||
|
||||
final var aciPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.ACI));
|
||||
final var pniPreKeys = generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));
|
||||
final var masterKey = accountEntropyPool.deriveMasterKey();
|
||||
final var recoveryPassword = masterKey.deriveRegistrationRecoveryPassword();
|
||||
final var registrationLock = includeRegistrationLock ? masterKey.deriveRegistrationLock() : null;
|
||||
final var restClient = new SignalRestClient(serviceEnvironmentConfig.signalServiceConfiguration(), userAgent);
|
||||
final var registrationApi = new RegistrationApiV2(restClient, true);
|
||||
|
||||
final RegistrationApiV2.RegisterAccountResponse response;
|
||||
try {
|
||||
response = handleResponseExceptionSuspend(cont -> registrationApi.registerAccount(null,
|
||||
account.getPassword(),
|
||||
null,
|
||||
recoveryPassword,
|
||||
null,
|
||||
account.getAccountAttributesV2ForRecovery(registrationLock, recoveryPassword),
|
||||
toRegistrationPreKeys(aciPreKeys),
|
||||
toRegistrationPreKeys(pniPreKeys),
|
||||
null,
|
||||
true,
|
||||
account.getAci(),
|
||||
totp,
|
||||
null,
|
||||
cont));
|
||||
} catch (BadRequestException e) {
|
||||
switch (e.getError()) {
|
||||
case RegisterAccountError.RegistrationLock ignored -> {
|
||||
if (includeRegistrationLock) {
|
||||
throw new RecoveryRequestFailedException("Registration lock recovery failed", e);
|
||||
}
|
||||
recoverAccount(totp, true, accountEntropyPool);
|
||||
return;
|
||||
}
|
||||
case RegisterAccountError.TwoFactorRequired ignored -> throw new TwoFactorRequired();
|
||||
case RegisterAccountError.RegistrationRecoveryPasswordIncorrect ignored ->
|
||||
throw new RecoveryRequestFailedException("Account key or recovery key is incorrect", e);
|
||||
case RegisterAccountError.RateLimited ignored -> throw new RateLimitException(null);
|
||||
case RegisterAccountError.PostQuantumRatchetRequired ignored ->
|
||||
throw new IOException("signal-cli is too old to register this account", e);
|
||||
default -> throw new IOException("Signal rejected recovery-key registration", e);
|
||||
}
|
||||
}
|
||||
|
||||
final var aci = ACI.parseOrThrow(response.getAci());
|
||||
final var pni = response.getPni() == null ? null : PNI.parseOrThrow(response.getPni());
|
||||
final var authCredentialSalt = response.getAuthCredentialSalt() == null
|
||||
? null
|
||||
: Base64.getDecoder().decode(response.getAuthCredentialSalt());
|
||||
account.finishRecoveryRegistration(aci,
|
||||
pni,
|
||||
response.getE164(),
|
||||
accountEntropyPool,
|
||||
authCredentialSalt,
|
||||
aciPreKeys,
|
||||
pni == null ? null : pniPreKeys);
|
||||
accountFileUpdater.updateAccountIdentifiers(response.getE164(), aci);
|
||||
finishManagerRegistration(response.getStorageCapable());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteLocalAccountData() throws IOException {
|
||||
account.deleteAccountData();
|
||||
@ -291,13 +417,17 @@ public class RegistrationManagerImpl implements RegistrationManager {
|
||||
account.finishRegistration(aci, pni, masterKey, pin, aciPreKeys, pniPreKeys);
|
||||
accountFileUpdater.updateAccountIdentifiers(account.getNumber(), aci);
|
||||
|
||||
finishManagerRegistration(response.getStorageCapable());
|
||||
}
|
||||
|
||||
private void finishManagerRegistration(final boolean storageCapable) throws IOException {
|
||||
ManagerImpl m = null;
|
||||
try {
|
||||
m = new ManagerImpl(account, pathConfig, accountFileUpdater, serviceEnvironmentConfig, userAgent);
|
||||
account = null;
|
||||
|
||||
m.refreshPreKeys();
|
||||
if (response.isStorageCapable()) {
|
||||
if (storageCapable) {
|
||||
m.syncRemoteStorage();
|
||||
}
|
||||
// Set an initial empty profile so user can be added to groups
|
||||
|
||||
@ -8,8 +8,10 @@ import org.signal.libsignal.metadata.certificate.CertificateValidator;
|
||||
import org.signal.libsignal.net.Network;
|
||||
import org.signal.libsignal.protocol.SignalProtocolAddress;
|
||||
import org.signal.libsignal.zkgroup.profiles.ClientZkProfileOperations;
|
||||
import org.signal.network.api.AccountApiV2;
|
||||
import org.signal.network.api.AttachmentApi;
|
||||
import org.signal.network.api.CallingApi;
|
||||
import org.signal.network.api.CdnApi;
|
||||
import org.signal.network.api.CdsApi;
|
||||
import org.signal.network.api.CertificateApi;
|
||||
import org.signal.network.api.LinkDeviceApi;
|
||||
@ -73,6 +75,7 @@ public class SignalDependencies {
|
||||
|
||||
private SignalServiceAccountManager accountManager;
|
||||
private AccountApi accountApi;
|
||||
private AccountApiV2 accountApiV2;
|
||||
private RateLimitChallengeApi rateLimitChallengeApi;
|
||||
private CdsApi cdsApi;
|
||||
private UsernameApi usernameApi;
|
||||
@ -89,6 +92,7 @@ public class SignalDependencies {
|
||||
private ClientZkOperations clientZkOperations;
|
||||
private ProfileService profileService;
|
||||
private ProfileApi profileApi;
|
||||
private CdnApi cdnApi;
|
||||
private CdnService cdnService;
|
||||
private PreKeyRepository preKeyRepository;
|
||||
private SignalRestClient signalRestClient;
|
||||
@ -221,6 +225,11 @@ public class SignalDependencies {
|
||||
return getOrCreate(() -> accountApi, () -> accountApi = new AccountApi(getAuthenticatedSignalWebSocket()));
|
||||
}
|
||||
|
||||
public AccountApiV2 getAccountApiV2() {
|
||||
return getOrCreate(() -> accountApiV2,
|
||||
() -> accountApiV2 = new AccountApiV2(getAuthenticatedSignalWebSocket()));
|
||||
}
|
||||
|
||||
public RateLimitChallengeApi getRateLimitChallengeApi() {
|
||||
return getOrCreate(() -> rateLimitChallengeApi,
|
||||
() -> rateLimitChallengeApi = new RateLimitChallengeApi(getAuthenticatedSignalWebSocket()));
|
||||
@ -264,7 +273,7 @@ public class SignalDependencies {
|
||||
|
||||
public AttachmentApi getAttachmentApi() {
|
||||
return getOrCreate(() -> attachmentApi,
|
||||
() -> attachmentApi = new AttachmentApi(getAuthenticatedSignalWebSocket(), getPushServiceSocket()));
|
||||
() -> attachmentApi = new AttachmentApi(getAuthenticatedSignalWebSocket()));
|
||||
}
|
||||
|
||||
public CallingApi getCallingApi() {
|
||||
@ -346,7 +355,7 @@ public class SignalDependencies {
|
||||
|
||||
public CdnService getCdnService() {
|
||||
return getOrCreate(() -> cdnService,
|
||||
() -> cdnService = new CdnService(getSignalRestClient(), getAttachmentApi()));
|
||||
() -> cdnService = new CdnService(getCdnApi(), getAttachmentApi(), () -> null));
|
||||
}
|
||||
|
||||
public PreKeyRepository getPreKeyRepository() {
|
||||
@ -371,7 +380,8 @@ public class SignalDependencies {
|
||||
ServiceConfig.MAX_ENVELOPE_SIZE,
|
||||
ServiceConfig.MAX_INCREMENTAL_MACS_PER_ENVELOPE,
|
||||
() -> true,
|
||||
getPreKeyRepository()));
|
||||
getPreKeyRepository(),
|
||||
getCdnService()));
|
||||
}
|
||||
|
||||
public List<SecureValueRecovery> getSecureValueRecovery() {
|
||||
@ -390,6 +400,10 @@ public class SignalDependencies {
|
||||
getClientZkProfileOperations()));
|
||||
}
|
||||
|
||||
public CdnApi getCdnApi() {
|
||||
return getOrCreate(() -> cdnApi, () -> cdnApi = new CdnApi(getSignalRestClient()));
|
||||
}
|
||||
|
||||
public ProfileService getProfileService() {
|
||||
return getOrCreate(() -> profileService,
|
||||
() -> profileService = new ProfileService(getClientZkProfileOperations(),
|
||||
|
||||
@ -8,9 +8,11 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemo
|
||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.AtomicMoveNotSupportedException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AttachmentStore {
|
||||
@ -51,8 +53,23 @@ public class AttachmentStore {
|
||||
|
||||
private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
|
||||
createAttachmentsDir();
|
||||
try (OutputStream output = new FileOutputStream(attachmentFile)) {
|
||||
storer.store(output);
|
||||
final var temporaryFile = Files.createTempFile(attachmentsPath.toPath(),
|
||||
attachmentFile.getName() + ".",
|
||||
".tmp");
|
||||
try {
|
||||
try (OutputStream output = Files.newOutputStream(temporaryFile)) {
|
||||
storer.store(output);
|
||||
}
|
||||
try {
|
||||
Files.move(temporaryFile,
|
||||
attachmentFile.toPath(),
|
||||
StandardCopyOption.ATOMIC_MOVE,
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (AtomicMoveNotSupportedException e) {
|
||||
Files.move(temporaryFile, attachmentFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
} finally {
|
||||
Files.deleteIfExists(temporaryFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -72,6 +72,7 @@ import org.signal.libsignal.protocol.state.SignedPreKeyRecord;
|
||||
import org.signal.libsignal.protocol.util.KeyHelper;
|
||||
import org.signal.libsignal.zkgroup.InvalidInputException;
|
||||
import org.signal.libsignal.zkgroup.profiles.ProfileKey;
|
||||
import org.signal.network.api.RegistrationApiV2;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.signalservice.api.SignalServiceAccountDataStore;
|
||||
@ -235,6 +236,28 @@ public class SignalAccount implements Closeable {
|
||||
IdentityKeyPair pniIdentityKey,
|
||||
ProfileKey profileKey,
|
||||
final Settings settings
|
||||
) throws IOException {
|
||||
return create(dataPath,
|
||||
accountPath,
|
||||
number,
|
||||
null,
|
||||
serviceEnvironment,
|
||||
aciIdentityKey,
|
||||
pniIdentityKey,
|
||||
profileKey,
|
||||
settings);
|
||||
}
|
||||
|
||||
public static SignalAccount create(
|
||||
File dataPath,
|
||||
String accountPath,
|
||||
String number,
|
||||
ACI aci,
|
||||
ServiceEnvironment serviceEnvironment,
|
||||
IdentityKeyPair aciIdentityKey,
|
||||
IdentityKeyPair pniIdentityKey,
|
||||
ProfileKey profileKey,
|
||||
final Settings settings
|
||||
) throws IOException {
|
||||
IOUtils.createPrivateDirectories(dataPath);
|
||||
var fileName = getFileName(dataPath, accountPath);
|
||||
@ -253,6 +276,7 @@ public class SignalAccount implements Closeable {
|
||||
signalAccount.deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
|
||||
|
||||
signalAccount.dataPath = dataPath;
|
||||
signalAccount.aciAccountData.setServiceId(aci);
|
||||
signalAccount.aciAccountData.setIdentityKeyPair(aciIdentityKey);
|
||||
signalAccount.pniAccountData.setIdentityKeyPair(pniIdentityKey);
|
||||
signalAccount.aciAccountData.setLocalRegistrationId(KeyHelper.generateRegistrationId(false));
|
||||
@ -352,7 +376,9 @@ public class SignalAccount implements Closeable {
|
||||
this.registered = true;
|
||||
this.deviceId = deviceId;
|
||||
setPreKeys(ServiceIdType.ACI, aciPreKeys);
|
||||
setPreKeys(ServiceIdType.PNI, pniPreKeys);
|
||||
if (pniPreKeys != null) {
|
||||
setPreKeys(ServiceIdType.PNI, pniPreKeys);
|
||||
}
|
||||
save();
|
||||
}
|
||||
|
||||
@ -394,6 +420,53 @@ public class SignalAccount implements Closeable {
|
||||
clearSessionId();
|
||||
}
|
||||
|
||||
public void finishRecoveryRegistration(
|
||||
final ACI aci,
|
||||
final PNI pni,
|
||||
final String number,
|
||||
final AccountEntropyPool accountEntropyPool,
|
||||
final byte[] authCredentialSalt,
|
||||
final PreKeyCollection aciPreKeys,
|
||||
final PreKeyCollection pniPreKeys
|
||||
) {
|
||||
this.pinMasterKey = null;
|
||||
this.accountEntropyPool = accountEntropyPool;
|
||||
this.authCredentialSalt = authCredentialSalt;
|
||||
this.number = number;
|
||||
getKeyValueStore().storeEntry(storageManifestVersion, -1L);
|
||||
this.setStorageManifest(null);
|
||||
this.storageKey = null;
|
||||
this.encryptedDeviceName = null;
|
||||
this.deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
|
||||
this.isMultiDevice = false;
|
||||
this.registered = true;
|
||||
this.aciAccountData.setServiceId(aci);
|
||||
this.pniAccountData.setServiceId(pni);
|
||||
if (pni == null) {
|
||||
this.pniAccountData.setIdentityKeyPair(null);
|
||||
}
|
||||
init();
|
||||
this.registrationLockPin = null;
|
||||
setLastReceiveTimestamp(0L);
|
||||
setLastAppliedPniChangeServerTimestamp(0L);
|
||||
save();
|
||||
|
||||
setPreKeys(ServiceIdType.ACI, aciPreKeys);
|
||||
if (pni != null && pniPreKeys != null) {
|
||||
setPreKeys(ServiceIdType.PNI, pniPreKeys);
|
||||
}
|
||||
aciAccountData.getSessionStore().archiveAllSessions();
|
||||
pniAccountData.getSessionStore().archiveAllSessions();
|
||||
getSenderKeyStore().deleteAll();
|
||||
getRecipientTrustedResolver().resolveSelfRecipientTrusted(getSelfRecipientAddress());
|
||||
trustSelfIdentity(ServiceIdType.ACI);
|
||||
if (pni != null) {
|
||||
trustSelfIdentity(ServiceIdType.PNI);
|
||||
}
|
||||
getKeyValueStore().storeEntry(lastRecipientsRefresh, null);
|
||||
clearSessionId();
|
||||
}
|
||||
|
||||
public void initDatabase() {
|
||||
getAccountDatabase();
|
||||
}
|
||||
@ -403,7 +476,7 @@ public class SignalAccount implements Closeable {
|
||||
}
|
||||
|
||||
private void migrateLegacyConfigs() {
|
||||
if (isPrimaryDevice() && getPniIdentityKeyPair() == null) {
|
||||
if (isPrimaryDevice() && (number != null || getPni() != null) && getPniIdentityKeyPair() == null) {
|
||||
logger.trace("Migrating legacy parts of account file");
|
||||
setPniIdentityKeyPair(KeyUtils.generateIdentityKeyPair());
|
||||
}
|
||||
@ -1430,7 +1503,56 @@ public class SignalAccount implements Closeable {
|
||||
}
|
||||
|
||||
public AccountAttributes.Capabilities getAccountCapabilities() {
|
||||
return getCapabilities(isPrimaryDevice());
|
||||
return getCapabilities(isPrimaryDevice(), number != null);
|
||||
}
|
||||
|
||||
public RegistrationApiV2.AccountAttributes getAccountAttributesV2() {
|
||||
return getAccountAttributesV2(false, getRegistrationLock());
|
||||
}
|
||||
|
||||
public RegistrationApiV2.AccountAttributes getAccountAttributesV2(
|
||||
final boolean includePniRegistrationId,
|
||||
final String registrationLock
|
||||
) {
|
||||
return getAccountAttributesV2(includePniRegistrationId,
|
||||
registrationLock,
|
||||
getRecoveryPassword(),
|
||||
number == null ? null : isDiscoverableByPhoneNumber());
|
||||
}
|
||||
|
||||
public RegistrationApiV2.AccountAttributes getAccountAttributesV2ForRecovery(
|
||||
final String registrationLock,
|
||||
final String recoveryPassword
|
||||
) {
|
||||
return getAccountAttributesV2(true, registrationLock, recoveryPassword, null);
|
||||
}
|
||||
|
||||
private RegistrationApiV2.AccountAttributes getAccountAttributesV2(
|
||||
final boolean includePniRegistrationId,
|
||||
final String registrationLock,
|
||||
final String recoveryPassword,
|
||||
final Boolean discoverableByPhoneNumber
|
||||
) {
|
||||
final var attributes = getAccountAttributes(null);
|
||||
final var capabilities = attributes.getCapabilities();
|
||||
return new RegistrationApiV2.AccountAttributes(attributes.getSignalingKey(),
|
||||
attributes.getRegistrationId(),
|
||||
attributes.getVoice(),
|
||||
attributes.getVideo(),
|
||||
attributes.getFetchesMessages(),
|
||||
registrationLock,
|
||||
attributes.getUnidentifiedAccessKey(),
|
||||
attributes.getUnrestrictedUnidentifiedAccess(),
|
||||
discoverableByPhoneNumber,
|
||||
new RegistrationApiV2.AccountAttributes.Capabilities(capabilities.getStorage(),
|
||||
capabilities.getVersionedExpirationTimer(),
|
||||
capabilities.getAttachmentBackfill(),
|
||||
capabilities.getSpqr(),
|
||||
capabilities.getUsernameChangeSyncMessage(),
|
||||
capabilities.getOptionalPhoneNumber()),
|
||||
attributes.getName(),
|
||||
includePniRegistrationId || getPni() != null ? attributes.getPniRegistrationId() : null,
|
||||
recoveryPassword);
|
||||
}
|
||||
|
||||
public ServiceId getAccountId(ServiceIdType serviceIdType) {
|
||||
@ -1643,6 +1765,10 @@ public class SignalAccount implements Closeable {
|
||||
return accountEntropyPool;
|
||||
}
|
||||
|
||||
public AccountEntropyPool getAccountEntropyPool() {
|
||||
return accountEntropyPool;
|
||||
}
|
||||
|
||||
public void setAccountEntropyPool(final AccountEntropyPool accountEntropyPool) {
|
||||
this.accountEntropyPool = accountEntropyPool;
|
||||
if (accountEntropyPool != null) {
|
||||
|
||||
@ -63,11 +63,14 @@ public class AccountsStore {
|
||||
public synchronized Set<AccountsStorage.Account> getAllAccounts() throws IOException {
|
||||
return readAccounts().stream()
|
||||
.filter(a -> a.environment() == null || serviceEnvironment.equals(a.environment()))
|
||||
.filter(a -> a.number() != null)
|
||||
.filter(a -> a.number() != null || a.uuid() != null)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public synchronized String getPathByNumber(String number) throws IOException {
|
||||
if (number == null) {
|
||||
return null;
|
||||
}
|
||||
return readAccounts().stream()
|
||||
.filter(a -> a.environment() == null || serviceEnvironment.equals(a.environment()))
|
||||
.filter(a -> number.equals(a.number()))
|
||||
@ -101,7 +104,7 @@ public class AccountsStore {
|
||||
if (number != null && number.equals(a.number())) {
|
||||
return new AccountsStorage.Account(a.path(), a.environment(), null, a.uuid());
|
||||
}
|
||||
if (aci != null && aci.toString().equals(a.toString())) {
|
||||
if (aci != null && aci.toString().equals(a.uuid())) {
|
||||
return new AccountsStorage.Account(a.path(), a.environment(), a.number(), null);
|
||||
}
|
||||
|
||||
@ -188,7 +191,7 @@ public class AccountsStore {
|
||||
private List<AccountsStorage.Account> readAccounts() throws IOException {
|
||||
final var pair = openFileChannel(getAccountsFile());
|
||||
try (final var fileChannel = pair.first(); final var lock = pair.second()) {
|
||||
final var storage = readAccountsLocked(fileChannel);
|
||||
var storage = readAccountsLocked(fileChannel);
|
||||
|
||||
var accountsVersion = storage.version() == null ? 1 : storage.version();
|
||||
if (accountsVersion > CURRENT_STORAGE_VERSION) {
|
||||
@ -197,7 +200,7 @@ public class AccountsStore {
|
||||
throw new IOException("Accounts file was created by a no longer supported older version: "
|
||||
+ accountsVersion);
|
||||
} else if (accountsVersion < CURRENT_STORAGE_VERSION) {
|
||||
return upgradeAccountsFile(fileChannel, storage, accountsVersion).accounts();
|
||||
storage = upgradeAccountsFile(fileChannel, storage, accountsVersion);
|
||||
}
|
||||
return storage.accounts();
|
||||
}
|
||||
|
||||
@ -33,9 +33,9 @@ public class AttachmentUtils {
|
||||
ResumableUploadSpec resumableUploadSpec
|
||||
) throws ResumeLocationInvalidException, IOException {
|
||||
final var uploadTimestamp = System.currentTimeMillis();
|
||||
final var probedStream = dimensions != null
|
||||
? new ProbedStream(streamDetails.getStream(), dimensions.width(), dimensions.height())
|
||||
: probeImageDimensions(streamDetails);
|
||||
final var probedStream = dimensions != null ? new ProbedStream(streamDetails.getStream(),
|
||||
dimensions.width(),
|
||||
dimensions.height()) : probeImageDimensions(streamDetails);
|
||||
return SignalServiceAttachmentStream.newStreamBuilder()
|
||||
.withStream(probedStream.inputStream())
|
||||
.withContentType(streamDetails.getContentType())
|
||||
|
||||
@ -42,6 +42,19 @@ public class PhoneNumberFormatter {
|
||||
throw new InvalidNumberException("No valid characters found.");
|
||||
}
|
||||
|
||||
if (localNumber == null) {
|
||||
if (!number.startsWith("+")) {
|
||||
throw new InvalidNumberException(
|
||||
"Use an international number including the country code for a numberless account.");
|
||||
}
|
||||
try {
|
||||
final var util = PhoneNumberUtil.getInstance();
|
||||
return util.format(util.parse(number, null), PhoneNumberFormat.E164);
|
||||
} catch (NumberParseException e) {
|
||||
throw new InvalidNumberException("Invalid international phone number.");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
|
||||
PhoneNumber localNumberObject = util.parse(localNumber, null);
|
||||
|
||||
@ -65,7 +65,7 @@ public class ProfileUtils {
|
||||
final SignalServiceProfile encryptedProfile,
|
||||
final ProfileCipher profileCipher
|
||||
) {
|
||||
if (encryptedProfile.isUnrestrictedUnidentifiedAccess()) {
|
||||
if (encryptedProfile.getUnrestrictedUnidentifiedAccess()) {
|
||||
return Profile.UnidentifiedAccessMode.UNRESTRICTED;
|
||||
}
|
||||
|
||||
@ -81,11 +81,13 @@ public class ProfileUtils {
|
||||
|
||||
public static HashSet<Profile.Capability> getCapabilities(final SignalServiceProfile encryptedProfile) {
|
||||
final var capabilities = new HashSet<Profile.Capability>();
|
||||
if (encryptedProfile.getCapabilities().isStorage()) {
|
||||
capabilities.add(Profile.Capability.storage);
|
||||
}
|
||||
if (encryptedProfile.getCapabilities().isStorageServiceEncryptionV2()) {
|
||||
capabilities.add(Profile.Capability.storageServiceEncryptionV2Capability);
|
||||
if (encryptedProfile.getCapabilities() != null) {
|
||||
if (encryptedProfile.getCapabilities().getStorage()) {
|
||||
capabilities.add(Profile.Capability.storage);
|
||||
}
|
||||
if (encryptedProfile.getCapabilities().getStorageServiceEncryptionV2()) {
|
||||
capabilities.add(Profile.Capability.storageServiceEncryptionV2Capability);
|
||||
}
|
||||
}
|
||||
|
||||
return capabilities;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package org.asamk.signal.manager.util;
|
||||
|
||||
import org.asamk.signal.manager.api.BadRequestException;
|
||||
import org.asamk.signal.manager.api.Pair;
|
||||
import org.signal.core.models.ServiceId;
|
||||
import org.signal.libsignal.net.BadRequestError;
|
||||
@ -194,8 +195,8 @@ public class Utils {
|
||||
}
|
||||
} else if (result instanceof RequestResult.RetryableNetworkError e) {
|
||||
throw e.getNetworkError();
|
||||
} else if (result instanceof RequestResult.NonSuccess) {
|
||||
throw new AssertionError();
|
||||
} else if (result instanceof RequestResult.NonSuccess<?> e) {
|
||||
throw new BadRequestException(e.getError());
|
||||
}
|
||||
throw new IllegalStateException("Unexpected value: " + result);
|
||||
}
|
||||
|
||||
@ -0,0 +1,289 @@
|
||||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
|
||||
import org.whispersystems.signalservice.api.util.StreamDetails;
|
||||
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class AttachmentHelperTest {
|
||||
|
||||
private static final String PAYLOAD = Base64.getEncoder()
|
||||
.encodeToString("private attachment marker ".repeat(200).getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void preparationFailureIsAttachmentInvalidAndNothingIsUploaded() {
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failSpecOnCall = 1;
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(uploader).uploadAttachments(List.of(inline("one"), inline("two"))));
|
||||
|
||||
assertEquals("inline attachment #1: spec failed", error.getMessage());
|
||||
assertRedacted(error);
|
||||
assertEquals(0, uploader.uploads.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadFailureOnSecondInlineAttachmentStopsBeforeTheThird() {
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failUploadOnCall = 2;
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(uploader).uploadAttachments(List.of(inline("one"), inline("two"), inline("three"))));
|
||||
|
||||
assertEquals("inline attachment #2: upload failed", error.getMessage());
|
||||
assertSame(uploader.uploadFailure, error.getCause());
|
||||
assertRedacted(error);
|
||||
assertEquals(List.of("one", "two"), uploader.uploads);
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadFailureOnSecondFileAttachmentNamesThePath() throws IOException {
|
||||
final var files = List.of(file("a.txt"), file("b.txt"), file("c.txt"));
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failUploadOnCall = 2;
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(uploader).uploadAttachments(files));
|
||||
|
||||
assertEquals(files.get(1) + ": upload failed", error.getMessage());
|
||||
assertInstanceOf(SocketException.class, error.getCause());
|
||||
assertEquals(List.of("a.txt", "b.txt"), uploader.uploads);
|
||||
}
|
||||
|
||||
@Test
|
||||
void successfulUploadsKeepInputOrder() throws Exception {
|
||||
final var uploader = new FakeUploader();
|
||||
final var pointers = helper(uploader).uploadAttachments(List.of(inline("one"),
|
||||
file("two.txt"),
|
||||
inline("three")));
|
||||
|
||||
assertEquals(List.of("one", "two.txt", "three"),
|
||||
pointers.stream()
|
||||
.map(SignalServiceAttachment::asPointer)
|
||||
.map(p -> p.getFileName().orElseThrow())
|
||||
.toList());
|
||||
assertEquals(3, uploader.specs);
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedInlineAttachmentDoesNotExposePayload() {
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(new FakeUploader()).uploadAttachments(List.of("data:application/octet-stream;base64,"
|
||||
+ PAYLOAD
|
||||
+ "!")));
|
||||
|
||||
assertEquals("inline attachment #1: Invalid data URI", error.getMessage());
|
||||
assertRedacted(error);
|
||||
}
|
||||
|
||||
@Test
|
||||
void upperCaseInlineAttachmentIsNotTreatedAsAFilePath() throws Exception {
|
||||
// Longer than PATH_MAX, so canonicalizing it as a file path would fail
|
||||
final var uploader = new FakeUploader();
|
||||
final var pointers = helper(uploader).uploadAttachments(List.of(
|
||||
"DATA:application/octet-stream;filename=upper;base64," + PAYLOAD));
|
||||
|
||||
assertEquals(1, pointers.size());
|
||||
assertEquals(List.of("upper"), uploader.uploads);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"data:text/plain;base64", "data://", "data:;base64//", "DATA://"})
|
||||
void inlineAttachmentWithoutCommaIsNotExposed(final String prefix) {
|
||||
// Short enough to reach file opening if inline input is incorrectly treated as a path.
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(new FakeUploader()).uploadAttachments(List.of(prefix + PAYLOAD.substring(0, 80))));
|
||||
|
||||
assertEquals("inline attachment #1: Invalid data URI", error.getMessage());
|
||||
assertRedacted(error);
|
||||
}
|
||||
|
||||
@Test
|
||||
void longInlineAttachmentWithoutCommaIsRejected() {
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(new FakeUploader()).uploadAttachments(List.of("data:" + PAYLOAD.repeat(160))));
|
||||
|
||||
assertEquals("inline attachment #1: Invalid data URI", error.getMessage());
|
||||
assertRedacted(error);
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultUploadUsesTheSameErrorContract() {
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failUploadOnCall = 1;
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(uploader).uploadAttachment(inline("image")));
|
||||
|
||||
assertEquals("inline attachment: upload failed", error.getMessage());
|
||||
assertSame(uploader.uploadFailure, error.getCause());
|
||||
assertRedacted(error);
|
||||
}
|
||||
|
||||
@Test
|
||||
void stickerPreparationFailureIsLabelled() {
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failSpecOnCall = 1;
|
||||
final var stream = new ByteArrayInputStream(new byte[4]);
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(uploader).createStickerAttachmentStream(new StreamDetails(stream, "image/webp", 4)));
|
||||
|
||||
assertEquals("sticker: spec failed", error.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void longTextFailureNeverEchoesTheText(final boolean preparation) {
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failSpecOnCall = preparation ? 1 : 0;
|
||||
uploader.failUploadOnCall = preparation ? 0 : 1;
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper(uploader).uploadLongTextAttachment(PAYLOAD.getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
assertEquals("long text attachment: " + (preparation ? "spec" : "upload") + " failed", error.getMessage());
|
||||
assertRedacted(error);
|
||||
assertInstanceOf(SocketException.class, error.getCause());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"quote thumbnail #1", "link preview image #1"})
|
||||
void imageFailuresUseTheirLabel(final String label) {
|
||||
final var uploader = new FakeUploader();
|
||||
uploader.failSpecOnCall = 1;
|
||||
final var helper = helper(uploader);
|
||||
final var preparationError = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper.uploadAttachment(inline("image"), label));
|
||||
assertEquals(label + ": spec failed", preparationError.getMessage());
|
||||
assertRedacted(preparationError);
|
||||
assertEquals(0, uploader.uploads.size());
|
||||
|
||||
uploader.failUploadOnCall = 1;
|
||||
final var uploadError = assertThrows(AttachmentInvalidException.class,
|
||||
() -> helper.uploadAttachment(inline("image"), label));
|
||||
assertEquals(label + ": upload failed", uploadError.getMessage());
|
||||
assertRedacted(uploadError);
|
||||
}
|
||||
|
||||
@Test
|
||||
void filesInTheDataDirectoryAreStillRejected() throws IOException {
|
||||
final var dataDir = Files.createDirectory(tempDir.resolve("data")).toFile();
|
||||
final var secret = new File(dataDir, "account.db");
|
||||
Files.writeString(secret.toPath(), "secret");
|
||||
final var uploader = new FakeUploader();
|
||||
final var error = assertThrows(AttachmentInvalidException.class,
|
||||
() -> new AttachmentHelper(uploader, () -> dataDir).uploadAttachments(List.of(secret.getPath())));
|
||||
|
||||
assertEquals(secret.getPath() + ": Attaching files from the signal-cli data directory is not allowed",
|
||||
error.getMessage());
|
||||
assertEquals(0, uploader.specs);
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidWithoutCauseMessageFallsBackToClassName() {
|
||||
final var cause = new SocketException();
|
||||
final var error = AttachmentHelper.invalid("inline attachment #1", cause);
|
||||
|
||||
assertEquals("inline attachment #1: java.net.SocketException", error.getMessage());
|
||||
assertSame(cause, error.getCause());
|
||||
}
|
||||
|
||||
private AttachmentHelper helper(final FakeUploader uploader) {
|
||||
return new AttachmentHelper(uploader, () -> tempDir.resolve("data").toFile());
|
||||
}
|
||||
|
||||
private String file(final String name) throws IOException {
|
||||
final var path = tempDir.resolve(name);
|
||||
Files.writeString(path, PAYLOAD);
|
||||
return path.toString();
|
||||
}
|
||||
|
||||
private static String inline(final String filename) {
|
||||
return "data:application/octet-stream;filename=" + filename + ";base64," + PAYLOAD;
|
||||
}
|
||||
|
||||
private static void assertRedacted(final AttachmentInvalidException error) {
|
||||
final var trace = new StringWriter();
|
||||
error.printStackTrace(new PrintWriter(trace));
|
||||
assertFalse(trace.toString().contains(PAYLOAD.substring(0, 32)));
|
||||
}
|
||||
|
||||
private static final class FakeUploader implements AttachmentHelper.Uploader {
|
||||
|
||||
final IOException uploadFailure = new SocketException("upload failed");
|
||||
int failSpecOnCall;
|
||||
int failUploadOnCall;
|
||||
int specs;
|
||||
final List<String> uploads = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public ResumableUploadSpec getResumableUploadSpec(final long ciphertextLength) throws IOException {
|
||||
if (++specs == failSpecOnCall) {
|
||||
throw new SocketException("spec failed");
|
||||
}
|
||||
return new ResumableUploadSpec(new byte[64],
|
||||
new byte[16],
|
||||
"cdn-key",
|
||||
3,
|
||||
"https://cdn.invalid/upload",
|
||||
Long.MAX_VALUE,
|
||||
Map.of());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SignalServiceAttachmentPointer upload(final SignalServiceAttachmentStream stream) throws IOException {
|
||||
final var fileName = stream.getFileName().orElse("");
|
||||
uploads.add(fileName);
|
||||
if (uploads.size() == failUploadOnCall) {
|
||||
throw uploadFailure;
|
||||
}
|
||||
return new SignalServiceAttachmentPointer(3,
|
||||
SignalServiceAttachmentRemoteId.from("cdn-key", 3),
|
||||
stream.getContentType(),
|
||||
new byte[64],
|
||||
Optional.of((int) stream.getLength()),
|
||||
Optional.empty(),
|
||||
0,
|
||||
0,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
0,
|
||||
Optional.of(fileName),
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
0,
|
||||
UUID.randomUUID());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,16 +1,22 @@
|
||||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.manager.api.CallInfo;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.storage.recipients.TestRecipientId;
|
||||
import org.asamk.signal.manager.util.Utils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
@ -23,6 +29,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
*/
|
||||
class CallManagerTest {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
// --- Reflection helpers for private static methods ---
|
||||
|
||||
private static final MethodHandle GET_RAW_IDENTITY_KEY_BYTES;
|
||||
@ -177,6 +185,38 @@ class CallManagerTest {
|
||||
assertTrue(foundDifferent, "generateCallId returned same value 21 times in a row");
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildConfig_usesProvidedLinkedDeviceId() throws Exception {
|
||||
var state = makeCallState(123L, CallInfo.State.RINGING_OUTGOING);
|
||||
|
||||
var config = OBJECT_MAPPER.readTree(CallManager.buildConfig(state, 7));
|
||||
|
||||
assertEquals(7, config.get("local_device_id").intValue());
|
||||
assertEquals(123L, config.get("call_id").longValue());
|
||||
assertTrue(config.get("is_outgoing").booleanValue());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"1, true", "1, false", "2, true", "2, false", "127, true", "127, false"})
|
||||
void buildConfig_readsAccountDeviceId(int deviceId, boolean outgoing) throws Exception {
|
||||
var constructor = SignalAccount.class.getDeclaredConstructor(FileChannel.class, FileLock.class);
|
||||
constructor.setAccessible(true);
|
||||
var account = constructor.newInstance(null, null);
|
||||
var deviceField = SignalAccount.class.getDeclaredField("deviceId");
|
||||
deviceField.setAccessible(true);
|
||||
deviceField.setInt(account, deviceId);
|
||||
var builder = CallManager.class.getDeclaredMethod("buildConfig", CallManager.CallState.class);
|
||||
builder.setAccessible(true);
|
||||
|
||||
try (var manager = new CallManager(new Context(account, null, null, null, null, null))) {
|
||||
var state = new CallManager.CallState(-1L, CallInfo.State.IDLE, null, null, outgoing);
|
||||
var config = new ObjectMapper().readTree((String) builder.invoke(manager, state));
|
||||
assertEquals(deviceId, config.path("local_device_id").asInt());
|
||||
assertEquals(outgoing, config.path("is_outgoing").asBoolean());
|
||||
assertEquals("18446744073709551615", config.path("call_id").asText());
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// handleStateChange state machine tests
|
||||
//
|
||||
|
||||
@ -0,0 +1,57 @@
|
||||
package org.asamk.signal.manager.helper;
|
||||
|
||||
import org.asamk.signal.manager.Settings;
|
||||
import org.asamk.signal.manager.api.ServiceEnvironment;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.signal.core.models.ServiceId.ACI;
|
||||
import org.signal.storageservice.storage.protos.groups.local.DecryptedGroup;
|
||||
import org.signal.storageservice.storage.protos.groups.local.DecryptedMember;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class NumberlessGroupTest {
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@Test
|
||||
void findsOurGroupMembershipByAciWhenPniIsAbsent() throws Exception {
|
||||
final var aci = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
final var otherAci = ACI.parseOrThrow("22222222-2222-4222-8222-222222222222");
|
||||
try (final var account = SignalAccount.createLinkedAccount(directory.toFile(),
|
||||
"account",
|
||||
ServiceEnvironment.STAGING,
|
||||
Settings.DEFAULT); final var context = new Context(account, null, null, null, null, null)) {
|
||||
account.setProvisioningData(null,
|
||||
aci,
|
||||
null,
|
||||
"test-password",
|
||||
new byte[]{1},
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
null,
|
||||
KeyUtils.createProfileKey(),
|
||||
null,
|
||||
new byte[32],
|
||||
null);
|
||||
final var otherMember = new DecryptedMember.Builder().aciBytes(otherAci.toByteString())
|
||||
.joinedAtRevision(1)
|
||||
.build();
|
||||
final var selfMember = new DecryptedMember.Builder().aciBytes(aci.toByteString())
|
||||
.joinedAtRevision(4)
|
||||
.build();
|
||||
final var group = new DecryptedGroup.Builder().revision(9)
|
||||
.members(List.of(otherMember, selfMember))
|
||||
.build();
|
||||
assertEquals(4, context.getGroupV2Helper().findRevisionWeWereAdded(group));
|
||||
assertEquals(9,
|
||||
context.getGroupV2Helper()
|
||||
.findRevisionWeWereAdded(group.newBuilder().members(List.of(otherMember)).build()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -39,8 +39,8 @@ class StorageHelperTest {
|
||||
Set.of(pendingId)));
|
||||
assertFalse(StorageHelper.containsOnlyIdentityConflictsPendingRepair(write(List.of()), Set.of(pendingId)));
|
||||
assertFalse(StorageHelper.containsOnlyIdentityConflictsPendingRepair(new WriteOperationResult(null,
|
||||
List.of(pendingRecord),
|
||||
List.of(new byte[]{3})), Set.of(pendingId)));
|
||||
List.of(pendingRecord),
|
||||
List.of(new byte[]{3})), Set.of(pendingId)));
|
||||
}
|
||||
|
||||
private static SignalStorageRecord record(final StorageId id) {
|
||||
|
||||
@ -0,0 +1,184 @@
|
||||
package org.asamk.signal.manager.internal;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.manager.Settings;
|
||||
import org.asamk.signal.manager.api.ServiceEnvironment;
|
||||
import org.asamk.signal.manager.config.ServiceConfig;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.signal.core.models.ServiceId.ACI;
|
||||
import org.signal.core.models.ServiceId.PNI;
|
||||
import org.signal.network.api.RegistrationApiV2;
|
||||
import org.signal.network.rest.SignalRestClient;
|
||||
import org.whispersystems.signalservice.api.push.ServiceIdType;
|
||||
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
|
||||
import org.whispersystems.signalservice.internal.push.ProvisionMessage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import okhttp3.Credentials;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Protocol;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.ResponseBody;
|
||||
import okio.Buffer;
|
||||
import okio.ByteString;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class NumberlessProvisioningTest {
|
||||
|
||||
private static final ACI ACI_ID = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
private static final PNI PNI_ID = PNI.parseOrThrow("22222222-2222-4222-8222-222222222222");
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@Test
|
||||
void numberlessProvisioningRequiresGroupCredentialSalt() {
|
||||
assertThrows(IOException.class, () -> ProvisioningManagerImpl.parsePni(new ProvisionMessage.Builder().build()));
|
||||
final var emptySalt = new ProvisionMessage.Builder().authCredentialSalt(ByteString.EMPTY).build();
|
||||
assertThrows(IOException.class, () -> ProvisioningManagerImpl.parsePni(emptySalt));
|
||||
assertNull(ProvisioningManagerImpl.toRegistrationPreKeys(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsNumberlessProvisioningButRejectsInconsistentPhoneIdentity() throws Exception {
|
||||
final var message = new ProvisionMessage.Builder().authCredentialSalt(ByteString.of(new byte[32]));
|
||||
assertNull(ProvisioningManagerImpl.parsePni(message.build()));
|
||||
assertThrows(IOException.class, () -> ProvisioningManagerImpl.parsePni(message.pni(PNI_ID.toString()).build()));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ProvisioningManagerImpl.parsePni(new ProvisionMessage.Builder().number("+12025550123").build()));
|
||||
assertEquals(PNI_ID,
|
||||
ProvisioningManagerImpl.parsePni(new ProvisionMessage.Builder().number("+12025550123")
|
||||
.pni(PNI_ID.toString())
|
||||
.build()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void numberlessLinkRequestOmitsPniAndAuthenticatesWithAci() throws Exception {
|
||||
checkLinkRequest(null, null, 200, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void numberedLinkRequestStillIncludesPniKeys() throws Exception {
|
||||
checkLinkRequest("+12025550123", PNI_ID, 200, null);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({
|
||||
"422, Signal rejected the device linking request",
|
||||
"403, [403] Device verification failed",
|
||||
"409, Linked device is missing a required account capability",
|
||||
"411, Account has reached its linked device limit",
|
||||
"429, Device linking rate limited; try again later"
|
||||
})
|
||||
void linkingErrorsAreReportedWithoutCrashingOrIncludingServerBody(
|
||||
final int statusCode,
|
||||
final String errorMessage
|
||||
) throws Exception {
|
||||
checkLinkRequest(null, null, statusCode, errorMessage);
|
||||
}
|
||||
|
||||
private void checkLinkRequest(
|
||||
final String number,
|
||||
final PNI pni,
|
||||
final int statusCode,
|
||||
final String errorMessage
|
||||
) throws Exception {
|
||||
final var request = new AtomicReference<Request>();
|
||||
final var body = new AtomicReference<JsonNode>();
|
||||
final var mapper = new ObjectMapper();
|
||||
final var client = new OkHttpClient.Builder().addInterceptor(chain -> {
|
||||
request.set(chain.request());
|
||||
final var buffer = new Buffer();
|
||||
chain.request().body().writeTo(buffer);
|
||||
body.set(mapper.readTree(buffer.readUtf8()));
|
||||
return new Response.Builder().request(chain.request())
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
.code(statusCode)
|
||||
.message("Test response")
|
||||
.body(ResponseBody.create(statusCode == 200 ? "{\"deviceId\":2}" : "secret payload",
|
||||
MediaType.get("application/json")))
|
||||
.build();
|
||||
}).build();
|
||||
final var config = ServiceConfig.getServiceEnvironmentConfig(ServiceEnvironment.STAGING, "signal-cli-test");
|
||||
final var restClient = new SignalRestClient(config.signalServiceConfiguration(),
|
||||
"signal-cli-test",
|
||||
null,
|
||||
false,
|
||||
config.signalServiceConfiguration(),
|
||||
() -> false,
|
||||
1000L,
|
||||
new SecureRandom(),
|
||||
client);
|
||||
final var api = new RegistrationApiV2(restClient, false);
|
||||
|
||||
try (final var account = SignalAccount.createLinkedAccount(directory.toFile(),
|
||||
"account",
|
||||
ServiceEnvironment.STAGING,
|
||||
Settings.DEFAULT)) {
|
||||
account.setProvisioningData(number,
|
||||
ACI_ID,
|
||||
pni,
|
||||
"test-password",
|
||||
new byte[]{1},
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
pni == null ? null : KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.createProfileKey(),
|
||||
null,
|
||||
new byte[32],
|
||||
null);
|
||||
final var aciKeys = KeyUtils.generatePreKeysForType(account.getAccountData(ServiceIdType.ACI));
|
||||
final var pniKeys = pni == null
|
||||
? null
|
||||
: KeyUtils.generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));
|
||||
if (statusCode == 200) {
|
||||
assertEquals(2,
|
||||
ProvisioningManagerImpl.registerLinkedDevice(api, account, "test-code", aciKeys, pniKeys));
|
||||
} else {
|
||||
final var error = assertThrows(IOException.class,
|
||||
() -> ProvisioningManagerImpl.registerLinkedDevice(api,
|
||||
account,
|
||||
"test-code",
|
||||
aciKeys,
|
||||
pniKeys));
|
||||
assertEquals(errorMessage, error.getMessage());
|
||||
if (statusCode == 403) {
|
||||
assertInstanceOf(AuthorizationFailedException.class, error);
|
||||
}
|
||||
}
|
||||
assertEquals("PUT", request.get().method());
|
||||
assertEquals("/v1/devices/link", request.get().url().encodedPath());
|
||||
assertEquals(Credentials.basic(ACI_ID.toString(), "test-password"), request.get().header("Authorization"));
|
||||
assertTrue(body.get()
|
||||
.path("accountAttributes")
|
||||
.path("capabilities")
|
||||
.path("optionalPhoneNumber")
|
||||
.asBoolean());
|
||||
assertTrue(body.get().hasNonNull("aciSignedPreKey"));
|
||||
assertTrue(body.get().hasNonNull("aciPqLastResortPreKey"));
|
||||
assertEquals(pni != null, body.get().has("pniSignedPreKey"));
|
||||
assertEquals(pni != null, body.get().has("pniPqLastResortPreKey"));
|
||||
assertEquals(pni != null, body.get().path("accountAttributes").has("pniRegistrationId"));
|
||||
} finally {
|
||||
client.dispatcher().executorService().shutdown();
|
||||
client.connectionPool().evictAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,256 @@
|
||||
package org.asamk.signal.manager.internal;
|
||||
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.Settings;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.api.ServiceEnvironment;
|
||||
import org.asamk.signal.manager.config.ServiceConfig;
|
||||
import org.asamk.signal.manager.helper.AccountFileUpdater;
|
||||
import org.asamk.signal.manager.helper.Context;
|
||||
import org.asamk.signal.manager.helper.ReceiveHelper;
|
||||
import org.asamk.signal.manager.storage.SignalAccount;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.signal.core.models.ServiceId.ACI;
|
||||
import org.whispersystems.signalservice.api.push.ServiceIdType;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.security.Security;
|
||||
import java.time.Duration;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@Timeout(60)
|
||||
class ReceiveThreadTest {
|
||||
|
||||
private static final Duration TIMEOUT = Duration.ofSeconds(10);
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
private ManagerImpl manager;
|
||||
private FakeReceiveHelper receiveHelper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
// ManagerImpl.close() creates the push service socket, whose trust store needs BouncyCastle
|
||||
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
final var account = SignalAccount.createLinkedAccount(directory.toFile(),
|
||||
"account",
|
||||
ServiceEnvironment.STAGING,
|
||||
Settings.DEFAULT);
|
||||
account.setProvisioningData(null,
|
||||
ACI.parseOrThrow("11111111-1111-4111-8111-111111111111"),
|
||||
null,
|
||||
"test-password",
|
||||
new byte[]{1},
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
null,
|
||||
KeyUtils.createProfileKey(),
|
||||
null,
|
||||
new byte[32],
|
||||
null);
|
||||
account.finishLinking(2, KeyUtils.generatePreKeysForType(account.getAccountData(ServiceIdType.ACI)), null);
|
||||
manager = new ManagerImpl(account,
|
||||
PathConfig.createDefault(directory.toFile()),
|
||||
new AccountFileUpdater() {
|
||||
@Override
|
||||
public void updateAccountIdentifiers(final String number, final ACI aci) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAccount() {
|
||||
}
|
||||
},
|
||||
ServiceConfig.getServiceEnvironmentConfig(ServiceEnvironment.STAGING, "signal-cli-test"),
|
||||
"signal-cli-test");
|
||||
|
||||
// Replace the receive helper, so the receive thread doesn't connect to the server
|
||||
final var contextField = ManagerImpl.class.getDeclaredField("context");
|
||||
contextField.setAccessible(true);
|
||||
final var context = (Context) contextField.get(manager);
|
||||
receiveHelper = new FakeReceiveHelper(context);
|
||||
final var receiveHelperField = Context.class.getDeclaredField("receiveHelper");
|
||||
receiveHelperField.setAccessible(true);
|
||||
receiveHelperField.set(context, receiveHelper);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
receiveHelper.setHoldStoppingLoops(false);
|
||||
manager.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void handlerAddedWhileReceiveThreadIsStoppingDoesNotStartSecondReceiveThread() throws Exception {
|
||||
final var first = newHandler();
|
||||
final var second = newHandler();
|
||||
|
||||
manager.addReceiveHandler(first);
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.runningLoops == 1));
|
||||
|
||||
receiveHelper.setHoldStoppingLoops(true);
|
||||
final var remover = Thread.ofPlatform().start(() -> manager.removeReceiveHandler(first));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.stoppingLoops == 1));
|
||||
|
||||
// Like a client that reconnects while the receive thread for its previous connection is still stopping
|
||||
manager.addReceiveHandler(second);
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.startedThreads == 1));
|
||||
|
||||
receiveHelper.setHoldStoppingLoops(false);
|
||||
assertTrue(remover.join(TIMEOUT));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.startedLoops == 2 && receiveHelper.runningLoops == 1));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.startedThreads == 2 && receiveHelper.maxRunningLoops == 1));
|
||||
|
||||
manager.removeReceiveHandler(second);
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.runningLoops == 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopRequestedForStoppingReceiveThreadDoesNotStopNextReceiveThread() throws Exception {
|
||||
final var first = newHandler();
|
||||
final var second = newHandler();
|
||||
final var third = newHandler();
|
||||
|
||||
manager.addReceiveHandler(first);
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.runningLoops == 1));
|
||||
|
||||
receiveHelper.setHoldStoppingLoops(true);
|
||||
final var firstRemover = Thread.ofPlatform().start(() -> manager.removeReceiveHandler(first));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.stoppingLoops == 1));
|
||||
manager.addReceiveHandler(second);
|
||||
final var secondRemover = Thread.ofPlatform().start(() -> manager.removeReceiveHandler(second));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.stopRequests == 2));
|
||||
|
||||
receiveHelper.setHoldStoppingLoops(false);
|
||||
assertTrue(firstRemover.join(TIMEOUT));
|
||||
assertTrue(secondRemover.join(TIMEOUT));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.runningLoops == 0 && receiveHelper.startedThreads == 1));
|
||||
|
||||
manager.addReceiveHandler(third);
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.startedLoops == 2 && receiveHelper.runningLoops == 1));
|
||||
assertTrue(receiveHelper.await(() -> receiveHelper.startedThreads == 2 && receiveHelper.maxRunningLoops == 1));
|
||||
}
|
||||
|
||||
private static Manager.ReceiveMessageHandler newHandler() {
|
||||
// An anonymous class instead of a lambda, so every handler is a separate instance
|
||||
return new Manager.ReceiveMessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(final MessageEnvelope envelope, final Throwable e) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Instead of receiving messages from the server, a receive loop waits until it has been asked to stop.
|
||||
* While holdStoppingLoops is set, a loop that has been asked to stop doesn't return yet, like a loop that
|
||||
* is still busy when the last handler is removed.
|
||||
*/
|
||||
private static class FakeReceiveHelper extends ReceiveHelper {
|
||||
|
||||
private final Object lock = new Object();
|
||||
private boolean stopRequested;
|
||||
private boolean holdStoppingLoops;
|
||||
private int stopRequests;
|
||||
private int startedThreads;
|
||||
private int startedLoops;
|
||||
private int runningLoops;
|
||||
private int maxRunningLoops;
|
||||
private int stoppingLoops;
|
||||
|
||||
FakeReceiveHelper(final Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean requestStopReceiveMessages() {
|
||||
super.requestStopReceiveMessages();
|
||||
synchronized (lock) {
|
||||
stopRequested = true;
|
||||
stopRequests++;
|
||||
lock.notifyAll();
|
||||
}
|
||||
// The fake loop isn't blocked in a read that needs to be interrupted
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearStopRequest() {
|
||||
super.clearStopRequest();
|
||||
synchronized (lock) {
|
||||
stopRequested = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessagesContinuously(final Manager.ReceiveMessageHandler handler) {
|
||||
synchronized (lock) {
|
||||
startedThreads++;
|
||||
lock.notifyAll();
|
||||
}
|
||||
super.receiveMessagesContinuously(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(
|
||||
final Optional<Duration> timeout,
|
||||
final Integer maxMessages,
|
||||
final Manager.ReceiveMessageHandler handler
|
||||
) {
|
||||
synchronized (lock) {
|
||||
startedLoops++;
|
||||
runningLoops++;
|
||||
maxRunningLoops = Math.max(maxRunningLoops, runningLoops);
|
||||
lock.notifyAll();
|
||||
if (await(() -> stopRequested)) {
|
||||
stoppingLoops++;
|
||||
lock.notifyAll();
|
||||
await(() -> !holdStoppingLoops);
|
||||
stoppingLoops--;
|
||||
}
|
||||
runningLoops--;
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
void setHoldStoppingLoops(final boolean hold) {
|
||||
synchronized (lock) {
|
||||
holdStoppingLoops = hold;
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits until the condition, which is evaluated while holding the lock, is true.
|
||||
* Returns false if it didn't become true within the timeout.
|
||||
*/
|
||||
boolean await(final BooleanSupplier condition) {
|
||||
synchronized (lock) {
|
||||
final var deadline = System.nanoTime() + TIMEOUT.toNanos();
|
||||
while (!condition.getAsBoolean()) {
|
||||
final var remainingMillis = (deadline - System.nanoTime()) / 1_000_000;
|
||||
if (remainingMillis <= 0) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
lock.wait(remainingMillis);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
package org.asamk.signal.manager.storage;
|
||||
|
||||
import org.asamk.signal.manager.Settings;
|
||||
import org.asamk.signal.manager.api.ServiceEnvironment;
|
||||
import org.asamk.signal.manager.util.KeyUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.signal.core.models.AccountEntropyPool;
|
||||
import org.signal.core.models.ServiceId.ACI;
|
||||
import org.whispersystems.signalservice.api.push.ServiceIdType;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class NumberlessAccountTest {
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@Test
|
||||
void numberlessLinkedAccountSurvivesReload() throws Exception {
|
||||
final var aci = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
final var identity = KeyUtils.generateIdentityKeyPair();
|
||||
final var salt = new byte[32];
|
||||
salt[0] = 42;
|
||||
try (final var account = SignalAccount.createLinkedAccount(directory.toFile(),
|
||||
"account",
|
||||
ServiceEnvironment.STAGING,
|
||||
Settings.DEFAULT)) {
|
||||
account.setProvisioningData(null,
|
||||
aci,
|
||||
null,
|
||||
"test-password",
|
||||
new byte[]{1},
|
||||
identity,
|
||||
null,
|
||||
KeyUtils.createProfileKey(),
|
||||
null,
|
||||
salt,
|
||||
null);
|
||||
account.finishLinking(2, KeyUtils.generatePreKeysForType(account.getAccountData(ServiceIdType.ACI)), null);
|
||||
}
|
||||
|
||||
try (final var account = SignalAccount.load(directory.toFile(), "account", true, Settings.DEFAULT)) {
|
||||
assertTrue(account.isRegistered());
|
||||
assertFalse(account.isPrimaryDevice());
|
||||
assertEquals(2, account.getDeviceId());
|
||||
assertEquals(aci, account.getAci());
|
||||
assertNull(account.getNumber());
|
||||
assertNull(account.getPni());
|
||||
assertNull(account.getPniIdentityKeyPair());
|
||||
assertNull(account.getSignalServiceDataStore().pniOrNull());
|
||||
assertArrayEquals(identity.serialize(), account.getAciIdentityKeyPair().serialize());
|
||||
assertArrayEquals(salt, account.getAuthCredentialSalt());
|
||||
assertNull(account.getAccountAttributesV2().getPniRegistrationId());
|
||||
assertNull(account.getAccountAttributesV2().getDiscoverableByPhoneNumber());
|
||||
assertTrue(account.getAccountAttributesV2().getCapabilities().getOptionalPhoneNumber());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void recoveredNumberlessPrimaryPreservesRecoveryMaterialAndDropsPniIdentity() throws Exception {
|
||||
final var aci = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
final var entropyPool = AccountEntropyPool.Companion.generate();
|
||||
final var salt = new byte[32];
|
||||
salt[0] = 42;
|
||||
try (final var account = SignalAccount.create(directory.toFile(),
|
||||
"account",
|
||||
null,
|
||||
aci,
|
||||
ServiceEnvironment.STAGING,
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.createProfileKey(),
|
||||
Settings.DEFAULT)) {
|
||||
final var aciPreKeys = KeyUtils.generatePreKeysForType(account.getAccountData(ServiceIdType.ACI));
|
||||
final var pniPreKeys = KeyUtils.generatePreKeysForType(account.getAccountData(ServiceIdType.PNI));
|
||||
account.finishRecoveryRegistration(aci, null, null, entropyPool, salt, aciPreKeys, pniPreKeys);
|
||||
}
|
||||
|
||||
try (final var account = SignalAccount.load(directory.toFile(), "account", true, Settings.DEFAULT)) {
|
||||
assertTrue(account.isRegistered());
|
||||
assertTrue(account.isPrimaryDevice());
|
||||
assertEquals(aci, account.getAci());
|
||||
assertNull(account.getNumber());
|
||||
assertNull(account.getPni());
|
||||
assertNull(account.getPniIdentityKeyPair());
|
||||
assertEquals(entropyPool.getValue(), account.getAccountEntropyPool().getValue());
|
||||
assertArrayEquals(salt, account.getAuthCredentialSalt());
|
||||
assertTrue(account.getAccountAttributesV2().getCapabilities().getOptionalPhoneNumber());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void recoveryAttributesOmitPhoneNumberDiscoverabilityForNumberedAccount() throws Exception {
|
||||
final var aci = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
try (final var account = SignalAccount.create(directory.toFile(),
|
||||
"account",
|
||||
"+12025550123",
|
||||
aci,
|
||||
ServiceEnvironment.STAGING,
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.generateIdentityKeyPair(),
|
||||
KeyUtils.createProfileKey(),
|
||||
Settings.DEFAULT)) {
|
||||
assertNull(account.getAccountAttributesV2ForRecovery(null, "recovery-password")
|
||||
.getDiscoverableByPhoneNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,81 @@
|
||||
package org.asamk.signal.manager.storage.accounts;
|
||||
|
||||
import org.asamk.signal.manager.api.ServiceEnvironment;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.signal.core.models.ServiceId.ACI;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AccountsStoreTest {
|
||||
|
||||
private static final ACI OLD_ACI = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
private static final ACI NEW_ACI = ACI.parseOrThrow("22222222-2222-4222-8222-222222222222");
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
@Test
|
||||
void discoversNumberlessAccountsByAciAndKeepsEnvironmentsSeparate() throws Exception {
|
||||
final var store = new AccountsStore(directory.toFile(), ServiceEnvironment.STAGING, path -> null);
|
||||
final var aci = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
final var path = store.addAccount(null, aci);
|
||||
store.addAccount("+12025550123", null);
|
||||
|
||||
final var reopened = new AccountsStore(directory.toFile(), ServiceEnvironment.STAGING, ignored -> null);
|
||||
assertEquals(2, reopened.getAllAccounts().size());
|
||||
assertEquals(path, reopened.getPathByAci(aci));
|
||||
assertNull(reopened.getPathByNumber(null));
|
||||
assertEquals(Set.of("+12025550123"), reopened.getAllNumbers());
|
||||
assertTrue(new AccountsStore(directory.toFile(), ServiceEnvironment.LIVE, ignored -> null).getAllAccounts()
|
||||
.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updatingAnAciDoesNotLeaveDuplicateNumberlessAccounts() throws Exception {
|
||||
final var store = new AccountsStore(directory.toFile(), ServiceEnvironment.STAGING, path -> null);
|
||||
final var aci = ACI.parseOrThrow("11111111-1111-4111-8111-111111111111");
|
||||
final var oldPath = store.addAccount(null, aci);
|
||||
Files.createFile(directory.resolve(oldPath));
|
||||
final var newPath = store.addAccount(null, null);
|
||||
store.updateAccount(newPath, null, aci);
|
||||
|
||||
assertEquals(newPath, store.getPathByAci(aci));
|
||||
assertEquals(1, store.getAllAccounts().size());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void replacingANumberKeepsTheOldAccountAvailableByAci(final boolean update) throws Exception {
|
||||
final var store = new AccountsStore(directory.toFile(), ServiceEnvironment.STAGING, path -> null);
|
||||
final var oldPath = store.addAccount("+12025550123", OLD_ACI);
|
||||
Files.createFile(directory.resolve(oldPath));
|
||||
final String newPath;
|
||||
if (update) {
|
||||
newPath = store.addAccount("+12025550124", NEW_ACI);
|
||||
store.updateAccount(newPath, "+12025550123", NEW_ACI);
|
||||
} else {
|
||||
newPath = store.addAccount("+12025550123", NEW_ACI);
|
||||
}
|
||||
|
||||
final var reopened = new AccountsStore(directory.toFile(), ServiceEnvironment.STAGING, path -> null);
|
||||
assertEquals(Set.of(oldPath, newPath), getAccountPaths(reopened));
|
||||
assertEquals(newPath, reopened.getPathByNumber("+12025550123"));
|
||||
assertEquals(oldPath, reopened.getPathByAci(OLD_ACI));
|
||||
}
|
||||
|
||||
private Set<String> getAccountPaths(final AccountsStore store) throws IOException {
|
||||
return store.getAllAccounts().stream().map(AccountsStorage.Account::path).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
}
|
||||
@ -50,7 +50,11 @@ class AttachmentUtilsTest {
|
||||
final var blurHash = "LEHV6nWB2yk8pyo0adR*.7kCMdnj";
|
||||
final var details = new StreamDetails(new ByteArrayInputStream(bytes), "video/mp4", bytes.length);
|
||||
final var attachment = AttachmentUtils.createAttachmentStream(details,
|
||||
Optional.of("clip.mp4"), false, new AttachmentDimensions(1080, 1920), blurHash, null);
|
||||
Optional.of("clip.mp4"),
|
||||
false,
|
||||
new AttachmentDimensions(1080, 1920),
|
||||
blurHash,
|
||||
null);
|
||||
|
||||
assertEquals(1080, attachment.getWidth());
|
||||
assertEquals(1920, attachment.getHeight());
|
||||
@ -64,7 +68,11 @@ class AttachmentUtilsTest {
|
||||
final var stream = new ByteArrayInputStream(imageBytes);
|
||||
final var details = new StreamDetails(stream, "image/png", imageBytes.length);
|
||||
final var attachment = AttachmentUtils.createAttachmentStream(details,
|
||||
Optional.of("meme.png"), false, new AttachmentDimensions(100, 200), null, null);
|
||||
Optional.of("meme.png"),
|
||||
false,
|
||||
new AttachmentDimensions(100, 200),
|
||||
null,
|
||||
null);
|
||||
|
||||
assertEquals(imageBytes.length, stream.available());
|
||||
assertEquals(100, attachment.getWidth());
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
package org.asamk.signal.manager.util;
|
||||
|
||||
import org.asamk.signal.manager.api.InvalidNumberException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class PhoneNumberFormatterTest {
|
||||
|
||||
@Test
|
||||
void numberlessAccountsCanAddressInternationalPhoneNumbers() throws Exception {
|
||||
assertEquals("+12025550123", PhoneNumberFormatter.formatNumber("+1 (202) 555-0123", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void numberlessAccountsCannotInferACountryCode() {
|
||||
assertThrows(InvalidNumberException.class, () -> PhoneNumberFormatter.formatNumber("2025550123", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void numberedAccountsStillInferTheirCountryCode() throws Exception {
|
||||
assertEquals("+12025550123", PhoneNumberFormatter.formatNumber("2025550123", "+12025550124"));
|
||||
}
|
||||
}
|
||||
@ -1 +1 @@
|
||||
0.102.1
|
||||
0.105.0
|
||||
|
||||
@ -15,9 +15,13 @@ signal-cli-dbus - A commandline and dbus interface for the Signal messenger
|
||||
|
||||
*signal-cli* [--verbose] [--data-dir DATA_DIR] [-a ACCOUNT] [-o {plain-text,json}] daemon [--dbus] [--dbus-system]
|
||||
|
||||
*dbus-send* [--system | --session] [--print-reply] --type=method_call --dest="org.asamk.Signal" /org/asamk/Signal[/_<phonenumber>] org.asamk.Signal.<method> [string:<string argument>] [array:<type>:<array argument>]
|
||||
*dbus-send* [--system | --session] [--print-reply] --type=method_call --dest="org.asamk.Signal" /org/asamk/Signal[/<account-id>] org.asamk.Signal.<method> [string:<string argument>] [array:<type>:<array argument>]
|
||||
|
||||
Note: when daemon was started without explicit `-a ACCOUNT`, the `dbus-send` command requires adding the phone number in `/org/asamk/Signal/_<phonenumber>`.
|
||||
Note: when daemon was started without explicit `-a ACCOUNT`, use the account's
|
||||
object path returned by `org.asamk.SignalControl.listAccounts` or
|
||||
`org.asamk.SignalControl.getAccount`. Numbered accounts use
|
||||
`/org/asamk/Signal/_<phonenumber>`. Accounts without a phone number use
|
||||
`/org/asamk/Signal/<ACI>`, with hyphens replaced by underscores.
|
||||
|
||||
== Description
|
||||
|
||||
@ -46,9 +50,16 @@ The final parenthetical value (such as "boolean:") is the type indicator used by
|
||||
Exceptions are the names of the Java Exceptions returned in the body field.
|
||||
They typically contain an additional message with details.
|
||||
All Exceptions begin with "org.asamk.Signal.Error." which is omitted here for better readability.
|
||||
When sending a message, AttachmentInvalid means an attachment could not be read or uploaded, and the message
|
||||
was not sent to anyone; Failure may mean that it reached some recipients.
|
||||
|
||||
Phone numbers always have the format +<countrycode><regional number>
|
||||
|
||||
For an account without a phone number, use its ACI (Account ID) with the
|
||||
`signal-cli --dbus -a ACCOUNT` client. ACI selectors also work for numbered accounts.
|
||||
An account without a phone number cannot infer the country code of a recipient;
|
||||
use a complete international phone number, an ACI, or a Signal username.
|
||||
|
||||
== Methods
|
||||
|
||||
=== SignalControl interface
|
||||
@ -73,11 +84,29 @@ For example:
|
||||
dbus-send --session --dest=org.asamk.Signal --type=method_call --print-reply /org/asamk/Signal org.asamk.Signal.link string:"My secondary client" | tr '\n' '\0' | sed 's/.*string //g' | sed 's/\"//g' | qrencode -s10 -tANSI256
|
||||
----
|
||||
|
||||
listAccounts() -> accountList<as>::
|
||||
listAccounts() -> accountList<ao>::
|
||||
* accountList : Array of all attached accounts in DBus object path form
|
||||
|
||||
Exceptions: None
|
||||
|
||||
getAccount(identifier<s>) -> account<o>::
|
||||
* identifier : Phone number or ACI of an attached account
|
||||
* account : DBus object path of the account
|
||||
|
||||
Exceptions: org.asamk.SignalControl.Error.Failure if the account is unknown
|
||||
|
||||
startLink() -> deviceLinkUri<s>::
|
||||
* deviceLinkUri : Provisioning URI to scan with the primary Signal device
|
||||
|
||||
finishLink(deviceLinkUri<s>, newDeviceName<s>) -> identifier<s>::
|
||||
* deviceLinkUri : URI returned by startLink
|
||||
* newDeviceName : Name for the linked device
|
||||
* identifier : Phone number of the linked account, or its ACI when it has no phone number
|
||||
|
||||
Both linking methods support accounts with or without a phone number.
|
||||
|
||||
Exceptions: org.asamk.SignalControl.Error.Failure
|
||||
|
||||
register(number<s>, voiceVerification<b>) -> <>::
|
||||
* number : Phone number
|
||||
* voiceVerification : true = use voice verification; false = use SMS verification
|
||||
@ -115,9 +144,10 @@ Exceptions: None
|
||||
|
||||
The following methods listen to the account's object path, which is constructed as follows:
|
||||
|
||||
- for single-account mode: "/org/asamk/Signal/"
|
||||
- for multi-account mode: "/org/asamk/Signal/" + DBusNumber
|
||||
* DBusNumber: account's phone number, with underscore (_) replacing plus (+)
|
||||
- for single-account mode: "/org/asamk/Signal"
|
||||
- for multi-account mode: "/org/asamk/Signal/" + DBusIdentifier
|
||||
* DBusIdentifier: account's phone number with underscore (_) replacing plus (+),
|
||||
or its ACI with underscores replacing hyphens when there is no phone number
|
||||
|
||||
getContactName(number<s>) -> name<s>::
|
||||
* number : Phone number
|
||||
@ -135,7 +165,12 @@ May result in e.g. two entries if a contact and profile name is set.
|
||||
Exceptions: None
|
||||
|
||||
getSelfNumber() -> number<s>::
|
||||
* number : Your phone number
|
||||
* number : Your phone number, or an empty string for an account without a phone number
|
||||
|
||||
Exceptions: None
|
||||
|
||||
getSelfACI() -> aci<s>::
|
||||
* aci : Your account's ACI (Account ID), available with or without a phone number
|
||||
|
||||
Exceptions: None
|
||||
|
||||
@ -167,6 +202,14 @@ This is a concatenated list of all defined contacts as well of profiles known (e
|
||||
|
||||
Exceptions: None
|
||||
|
||||
listRecipientIdentifiers() -> identifiers<as>::
|
||||
* identifiers : String array of known recipient identifiers, including recipients without a phone number
|
||||
|
||||
Uses phone numbers where known and includes ACIs for recipients without a phone
|
||||
number. Unlike `listNumbers`, this method also includes numberless recipients.
|
||||
|
||||
Exceptions: None
|
||||
|
||||
removePin() -> <>::
|
||||
|
||||
Removes registration PIN protection.
|
||||
|
||||
@ -38,6 +38,11 @@ With `--http` signal-cli exposes three endpoints;
|
||||
* GET /api/v1/events : Returns a Server-Sent Events (SSE) stream of incoming messages
|
||||
* GET /api/v1/check : Responds with 200 OK if daemon is running
|
||||
|
||||
Each event of `/api/v1/events` has an `id`.
|
||||
signal-cli keeps the last 1000 events in memory.
|
||||
A client that reconnects with a `Last-Event-ID` header first receives the events after that id.
|
||||
If the id is unknown, e.g. because signal-cli was restarted, all kept events are sent.
|
||||
|
||||
== Basic usage
|
||||
|
||||
In JSON-RPC mode, signal-cli will read requests from stdin.
|
||||
@ -76,6 +81,9 @@ Messages are then sent similar to the automatic mode, but wrapped in a subscript
|
||||
When the daemon/jsonRpc command is started without an account parameter (-a), signal-cli will provide all local accounts and additional commands to register (`register`) and link (`startLink`, `finishLink`) new accounts.
|
||||
|
||||
In multi-account mode, requests for a single account require an additional `account` param.
|
||||
For an account without a phone number, this parameter must be its ACI (Account ID).
|
||||
`listAccounts` returns the ACI in `aci` and a null `number` for these accounts.
|
||||
Receive events identify them by ACI in the `account` field.
|
||||
|
||||
REQUEST: `{"jsonrpc":"2.0","method":"listGroups","id":"my special mark","params":{"account":"+33123456789"}}`
|
||||
|
||||
@ -132,7 +140,10 @@ Params:
|
||||
|
||||
REQUEST: `{"jsonrpc":"2.0","method":"finishLink","id":"6","params":{"deviceLinkUri":"sgnl://linkdevice?uuid=X&pub_key=X","deviceName":"new-name"}}`
|
||||
|
||||
RESPONSE: `{"jsonrpc":"2.0","result":{"deviceLinkUri":"sgnl://linkdevice?uuid=X&pub_key=X"},"id":"6"}`
|
||||
RESPONSE: `{"jsonrpc":"2.0","result":{"number":null,"aci":"11111111-1111-4111-8111-111111111111"},"id":"6"}`
|
||||
|
||||
The `number` field contains the phone number for a numbered account. Linking a numberless
|
||||
account uses the same QR flow; no Account Key is supplied to `finishLink`.
|
||||
|
||||
== Examples
|
||||
|
||||
|
||||
@ -19,7 +19,8 @@ signal-cli - A commandline interface for the Signal messenger
|
||||
|
||||
signal-cli is a commandline interface for libsignal-service-java.
|
||||
It supports registering, verifying, sending and receiving messages.
|
||||
For registering you need a phone number where you can receive SMS or incoming calls.
|
||||
Accounts can be registered using a phone number that can receive SMS or incoming calls.
|
||||
Existing accounts can also be recovered using the Account Key and Recovery Key shown by Signal Android.
|
||||
signal-cli was primarily developed to be used on servers to notify admins of important events.
|
||||
For this use-case, it has a dbus and a JSON-RPC interface, that can be used to send messages from other programs.
|
||||
|
||||
@ -65,6 +66,8 @@ Make sure you have full read/write access to the given directory.
|
||||
*-a* ACCOUNT, *--account* ACCOUNT::
|
||||
Specify your phone number, that will be your identifier.
|
||||
The phone number must include the country calling code, i.e. the number must start with a "+" sign.
|
||||
An ACI (the Account Key shown by Signal Android) can also select an account and is required for an account without a phone number.
|
||||
The Account Key can be given as 32 hexadecimal characters or as a UUID with dashes.
|
||||
|
||||
This flag must not be given for the `link` command.
|
||||
It is optional for the `daemon` command.
|
||||
@ -102,12 +105,13 @@ Disable message send log (for resending messages that recipient couldn't decrypt
|
||||
|
||||
=== register
|
||||
|
||||
Register a phone number with SMS or voice verification.
|
||||
Use the verify command to complete the verification.
|
||||
Register a phone number with SMS or voice verification, or recover an existing account using its Account Key and Recovery Key.
|
||||
Use the verify command to complete SMS or voice verification.
|
||||
|
||||
If the account is just deactivated, the register command will just reactivate account, without requiring an SMS verification.
|
||||
By default the unregister command just deactivates the account, in which case it can be reactivated without sms verification if the local data is still available.
|
||||
If the account was deleted (with --delete-account) it cannot be reactivated.
|
||||
If normal recovery of an existing local account fails and its Account Key and Recovery Key are available locally, recovery using those credentials is attempted before SMS verification.
|
||||
|
||||
*-v*, *--voice*::
|
||||
The verification should be done over voice, not SMS.
|
||||
@ -120,11 +124,23 @@ For the staging environment, use: https://signalcaptchas.org/staging/registratio
|
||||
After solving the captcha, right-click on the "Open Signal" link and copy the link.
|
||||
|
||||
*--reregister*::
|
||||
Register even if account is already registered.
|
||||
Register even if the local account is already registered.
|
||||
This option is required when using `--recovery-key` with an account that is still marked as registered locally.
|
||||
|
||||
*--recovery-key* RECOVERY-KEY::
|
||||
Recover an existing account using the 64-character Recovery Key shown by Signal Android.
|
||||
Specify the corresponding ACI (Account Key) with `-a`.
|
||||
This option cannot be combined with `--voice` or `--captcha`.
|
||||
|
||||
*--totp* TOKEN::
|
||||
The six-digit TOTP token requested during Account Key and Recovery Key based account recovery.
|
||||
If automatic account recovery reports that a TOTP token is required, rerun `register` with both `--recovery-key` and `--totp`.
|
||||
This option requires `--recovery-key`.
|
||||
This option cannot be combined with `--voice` or `--captcha`.
|
||||
|
||||
=== verify
|
||||
|
||||
Verify the number using the code received via SMS or voice.
|
||||
Verify a number using the code received via SMS or voice.
|
||||
|
||||
VERIFICATIONCODE::
|
||||
The verification code.
|
||||
@ -287,7 +303,12 @@ Remove the registration lock pin.
|
||||
Link to an existing device, instead of registering a new number.
|
||||
This shows a "sgnl://linkdevice?uuid=..." URI.
|
||||
If you want to connect to another signal-cli instance, you can just use this URI.
|
||||
If you want to link to an Android/iOS device, create a QR code with the URI (e.g. with qrencode) and scan that in the Signal app.
|
||||
If you want to link to an Android/iOS device, scan the QR code that signal-cli prints in the terminal with the Signal app.
|
||||
If signal-cli is not running in a terminal, only the URI is printed and you can create a QR code from it yourself (e.g. with qrencode).
|
||||
|
||||
Accounts without a phone number can also be linked.
|
||||
After linking, use the account's ACI (Account Key) with `-a`; recipient phone numbers must include the international country code.
|
||||
Existing primary accounts without a phone number can be recovered using the `register --recovery-key` command.
|
||||
|
||||
*-n* NAME, *--name* NAME::
|
||||
Optionally specify a name to describe this new device.
|
||||
@ -1124,6 +1145,10 @@ Expose a JSON-RPC interface as http endpoint (default localhost:8080).
|
||||
The JSON-RPC endpoint is `/api/v1/rpc`. +
|
||||
See **signal-cli-jsonrpc**(5) for info on the JSON-RPC interface.
|
||||
|
||||
*--sse-keepalive-interval* SECONDS::
|
||||
Seconds between keep-alive messages on the http events endpoint `/api/v1/events` (default 15).
|
||||
A shorter interval lets signal-cli notice sooner that a client has disconnected.
|
||||
|
||||
*--dbus*::
|
||||
Export DBus interface on user bus. +
|
||||
See **signal-cli-dbus**(5) for info on the dbus interface.
|
||||
|
||||
@ -20,6 +20,9 @@ import java.util.Map;
|
||||
*/
|
||||
public interface Signal extends DBusInterface {
|
||||
|
||||
/**
|
||||
* Returns the account's phone number, or an empty string for a numberless account.
|
||||
*/
|
||||
String getSelfNumber();
|
||||
|
||||
String getSelfACI();
|
||||
@ -186,6 +189,8 @@ public interface Signal extends DBusInterface {
|
||||
|
||||
List<String> listNumbers();
|
||||
|
||||
List<String> listRecipientIdentifiers();
|
||||
|
||||
List<String> getContactNumber(final String name) throws Error.Failure;
|
||||
|
||||
@Deprecated
|
||||
|
||||
@ -37,7 +37,10 @@ public interface SignalControl extends DBusInterface {
|
||||
|
||||
List<DBusPath> listAccounts();
|
||||
|
||||
DBusPath getAccount(String number);
|
||||
/**
|
||||
* Resolves a phone number or ACI to an exported account's object path.
|
||||
*/
|
||||
DBusPath getAccount(String identifier) throws Error.Failure;
|
||||
|
||||
interface Error {
|
||||
|
||||
|
||||
32
src/main/java/org/asamk/signal/AccountIdentifier.java
Normal file
32
src/main/java/org/asamk/signal/AccountIdentifier.java
Normal file
@ -0,0 +1,32 @@
|
||||
package org.asamk.signal;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class AccountIdentifier {
|
||||
|
||||
private AccountIdentifier() {
|
||||
}
|
||||
|
||||
public static String normalize(final String identifier) {
|
||||
if (identifier == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final var compact = identifier.replace("-", "").replaceAll("\\s", "").toLowerCase(Locale.ROOT);
|
||||
if (compact.length() != 32 || !compact.matches("[0-9a-f]{32}")) {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
final var uuid = "%s-%s-%s-%s-%s".formatted(compact.substring(0, 8),
|
||||
compact.substring(8, 12),
|
||||
compact.substring(12, 16),
|
||||
compact.substring(16, 20),
|
||||
compact.substring(20));
|
||||
try {
|
||||
return UUID.fromString(uuid).toString();
|
||||
} catch (IllegalArgumentException e) {
|
||||
return identifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -142,7 +142,7 @@ public class App {
|
||||
final var outputWriter = getOutputWriter(command);
|
||||
final var commandHandler = new CommandHandler(ns, outputWriter);
|
||||
|
||||
var account = ns.getString("account");
|
||||
var account = AccountIdentifier.normalize(ns.getString("account"));
|
||||
|
||||
final var useDbus = Boolean.TRUE.equals(ns.getBoolean("global-dbus"));
|
||||
final var useDbusSystem = Boolean.TRUE.equals(ns.getBoolean("global-dbus-system"));
|
||||
@ -187,8 +187,8 @@ public class App {
|
||||
}
|
||||
|
||||
if (command instanceof RegistrationCommand registrationCommand) {
|
||||
if (!Manager.isValidNumber(account, null)) {
|
||||
throw new UserErrorException("Invalid account (phone number), make sure you include the country code.");
|
||||
if (!Manager.isValidNumber(account, null) && !Manager.isValidAci(account)) {
|
||||
throw new UserErrorException("Invalid account (E164 phone number or Account Key).");
|
||||
}
|
||||
handleRegistrationCommand(registrationCommand, account, signalAccountFiles, commandHandler);
|
||||
return;
|
||||
@ -208,14 +208,15 @@ public class App {
|
||||
private static String getAccountIfOnlyOne(final SignalAccountFiles signalAccountFiles) throws IOErrorException, UserErrorException {
|
||||
Set<String> accounts;
|
||||
try {
|
||||
accounts = signalAccountFiles.getAllLocalAccountNumbers();
|
||||
accounts = signalAccountFiles.getAllLocalAccountIdentifiers();
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to load local accounts file", e);
|
||||
}
|
||||
if (accounts.isEmpty()) {
|
||||
throw new UserErrorException("No local users found, you first need to register or link an account");
|
||||
} else if (accounts.size() > 1) {
|
||||
throw new UserErrorException("Multiple users found, you need to specify an account (phone number) with -a");
|
||||
throw new UserErrorException(
|
||||
"Multiple users found, you need to specify an account (phone number or ACI) with -a");
|
||||
}
|
||||
return accounts.stream().findFirst().get();
|
||||
}
|
||||
@ -223,9 +224,9 @@ public class App {
|
||||
private OutputWriter getOutputWriter(final Command command) throws UserErrorException {
|
||||
final var outputTypeInput = ns.<OutputType>get("output");
|
||||
final var outputType = outputTypeInput == null ? command.getSupportedOutputTypes()
|
||||
.stream()
|
||||
.findFirst()
|
||||
.orElse(null) : outputTypeInput;
|
||||
.stream()
|
||||
.findFirst()
|
||||
.orElse(null) : outputTypeInput;
|
||||
final var writer = new BufferedWriter(new OutputStreamWriter(System.out, IOUtils.getConsoleCharset()));
|
||||
final var outputWriter = outputType == null
|
||||
? null
|
||||
|
||||
@ -8,7 +8,7 @@ public class BaseConfig {
|
||||
public static final String PROJECT_VERSION = BaseConfig.class.getPackage().getImplementationVersion();
|
||||
|
||||
static final String USER_AGENT_SIGNAL_ANDROID = Optional.ofNullable(System.getenv("SIGNAL_CLI_USER_AGENT"))
|
||||
.orElse("Signal-Android/8.27.0");
|
||||
.orElse("Signal-Android/8.31.1");
|
||||
static final String USER_AGENT_SIGNAL_CLI = PROJECT_NAME == null
|
||||
? "signal-cli"
|
||||
: PROJECT_NAME + "/" + PROJECT_VERSION;
|
||||
|
||||
@ -39,7 +39,7 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
|
||||
writer.println("Envelope from: {} (device: {}) to {}",
|
||||
source.map(this::formatContact).orElse("unknown source"),
|
||||
envelope.sourceDevice(),
|
||||
m.getSelfNumber());
|
||||
m.getSelfIdentifier());
|
||||
writer.println("Timestamp: {}", DateUtils.formatTimestamp(envelope.timestamp()));
|
||||
writer.println("Server timestamps: received: {} delivered: {}",
|
||||
DateUtils.formatTimestamp(envelope.serverReceivedTimestamp()),
|
||||
@ -55,13 +55,13 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
|
||||
final var recipientName = e.getSender().getLegacyIdentifier();
|
||||
writer.println(
|
||||
"Use 'signal-cli -a {} listIdentities -n {}', verify the key and run 'signal-cli -a {} trust -v \"FINGER_PRINT\" {}' to mark it as trusted",
|
||||
m.getSelfNumber(),
|
||||
m.getSelfIdentifier(),
|
||||
recipientName,
|
||||
m.getSelfNumber(),
|
||||
m.getSelfIdentifier(),
|
||||
recipientName);
|
||||
writer.println(
|
||||
"If you don't care about security, use 'signal-cli -a {} trust -a {}' to trust it without verification",
|
||||
m.getSelfNumber(),
|
||||
m.getSelfIdentifier(),
|
||||
recipientName);
|
||||
} else {
|
||||
writer.println("Exception: {} ({})", exception.getMessage(), exception.getClass().getSimpleName());
|
||||
@ -614,8 +614,8 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
|
||||
writer.println("Size: {}{}",
|
||||
attachment.size().isPresent() ? attachment.size().get() + " bytes" : "<unavailable>",
|
||||
attachment.preview().isPresent() ? " (Preview is available: "
|
||||
+ attachment.preview().get().length
|
||||
+ " bytes)" : "");
|
||||
+ attachment.preview().get().length
|
||||
+ " bytes)" : "");
|
||||
}
|
||||
if (attachment.thumbnail().isPresent()) {
|
||||
writer.println("Thumbnail:");
|
||||
|
||||
@ -30,6 +30,7 @@ import java.net.InetSocketAddress;
|
||||
import java.net.UnixDomainSocketAddress;
|
||||
import java.nio.channels.Channel;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.SequencedCollection;
|
||||
@ -69,6 +70,10 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||
.nargs("?")
|
||||
.setConst("localhost:8080")
|
||||
.help("Expose a JSON-RPC interface as http endpoint (default localhost:8080).");
|
||||
subparser.addArgument("--sse-keepalive-interval")
|
||||
.type(int.class)
|
||||
.setDefault(15)
|
||||
.help("Seconds between keep-alive messages on the http events endpoint (default 15).");
|
||||
subparser.addArgument("--no-receive-stdout")
|
||||
.help("Don’t print received messages to stdout.")
|
||||
.action(Arguments.storeTrue());
|
||||
@ -188,7 +193,11 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||
final var httpAddress = ns.getString("http");
|
||||
if (httpAddress != null) {
|
||||
final var address = IOUtils.parseInetSocketAddress(httpAddress);
|
||||
daemonHandler.runHttp(address);
|
||||
final var keepAliveInterval = ns.getInt("sse-keepalive-interval");
|
||||
if (keepAliveInterval < 1) {
|
||||
throw new UserErrorException("--sse-keepalive-interval must be at least 1 second.");
|
||||
}
|
||||
daemonHandler.runHttp(address, Duration.ofSeconds(keepAliveInterval));
|
||||
}
|
||||
|
||||
final var isDbusSystem = Boolean.TRUE.equals(ns.getBoolean("dbus-system"));
|
||||
@ -235,7 +244,7 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||
|
||||
public abstract void runDbus(boolean isDbusSystem, final String busname) throws CommandException;
|
||||
|
||||
public abstract void runHttp(InetSocketAddress address) throws CommandException;
|
||||
public abstract void runHttp(InetSocketAddress address, Duration keepAliveInterval) throws CommandException;
|
||||
|
||||
protected final void runSocket(final SocketHandler socketHandler) {
|
||||
socketHandler.init();
|
||||
@ -291,8 +300,8 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runHttp(InetSocketAddress address) throws CommandException {
|
||||
runHttp(new HttpServerHandler(address, m));
|
||||
public void runHttp(final InetSocketAddress address, final Duration keepAliveInterval) throws CommandException {
|
||||
runHttp(new HttpServerHandler(address, keepAliveInterval, m));
|
||||
}
|
||||
}
|
||||
|
||||
@ -316,8 +325,8 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runHttp(final InetSocketAddress address) throws CommandException {
|
||||
runHttp(new HttpServerHandler(address, c));
|
||||
public void runHttp(final InetSocketAddress address, final Duration keepAliveInterval) throws CommandException {
|
||||
runHttp(new HttpServerHandler(address, keepAliveInterval, c));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,9 +54,9 @@ public class FinishLinkCommand implements JsonRpcMultiCommand<FinishLinkCommand.
|
||||
if (deviceName == null) {
|
||||
deviceName = "cli";
|
||||
}
|
||||
final String number;
|
||||
final String identifier;
|
||||
try {
|
||||
number = provisioningManager.finishDeviceLink(deviceName);
|
||||
identifier = provisioningManager.finishDeviceLink(deviceName);
|
||||
} catch (TimeoutException e) {
|
||||
throw new UserErrorException("Link request timed out, please try again.");
|
||||
} catch (IOException e) {
|
||||
@ -73,10 +73,11 @@ public class FinishLinkCommand implements JsonRpcMultiCommand<FinishLinkCommand.
|
||||
+ "\" before trying again.");
|
||||
}
|
||||
|
||||
jsonWriter.write(new JsonFinishLink(number));
|
||||
final var manager = m.getManager(identifier);
|
||||
jsonWriter.write(new JsonFinishLink(manager.getSelfNumber(), manager.getSelfACI()));
|
||||
}
|
||||
|
||||
public record FinishLinkParams(String deviceLinkUri, String deviceName) {}
|
||||
|
||||
private record JsonFinishLink(String number) {}
|
||||
private record JsonFinishLink(String number, String aci) {}
|
||||
}
|
||||
|
||||
@ -27,19 +27,22 @@ public class ListAccountsCommand implements JsonRpcMultiLocalCommand {
|
||||
final MultiAccountManager c,
|
||||
final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var accountNumbers = c.getAccountNumbers();
|
||||
final var managers = c.getManagers();
|
||||
switch (outputWriter) {
|
||||
case JsonWriter jsonWriter -> {
|
||||
final var jsonAccounts = accountNumbers.stream().map(JsonAccount::new).toList();
|
||||
final var jsonAccounts = managers.stream()
|
||||
.map(m -> new JsonAccount(m.getSelfNumber(), m.getSelfACI()))
|
||||
.toList();
|
||||
jsonWriter.write(jsonAccounts);
|
||||
}
|
||||
case PlainTextWriter plainTextWriter -> {
|
||||
for (final var number : accountNumbers) {
|
||||
plainTextWriter.println("Number: {}", number);
|
||||
for (final var manager : managers) {
|
||||
plainTextWriter.println(manager.getSelfNumber() != null ? "Number: {}" : "ACI: {}",
|
||||
manager.getSelfIdentifier());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private record JsonAccount(String number) {}
|
||||
private record JsonAccount(String number, String aci) {}
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
|
||||
r.getProfile().getPhoneNumberSharingMode() == null
|
||||
? ""
|
||||
: String.valueOf(r.getProfile().getPhoneNumberSharingMode()
|
||||
== PhoneNumberSharingMode.EVERYBODY),
|
||||
== PhoneNumberSharingMode.EVERYBODY),
|
||||
r.getDiscoverable() == null ? "" : String.valueOf(r.getDiscoverable()));
|
||||
}
|
||||
}
|
||||
@ -121,17 +121,17 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
|
||||
final var jsonInternal = !internal
|
||||
? null
|
||||
: new JsonContact.JsonInternal(r.getProfile()
|
||||
.getCapabilities()
|
||||
.stream()
|
||||
.map(Enum::name)
|
||||
.toList(),
|
||||
.getCapabilities()
|
||||
.stream()
|
||||
.map(Enum::name)
|
||||
.toList(),
|
||||
r.getProfile().getUnidentifiedAccessMode() == Profile.UnidentifiedAccessMode.UNKNOWN
|
||||
? null
|
||||
? null
|
||||
: r.getProfile().getUnidentifiedAccessMode().name(),
|
||||
r.getProfile().getPhoneNumberSharingMode() == null
|
||||
? null
|
||||
? null
|
||||
: r.getProfile().getPhoneNumberSharingMode()
|
||||
== PhoneNumberSharingMode.EVERYBODY,
|
||||
== PhoneNumberSharingMode.EVERYBODY,
|
||||
r.getDiscoverable());
|
||||
return new JsonContact(address.number().orElse(null),
|
||||
address.uuid().map(UUID::toString).orElse(null),
|
||||
@ -159,9 +159,9 @@ public class ListContactsCommand implements JsonRpcLocalCommand {
|
||||
r.getProfile().getAboutEmoji(),
|
||||
r.getProfile().getAvatarUrlPath() != null,
|
||||
r.getProfile().getMobileCoinAddress() == null
|
||||
? null
|
||||
? null
|
||||
: Base64.getEncoder()
|
||||
.encodeToString(r.getProfile().getMobileCoinAddress())),
|
||||
.encodeToString(r.getProfile().getMobileCoinAddress())),
|
||||
jsonInternal);
|
||||
}).toList();
|
||||
writer.write(jsonContacts);
|
||||
|
||||
@ -15,6 +15,7 @@ import org.asamk.signal.manager.RegistrationManager;
|
||||
import org.asamk.signal.manager.api.CaptchaRequiredException;
|
||||
import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
|
||||
import org.asamk.signal.manager.api.RateLimitException;
|
||||
import org.asamk.signal.manager.api.TwoFactorRequired;
|
||||
import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.util.CommandUtil;
|
||||
@ -41,6 +42,8 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
|
||||
subparser.addArgument("--reregister")
|
||||
.action(Arguments.storeTrue())
|
||||
.help("Register even if account is already registered");
|
||||
subparser.addArgument("--recovery-key").help("Recover an account using its 64-character Signal Recovery Key.");
|
||||
subparser.addArgument("--totp").help("A six-digit TOTP token required for account recovery.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -48,8 +51,10 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
|
||||
final boolean voiceVerification = Boolean.TRUE.equals(ns.getBoolean("voice"));
|
||||
final var captcha = ns.getString("captcha");
|
||||
final var reregister = Boolean.TRUE.equals(ns.getBoolean("reregister"));
|
||||
final var recoveryKey = ns.getString("recovery-key");
|
||||
final var totp = ns.getString("totp");
|
||||
|
||||
register(m, voiceVerification, captcha, reregister);
|
||||
register(m, voiceVerification, captcha, reregister, recoveryKey, totp);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -68,15 +73,53 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
|
||||
final RegistrationManager m,
|
||||
final JsonWriter jsonWriter
|
||||
) throws CommandException {
|
||||
register(m, Boolean.TRUE.equals(request.voice()), request.captcha(), Boolean.TRUE.equals(request.reregister()));
|
||||
register(m,
|
||||
Boolean.TRUE.equals(request.voice()),
|
||||
request.captcha(),
|
||||
Boolean.TRUE.equals(request.reregister()),
|
||||
request.recoveryKey(),
|
||||
request.totp());
|
||||
}
|
||||
|
||||
private void register(
|
||||
final RegistrationManager m,
|
||||
final boolean voiceVerification,
|
||||
final String captcha,
|
||||
final boolean reregister
|
||||
final boolean reregister,
|
||||
final String recoveryKey,
|
||||
final String totpValue
|
||||
) throws CommandException {
|
||||
if (recoveryKey == null && totpValue != null) {
|
||||
throw new UserErrorException("--totp requires --recovery-key");
|
||||
}
|
||||
|
||||
final Integer totp;
|
||||
if (totpValue == null) {
|
||||
totp = null;
|
||||
} else if (!totpValue.matches("[0-9]{6}")) {
|
||||
throw new UserErrorException("TOTP token must contain exactly six digits");
|
||||
} else {
|
||||
totp = Integer.parseInt(totpValue);
|
||||
}
|
||||
|
||||
if (recoveryKey != null) {
|
||||
if (voiceVerification || captcha != null) {
|
||||
throw new UserErrorException("--recovery-key cannot be combined with --voice or --captcha");
|
||||
}
|
||||
try {
|
||||
m.registerWithRecoveryKey(recoveryKey, reregister, totp);
|
||||
} catch (RateLimitException e) {
|
||||
final var message = CommandUtil.getRateLimitMessage(e);
|
||||
throw new RateLimitErrorException(message, e);
|
||||
} catch (TwoFactorRequired e) {
|
||||
throw new UserErrorException("A TOTP token is required; rerun register with --totp TOKEN");
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to register: %s (%s)".formatted(e.getMessage(),
|
||||
e.getClass().getSimpleName()), e);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
m.register(voiceVerification, captcha, reregister);
|
||||
} catch (RateLimitException e) {
|
||||
@ -87,6 +130,9 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
|
||||
throw new UserErrorException(message);
|
||||
} catch (NonNormalizedPhoneNumberException e) {
|
||||
throw new UserErrorException("Failed to register: " + e.getMessage(), e);
|
||||
} catch (TwoFactorRequired e) {
|
||||
throw new UserErrorException(
|
||||
"A TOTP token is required; rerun register with --recovery-key RECOVERY-KEY --totp TOKEN");
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to register: %s (%s)".formatted(e.getMessage(),
|
||||
e.getClass().getSimpleName()), e);
|
||||
@ -99,5 +145,7 @@ public class RegisterCommand implements RegistrationCommand, JsonRpcRegistration
|
||||
}
|
||||
}
|
||||
|
||||
public record RegistrationParams(Boolean voice, String captcha, Boolean reregister) {}
|
||||
public record RegistrationParams(
|
||||
Boolean voice, String captcha, Boolean reregister, String recoveryKey, String totp
|
||||
) {}
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ public class DbusCommandHandler {
|
||||
try {
|
||||
if (command instanceof ProvisioningCommand c) {
|
||||
if (account != null) {
|
||||
throw new UserErrorException("You cannot specify a account (phone number) when linking");
|
||||
throw new UserErrorException("You cannot specify an account when linking");
|
||||
}
|
||||
|
||||
handleProvisioningCommand(c, dBusConn, busname, commandHandler);
|
||||
@ -74,7 +74,9 @@ public class DbusCommandHandler {
|
||||
throw new UserErrorException("Command only works in multi-account mode");
|
||||
}
|
||||
|
||||
var accountObjectPath = account == null ? tryGetSingleAccountObjectPath(dBusConn, busname) : null;
|
||||
var accountObjectPath = account == null
|
||||
? tryGetSingleAccountObjectPath(dBusConn, busname)
|
||||
: getAccountObjectPath(account, dBusConn, busname);
|
||||
if (accountObjectPath == null) {
|
||||
accountObjectPath = DbusConfig.getObjectPath(account);
|
||||
}
|
||||
@ -86,6 +88,24 @@ public class DbusCommandHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private static String getAccountObjectPath(
|
||||
final String account,
|
||||
final DBusConnection dBusConn,
|
||||
final String busname
|
||||
) throws DBusException, CommandException {
|
||||
final var control = dBusConn.getRemoteObject(busname, DbusConfig.getObjectPath(), SignalControl.class);
|
||||
try {
|
||||
return control.getAccount(account).getPath();
|
||||
} catch (UnknownMethod e) {
|
||||
// A single-account daemon exports Signal directly at the base path.
|
||||
final var signal = dBusConn.getRemoteObject(busname, DbusConfig.getObjectPath(), Signal.class);
|
||||
if (!account.equals(signal.getSelfNumber()) && !account.equals(signal.getSelfACI())) {
|
||||
throw new UserErrorException("Unknown account: " + account);
|
||||
}
|
||||
return DbusConfig.getObjectPath();
|
||||
}
|
||||
}
|
||||
|
||||
private static String tryGetSingleAccountObjectPath(
|
||||
final DBusConnection dBusConn,
|
||||
final String busname
|
||||
@ -97,7 +117,7 @@ public class DbusCommandHandler {
|
||||
throw new UserErrorException("No local users found, you first need to register or link an account");
|
||||
} else if (accounts.size() > 1) {
|
||||
throw new UserErrorException(
|
||||
"Multiple users found, you need to specify an account (phone number) with -a");
|
||||
"Multiple users found, you need to specify an account (phone number or ACI) with -a");
|
||||
}
|
||||
|
||||
return accounts.getFirst().getPath();
|
||||
|
||||
@ -67,7 +67,7 @@ public class DbusHandler implements AutoCloseable {
|
||||
}
|
||||
});
|
||||
c.addOnManagerRemovedHandler(m -> {
|
||||
final var path = DbusConfig.getObjectPath(m.getSelfNumber());
|
||||
final var path = DbusConfig.getObjectPath(m.getSelfIdentifier());
|
||||
try {
|
||||
final var object = connection.getExportedObject(null, path);
|
||||
if (object instanceof DbusSignalImpl dbusSignal) {
|
||||
@ -135,11 +135,11 @@ public class DbusHandler implements AutoCloseable {
|
||||
final var signal = new DbusSignalImpl(m, conn, objectPath, noReceiveOnStart);
|
||||
closeables.add(signal);
|
||||
|
||||
return Thread.ofPlatform().name("dbus-init-" + m.getSelfNumber()).start(signal::initObjects);
|
||||
return Thread.ofPlatform().name("dbus-init-" + m.getSelfIdentifier()).start(signal::initObjects);
|
||||
}
|
||||
|
||||
private Thread exportManager(final DBusConnection conn, final Manager m) {
|
||||
final var objectPath = DbusConfig.getObjectPath(m.getSelfNumber());
|
||||
final var objectPath = DbusConfig.getObjectPath(m.getSelfIdentifier());
|
||||
return exportDbusObject(conn, objectPath, m);
|
||||
}
|
||||
|
||||
|
||||
@ -79,6 +79,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Function;
|
||||
@ -112,7 +113,8 @@ public class DbusManagerImpl implements Manager {
|
||||
|
||||
@Override
|
||||
public String getSelfNumber() {
|
||||
return signal.getSelfNumber();
|
||||
final var number = signal.getSelfNumber();
|
||||
return number.isEmpty() ? null : number;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -440,7 +442,7 @@ public class DbusManagerImpl implements Manager {
|
||||
numbers.forEach(n -> signal.sendTyping(n, action == TypingAction.STOP));
|
||||
return 0L;
|
||||
}, () -> {
|
||||
signal.sendTyping(signal.getSelfNumber(), action == TypingAction.STOP);
|
||||
signal.sendTyping(getSelfIdentifier(), action == TypingAction.STOP);
|
||||
return 0L;
|
||||
}, groupId -> {
|
||||
signal.sendGroupTyping(groupId, action == TypingAction.STOP);
|
||||
@ -488,7 +490,7 @@ public class DbusManagerImpl implements Manager {
|
||||
) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
|
||||
return handleMessage(recipients,
|
||||
numbers -> signal.sendRemoteDeleteMessage(targetSentTimestamp, numbers),
|
||||
() -> signal.sendRemoteDeleteMessage(targetSentTimestamp, signal.getSelfNumber()),
|
||||
() -> signal.sendRemoteDeleteMessage(targetSentTimestamp, getSelfIdentifier()),
|
||||
groupId -> signal.sendGroupRemoteDeleteMessage(targetSentTimestamp, groupId));
|
||||
}
|
||||
|
||||
@ -512,7 +514,7 @@ public class DbusManagerImpl implements Manager {
|
||||
remove,
|
||||
targetAuthor.getIdentifier(),
|
||||
targetSentTimestamp,
|
||||
signal.getSelfNumber()),
|
||||
getSelfIdentifier()),
|
||||
groupId -> signal.sendGroupMessageReaction(emoji,
|
||||
remove,
|
||||
targetAuthor.getIdentifier(),
|
||||
@ -826,43 +828,47 @@ public class DbusManagerImpl implements Manager {
|
||||
final Collection<RecipientIdentifier.Single> addresses,
|
||||
final Optional<String> name
|
||||
) {
|
||||
final var numbers = addresses.stream()
|
||||
.filter(s -> s instanceof RecipientIdentifier.Number)
|
||||
.map(s -> ((RecipientIdentifier.Number) s).number())
|
||||
final var identifiers = addresses.stream()
|
||||
.map(RecipientIdentifier.Single::getIdentifier)
|
||||
.collect(Collectors.toSet());
|
||||
return signal.listNumbers().stream().filter(n -> addresses.isEmpty() || numbers.contains(n)).map(n -> {
|
||||
final var contactBlocked = signal.isContactBlocked(n);
|
||||
if (blocked.isPresent() && blocked.get() != contactBlocked) {
|
||||
return null;
|
||||
}
|
||||
final var contactName = signal.getContactName(n);
|
||||
if (onlyContacts && contactName.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (name.isPresent() && !name.get().equals(contactName)) {
|
||||
return null;
|
||||
}
|
||||
return Recipient.newBuilder()
|
||||
.withAddress(new RecipientAddress(n))
|
||||
.withContact(new Contact(contactName,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
false,
|
||||
contactBlocked,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
null))
|
||||
.build();
|
||||
}).filter(Objects::nonNull).toList();
|
||||
return signal.listRecipientIdentifiers()
|
||||
.stream()
|
||||
.filter(n -> addresses.isEmpty() || identifiers.contains(n))
|
||||
.map(n -> {
|
||||
final var contactBlocked = signal.isContactBlocked(n);
|
||||
if (blocked.isPresent() && blocked.get() != contactBlocked) {
|
||||
return null;
|
||||
}
|
||||
final var contactName = signal.getContactName(n);
|
||||
if (onlyContacts && contactName.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (name.isPresent() && !name.get().equals(contactName)) {
|
||||
return null;
|
||||
}
|
||||
return Recipient.newBuilder()
|
||||
.withAddress(getRecipientAddress(n))
|
||||
.withContact(new Contact(contactName,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
0,
|
||||
1,
|
||||
0,
|
||||
false,
|
||||
contactBlocked,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
null))
|
||||
.build();
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -887,16 +893,16 @@ public class DbusManagerImpl implements Manager {
|
||||
(String) group.get("Description").getValue(),
|
||||
GroupInviteLinkUrl.fromUri((String) group.get("GroupInviteLink").getValue()),
|
||||
((List<String>) group.get("Members").getValue()).stream()
|
||||
.map(m -> new GroupMember(new RecipientAddress(m), admins.contains(m), null, null))
|
||||
.map(m -> new GroupMember(getRecipientAddress(m), admins.contains(m), null, null))
|
||||
.collect(Collectors.toSet()),
|
||||
((List<String>) group.get("PendingMembers").getValue()).stream()
|
||||
.map(RecipientAddress::new)
|
||||
.map(DbusManagerImpl::getRecipientAddress)
|
||||
.collect(Collectors.toSet()),
|
||||
((List<String>) group.get("RequestingMembers").getValue()).stream()
|
||||
.map(RecipientAddress::new)
|
||||
.map(DbusManagerImpl::getRecipientAddress)
|
||||
.collect(Collectors.toSet()),
|
||||
((List<String>) group.get("Banned").getValue()).stream()
|
||||
.map(RecipientAddress::new)
|
||||
.map(DbusManagerImpl::getRecipientAddress)
|
||||
.collect(Collectors.toSet()),
|
||||
(boolean) group.get("IsBlocked").getValue(),
|
||||
(int) group.get("MessageExpirationTimer").getValue(),
|
||||
@ -927,7 +933,10 @@ public class DbusManagerImpl implements Manager {
|
||||
final var group = getRemoteObject(identityPath, Signal.Identity.class).GetAll("org.asamk.Signal.Identity");
|
||||
final var aci = (String) group.get("Uuid").getValue();
|
||||
final var number = (String) group.get("Number").getValue();
|
||||
return new Identity(new RecipientAddress(aci, null, number, null),
|
||||
return new Identity(new RecipientAddress(aci.isEmpty() ? null : aci,
|
||||
null,
|
||||
number.isEmpty() ? null : number,
|
||||
null),
|
||||
(byte[]) group.get("Fingerprint").getValue(),
|
||||
(String) group.get("SafetyNumber").getValue(),
|
||||
(byte[]) group.get("ScannableSafetyNumber").getValue(),
|
||||
@ -1090,6 +1099,14 @@ public class DbusManagerImpl implements Manager {
|
||||
return string == null ? "" : string;
|
||||
}
|
||||
|
||||
private static RecipientAddress getRecipientAddress(final String identifier) {
|
||||
try {
|
||||
return new RecipientAddress(UUID.fromString(identifier));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return new RecipientAddress(identifier);
|
||||
}
|
||||
}
|
||||
|
||||
private <T extends DBusInterface> T getRemoteObject(final DBusPath path, final Class<T> type) {
|
||||
try {
|
||||
return connection.getRemoteObject(busname, path.getPath(), type);
|
||||
@ -1102,7 +1119,7 @@ public class DbusManagerImpl implements Manager {
|
||||
try {
|
||||
this.dbusMsgHandler = messageReceived -> {
|
||||
final var extras = messageReceived.getExtras();
|
||||
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
|
||||
final var envelope = new MessageEnvelope(Optional.of(getRecipientAddress(messageReceived.getSender())),
|
||||
0,
|
||||
messageReceived.getTimestamp(),
|
||||
0,
|
||||
@ -1149,7 +1166,7 @@ public class DbusManagerImpl implements Manager {
|
||||
connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
|
||||
this.dbusEditMsgHandler = messageReceived -> {
|
||||
final var extras = messageReceived.getExtras();
|
||||
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
|
||||
final var envelope = new MessageEnvelope(Optional.of(getRecipientAddress(messageReceived.getSender())),
|
||||
0,
|
||||
messageReceived.getTimestamp(),
|
||||
0,
|
||||
@ -1203,7 +1220,7 @@ public class DbusManagerImpl implements Manager {
|
||||
case "delivery" -> MessageEnvelope.Receipt.Type.DELIVERY;
|
||||
default -> MessageEnvelope.Receipt.Type.UNKNOWN;
|
||||
};
|
||||
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(receiptReceived.getSender())),
|
||||
final var envelope = new MessageEnvelope(Optional.of(getRecipientAddress(receiptReceived.getSender())),
|
||||
0,
|
||||
receiptReceived.getTimestamp(),
|
||||
0,
|
||||
@ -1224,7 +1241,7 @@ public class DbusManagerImpl implements Manager {
|
||||
|
||||
this.dbusSyncHandler = syncReceived -> {
|
||||
final var extras = syncReceived.getExtras();
|
||||
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(syncReceived.getSource())),
|
||||
final var envelope = new MessageEnvelope(Optional.of(getRecipientAddress(syncReceived.getSource())),
|
||||
0,
|
||||
syncReceived.getTimestamp(),
|
||||
0,
|
||||
@ -1238,7 +1255,7 @@ public class DbusManagerImpl implements Manager {
|
||||
syncReceived.getTimestamp(),
|
||||
syncReceived.getDestination().isEmpty()
|
||||
? Optional.empty()
|
||||
: Optional.of(new RecipientAddress(syncReceived.getDestination())),
|
||||
: Optional.of(getRecipientAddress(syncReceived.getDestination())),
|
||||
Set.of(),
|
||||
Optional.of(new MessageEnvelope.Data(syncReceived.getTimestamp(),
|
||||
syncReceived.getGroupId().length > 0
|
||||
@ -1343,7 +1360,7 @@ public class DbusManagerImpl implements Manager {
|
||||
|
||||
final List<Map<String, Variant<?>>> mentions = getValue(extras, "mentions");
|
||||
return mentions.stream()
|
||||
.map(a -> new MessageEnvelope.Data.Mention(new RecipientAddress(this.<String>getValue(a, "recipient")),
|
||||
.map(a -> new MessageEnvelope.Data.Mention(getRecipientAddress(this.<String>getValue(a, "recipient")),
|
||||
getValue(a, "start"),
|
||||
getValue(a, "length")))
|
||||
.toList();
|
||||
|
||||
@ -43,14 +43,6 @@ public class DbusMultiAccountManagerImpl implements MultiAccountManager {
|
||||
this.busname = busname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAccountNumbers() {
|
||||
return signalControl.listAccounts()
|
||||
.stream()
|
||||
.map(a -> getRemoteObject(a, Signal.class).getSelfNumber())
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Manager> getManagers() {
|
||||
return signalControl.listAccounts()
|
||||
@ -66,6 +58,13 @@ public class DbusMultiAccountManagerImpl implements MultiAccountManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeOnManagerAddedHandler(final Consumer<Manager> handler) {
|
||||
synchronized (onManagerAddedHandlers) {
|
||||
onManagerAddedHandlers.remove(handler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOnManagerRemovedHandler(final Consumer<Manager> handler) {
|
||||
synchronized (onManagerRemovedHandlers) {
|
||||
@ -74,8 +73,15 @@ public class DbusMultiAccountManagerImpl implements MultiAccountManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Manager getManager(final String phoneNumber) {
|
||||
return new DbusManagerImpl(getRemoteObject(signalControl.getAccount(phoneNumber), Signal.class),
|
||||
public void removeOnManagerRemovedHandler(final Consumer<Manager> handler) {
|
||||
synchronized (onManagerRemovedHandlers) {
|
||||
onManagerRemovedHandlers.remove(handler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Manager getManager(final String identifier) {
|
||||
return new DbusManagerImpl(getRemoteObject(signalControl.getAccount(identifier), Signal.class),
|
||||
connection,
|
||||
busname);
|
||||
}
|
||||
|
||||
@ -165,7 +165,10 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
|
||||
}
|
||||
if (message.remoteDeleteId().isPresent()) {
|
||||
extras.put("remoteDelete",
|
||||
new Variant<>(Map.of("timestamp", new Variant<>(message.remoteDeleteId().get())), "a{sv}"));
|
||||
new Variant<>(Map.of("targetTimestamp",
|
||||
new Variant<>(message.remoteDeleteId().get()),
|
||||
"timestamp",
|
||||
new Variant<>(message.remoteDeleteId().get())), "a{sv}"));
|
||||
}
|
||||
if (message.sticker().isPresent()) {
|
||||
final var sticker = message.sticker().get();
|
||||
|
||||
@ -5,6 +5,7 @@ import org.asamk.signal.manager.RegistrationManager;
|
||||
import org.asamk.signal.manager.api.CaptchaRequiredException;
|
||||
import org.asamk.signal.manager.api.IncorrectPinException;
|
||||
import org.asamk.signal.manager.api.PinLockedException;
|
||||
import org.asamk.signal.manager.api.TwoFactorRequired;
|
||||
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
@ -53,6 +54,15 @@ public class DbusRegistrationManagerImpl implements RegistrationManager {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerWithRecoveryKey(
|
||||
final String recoveryKey,
|
||||
final boolean forceRegister,
|
||||
final Integer totp
|
||||
) throws IOException, TwoFactorRequired {
|
||||
throw new IOException("Recovery-key registration is not supported over D-Bus");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteLocalAccountData() throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
@ -159,11 +159,19 @@ public class DbusSignalControlImpl implements org.asamk.SignalControl {
|
||||
|
||||
@Override
|
||||
public List<DBusPath> listAccounts() {
|
||||
return c.getAccountNumbers().stream().map(u -> new DBusPath(DbusConfig.getObjectPath(u))).toList();
|
||||
return c.getManagers()
|
||||
.stream()
|
||||
.map(m -> new DBusPath(DbusConfig.getObjectPath(m.getSelfIdentifier())))
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DBusPath getAccount(final String number) {
|
||||
return new DBusPath(DbusConfig.getObjectPath(number));
|
||||
public DBusPath getAccount(final String identifier) {
|
||||
final var manager = c.getManagers()
|
||||
.stream()
|
||||
.filter(m -> identifier.equals(m.getSelfNumber()) || identifier.equals(m.getSelfACI()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new Error.Failure("Unknown account: " + identifier));
|
||||
return new DBusPath(DbusConfig.getObjectPath(manager.getSelfIdentifier()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,7 +138,7 @@ public class DbusSignalImpl implements Signal, AutoCloseable {
|
||||
|
||||
@Override
|
||||
public String getSelfNumber() {
|
||||
return m.getSelfNumber();
|
||||
return emptyIfNull(m.getSelfNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -715,7 +715,7 @@ public class DbusSignalImpl implements Signal, AutoCloseable {
|
||||
retryAfterMilliseconds == null
|
||||
? ""
|
||||
: ", retry at " + DateUtils.formatTimestamp(System.currentTimeMillis()
|
||||
+ retryAfterMilliseconds)
|
||||
+ retryAfterMilliseconds)
|
||||
));
|
||||
}
|
||||
|
||||
@ -803,6 +803,15 @@ public class DbusSignalImpl implements Signal, AutoCloseable {
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listRecipientIdentifiers() {
|
||||
return m.getRecipients(false, Optional.empty(), Set.of(), Optional.empty())
|
||||
.stream()
|
||||
.map(r -> r.getAddress().getLegacyIdentifier())
|
||||
.distinct()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getContactNumber(final String name) {
|
||||
return m.getRecipients(false, Optional.empty(), Set.of(), Optional.of(name))
|
||||
|
||||
@ -5,13 +5,13 @@ import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import org.asamk.signal.commands.Commands;
|
||||
import org.asamk.signal.json.JsonReceiveMessageHandler;
|
||||
import org.asamk.signal.jsonrpc.JsonRpcReader;
|
||||
import org.asamk.signal.jsonrpc.JsonRpcResponse;
|
||||
import org.asamk.signal.jsonrpc.JsonRpcSender;
|
||||
import org.asamk.signal.jsonrpc.SignalJsonRpcCommandHandler;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.MultiAccountManager;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.api.Pair;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.slf4j.Logger;
|
||||
@ -19,6 +19,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Duration;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@ -34,6 +35,7 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
private final ObjectMapper objectMapper = Util.createJsonObjectMapper();
|
||||
|
||||
private final InetSocketAddress address;
|
||||
private final Duration keepAliveInterval;
|
||||
|
||||
private final SignalJsonRpcCommandHandler commandHandler;
|
||||
private final MultiAccountManager c;
|
||||
@ -41,17 +43,24 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
private HttpServer server;
|
||||
private final AtomicBoolean shutdown = new AtomicBoolean(false);
|
||||
private final Set<String> allowedHosts;
|
||||
private final SseEventBuffer eventBuffer = new SseEventBuffer(objectMapper);
|
||||
|
||||
public HttpServerHandler(final InetSocketAddress address, final Manager m) {
|
||||
public HttpServerHandler(final InetSocketAddress address, final Duration keepAliveInterval, final Manager m) {
|
||||
this.address = address;
|
||||
this.keepAliveInterval = keepAliveInterval;
|
||||
commandHandler = new SignalJsonRpcCommandHandler(m, Commands::getCommand);
|
||||
this.c = null;
|
||||
this.m = m;
|
||||
this.allowedHosts = buildAllowedHosts(address);
|
||||
}
|
||||
|
||||
public HttpServerHandler(final InetSocketAddress address, final MultiAccountManager c) {
|
||||
public HttpServerHandler(
|
||||
final InetSocketAddress address,
|
||||
final Duration keepAliveInterval,
|
||||
final MultiAccountManager c
|
||||
) {
|
||||
this.address = address;
|
||||
this.keepAliveInterval = keepAliveInterval;
|
||||
commandHandler = new SignalJsonRpcCommandHandler(c, Commands::getCommand);
|
||||
this.c = c;
|
||||
this.m = null;
|
||||
@ -93,6 +102,7 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
// Increase this delay when https://bugs.openjdk.org/browse/JDK-8304065 is fixed
|
||||
server.stop(2);
|
||||
server = null;
|
||||
eventBuffer.close();
|
||||
shutdown.set(false);
|
||||
}
|
||||
}
|
||||
@ -199,22 +209,27 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
|
||||
// Flush HTTP response headers to the client immediately.
|
||||
// Without this, the JVM HttpServer buffers everything until a later write
|
||||
// in the keep-alive loop (15 s), causing clients with shorter timeouts
|
||||
// in the keep-alive loop, causing clients with shorter timeouts
|
||||
// (e.g. 10 s) to abort before receiving the initial response.
|
||||
httpExchange.getResponseBody().flush();
|
||||
|
||||
final var shouldStop = new AtomicBoolean(false);
|
||||
final var handlers = subscribeReceiveHandlers(managers, sender, () -> {
|
||||
shouldStop.set(true);
|
||||
synchronized (this) {
|
||||
this.notifyAll();
|
||||
}
|
||||
});
|
||||
managers.forEach(eventBuffer::record);
|
||||
final var client = eventBuffer.connect(managers,
|
||||
sender,
|
||||
httpExchange.getRequestHeaders().getFirst("Last-Event-ID"),
|
||||
() -> {
|
||||
shouldStop.set(true);
|
||||
synchronized (this) {
|
||||
this.notifyAll();
|
||||
}
|
||||
});
|
||||
final var handlers = subscribeReceiveHandlers(managers);
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
synchronized (this) {
|
||||
wait(15_000);
|
||||
wait(keepAliveInterval.toMillis());
|
||||
}
|
||||
if (shouldStop.get() || shutdown.get()) {
|
||||
break;
|
||||
@ -227,6 +242,7 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
eventBuffer.disconnect(client);
|
||||
for (final var pair : handlers) {
|
||||
unsubscribeReceiveHandler(pair);
|
||||
}
|
||||
@ -276,19 +292,17 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
throw new AssertionError("Unreachable state");
|
||||
}
|
||||
|
||||
private List<Pair<Manager, Manager.ReceiveMessageHandler>> subscribeReceiveHandlers(
|
||||
final List<Manager> managers,
|
||||
final ServerSentEventSender sender,
|
||||
Callable unsubscribe
|
||||
) {
|
||||
/**
|
||||
* The events are sent by eventBuffer, these handlers only keep the managers receiving while the client is connected.
|
||||
*/
|
||||
private List<Pair<Manager, Manager.ReceiveMessageHandler>> subscribeReceiveHandlers(final List<Manager> managers) {
|
||||
return managers.stream().map(m1 -> {
|
||||
final var receiveMessageHandler = new JsonReceiveMessageHandler(m1, s -> {
|
||||
try {
|
||||
sender.sendEvent(null, "receive", List.of(objectMapper.writeValueAsString(s)));
|
||||
} catch (IOException e) {
|
||||
unsubscribe.call();
|
||||
// An anonymous class instead of a lambda, as every connection needs its own handler instance
|
||||
final var receiveMessageHandler = new Manager.ReceiveMessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(final MessageEnvelope envelope, final Throwable e) {
|
||||
}
|
||||
});
|
||||
};
|
||||
m1.addReceiveHandler(receiveMessageHandler);
|
||||
return new Pair<>(m1, (Manager.ReceiveMessageHandler) receiveMessageHandler);
|
||||
}).toList();
|
||||
@ -300,11 +314,6 @@ public class HttpServerHandler implements AutoCloseable {
|
||||
m.removeReceiveHandler(handler);
|
||||
}
|
||||
|
||||
private interface Callable {
|
||||
|
||||
void call();
|
||||
}
|
||||
|
||||
private Set<String> buildAllowedHosts(final InetSocketAddress address) {
|
||||
final var s = new HashSet<String>();
|
||||
final var host = address == null ? null : address.getHostString();
|
||||
|
||||
153
src/main/java/org/asamk/signal/http/SseEventBuffer.java
Normal file
153
src/main/java/org/asamk/signal/http/SseEventBuffer.java
Normal file
@ -0,0 +1,153 @@
|
||||
package org.asamk.signal.http;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.json.JsonReceiveMessageHandler;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Keeps the most recent SSE events and sends new events to the connected clients.
|
||||
* A client that reconnects with a Last-Event-ID header first gets the buffered events it missed.
|
||||
* <p>
|
||||
* Event ids have the form "{start time}-{sequence}", so an id from an earlier daemon run never matches.
|
||||
*/
|
||||
final class SseEventBuffer implements AutoCloseable {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SseEventBuffer.class);
|
||||
|
||||
static final int MAX_EVENTS = 1000;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final String idPrefix = System.currentTimeMillis() + "-";
|
||||
private final Map<Manager, Manager.ReceiveMessageHandler> recorders = new HashMap<>();
|
||||
private final ArrayDeque<Event> events = new ArrayDeque<>();
|
||||
private final List<Client> clients = new ArrayList<>();
|
||||
private long nextSequence = 1;
|
||||
|
||||
SseEventBuffer(final ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start recording the messages of the given account.
|
||||
* The handler is registered as a weak listener, so it never starts receiving by itself.
|
||||
*/
|
||||
void record(final Manager m) {
|
||||
synchronized (recorders) {
|
||||
if (recorders.containsKey(m)) {
|
||||
return;
|
||||
}
|
||||
final var handler = new JsonReceiveMessageHandler(m, s -> add(m, s));
|
||||
m.addReceiveHandler(handler, true);
|
||||
recorders.put(m, handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a client for the given accounts.
|
||||
* If lastEventId is set, the buffered events after it are sent first.
|
||||
* An unknown id (too old, or from an earlier run) sends all buffered events.
|
||||
*/
|
||||
synchronized Client connect(
|
||||
final Collection<Manager> managers,
|
||||
final ServerSentEventSender sender,
|
||||
final String lastEventId,
|
||||
final Runnable onClosed
|
||||
) {
|
||||
final var client = new Client(managers, sender, onClosed);
|
||||
if (lastEventId != null) {
|
||||
final var lastSequence = getSequence(lastEventId);
|
||||
for (final var event : events) {
|
||||
if (event.sequence() > lastSequence && managers.contains(event.manager())) {
|
||||
client.send(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
clients.add(client);
|
||||
return client;
|
||||
}
|
||||
|
||||
synchronized void disconnect(final Client client) {
|
||||
clients.remove(client);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
synchronized (recorders) {
|
||||
recorders.forEach(Manager::removeReceiveHandler);
|
||||
recorders.clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void add(final Manager m, final Object message) {
|
||||
final String data;
|
||||
try {
|
||||
data = objectMapper.writeValueAsString(message);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to serialize received message, ignoring", e);
|
||||
return;
|
||||
}
|
||||
|
||||
final Event event;
|
||||
final List<Client> receivers;
|
||||
synchronized (this) {
|
||||
event = new Event(nextSequence++, m, data);
|
||||
events.addLast(event);
|
||||
if (events.size() > MAX_EVENTS) {
|
||||
events.removeFirst();
|
||||
}
|
||||
receivers = clients.stream().filter(c -> c.managers.contains(m)).toList();
|
||||
}
|
||||
// Sent outside the lock, so a slow client doesn't block the other accounts
|
||||
for (final var client : receivers) {
|
||||
client.send(event);
|
||||
}
|
||||
}
|
||||
|
||||
private long getSequence(final String eventId) {
|
||||
if (eventId.startsWith(idPrefix)) {
|
||||
try {
|
||||
return Long.parseLong(eventId.substring(idPrefix.length()));
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private record Event(long sequence, Manager manager, String data) {}
|
||||
|
||||
final class Client {
|
||||
|
||||
private final Collection<Manager> managers;
|
||||
private final ServerSentEventSender sender;
|
||||
private final Runnable onClosed;
|
||||
|
||||
private Client(
|
||||
final Collection<Manager> managers,
|
||||
final ServerSentEventSender sender,
|
||||
final Runnable onClosed
|
||||
) {
|
||||
this.managers = managers;
|
||||
this.sender = sender;
|
||||
this.onClosed = onClosed;
|
||||
}
|
||||
|
||||
private void send(final Event event) {
|
||||
try {
|
||||
sender.sendEvent(idPrefix + event.sequence(), "receive", List.of(event.data()));
|
||||
} catch (IOException e) {
|
||||
onClosed.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -8,7 +8,11 @@ import io.micronaut.jsonschema.JsonSchema;
|
||||
|
||||
@JsonSchema(title = "AdminDelete")
|
||||
public record JsonAdminDelete(
|
||||
@Deprecated String targetAuthor, String targetAuthorNumber, String targetAuthorUuid, long targetSentTimestamp
|
||||
@Deprecated String targetAuthor,
|
||||
String targetAuthorNumber,
|
||||
String targetAuthorUuid,
|
||||
long targetTimestamp,
|
||||
@Deprecated long targetSentTimestamp
|
||||
) {
|
||||
|
||||
static JsonAdminDelete from(MessageEnvelope.Data.AdminDelete adminDelete) {
|
||||
@ -16,9 +20,13 @@ public record JsonAdminDelete(
|
||||
final var targetAuthor = address.getLegacyIdentifier();
|
||||
final var targetAuthorNumber = address.number().orElse(null);
|
||||
final var targetAuthorUuid = address.uuid().map(UUID::toString).orElse(null);
|
||||
final var targetSentTimestamp = adminDelete.targetSentTimestamp();
|
||||
final var targetTimestamp = adminDelete.targetSentTimestamp();
|
||||
|
||||
return new JsonAdminDelete(targetAuthor, targetAuthorNumber, targetAuthorUuid, targetSentTimestamp);
|
||||
return new JsonAdminDelete(targetAuthor,
|
||||
targetAuthorNumber,
|
||||
targetAuthorUuid,
|
||||
targetTimestamp,
|
||||
targetTimestamp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -43,7 +43,7 @@ record JsonDataMessage(
|
||||
static JsonDataMessage from(MessageEnvelope.Data dataMessage, Manager m) {
|
||||
final var timestamp = dataMessage.timestamp();
|
||||
final var groupInfo = dataMessage.groupContext().isPresent() ? JsonGroupInfo.from(dataMessage.groupContext()
|
||||
.get(), m) : null;
|
||||
.get(), m) : null;
|
||||
final var storyContext = dataMessage.storyContext().isPresent()
|
||||
? JsonStoryContext.from(dataMessage.storyContext().get())
|
||||
: null;
|
||||
@ -59,32 +59,32 @@ record JsonDataMessage(
|
||||
final var quote = dataMessage.quote().isPresent() ? JsonQuote.from(dataMessage.quote().get()) : null;
|
||||
final var payment = dataMessage.payment().isPresent() ? JsonPayment.from(dataMessage.payment().get()) : null;
|
||||
final var mentions = !dataMessage.mentions().isEmpty() ? dataMessage.mentions()
|
||||
.stream()
|
||||
.map(JsonMention::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonMention::from)
|
||||
.toList() : null;
|
||||
final var previews = !dataMessage.previews().isEmpty() ? dataMessage.previews()
|
||||
.stream()
|
||||
.map(JsonPreview::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonPreview::from)
|
||||
.toList() : null;
|
||||
final var remoteDelete = dataMessage.remoteDeleteId().isPresent()
|
||||
? new JsonRemoteDelete(dataMessage.remoteDeleteId().get())
|
||||
? new JsonRemoteDelete(dataMessage.remoteDeleteId().get(), dataMessage.remoteDeleteId().get())
|
||||
: null;
|
||||
final var attachments = !dataMessage.attachments().isEmpty() ? dataMessage.attachments()
|
||||
.stream()
|
||||
.map(JsonAttachment::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonAttachment::from)
|
||||
.toList() : null;
|
||||
final var sticker = dataMessage.sticker().isPresent() ? JsonSticker.from(dataMessage.sticker().get()) : null;
|
||||
final var contacts = !dataMessage.sharedContacts().isEmpty() ? dataMessage.sharedContacts()
|
||||
.stream()
|
||||
.map(JsonSharedContact::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonSharedContact::from)
|
||||
.toList() : null;
|
||||
final var pollCreate = dataMessage.pollCreate().map(JsonPollCreate::from).orElse(null);
|
||||
final var pollVote = dataMessage.pollVote().map(JsonPollVote::from).orElse(null);
|
||||
final var pollTerminate = dataMessage.pollTerminate().map(JsonPollTerminate::from).orElse(null);
|
||||
final var textStyles = !dataMessage.textStyles().isEmpty() ? dataMessage.textStyles()
|
||||
.stream()
|
||||
.map(JsonTextStyle::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonTextStyle::from)
|
||||
.toList() : null;
|
||||
final var pinMessage = dataMessage.pinMessage().map(JsonPinMessage::from).orElse(null);
|
||||
final var unpinMessage = dataMessage.unpinMessage().map(JsonUnpinMessage::from).orElse(null);
|
||||
final var adminDelete = dataMessage.adminDelete().map(JsonAdminDelete::from).orElse(null);
|
||||
|
||||
@ -34,14 +34,14 @@ public record JsonQuote(
|
||||
: null;
|
||||
|
||||
final var attachments = !quote.attachments().isEmpty() ? quote.attachments()
|
||||
.stream()
|
||||
.map(JsonQuotedAttachment::from)
|
||||
.toList() : List.<JsonQuotedAttachment>of();
|
||||
.stream()
|
||||
.map(JsonQuotedAttachment::from)
|
||||
.toList() : List.<JsonQuotedAttachment>of();
|
||||
|
||||
final var textStyles = !quote.textStyles().isEmpty() ? quote.textStyles()
|
||||
.stream()
|
||||
.map(JsonTextStyle::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonTextStyle::from)
|
||||
.toList() : null;
|
||||
|
||||
return new JsonQuote(id, author, authorNumber, authorUuid, text, mentions, attachments, textStyles);
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ public class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler
|
||||
@Override
|
||||
public void handleMessage(MessageEnvelope envelope, Throwable exception) {
|
||||
final var object = new HashMap<String, Object>();
|
||||
object.put("account", m.getSelfNumber());
|
||||
object.put("account", m.getSelfIdentifier());
|
||||
if (exception != null) {
|
||||
object.put("exception", JsonError.from(exception));
|
||||
}
|
||||
|
||||
@ -3,4 +3,4 @@ package org.asamk.signal.json;
|
||||
import io.micronaut.jsonschema.JsonSchema;
|
||||
|
||||
@JsonSchema(title = "RemoteDelete")
|
||||
record JsonRemoteDelete(long timestamp) {}
|
||||
record JsonRemoteDelete(long targetTimestamp, @Deprecated long timestamp) {}
|
||||
|
||||
@ -27,13 +27,13 @@ public record JsonSendMessageResult(
|
||||
result.isSuccess()
|
||||
? Type.SUCCESS
|
||||
: result.isRateLimitFailure()
|
||||
? Type.RATE_LIMIT_FAILURE
|
||||
? Type.RATE_LIMIT_FAILURE
|
||||
: result.isNetworkFailure()
|
||||
? Type.NETWORK_FAILURE
|
||||
? Type.NETWORK_FAILURE
|
||||
: result.isUnregisteredFailure()
|
||||
? Type.UNREGISTERED_FAILURE
|
||||
? Type.UNREGISTERED_FAILURE
|
||||
: result.isInvalidPreKeyFailure()
|
||||
? Type.INVALID_PRE_KEY_FAILURE
|
||||
? Type.INVALID_PRE_KEY_FAILURE
|
||||
: Type.IDENTITY_FAILURE,
|
||||
result.proofRequiredFailure() != null ? result.proofRequiredFailure().getToken() : null,
|
||||
rateLimitRetryAfterMilliseconds == null ? null : Math.ceilDiv(rateLimitRetryAfterMilliseconds, 1000L));
|
||||
|
||||
@ -31,9 +31,9 @@ public record JsonSharedContact(
|
||||
: null;
|
||||
|
||||
final var address = !contact.address().isEmpty() ? contact.address()
|
||||
.stream()
|
||||
.map(JsonContactAddress::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonContactAddress::from)
|
||||
.toList() : null;
|
||||
|
||||
final var organization = contact.organization().orElse(null);
|
||||
|
||||
|
||||
@ -50,9 +50,9 @@ record JsonSyncMessage(
|
||||
}
|
||||
|
||||
final var readMessages = !syncMessage.read().isEmpty() ? syncMessage.read()
|
||||
.stream()
|
||||
.map(JsonSyncReadMessage::from)
|
||||
.toList() : null;
|
||||
.stream()
|
||||
.map(JsonSyncReadMessage::from)
|
||||
.toList() : null;
|
||||
|
||||
final JsonSyncMessageType type;
|
||||
if (syncMessage.contacts().isPresent()) {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package org.asamk.signal.jsonrpc;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import com.fasterxml.jackson.core.StreamReadConstraints;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@ -8,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ContainerNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.ValueNode;
|
||||
|
||||
import org.asamk.signal.manager.config.ServiceConfig;
|
||||
import org.asamk.signal.util.Util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -24,6 +26,7 @@ import java.util.stream.StreamSupport;
|
||||
public class JsonRpcReader {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(JsonRpcReader.class);
|
||||
private static final int MAX_JSON_STRING_LENGTH = ((ServiceConfig.MAX_ATTACHMENT_SIZE + 2) / 3) * 4 + 1024;
|
||||
|
||||
private final JsonRpcSender jsonRpcSender;
|
||||
private final ObjectMapper objectMapper;
|
||||
@ -34,14 +37,23 @@ public class JsonRpcReader {
|
||||
this.jsonRpcSender = jsonRpcSender;
|
||||
this.input = null;
|
||||
this.lineSupplier = lineSupplier;
|
||||
this.objectMapper = Util.createJsonObjectMapper();
|
||||
this.objectMapper = createObjectMapper();
|
||||
}
|
||||
|
||||
public JsonRpcReader(final JsonRpcSender jsonRpcSender, final InputStream input) {
|
||||
this.jsonRpcSender = jsonRpcSender;
|
||||
this.input = input;
|
||||
this.lineSupplier = null;
|
||||
this.objectMapper = Util.createJsonObjectMapper();
|
||||
this.objectMapper = createObjectMapper();
|
||||
}
|
||||
|
||||
private static ObjectMapper createObjectMapper() {
|
||||
final var objectMapper = Util.createJsonObjectMapper();
|
||||
objectMapper.getFactory()
|
||||
.setStreamReadConstraints(StreamReadConstraints.builder()
|
||||
.maxStringLength(MAX_JSON_STRING_LENGTH)
|
||||
.build());
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
public void readMessages(final RequestHandler requestHandler, final Consumer<JsonRpcResponse> responseHandler) {
|
||||
|
||||
@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ContainerNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.asamk.signal.AccountIdentifier;
|
||||
import org.asamk.signal.commands.Command;
|
||||
import org.asamk.signal.commands.JsonRpcMultiCommand;
|
||||
import org.asamk.signal.commands.JsonRpcRegistrationCommand;
|
||||
@ -126,7 +127,7 @@ public class SignalJsonRpcCommandHandler {
|
||||
|
||||
private Manager getManagerFromParams(final ContainerNode<?> params) throws JsonRpcException {
|
||||
if (params != null && params.hasNonNull("account")) {
|
||||
final var manager = c.getManager(params.get("account").asText());
|
||||
final var manager = c.getManager(AccountIdentifier.normalize(params.get("account").asText()));
|
||||
((ObjectNode) params).remove("account");
|
||||
if (manager == null) {
|
||||
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_PARAMS,
|
||||
@ -140,7 +141,7 @@ public class SignalJsonRpcCommandHandler {
|
||||
|
||||
private Pair<String, RegistrationManager> getRegistrationManagerFromParams(final ContainerNode<?> params) {
|
||||
if (params != null && params.has("account")) {
|
||||
final var account = params.get("account").asText();
|
||||
final var account = AccountIdentifier.normalize(params.get("account").asText());
|
||||
((ObjectNode) params).remove("account");
|
||||
try {
|
||||
return new Pair<>(account, c.getNewRegistrationManager(account));
|
||||
|
||||
@ -31,6 +31,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -61,17 +62,31 @@ public class SignalJsonRpcDispatcherHandler {
|
||||
public void handleConnection(final MultiAccountManager c) {
|
||||
this.commandHandler = new SignalJsonRpcCommandHandler(c, this::getCommand);
|
||||
|
||||
final Consumer<Manager> onManagerAddedReceiveHandler = m -> subscribeReceive(m, true);
|
||||
final Consumer<Manager> onManagerRemovedReceiveHandler = this::unsubscribeReceive;
|
||||
final Consumer<Manager> onManagerAddedReceiveSubscriptionsHandler = m -> receiveHandlers.forEach((subscriptionId, handlers) -> handlers.add(
|
||||
createReceiveHandler(m, subscriptionId, false)));
|
||||
final Consumer<Manager> onManagerAddedCallEventSubscriptionsHandler = m -> callEventHandlers.forEach((subscriptionId, handlers) -> handlers.add(
|
||||
createCallEventHandler(m, subscriptionId)));
|
||||
|
||||
if (!noReceiveOnStart) {
|
||||
this.subscribeReceive(c.getManagers(), true);
|
||||
c.addOnManagerAddedHandler(m -> subscribeReceive(m, true));
|
||||
c.addOnManagerRemovedHandler(this::unsubscribeReceive);
|
||||
c.addOnManagerAddedHandler(onManagerAddedReceiveHandler);
|
||||
c.addOnManagerRemovedHandler(onManagerRemovedReceiveHandler);
|
||||
}
|
||||
c.addOnManagerAddedHandler(m -> receiveHandlers.forEach((subscriptionId, handlers) -> handlers.add(
|
||||
createReceiveHandler(m, subscriptionId, false))));
|
||||
c.addOnManagerAddedHandler(m -> callEventHandlers.forEach((subscriptionId, handlers) -> handlers.add(
|
||||
createCallEventHandler(m, subscriptionId))));
|
||||
c.addOnManagerAddedHandler(onManagerAddedReceiveSubscriptionsHandler);
|
||||
c.addOnManagerAddedHandler(onManagerAddedCallEventSubscriptionsHandler);
|
||||
|
||||
handleConnection();
|
||||
try {
|
||||
handleConnection();
|
||||
} finally {
|
||||
if (!noReceiveOnStart) {
|
||||
c.removeOnManagerAddedHandler(onManagerAddedReceiveHandler);
|
||||
c.removeOnManagerRemovedHandler(onManagerRemovedReceiveHandler);
|
||||
}
|
||||
c.removeOnManagerAddedHandler(onManagerAddedReceiveSubscriptionsHandler);
|
||||
c.removeOnManagerAddedHandler(onManagerAddedCallEventSubscriptionsHandler);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleConnection(final Manager m) {
|
||||
|
||||
@ -1415,6 +1415,9 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "java.math.BigDecimal"
|
||||
},
|
||||
{
|
||||
"type": "java.math.BigInteger"
|
||||
},
|
||||
@ -1771,6 +1774,9 @@
|
||||
{
|
||||
"type": "kotlin.Any"
|
||||
},
|
||||
{
|
||||
"type": "kotlin.Array"
|
||||
},
|
||||
{
|
||||
"type": "kotlin.Boolean"
|
||||
},
|
||||
@ -1946,42 +1952,7 @@
|
||||
},
|
||||
{
|
||||
"type": "kotlin.reflect.jvm.internal.impl.resolve.scopes.DescriptorKindFilter",
|
||||
"allPublicFields": true,
|
||||
"fields": [
|
||||
{
|
||||
"name": "ALL"
|
||||
},
|
||||
{
|
||||
"name": "CALLABLES"
|
||||
},
|
||||
{
|
||||
"name": "CLASSIFIERS"
|
||||
},
|
||||
{
|
||||
"name": "Companion"
|
||||
},
|
||||
{
|
||||
"name": "FUNCTIONS"
|
||||
},
|
||||
{
|
||||
"name": "NON_SINGLETON_CLASSIFIERS"
|
||||
},
|
||||
{
|
||||
"name": "PACKAGES"
|
||||
},
|
||||
{
|
||||
"name": "SINGLETON_CLASSIFIERS"
|
||||
},
|
||||
{
|
||||
"name": "TYPE_ALIASES"
|
||||
},
|
||||
{
|
||||
"name": "VALUES"
|
||||
},
|
||||
{
|
||||
"name": "VARIABLES"
|
||||
}
|
||||
]
|
||||
"allPublicFields": true
|
||||
},
|
||||
{
|
||||
"type": "kotlinx.coroutines.AwaitAll"
|
||||
@ -2513,6 +2484,10 @@
|
||||
"type": "org.asamk.signal.commands.ListAccountsCommand$JsonAccount",
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "aci",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "number",
|
||||
"parameterTypes": []
|
||||
@ -5118,6 +5093,15 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.bouncycastle.jcajce.provider.asymmetric.XMSS$Mappings",
|
||||
"methods": [
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.bouncycastle.jcajce.provider.asymmetric.edec.SignatureSpi$Ed25519",
|
||||
"methods": [
|
||||
@ -7184,6 +7168,9 @@
|
||||
"type": "org.signal.libsignal.zkgroup.profiles.ExpiringProfileKeyCredential",
|
||||
"allDeclaredFields": true
|
||||
},
|
||||
{
|
||||
"type": "org.signal.libsignal.zkgroup.profiles.ExpiringProfileKeyCredentialResponse"
|
||||
},
|
||||
{
|
||||
"type": "org.signal.libsignal.zkgroup.profiles.ProfileKey",
|
||||
"allDeclaredFields": true
|
||||
@ -8321,6 +8308,13 @@
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": [
|
||||
"int",
|
||||
"int"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -8463,6 +8457,12 @@
|
||||
"allDeclaredMethods": true,
|
||||
"allDeclaredConstructors": true
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.api.profiles.SignalServiceProfile$Companion"
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.api.profiles.SignalServiceProfile$RequestType"
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.api.profiles.SignalServiceProfileWrite",
|
||||
"allDeclaredFields": true,
|
||||
@ -8471,7 +8471,13 @@
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.api.provisioning.ProvisioningMessage",
|
||||
"allDeclaredFields": true
|
||||
"allDeclaredFields": true,
|
||||
"methods": [
|
||||
{
|
||||
"name": "getBody",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.api.push.ServiceId",
|
||||
@ -8919,6 +8925,9 @@
|
||||
"allDeclaredMethods": true,
|
||||
"allDeclaredConstructors": true
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.AuthCredentials$Companion"
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.ByteArrayDeserializerBase64",
|
||||
"methods": [
|
||||
@ -8928,6 +8937,15 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.ByteArraySerializerBase64NoPadding",
|
||||
"methods": [
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.CdsiAuthResponse",
|
||||
"allDeclaredFields": true,
|
||||
@ -8935,6 +8953,13 @@
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": [
|
||||
"java.lang.String",
|
||||
"java.lang.String"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -9096,6 +9121,14 @@
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
},
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": [
|
||||
"long",
|
||||
"org.signal.libsignal.protocol.kem.KEMPublicKey",
|
||||
"byte[]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "getKeyId",
|
||||
"parameterTypes": []
|
||||
@ -9182,6 +9215,9 @@
|
||||
"allDeclaredMethods": true,
|
||||
"allDeclaredConstructors": true
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.MismatchedDevices$Companion"
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.OneTimePreKeyCounts",
|
||||
"allDeclaredFields": true,
|
||||
@ -9583,6 +9619,61 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.StickerUploadAttributes",
|
||||
"fields": [
|
||||
{
|
||||
"name": "acl"
|
||||
},
|
||||
{
|
||||
"name": "algorithm"
|
||||
},
|
||||
{
|
||||
"name": "credential"
|
||||
},
|
||||
{
|
||||
"name": "date"
|
||||
},
|
||||
{
|
||||
"name": "id"
|
||||
},
|
||||
{
|
||||
"name": "key"
|
||||
},
|
||||
{
|
||||
"name": "policy"
|
||||
},
|
||||
{
|
||||
"name": "signature"
|
||||
}
|
||||
],
|
||||
"methods": [
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.StickerUploadAttributesResponse",
|
||||
"fields": [
|
||||
{
|
||||
"name": "manifest"
|
||||
},
|
||||
{
|
||||
"name": "packId"
|
||||
},
|
||||
{
|
||||
"name": "stickers"
|
||||
}
|
||||
],
|
||||
"methods": [
|
||||
{
|
||||
"name": "<init>",
|
||||
"parameterTypes": []
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "org.whispersystems.signalservice.internal.push.SubmitRecaptchaChallengePayload",
|
||||
"allDeclaredFields": true
|
||||
|
||||
23
src/test/java/org/asamk/signal/AccountIdentifierTest.java
Normal file
23
src/test/java/org/asamk/signal/AccountIdentifierTest.java
Normal file
@ -0,0 +1,23 @@
|
||||
package org.asamk.signal;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class AccountIdentifierTest {
|
||||
|
||||
@Test
|
||||
void normalizesSignalAndroidAccountKeys() {
|
||||
assertEquals("a6b28482-2e32-83d0-7f23-91360a4c2b91",
|
||||
AccountIdentifier.normalize("A6B284822E3283D07F2391360A4C2B91"));
|
||||
assertEquals("a6b28482-2e32-83d0-7f23-91360a4c2b91",
|
||||
AccountIdentifier.normalize("A6B28482-2E32-83D0-7F23-91360A4C2B91"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void leavesNonAccountIdentifiersUnchanged() {
|
||||
assertEquals("+12025550123", AccountIdentifier.normalize("+12025550123"));
|
||||
assertEquals("not-an-account", AccountIdentifier.normalize("not-an-account"));
|
||||
assertEquals("NOT-AN-ACCOUNT", AccountIdentifier.normalize("NOT-AN-ACCOUNT"));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,59 @@
|
||||
package org.asamk.signal.commands;
|
||||
|
||||
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.AttachmentInvalidException;
|
||||
import org.asamk.signal.output.JsonWriter;
|
||||
import org.asamk.signal.testutil.ManagerMock;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.net.SocketException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class SendCommandErrorTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("failures")
|
||||
void uploadAndTransmissionFailuresKeepTheirClassification(final Exception cause) {
|
||||
final var error = send(cause);
|
||||
|
||||
assertEquals("Failed to send message: " + cause.getMessage() + " (" + cause.getClass().getSimpleName() + ")",
|
||||
error.getMessage());
|
||||
assertSame(cause, error.getCause());
|
||||
}
|
||||
|
||||
private static Stream<Exception> failures() {
|
||||
return Stream.of(new AttachmentInvalidException("inline attachment #2: Connection reset"),
|
||||
new SocketException("Connection reset"),
|
||||
new IOException("Server error: 500"));
|
||||
}
|
||||
|
||||
private static UnexpectedErrorException send(final Exception failure) {
|
||||
final var delegate = ManagerMock.create("+15551234567");
|
||||
final var manager = (Manager) Proxy.newProxyInstance(Manager.class.getClassLoader(),
|
||||
new Class<?>[]{Manager.class}, (proxy, method, args) -> {
|
||||
if (method.getName().equals("sendMessage")) {
|
||||
throw failure;
|
||||
}
|
||||
return method.invoke(delegate, args);
|
||||
});
|
||||
// Upload and transmission failures must map to the same exception type, so the exit code and the
|
||||
// JSON-RPC error code stay the same.
|
||||
return assertThrows(UnexpectedErrorException.class,
|
||||
() -> new SendCommand().handleCommand(new JsonRpcNamespace(Map.of("noteToSelf",
|
||||
true,
|
||||
"message",
|
||||
"hi",
|
||||
"attachments",
|
||||
List.of("photo.jpg"))), manager, (JsonWriter) ignored -> {}));
|
||||
}
|
||||
}
|
||||
@ -29,42 +29,65 @@ class SendCommandParsingTest {
|
||||
@Test
|
||||
void cliPreservesEmptyAndMissingDimensionPositions() throws Exception {
|
||||
final var parser = ArgumentParsers.newFor("signal-cli", DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS)
|
||||
.includeArgumentNamesAsKeysInResult(true).build();
|
||||
.includeArgumentNamesAsKeysInResult(true)
|
||||
.build();
|
||||
new SendCommand().attachToSubparser(parser.addSubparsers().addParser("send"));
|
||||
final var message = send(parser.parseArgs(new String[]{"send", "--note-to-self",
|
||||
"-a", "photo.jpg", "portrait.mp4", "other.mp4", "--attachment-dimensions", "", "1080x1920"}));
|
||||
final var message = send(parser.parseArgs(new String[]{
|
||||
"send",
|
||||
"--note-to-self",
|
||||
"-a",
|
||||
"photo.jpg",
|
||||
"portrait.mp4",
|
||||
"other.mp4",
|
||||
"--attachment-dimensions",
|
||||
"",
|
||||
"1080x1920"
|
||||
}));
|
||||
|
||||
assertEquals(ATTACHMENTS, message.attachments());
|
||||
assertEquals(Arrays.asList(null, new Message.AttachmentDimensions(1080, 1920)),
|
||||
message.attachmentDimensions());
|
||||
assertEquals(Arrays.asList(null, new Message.AttachmentDimensions(1080, 1920)), message.attachmentDimensions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonRpcPassesDimensionsIntoTheSentMessage() throws Exception {
|
||||
final var message = send(namespace(List.of("", "1920x1080")));
|
||||
assertEquals(ATTACHMENTS, message.attachments());
|
||||
assertEquals(Arrays.asList(null, new Message.AttachmentDimensions(1920, 1080)),
|
||||
message.attachmentDimensions());
|
||||
assertEquals(Arrays.asList(null, new Message.AttachmentDimensions(1920, 1080)), message.attachmentDimensions());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cliPreservesBlurHashPositions() throws Exception {
|
||||
final var parser = ArgumentParsers.newFor("signal-cli", DefaultSettings.VERSION_0_9_0_DEFAULT_SETTINGS)
|
||||
.includeArgumentNamesAsKeysInResult(true).build();
|
||||
.includeArgumentNamesAsKeysInResult(true)
|
||||
.build();
|
||||
new SendCommand().attachToSubparser(parser.addSubparsers().addParser("send"));
|
||||
final var message = send(parser.parseArgs(new String[]{"send", "--note-to-self",
|
||||
"-a", "photo.jpg", "portrait.mp4", "other.mp4", "--attachment-blurhash", "", BLUR_HASH}));
|
||||
final var message = send(parser.parseArgs(new String[]{
|
||||
"send",
|
||||
"--note-to-self",
|
||||
"-a",
|
||||
"photo.jpg",
|
||||
"portrait.mp4",
|
||||
"other.mp4",
|
||||
"--attachment-blurhash",
|
||||
"",
|
||||
BLUR_HASH
|
||||
}));
|
||||
|
||||
assertEquals(List.of("", BLUR_HASH), message.attachmentBlurHashes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonRpcPassesBlurHashesWithDimensions() throws Exception {
|
||||
final var message = send(new JsonRpcNamespace(Map.of("noteToSelf", true, "attachments", ATTACHMENTS,
|
||||
"attachmentDimensions", List.of("", "1920x1080"), "attachmentBlurhash", List.of("", BLUR_HASH))));
|
||||
final var message = send(new JsonRpcNamespace(Map.of("noteToSelf",
|
||||
true,
|
||||
"attachments",
|
||||
ATTACHMENTS,
|
||||
"attachmentDimensions",
|
||||
List.of("", "1920x1080"),
|
||||
"attachmentBlurhash",
|
||||
List.of("", BLUR_HASH))));
|
||||
|
||||
assertEquals(Arrays.asList(null, new Message.AttachmentDimensions(1920, 1080)),
|
||||
message.attachmentDimensions());
|
||||
assertEquals(Arrays.asList(null, new Message.AttachmentDimensions(1920, 1080)), message.attachmentDimensions());
|
||||
assertEquals(List.of("", BLUR_HASH), message.attachmentBlurHashes());
|
||||
}
|
||||
|
||||
@ -91,27 +114,37 @@ class SendCommandParsingTest {
|
||||
}
|
||||
|
||||
private JsonRpcNamespace blurHashNamespace(List<String> blurHashes) {
|
||||
return new JsonRpcNamespace(Map.of("noteToSelf", true, "attachments", ATTACHMENTS,
|
||||
"attachmentBlurhash", blurHashes));
|
||||
return new JsonRpcNamespace(Map.of("noteToSelf",
|
||||
true,
|
||||
"attachments",
|
||||
ATTACHMENTS,
|
||||
"attachmentBlurhash",
|
||||
blurHashes));
|
||||
}
|
||||
|
||||
private JsonRpcNamespace namespace(List<String> dimensions) {
|
||||
return new JsonRpcNamespace(Map.of("noteToSelf", true, "attachments", ATTACHMENTS,
|
||||
"attachmentDimensions", dimensions));
|
||||
return new JsonRpcNamespace(Map.of("noteToSelf",
|
||||
true,
|
||||
"attachments",
|
||||
ATTACHMENTS,
|
||||
"attachmentDimensions",
|
||||
dimensions));
|
||||
}
|
||||
|
||||
private Message send(Namespace namespace) throws Exception {
|
||||
final var captured = new AtomicReference<Message>();
|
||||
final var delegate = ManagerMock.create("+15551234567");
|
||||
final var manager = (Manager) Proxy.newProxyInstance(Manager.class.getClassLoader(),
|
||||
new Class<?>[]{Manager.class}, (proxy, method, args) -> {
|
||||
new Class<?>[]{Manager.class},
|
||||
(proxy, method, args) -> {
|
||||
if (method.getName().equals("sendMessage")) {
|
||||
captured.set((Message) args[0]);
|
||||
return new SendMessageResults(1, Map.of());
|
||||
}
|
||||
return method.invoke(delegate, args);
|
||||
});
|
||||
new SendCommand().handleCommand(namespace, manager, (JsonWriter) ignored -> {});
|
||||
new SendCommand().handleCommand(namespace, manager, (JsonWriter) ignored -> {
|
||||
});
|
||||
return captured.get();
|
||||
}
|
||||
}
|
||||
|
||||
415
src/test/java/org/asamk/signal/dbus/NumberlessDbusTest.java
Normal file
415
src/test/java/org/asamk/signal/dbus/NumberlessDbusTest.java
Normal file
@ -0,0 +1,415 @@
|
||||
package org.asamk.signal.dbus;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.SignalControl;
|
||||
import org.asamk.signal.DbusConfig;
|
||||
import org.asamk.signal.commands.CommandHandler;
|
||||
import org.asamk.signal.commands.ListGroupsCommand;
|
||||
import org.asamk.signal.commands.LocalCommand;
|
||||
import org.asamk.signal.commands.exceptions.UserErrorException;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.api.Group;
|
||||
import org.asamk.signal.manager.api.GroupId;
|
||||
import org.asamk.signal.manager.api.GroupMember;
|
||||
import org.asamk.signal.manager.api.GroupPermission;
|
||||
import org.asamk.signal.manager.api.Identity;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.api.Recipient;
|
||||
import org.asamk.signal.manager.api.RecipientAddress;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.asamk.signal.manager.api.SendMessageResults;
|
||||
import org.asamk.signal.manager.api.TrustLevel;
|
||||
import org.asamk.signal.manager.api.TypingAction;
|
||||
import org.asamk.signal.manager.internal.MultiAccountManagerImpl;
|
||||
import org.freedesktop.dbus.DBusPath;
|
||||
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||
import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder;
|
||||
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class NumberlessDbusTest {
|
||||
|
||||
private static final String NUMBER = "+12025550123";
|
||||
private static final String ACI = "11111111-1111-4111-8111-111111111111";
|
||||
private static final String OTHER_ACI = "22222222-2222-4222-8222-222222222222";
|
||||
private static final String THIRD_ACI = "33333333-3333-4333-8333-333333333333";
|
||||
private static final String NUMBERLESS_PATH = "/org/asamk/Signal/11111111_1111_4111_8111_111111111111";
|
||||
private static final String NUMBERED_PATH = "/org/asamk/Signal/_12025550123";
|
||||
|
||||
@Test
|
||||
void resolvesNumberlessAndNumberedAccountsWithoutChangingNumberedPaths() {
|
||||
final var numberless = new TestAccount(null, ACI);
|
||||
final var numbered = new TestAccount(NUMBER, OTHER_ACI);
|
||||
final var accounts = new MultiAccountManagerImpl(List.of(numberless.manager, numbered.manager), null);
|
||||
final var control = new DbusSignalControlImpl(accounts, DbusConfig.getObjectPath());
|
||||
|
||||
assertEquals(Set.of(new DBusPath(NUMBERLESS_PATH), new DBusPath(NUMBERED_PATH)),
|
||||
Set.copyOf(control.listAccounts()));
|
||||
assertEquals(NUMBERLESS_PATH, control.getAccount(ACI).getPath());
|
||||
assertEquals(NUMBERED_PATH, control.getAccount(NUMBER).getPath());
|
||||
assertEquals(NUMBERED_PATH, control.getAccount(OTHER_ACI).getPath());
|
||||
assertThrows(SignalControl.Error.Failure.class, () -> control.getAccount(THIRD_ACI));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = NUMBER)
|
||||
void mapsOptionalPhoneNumbersAndAddressesSelfByAnAvailableIdentifier(final String number) throws Exception {
|
||||
final var account = new TestAccount(number, ACI);
|
||||
final var signal = new DbusSignalImpl(account.manager, null, DbusConfig.getObjectPath(), true);
|
||||
final var client = new DbusManagerImpl(signal, null, "org.asamk.Signal.Test");
|
||||
|
||||
assertEquals(number == null ? "" : number, signal.getSelfNumber());
|
||||
assertEquals(number, client.getSelfNumber());
|
||||
assertEquals(number == null ? ACI : number, client.getSelfIdentifier());
|
||||
checkSelfOperations(client,
|
||||
account,
|
||||
number == null
|
||||
? new RecipientIdentifier.Uuid(UUID.fromString(ACI))
|
||||
: new RecipientIdentifier.Number(number));
|
||||
}
|
||||
|
||||
// Run with: dbus-run-session -- ./gradlew --no-daemon :test --tests '*NumberlessDbusTest*' --rerun-tasks
|
||||
@Nested
|
||||
@Timeout(20)
|
||||
@EnabledIfEnvironmentVariable(named = "DBUS_SESSION_BUS_ADDRESS", matches = ".+")
|
||||
class SessionBus {
|
||||
|
||||
@Test
|
||||
void exportsMixedAccountsRoutesAciSelectorsAndRemovesOnlyTheClosedAccount() throws Exception {
|
||||
final var numberless = new TestAccount(null, ACI);
|
||||
final var numbered = new TestAccount(NUMBER, OTHER_ACI);
|
||||
final var secondNumberless = new TestAccount(null, THIRD_ACI);
|
||||
final var accounts = new MultiAccountManagerImpl(List.of(numberless.manager,
|
||||
numbered.manager,
|
||||
secondNumberless.manager), null);
|
||||
final var busname = newBusname();
|
||||
try (final var server = new DbusHandler(false,
|
||||
busname,
|
||||
accounts,
|
||||
true); final var connection = DBusConnectionBuilder.forSessionBus().withShared(false).build()) {
|
||||
server.init();
|
||||
final var control = connection.getRemoteObject(busname,
|
||||
DbusConfig.getObjectPath(),
|
||||
SignalControl.class);
|
||||
assertEquals(Set.of(NUMBERLESS_PATH, NUMBERED_PATH, DbusConfig.getObjectPath(THIRD_ACI)),
|
||||
control.listAccounts().stream().map(DBusPath::getPath).collect(Collectors.toSet()));
|
||||
assertEquals(NUMBERED_PATH, control.getAccount(OTHER_ACI).getPath());
|
||||
assertEquals(NUMBERED_PATH, control.getAccount(NUMBER).getPath());
|
||||
assertEquals(NUMBERLESS_PATH, control.getAccount(ACI).getPath());
|
||||
assertEquals(ACI, selectAccount(ACI, connection, busname));
|
||||
assertEquals(OTHER_ACI, selectAccount(OTHER_ACI, connection, busname));
|
||||
assertEquals(OTHER_ACI, selectAccount(NUMBER, connection, busname));
|
||||
assertThrows(UserErrorException.class, () -> selectAccount(null, connection, busname));
|
||||
|
||||
final var client = new DbusMultiAccountManagerImpl(control, connection, busname);
|
||||
assertEquals(3, client.getManagers().size());
|
||||
assertEquals(2, client.getManagers().stream().filter(m -> m.getSelfNumber() == null).count());
|
||||
assertNull(client.getManager(ACI).getSelfNumber());
|
||||
assertEquals(NUMBER, client.getManager(OTHER_ACI).getSelfNumber());
|
||||
|
||||
final var removed = connection.getRemoteObject(busname, NUMBERLESS_PATH, Signal.class);
|
||||
numberless.manager.close();
|
||||
assertEquals(2, control.listAccounts().size());
|
||||
assertThrows(SignalControl.Error.Failure.class, () -> control.getAccount(ACI));
|
||||
assertThrows(DBusExecutionException.class, removed::getSelfACI);
|
||||
assertEquals(THIRD_ACI, client.getManager(THIRD_ACI).getSelfACI());
|
||||
assertEquals(NUMBER, client.getManager(NUMBER).getSelfNumber());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@NullSource
|
||||
@ValueSource(strings = NUMBER)
|
||||
void selectsSingleAccountDaemonsImplicitlyAndByAci(final String number) throws Exception {
|
||||
final var account = new TestAccount(number, ACI);
|
||||
final var busname = newBusname();
|
||||
try (final var server = new DbusHandler(false,
|
||||
busname,
|
||||
account.manager,
|
||||
true); final var connection = DBusConnectionBuilder.forSessionBus().withShared(false).build()) {
|
||||
server.init();
|
||||
assertEquals(ACI, selectAccount(null, connection, busname));
|
||||
assertEquals(ACI, selectAccount(ACI, connection, busname));
|
||||
if (number != null) {
|
||||
assertEquals(ACI, selectAccount(number, connection, busname));
|
||||
}
|
||||
assertThrows(UserErrorException.class, () -> selectAccount(OTHER_ACI, connection, busname));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void preservesAciAddressesInContactsGroupMembersAndIdentities() throws Exception {
|
||||
final var account = new TestAccount(null, ACI);
|
||||
final var contact = new RecipientAddress(UUID.fromString(OTHER_ACI));
|
||||
final var numberedContact = new RecipientAddress(NUMBER);
|
||||
account.recipients = List.of(Recipient.newBuilder().withAddress(contact).build(),
|
||||
Recipient.newBuilder().withAddress(numberedContact).build());
|
||||
account.identities = List.of(new Identity(contact,
|
||||
new byte[]{1},
|
||||
"test",
|
||||
new byte[]{2},
|
||||
TrustLevel.TRUSTED_UNVERIFIED,
|
||||
123));
|
||||
final var groupId = GroupId.unknownVersion(new byte[32]);
|
||||
account.groups = List.of(new Group(groupId,
|
||||
"Test group",
|
||||
"",
|
||||
null,
|
||||
Set.of(new GroupMember(contact, true, null, null),
|
||||
new GroupMember(numberedContact, false, null, null)),
|
||||
Set.of(contact),
|
||||
Set.of(contact),
|
||||
Set.of(contact),
|
||||
false,
|
||||
0,
|
||||
GroupPermission.EVERY_MEMBER,
|
||||
GroupPermission.EVERY_MEMBER,
|
||||
GroupPermission.EVERY_MEMBER,
|
||||
true,
|
||||
true,
|
||||
false));
|
||||
final var busname = newBusname();
|
||||
try (final var server = new DbusHandler(false,
|
||||
busname,
|
||||
account.manager,
|
||||
true); final var connection = DBusConnectionBuilder.forSessionBus().withShared(false).build()) {
|
||||
server.init();
|
||||
final var signal = connection.getRemoteObject(busname, DbusConfig.getObjectPath(), Signal.class);
|
||||
assertEquals(List.of(NUMBER), signal.listNumbers());
|
||||
assertEquals(Set.of(OTHER_ACI, NUMBER), Set.copyOf(signal.listRecipientIdentifiers()));
|
||||
try (final var client = new DbusManagerImpl(signal, connection, busname)) {
|
||||
final var contacts = client.getRecipients(false,
|
||||
Optional.empty(),
|
||||
Set.of(new RecipientIdentifier.Uuid(UUID.fromString(OTHER_ACI))),
|
||||
Optional.empty());
|
||||
assertEquals(1, contacts.size());
|
||||
assertEquals(contact, contacts.getFirst().getAddress());
|
||||
assertEquals(contact, client.getIdentities().getFirst().recipient());
|
||||
final var group = client.getGroups().getFirst();
|
||||
assertEquals(Set.of(contact, numberedContact),
|
||||
group.members().stream().map(GroupMember::recipientAddress).collect(Collectors.toSet()));
|
||||
assertEquals(Set.of(contact), group.pendingMembers());
|
||||
assertEquals(Set.of(contact), group.requestingMembers());
|
||||
assertEquals(Set.of(contact), group.bannedMembers());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendsAndReceivesNumberlessDirectGroupAndSelfMessagesOverDbus() throws Exception {
|
||||
final var account = new TestAccount(null, ACI);
|
||||
final var accounts = new MultiAccountManagerImpl(List.of(account.manager), null);
|
||||
final var busname = newBusname();
|
||||
try (final var server = new DbusHandler(false,
|
||||
busname,
|
||||
accounts,
|
||||
true); final var connection = DBusConnectionBuilder.forSessionBus().withShared(false).build()) {
|
||||
server.init();
|
||||
assertEquals(ACI, selectAccount(null, connection, busname));
|
||||
final var signal = connection.getRemoteObject(busname, NUMBERLESS_PATH, Signal.class);
|
||||
assertEquals("", signal.getSelfNumber());
|
||||
final var groupId = GroupId.unknownVersion(new byte[32]);
|
||||
assertEquals(42, signal.sendMessage("Direct", List.of(), OTHER_ACI));
|
||||
assertEquals(42, signal.sendGroupMessage("Group", List.of(), groupId.serialize()));
|
||||
assertEquals(42, signal.sendNoteToSelfMessage("Self", List.of()));
|
||||
assertEquals(Set.of(new RecipientIdentifier.Uuid(UUID.fromString(OTHER_ACI))),
|
||||
account.calls.get(0).arguments().get(1));
|
||||
assertEquals(Set.of(new RecipientIdentifier.Group(groupId)), account.calls.get(1).arguments().get(1));
|
||||
assertEquals(Set.of(RecipientIdentifier.NoteToSelf.INSTANCE), account.calls.get(2).arguments().get(1));
|
||||
assertThrows(Signal.Error.InvalidNumber.class,
|
||||
() -> signal.sendMessage("Invalid local number", List.of(), "2025550124"));
|
||||
|
||||
account.calls.clear();
|
||||
try (final var client = new DbusManagerImpl(signal, connection, busname)) {
|
||||
checkSelfOperations(client, account, new RecipientIdentifier.Uuid(UUID.fromString(ACI)));
|
||||
final var received = new LinkedBlockingQueue<MessageEnvelope>();
|
||||
client.addReceiveHandler((envelope, error) -> received.add(envelope));
|
||||
for (final var group : List.of(Optional.<GroupId>empty(), Optional.of(groupId))) {
|
||||
account.emit(messageEnvelope(group));
|
||||
final var envelope = received.poll(5, TimeUnit.SECONDS);
|
||||
assertNotNull(envelope);
|
||||
assertEquals(OTHER_ACI, envelope.sourceAddress().orElseThrow().aci().orElseThrow());
|
||||
assertEquals(Optional.empty(), envelope.sourceAddress().orElseThrow().number());
|
||||
assertEquals("Incoming", envelope.data().orElseThrow().body().orElseThrow());
|
||||
assertEquals(Optional.of(ACI),
|
||||
envelope.data().orElseThrow().mentions().getFirst().recipient().aci());
|
||||
assertEquals(group,
|
||||
envelope.data()
|
||||
.orElseThrow()
|
||||
.groupContext()
|
||||
.map(MessageEnvelope.Data.GroupContext::groupId));
|
||||
}
|
||||
}
|
||||
assertEquals(0, account.receivers.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkSelfOperations(
|
||||
final Manager client,
|
||||
final TestAccount account,
|
||||
final RecipientIdentifier.Single self
|
||||
) throws Exception {
|
||||
final Set<RecipientIdentifier> recipients = Set.of(RecipientIdentifier.NoteToSelf.INSTANCE);
|
||||
client.sendTypingMessage(TypingAction.START, recipients);
|
||||
client.sendRemoteDeleteMessage(123, recipients);
|
||||
client.sendMessageReaction("\uD83D\uDC4D", false, self, 123, recipients, false, false);
|
||||
assertEquals(List.of("sendTypingMessage", "sendRemoteDeleteMessage", "sendMessageReaction"),
|
||||
account.calls.stream().map(Call::name).toList());
|
||||
assertEquals(Set.of(self), account.calls.get(0).arguments().get(1));
|
||||
assertEquals(Set.of(self), account.calls.get(1).arguments().get(1));
|
||||
assertEquals(Set.of(self), account.calls.get(2).arguments().get(4));
|
||||
}
|
||||
|
||||
private static String selectAccount(
|
||||
final String account,
|
||||
final DBusConnection connection,
|
||||
final String busname
|
||||
) throws Exception {
|
||||
final var selected = new AtomicReference<String>();
|
||||
final var handler = new CommandHandler(null, null) {
|
||||
@Override
|
||||
public void handleLocalCommand(final LocalCommand command, final Manager manager) {
|
||||
selected.set(manager.getSelfACI());
|
||||
}
|
||||
};
|
||||
DbusCommandHandler.handleCommand(new ListGroupsCommand(), account, connection, busname, handler);
|
||||
return selected.get();
|
||||
}
|
||||
|
||||
private static String newBusname() {
|
||||
return "org.asamk.Signal.Test" + UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
|
||||
private static MessageEnvelope messageEnvelope(final Optional<GroupId> groupId) {
|
||||
final var data = new MessageEnvelope.Data(123,
|
||||
groupId.map(id -> new MessageEnvelope.Data.GroupContext(id, false, 0)),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.of("Incoming"),
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of(new MessageEnvelope.Data.Mention(new RecipientAddress(UUID.fromString(ACI)), 0, 1)),
|
||||
List.of(),
|
||||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty());
|
||||
return new MessageEnvelope(Optional.of(new RecipientAddress(UUID.fromString(OTHER_ACI))),
|
||||
1,
|
||||
123,
|
||||
0,
|
||||
0,
|
||||
false,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.of(data),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty());
|
||||
}
|
||||
|
||||
private record Call(String name, List<Object> arguments) {}
|
||||
|
||||
private static final class TestAccount {
|
||||
|
||||
private final Manager manager;
|
||||
private final List<Call> calls = new CopyOnWriteArrayList<>();
|
||||
private final List<Runnable> closedListeners = new CopyOnWriteArrayList<>();
|
||||
private final List<Manager.ReceiveMessageHandler> receivers = new CopyOnWriteArrayList<>();
|
||||
private List<Recipient> recipients = List.of();
|
||||
private List<Group> groups = List.of();
|
||||
private List<Identity> identities = List.of();
|
||||
|
||||
private TestAccount(final String number, final String aci) {
|
||||
manager = (Manager) Proxy.newProxyInstance(Manager.class.getClassLoader(),
|
||||
new Class<?>[]{Manager.class},
|
||||
(proxy, method, args) -> {
|
||||
if (method.isDefault()) {
|
||||
return InvocationHandler.invokeDefault(proxy, method, args);
|
||||
}
|
||||
return switch (method.getName()) {
|
||||
case "getSelfNumber" -> number;
|
||||
case "getSelfACI" -> aci;
|
||||
case "getLinkedDevices" -> List.of();
|
||||
case "getGroups" -> groups;
|
||||
case "getGroup" -> groups.getFirst();
|
||||
case "getIdentities" -> identities;
|
||||
case "getRecipients" -> recipients;
|
||||
case "isContactBlocked" -> false;
|
||||
case "getContactOrProfileName" -> "Test contact";
|
||||
case "addAddressChangedListener" -> null;
|
||||
case "addClosedListener" -> {
|
||||
closedListeners.add((Runnable) args[0]);
|
||||
yield null;
|
||||
}
|
||||
case "close" -> {
|
||||
closedListeners.forEach(Runnable::run);
|
||||
closedListeners.clear();
|
||||
yield null;
|
||||
}
|
||||
case "addReceiveHandler" -> {
|
||||
receivers.add((Manager.ReceiveMessageHandler) args[0]);
|
||||
yield null;
|
||||
}
|
||||
case "removeReceiveHandler" -> {
|
||||
receivers.remove(args[0]);
|
||||
yield null;
|
||||
}
|
||||
case "sendMessage", "sendTypingMessage", "sendRemoteDeleteMessage",
|
||||
"sendMessageReaction" -> {
|
||||
calls.add(new Call(method.getName(), List.of(args)));
|
||||
yield new SendMessageResults(42, Map.of());
|
||||
}
|
||||
case "hashCode" -> System.identityHashCode(proxy);
|
||||
case "equals" -> proxy == args[0];
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
private void emit(final MessageEnvelope envelope) {
|
||||
receivers.forEach(receiver -> receiver.handleMessage(envelope, null));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -10,6 +10,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@ -44,7 +45,7 @@ class SseInitialFlushTest {
|
||||
void setUp() throws Exception {
|
||||
port = freePort();
|
||||
final Manager manager = ManagerMock.create("+10000000000");
|
||||
handler = new HttpServerHandler(new InetSocketAddress("127.0.0.1", port), manager);
|
||||
handler = new HttpServerHandler(new InetSocketAddress("127.0.0.1", port), Duration.ofSeconds(15), manager);
|
||||
handler.init();
|
||||
}
|
||||
|
||||
|
||||
201
src/test/java/org/asamk/signal/http/SseReplayTest.java
Normal file
201
src/test/java/org/asamk/signal/http/SseReplayTest.java
Normal file
@ -0,0 +1,201 @@
|
||||
package org.asamk.signal.http;
|
||||
|
||||
import org.asamk.signal.testutil.ManagerMock;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests the event ids, the replay of missed events for a client that reconnects with a
|
||||
* Last-Event-ID header, and the configurable keep-alive interval of GET /api/v1/events.
|
||||
*/
|
||||
class SseReplayTest {
|
||||
|
||||
private final ManagerMock.State state = new ManagerMock.State();
|
||||
private HttpServerHandler handler;
|
||||
private int port;
|
||||
|
||||
private void start(final Duration keepAliveInterval) throws Exception {
|
||||
try (var ss = new ServerSocket(0)) {
|
||||
port = ss.getLocalPort();
|
||||
}
|
||||
handler = new HttpServerHandler(new InetSocketAddress("127.0.0.1", port),
|
||||
keepAliveInterval,
|
||||
ManagerMock.create("+10000000000", state));
|
||||
handler.init();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (handler != null) {
|
||||
handler.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventsHaveIds() throws Exception {
|
||||
start(Duration.ofSeconds(15));
|
||||
try (var client = connect(null, 2)) {
|
||||
receive("one");
|
||||
|
||||
final var event = client.next();
|
||||
assertNotNull(event.id());
|
||||
assertTrue(event.data().contains("\"one\""));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void reconnectWithLastEventIdReplaysMissedEvents() throws Exception {
|
||||
start(Duration.ofSeconds(15));
|
||||
final String firstId;
|
||||
try (var client = connect(null, 2)) {
|
||||
receive("one");
|
||||
receive("two");
|
||||
firstId = client.next().id();
|
||||
client.next();
|
||||
}
|
||||
receive("three");
|
||||
|
||||
try (var client = connect(firstId, 3)) {
|
||||
assertTrue(client.next().data().contains("\"two\""));
|
||||
assertTrue(client.next().data().contains("\"three\""));
|
||||
|
||||
receive("four");
|
||||
assertTrue(client.next().data().contains("\"four\""));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownLastEventIdReplaysAllBufferedEvents() throws Exception {
|
||||
start(Duration.ofSeconds(15));
|
||||
try (var client = connect(null, 2)) {
|
||||
receive("one");
|
||||
receive("two");
|
||||
client.next();
|
||||
client.next();
|
||||
}
|
||||
|
||||
try (var client = connect("0-1", 3)) {
|
||||
assertTrue(client.next().data().contains("\"one\""));
|
||||
assertTrue(client.next().data().contains("\"two\""));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectWithoutLastEventIdReplaysNothing() throws Exception {
|
||||
start(Duration.ofSeconds(15));
|
||||
try (var client = connect(null, 2)) {
|
||||
receive("one");
|
||||
client.next();
|
||||
}
|
||||
|
||||
try (var client = connect(null, 3)) {
|
||||
receive("two");
|
||||
assertTrue(client.next().data().contains("\"two\""));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepAliveIntervalIsConfigurable() throws Exception {
|
||||
start(Duration.ofSeconds(1));
|
||||
try (var client = connect(null, 2)) {
|
||||
// The read timeout is 5 s, the default interval of 15 s would fail here
|
||||
assertEquals(":", client.nextLine());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Passes a message to the registered receive handlers, like the manager's receive thread does.
|
||||
*/
|
||||
private void receive(final String message) {
|
||||
final var exception = new RuntimeException(message);
|
||||
for (final var receiveHandler : state.receiveHandlers) {
|
||||
receiveHandler.handleMessage(null, exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects and waits until the server has registered the client, i.e. until the manager has
|
||||
* seen the given total number of addReceiveHandler calls.
|
||||
*/
|
||||
private SseClient connect(final String lastEventId, final int addReceiveHandlerCount) throws Exception {
|
||||
final var client = new SseClient(port, lastEventId);
|
||||
final var deadline = System.nanoTime() + Duration.ofSeconds(5).toNanos();
|
||||
while (state.addReceiveHandlerCount.get() < addReceiveHandlerCount) {
|
||||
if (System.nanoTime() > deadline) {
|
||||
client.close();
|
||||
throw new AssertionError("Client was not registered in time");
|
||||
}
|
||||
Thread.sleep(10);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
private record Event(String id, String data) {}
|
||||
|
||||
private static final class SseClient implements AutoCloseable {
|
||||
|
||||
private final HttpURLConnection connection;
|
||||
private final BufferedReader reader;
|
||||
|
||||
SseClient(final int port, final String lastEventId) throws IOException {
|
||||
final var url = URI.create("http://127.0.0.1:" + port + "/api/v1/events").toURL();
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestProperty("Accept", "text/event-stream");
|
||||
if (lastEventId != null) {
|
||||
connection.setRequestProperty("Last-Event-ID", lastEventId);
|
||||
}
|
||||
connection.setConnectTimeout(2_000);
|
||||
connection.setReadTimeout(5_000);
|
||||
assertEquals(200, connection.getResponseCode());
|
||||
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
String nextLine() throws IOException {
|
||||
final var line = reader.readLine();
|
||||
if (line == null) {
|
||||
throw new EOFException();
|
||||
}
|
||||
return line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the next event, skipping keep-alive comments.
|
||||
*/
|
||||
Event next() throws IOException {
|
||||
String id = null;
|
||||
String data = null;
|
||||
while (true) {
|
||||
final var line = nextLine();
|
||||
if (line.isEmpty()) {
|
||||
if (data != null) {
|
||||
return new Event(id, data);
|
||||
}
|
||||
} else if (line.startsWith("id:")) {
|
||||
id = line.substring("id:".length());
|
||||
} else if (line.startsWith("data:")) {
|
||||
data = line.substring("data:".length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,71 @@
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.commands.ListAccountsCommand;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.internal.MultiAccountManagerImpl;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class NumberlessAccountOutputTest {
|
||||
|
||||
private static final String ACI = "11111111-1111-4111-8111-111111111111";
|
||||
|
||||
@Test
|
||||
void receiveEventsIdentifyNumberlessAccountByAci() {
|
||||
final var output = new AtomicReference<Object>();
|
||||
new JsonReceiveMessageHandler(manager(null), output::set).handleMessage(null, null);
|
||||
assertEquals(ACI, ((Map<?, ?>) output.get()).get("account"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void receiveEventsKeepNumberedAccountIdentifiers() {
|
||||
final var output = new AtomicReference<Object>();
|
||||
new JsonReceiveMessageHandler(manager("+12025550123"), output::set).handleMessage(null, null);
|
||||
assertEquals("+12025550123", ((Map<?, ?>) output.get()).get("account"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAccountsIncludesAciWithoutPuttingItInTheNumberField() throws Exception {
|
||||
final var numberless = manager(null);
|
||||
final var numbered = manager("+12025550123");
|
||||
final var multi = new MultiAccountManagerImpl(List.of(numberless, numbered), null);
|
||||
final var output = new AtomicReference<Object>();
|
||||
new ListAccountsCommand().handleCommand(Map.of(), multi, output::set);
|
||||
final var accounts = new ObjectMapper().valueToTree(output.get());
|
||||
assertEquals(2, accounts.size());
|
||||
for (final var account : accounts) {
|
||||
assertEquals(ACI, account.path("aci").asText());
|
||||
assertFalse(ACI.equals(account.path("number").asText()));
|
||||
}
|
||||
assertSame(numbered, multi.getManager("+12025550123"));
|
||||
}
|
||||
|
||||
private static Manager manager(final String number) {
|
||||
return (Manager) Proxy.newProxyInstance(Manager.class.getClassLoader(),
|
||||
new Class<?>[]{Manager.class},
|
||||
(proxy, method, args) -> {
|
||||
if (method.isDefault()) {
|
||||
return InvocationHandler.invokeDefault(proxy, method, args);
|
||||
}
|
||||
return switch (method.getName()) {
|
||||
case "getSelfNumber" -> number;
|
||||
case "getSelfACI" -> ACI;
|
||||
case "hashCode" -> System.identityHashCode(proxy);
|
||||
case "equals" -> proxy == args[0];
|
||||
case "addClosedListener" -> null;
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -67,16 +67,13 @@ class SubscribeCallEventsTest {
|
||||
|
||||
final List<Manager> managers;
|
||||
final List<Consumer<Manager>> addedHandlers = new ArrayList<>();
|
||||
final List<Consumer<Manager>> removedHandlers = new ArrayList<>();
|
||||
int addedHandlerRegistrationCount;
|
||||
|
||||
StubMultiAccountManager(List<Manager> managers) {
|
||||
this.managers = new ArrayList<>(managers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAccountNumbers() {
|
||||
return managers.stream().map(Manager::getSelfNumber).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Manager> getManagers() {
|
||||
return managers;
|
||||
@ -84,11 +81,23 @@ class SubscribeCallEventsTest {
|
||||
|
||||
@Override
|
||||
public void addOnManagerAddedHandler(Consumer<Manager> handler) {
|
||||
addedHandlerRegistrationCount++;
|
||||
addedHandlers.add(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeOnManagerAddedHandler(Consumer<Manager> handler) {
|
||||
addedHandlers.remove(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOnManagerRemovedHandler(Consumer<Manager> handler) {
|
||||
removedHandlers.add(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeOnManagerRemovedHandler(Consumer<Manager> handler) {
|
||||
removedHandlers.remove(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -228,8 +237,9 @@ class SubscribeCallEventsTest {
|
||||
|
||||
assertEquals(1, manager1.state().addCallEventListenerCount.get(), "manager1 should have one listener");
|
||||
assertEquals(1, manager2.state().addCallEventListenerCount.get(), "manager2 should have one listener");
|
||||
// Also registers an onManagerAdded handler for receive and one for call events
|
||||
assertEquals(2, multi.addedHandlers.size(), "should register onManagerAdded handlers");
|
||||
assertEquals(2, multi.addedHandlerRegistrationCount, "should register onManagerAdded handlers");
|
||||
assertEquals(0, multi.addedHandlers.size(), "should remove onManagerAdded handlers after connection closes");
|
||||
assertEquals(0, multi.removedHandlers.size(), "should not register onManagerRemoved handlers in manual mode");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -268,4 +278,17 @@ class SubscribeCallEventsTest {
|
||||
manager1.state().addCallEventListenerCount.get(),
|
||||
"call events should not be auto-subscribed in multi mode");
|
||||
}
|
||||
|
||||
@Test
|
||||
void multiAccountConnectionRemovesAllManagerHandlersWhenReceiveIsEnabled() {
|
||||
var manager = createManager("+15551111111");
|
||||
var multi = new StubMultiAccountManager(List.of(manager.manager()));
|
||||
var handler = new SignalJsonRpcDispatcherHandler(new CapturingJsonWriter(), () -> null, false);
|
||||
|
||||
handler.handleConnection(multi);
|
||||
|
||||
assertEquals(3, multi.addedHandlerRegistrationCount);
|
||||
assertEquals(0, multi.addedHandlers.size(), "should remove all onManagerAdded handlers after close");
|
||||
assertEquals(0, multi.removedHandlers.size(), "should remove onManagerRemoved handler after close");
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public final class ManagerMock {
|
||||
@ -24,7 +25,8 @@ public final class ManagerMock {
|
||||
public final AtomicInteger addCallEventListenerCount = new AtomicInteger(0);
|
||||
public final AtomicInteger removeCallEventListenerCount = new AtomicInteger(0);
|
||||
|
||||
public final List<Manager.ReceiveMessageHandler> receiveHandlers = new ArrayList<>();
|
||||
// Written by server threads while tests read it
|
||||
public final List<Manager.ReceiveMessageHandler> receiveHandlers = new CopyOnWriteArrayList<>();
|
||||
public final AtomicInteger addReceiveHandlerCount = new AtomicInteger(0);
|
||||
public final AtomicInteger removeReceiveHandlerCount = new AtomicInteger(0);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user