Set libsignal network proxy to match java proxy

Fixes #1523
This commit is contained in:
AsamK 2025-02-07 18:29:59 +01:00
parent 8aa71c132f
commit 3cd07ae9cd
2 changed files with 55 additions and 2 deletions

View file

@ -2,9 +2,12 @@ package org.asamk.signal.manager.internal;
import org.asamk.signal.manager.config.ServiceConfig; import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.util.Utils;
import org.signal.libsignal.metadata.certificate.CertificateValidator; import org.signal.libsignal.metadata.certificate.CertificateValidator;
import org.signal.libsignal.net.Network; import org.signal.libsignal.net.Network;
import org.signal.libsignal.zkgroup.profiles.ClientZkProfileOperations; import org.signal.libsignal.zkgroup.profiles.ClientZkProfileOperations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.SignalServiceAccountManager; import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.SignalServiceDataStore; import org.whispersystems.signalservice.api.SignalServiceDataStore;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver; import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
@ -31,6 +34,9 @@ import org.whispersystems.signalservice.internal.push.PushServiceSocket;
import org.whispersystems.signalservice.internal.websocket.OkHttpWebSocketConnection; import org.whispersystems.signalservice.internal.websocket.OkHttpWebSocketConnection;
import org.whispersystems.signalservice.internal.websocket.WebSocketConnection; import org.whispersystems.signalservice.internal.websocket.WebSocketConnection;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -38,6 +44,8 @@ import java.util.function.Supplier;
public class SignalDependencies { public class SignalDependencies {
private static final Logger logger = LoggerFactory.getLogger(SignalDependencies.class);
private final Object LOCK = new Object(); private final Object LOCK = new Object();
private final ServiceEnvironmentConfig serviceEnvironmentConfig; private final ServiceEnvironmentConfig serviceEnvironmentConfig;
@ -129,8 +137,34 @@ public class SignalDependencies {
} }
public Network getLibSignalNetwork() { public Network getLibSignalNetwork() {
return getOrCreate(() -> libSignalNetwork, return getOrCreate(() -> libSignalNetwork, () -> {
() -> libSignalNetwork = new Network(serviceEnvironmentConfig.netEnvironment(), userAgent)); libSignalNetwork = new Network(serviceEnvironmentConfig.netEnvironment(), userAgent);
setSignalNetworkProxy(libSignalNetwork);
});
}
private void setSignalNetworkProxy(Network libSignalNetwork) {
final var proxy = Utils.getHttpsProxy();
if (proxy.address() instanceof InetSocketAddress addr) {
switch (proxy.type()) {
case Proxy.Type.DIRECT -> {
}
case Proxy.Type.HTTP -> {
try {
libSignalNetwork.setProxy("http", addr.getHostName(), addr.getPort(), null, null);
} catch (IOException e) {
logger.warn("Failed to set http proxy", e);
}
}
case Proxy.Type.SOCKS -> {
try {
libSignalNetwork.setProxy("socks", addr.getHostName(), addr.getPort(), null, null);
} catch (IOException e) {
logger.warn("Failed to set socks proxy", e);
}
}
}
}
} }
public SignalServiceAccountManager getAccountManager() { public SignalServiceAccountManager getAccountManager() {

View file

@ -15,6 +15,10 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
@ -202,4 +206,19 @@ public class Utils {
public static String nullIfEmpty(String string) { public static String nullIfEmpty(String string) {
return string == null || string.isEmpty() ? null : string; return string == null || string.isEmpty() ? null : string;
} }
public static Proxy getHttpsProxy() {
final URI uri;
try {
uri = new URI("https://example");
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
final var proxies = ProxySelector.getDefault().select(uri);
if (proxies.isEmpty()) {
return Proxy.NO_PROXY;
} else {
return proxies.getFirst();
}
}
} }