Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal Headers and Compression #191

Merged
merged 13 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ DATABASE_URL=postgres://root:root@127.0.0.1:5432/signal_db
DATABASE_URL_TEST=postgres://test:test@127.0.0.1:3306/signal_db_test
REDIS_URL=redis://127.0.0.1:6379
SERVER_ADDRESS=127.0.0.1
HTTPS_PORT=4444
HTTP_PORT=8888
HTTPS_PORT=443
HTTP_PORT=80
Comment on lines +17 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doens't work on linux

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you sudo it does

```

if you are on linux and do not want to sudo the program, you can change the HTTPS and HTTP ports to your liking.

3. Go into `server/cert`
4. Generate certificates by running the following
```zsh
Expand All @@ -36,7 +39,8 @@ cargo run
1. Go into `client`
2. Create a file called `.env` with the following content
```
SERVER_URL=https://localhost:4444
HTTPS_SERVER_URL=https://localhost:443
HTTP_SERVER_URL=http://localhost:80
DATABASE_URL=sqlite://./client/client_db/dev.db
DATABASE_URL_TEST=sqlite::memory:
CERT_PATH=../server/cert/rootCA.crt
Expand All @@ -53,6 +57,9 @@ cargo run
```
As an example, two clients should then be created and messages between them will be send.

### TLS Configuration
If you do not want to use HTTPS and WSS you can run the server and client with `--no-tls` and then they will just communicate over HTTP and WS

## Clean up
### Resetting the server database
1. Go into `server`
Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rustls = { version = "0.23.15", features = ["ring"] }
rustls-pemfile = "2.2.0"
derive_more = { version = "1.0.0", features = ["display", "error", "from"] }
display = "0.1.2"
flate2 = "1.0.35"

[build-dependencies]
tonic-build = "0.12.3"
65 changes: 31 additions & 34 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {
phone_number: String,
database_url: &str,
server_url: &str,
cert_path: &str,
cert_path: &Option<String>,
) -> Result<Client<Device, SignalServer>> {
let mut csprng = OsRng;
let aci_registration_id = OsRng.gen_range(1..16383);
Expand Down Expand Up @@ -214,7 +214,7 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {

pub async fn login(
database_url: &str,
cert_path: &str,
cert_path: &Option<String>,
server_url: &str,
) -> Result<Client<Device, SignalServer>> {
let pool = Client::<T, U>::connect_to_db(database_url, false).await?;
Expand Down Expand Up @@ -251,12 +251,18 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {
))
}

pub async fn send_message(
&mut self,
message: &str,
service_id: &ServiceId,
alias: &str,
) -> Result<()> {
pub async fn disconnect(&mut self) {
self.server_api.disconnect().await;
}

pub async fn send_message(&mut self, message: &str, alias: &str) -> Result<()> {
let service_id = self
.storage
.device
.get_service_id_by_nickname(alias)
.await
.map_err(DatabaseError::from)?;

let content = Content::builder()
.data_message(
DataMessage::builder()
Expand All @@ -271,28 +277,10 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {

let timestamp = SystemTime::now();

// Update the contact.
let to = match self.contact_manager.get_contact(service_id) {
Err(_) => {
self.add_contact(alias, service_id)
.await
.expect("Can add contact that does not exist yet");

let device_ids = self.get_new_device_ids(service_id).await?;

self.update_contact(alias, device_ids).await?;

self.contact_manager
.get_contact(service_id)
.expect("Can get contact that was just added.")
}
Ok(contact) => contact,
};

let msgs = encrypt(
&mut self.storage.protocol_store.identity_key_store,
&mut self.storage.protocol_store.session_store,
to,
self.contact_manager.get_contact(&service_id)?,
pad_message(content.encode_to_vec().as_ref()).as_ref(),
timestamp,
)
Expand Down Expand Up @@ -320,20 +308,20 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {
content: BASE64_STANDARD.encode(msg.1.serialize()),
})
.collect(),
online: false,
urgent: true,
online: true,
urgent: false,
timestamp: timestamp
.duration_since(UNIX_EPOCH)
.expect("can get the time since epoch")
.as_secs(),
};

match self.server_api.send_msg(&msgs, service_id).await {
match self.server_api.send_msg(&msgs, &service_id).await {
Ok(_) => Ok(()),
Err(_) => {
let device_ids = self.get_new_device_ids(service_id).await?;
let device_ids = self.get_new_device_ids(&service_id).await?;
self.update_contact(alias, device_ids).await?;
self.server_api.send_msg(&msgs, service_id).await
self.server_api.send_msg(&msgs, &service_id).await
}
}
}
Expand Down Expand Up @@ -374,9 +362,13 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {
}

pub async fn add_contact(&mut self, alias: &str, service_id: &ServiceId) -> Result<()> {
if self.contact_manager.get_contact(&service_id).is_ok() {
return Ok(());
}
self.contact_manager
.add_contact(&service_id)
.map_err(SignalClientError::ContactManagerError)?;

let contact = self
.contact_manager
.get_contact(&service_id)
Expand All @@ -392,7 +384,12 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {
.device
.insert_service_id_for_nickname(alias, &service_id)
.await
.map_err(|err| DatabaseError::Custom(Box::new(err)).into())
.map_err(|err| {
SignalClientError::DatabaseError(DatabaseError::Custom(Box::new(err)))
})?;

let device_ids = self.get_new_device_ids(&service_id).await?;
self.update_contact(alias, device_ids).await
}

pub async fn remove_contact(&mut self, alias: &str) -> Result<()> {
Expand All @@ -414,7 +411,7 @@ impl<T: ClientDB, U: SignalServerAPI> Client<T, U> {
.map_err(|err| DatabaseError::Custom(Box::new(err)).into())
}

pub async fn update_contact(&mut self, alias: &str, device_ids: Vec<DeviceId>) -> Result<()> {
async fn update_contact(&mut self, alias: &str, device_ids: Vec<DeviceId>) -> Result<()> {
let service_id = self
.storage
.device
Expand Down
Loading
Loading