libsignal-net: support for User-Agent header

This commit is contained in:
Sergey Skrobotov
2024-05-01 20:02:08 -07:00
parent b1eee0a551
commit ab733bf00a
23 changed files with 127 additions and 66 deletions

View File

@@ -35,9 +35,9 @@ public class Network {
*/
private final Svr3 svr3;
public Network(Environment env) {
public Network(Environment env, String userAgent) {
this.tokioAsyncContext = new TokioAsyncContext();
this.connectionManager = new ConnectionManager(env);
this.connectionManager = new ConnectionManager(env, userAgent);
this.svr3 = new Svr3(this);
}
@@ -45,7 +45,7 @@ public class Network {
* Sets the proxy host to be used for all new connections (until overridden).
*
* <p>Sets a domain name and port to be used to proxy all new outgoing connections. The proxy can
* be overridden by calling this method again or unset by calling {@link clearProxy}.
* be overridden by calling this method again or unset by calling {@link #clearProxy}.
*/
public void setProxy(String host, int port) {
this.connectionManager.setProxy(host, port);
@@ -54,7 +54,7 @@ public class Network {
/**
* Ensures that future connections will be made directly, not through a proxy.
*
* <p>Clears any proxy configuration set via {@link setProxy}. If none was set, calling this
* <p>Clears any proxy configuration set via {@link #setProxy}. If none was set, calling this
* method is a no-op.
*/
public void clearProxy() {
@@ -116,8 +116,8 @@ public class Network {
}
static class ConnectionManager extends NativeHandleGuard.SimpleOwner {
private ConnectionManager(Environment env) {
super(Native.ConnectionManager_new(env.value));
private ConnectionManager(Environment env, String userAgent) {
super(Native.ConnectionManager_new(env.value, userAgent));
}
private void setProxy(String host, int port) {

View File

@@ -16,6 +16,8 @@ import org.signal.libsignal.util.TestEnvironment;
public class ChatServiceTest {
private static final String USER_AGENT = "test";
private static final int EXPECTED_STATUS = 200;
private static final String EXPECTED_MESSAGE = "OK";
@@ -24,7 +26,7 @@ public class ChatServiceTest {
private static final Map<String, String> EXPECTED_HEADERS =
Map.of(
"user-agent", "test",
"content-type", "application/octet-stream",
"forwarded", "1.1.1.1");
@Test
@@ -106,7 +108,7 @@ public class ChatServiceTest {
final String PROXY_SERVER = TestEnvironment.get("LIBSIGNAL_TESTING_PROXY_SERVER");
Assume.assumeNotNull(PROXY_SERVER);
final Network net = new Network(Network.Environment.STAGING);
final Network net = new Network(Network.Environment.STAGING, USER_AGENT);
final ChatService chat = net.createChatService("", "");
// Just make sure we can connect.
chat.connectUnauthenticated().get();
@@ -119,7 +121,7 @@ public class ChatServiceTest {
Assume.assumeNotNull(PROXY_SERVER);
// The default TLS proxy config doesn't support staging, so we connect to production.
final Network net = new Network(Network.Environment.PRODUCTION);
final Network net = new Network(Network.Environment.PRODUCTION, USER_AGENT);
final String[] proxyComponents = PROXY_SERVER.split(":");
switch (proxyComponents.length) {
case 1:

View File

@@ -21,6 +21,8 @@ import org.signal.libsignal.util.TestEnvironment;
public class Svr3Test {
private static final String USER_AGENT = "test";
private final byte[] STORED_SECRET =
Hex.fromStringCondensedAssert(
"d2ae1668ac8a2bfd6170498332babad7cd72b9314631559a361310eee0a8adc6");
@@ -49,7 +51,7 @@ public class Svr3Test {
@Test
public void backupAndRestore() throws Exception {
Network net = new Network(Network.Environment.STAGING);
Network net = new Network(Network.Environment.STAGING, USER_AGENT);
byte[] restored =
net.svr3()
.backup(STORED_SECRET, "password", 2, this.auth)
@@ -60,7 +62,7 @@ public class Svr3Test {
@Test
public void noMoreTries() throws Exception {
Network net = new Network(Network.Environment.STAGING);
Network net = new Network(Network.Environment.STAGING, USER_AGENT);
// Backup and first restore should succeed
byte[] shareSet = net.svr3().backup(STORED_SECRET, "password", 1, this.auth).get();
net.svr3().restore("password", shareSet, this.auth).get();
@@ -75,7 +77,7 @@ public class Svr3Test {
@Test
public void failedRestore() throws Exception {
Network net = new Network(Network.Environment.STAGING);
Network net = new Network(Network.Environment.STAGING, USER_AGENT);
byte[] shareSet = net.svr3().backup(STORED_SECRET, "password", 1, this.auth).get();
try {
net.svr3().restore("wrong password", shareSet, this.auth).get();
@@ -87,7 +89,7 @@ public class Svr3Test {
@Test
public void zeroTries() throws Exception {
Network net = new Network(Network.Environment.STAGING);
Network net = new Network(Network.Environment.STAGING, USER_AGENT);
assertThrows(
IllegalArgumentException.class,
() -> net.svr3().backup(STORED_SECRET, "password", 0, this.auth).get());
@@ -95,7 +97,7 @@ public class Svr3Test {
@Test
public void badSecret() throws Exception {
Network net = new Network(Network.Environment.STAGING);
Network net = new Network(Network.Environment.STAGING, USER_AGENT);
try {
net.svr3().backup(new byte[31], "password", 1, this.auth).get();
} catch (ExecutionException ex) {
@@ -106,7 +108,7 @@ public class Svr3Test {
@Test
public void badShareSet() throws Exception {
Network net = new Network(Network.Environment.STAGING);
Network net = new Network(Network.Environment.STAGING, USER_AGENT);
byte[] shareSet = net.svr3().backup(STORED_SECRET, "password", 1, this.auth).get();
shareSet[0] ^= 0xff;
try {