Skip to content

Commit

Permalink
implement Devices.GetDevices (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiainen authored Oct 20, 2023
1 parent 9d304b4 commit 0db3666
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.signal.libsignal.chat;

import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.chat.device.GetDevicesRequest;
import org.signal.chat.device.GetDevicesResponse;
import org.signal.libsignal.internal.Native;
import org.signal.libsignal.internal.NativeHandleGuard;

public class DeviceClient implements NativeHandleGuard.Owner {

private static final String DEFAULT_TARGET = "https://grpcproxy.gluonhq.net:443";

private final long unsafeHandle;

public DeviceClient() {
this(DEFAULT_TARGET);
}

public DeviceClient(String target) {
this.unsafeHandle = Native.DeviceClient_New(target);
}

@Override @SuppressWarnings("deprecation")
protected void finalize() {
Native.ProfileClient_Destroy(this.unsafeHandle);
}

public long unsafeNativeHandleWithoutGuard() {
return this.unsafeHandle;
}

public GetDevicesResponse getDevices(GetDevicesRequest request, String authorization) throws SignalChatCommunicationFailureException {
try (NativeHandleGuard guard = new NativeHandleGuard(this)) {
byte[] serializedResponse = Native.DeviceClient_GetDevices(guard.nativeHandle(), request.toByteArray(), authorization);
return GetDevicesResponse.parseFrom(serializedResponse);
} catch (InvalidProtocolBufferException e) {
throw new SignalChatCommunicationFailureException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.signal.libsignal.profile;
package org.signal.libsignal.chat;

import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.chat.profile.GetVersionedProfileRequest;
import org.signal.chat.profile.GetVersionedProfileResponse;
import org.signal.libsignal.SignalChatCommunicationFailureException;
import org.signal.libsignal.internal.Native;
import org.signal.libsignal.internal.NativeHandleGuard;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.signal.libsignal;
package org.signal.libsignal.chat;

public class SignalChatCommunicationFailureException extends Exception {
public SignalChatCommunicationFailureException(String msg) { super(msg); }
Expand Down
5 changes: 4 additions & 1 deletion java/shared/java/org/signal/libsignal/internal/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.signal.libsignal.internal;

import org.signal.chat.profile.GetVersionedProfileResponse;
import org.signal.libsignal.grpc.GrpcReplyListener;
import org.signal.libsignal.protocol.message.CiphertextMessage;
import org.signal.libsignal.protocol.state.IdentityKeyStore;
Expand Down Expand Up @@ -178,6 +177,10 @@ private Native() {}
public static native byte[] DecryptionErrorMessage_GetSerialized(long obj);
public static native long DecryptionErrorMessage_GetTimestamp(long obj);

public static native void DeviceClient_Destroy(long handle);
public static native byte[] DeviceClient_GetDevices(long deviceClient, byte[] request, String authorization);
public static native long DeviceClient_New(String target);

public static native byte[] DeviceTransfer_GenerateCertificate(byte[] privateKey, String name, int daysToExpire);
public static native byte[] DeviceTransfer_GeneratePrivateKey();

Expand Down
1 change: 0 additions & 1 deletion rust/bridge/jni/bin/Native.java.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.signal.libsignal.internal;

import org.signal.chat.profile.GetVersionedProfileResponse;
import org.signal.libsignal.grpc.GrpcReplyListener;
import org.signal.libsignal.protocol.message.CiphertextMessage;
import org.signal.libsignal.protocol.state.IdentityKeyStore;
Expand Down
1 change: 1 addition & 0 deletions rust/bridge/shared/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
// SPDX-License-Identifier: AGPL-3.0-only
//

pub mod device;
pub mod profile;
27 changes: 27 additions & 0 deletions rust/bridge/shared/src/chat/device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Copyright 2023 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

use libsignal_bridge_macros::*;
use signal_chat::Result;
use signal_chat::device::DeviceClient;

use crate::support::*;
use crate::*;

bridge_handle!(DeviceClient, clone = false, mut = true);

#[bridge_fn(ffi = false, node = false)]
pub fn DeviceClient_New(target: String) -> Result<DeviceClient> {
DeviceClient::new(target)
}

#[bridge_fn(ffi = false, node = false)]
pub fn DeviceClient_GetDevices(
device_client: &mut DeviceClient,
request: &[u8],
authorization: String,
) -> Result<Vec<u8>> {
device_client.get_devices(request, authorization)
}
8 changes: 8 additions & 0 deletions rust/chat/src/device.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Copyright 2023 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

pub mod client;

pub use client::DeviceClient;
64 changes: 64 additions & 0 deletions rust/chat/src/device/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Copyright 2023 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

use prost::Message;

use crate::error::{Error, Result};
use crate::proto::signal::device::{devices_client, GetDevicesRequest};
use std::panic::RefUnwindSafe;

pub struct DeviceClient {
target: String,
pub tokio_runtime: tokio::runtime::Runtime,
}

impl RefUnwindSafe for DeviceClient {}

impl DeviceClient {
pub fn new(target: String) -> Result<Self> {
Ok(DeviceClient {
target,
tokio_runtime: tokio::runtime::Builder::new_multi_thread()
.enable_io()
.enable_time()
.build()
.map_err(|e| Error::InvalidArgument(format!("tokio.create_runtime: {:?}", e)))?,
})
}

pub fn target(&mut self, target: &str) {
self.target = target.to_owned();
}

pub fn get_devices(&self, request: &[u8], authorization: String) -> Result<Vec<u8>> {
self.tokio_runtime
.block_on(async { self.async_get_devices(request, authorization).await })
}

async fn async_get_devices(&self, request: &[u8], authorization: String) -> Result<Vec<u8>> {
let channel = tonic::transport::Channel::from_shared(self.target.clone())
.map_err(|e| Error::InvalidArgument(format!("devices_client.connect: {:?}", e)))?
.connect()
.await
.map_err(|e| Error::InvalidArgument(format!("devices_client.connect: {:?}", e)))?;

let mut devices_client = devices_client::DevicesClient::with_interceptor(channel, move |mut req: tonic::Request<()>| {
req.metadata_mut()
.insert("authorization", tonic::metadata::MetadataValue::try_from(&authorization).unwrap());
Ok(req)
});

let request: GetDevicesRequest = Message::decode(&request[..])
.map_err(|e| Error::InvalidArgument(format!("devices_client.decode: {:?}", e)))?;

let response = devices_client
.get_devices(request)
.await
.map_err(|e| Error::InvalidArgument(format!("devices_client.get_devices: {:?}", e)))?;

let response = response.into_inner();
Ok(response.encode_to_vec())
}
}
1 change: 1 addition & 0 deletions rust/chat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

mod error;
pub mod device;
pub mod profile;
mod proto;

Expand Down

0 comments on commit 0db3666

Please sign in to comment.