mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
convert mtxchat to use tls.root_store()
a ureq::Agent is configured to use tls.root_store(), and used for all comms with the matrix server
This commit is contained in:
parent
c22419f7c7
commit
a06d84d8a9
4 changed files with 53 additions and 33 deletions
|
|
@ -36,8 +36,8 @@ percent-encoding = "2.2"
|
|||
rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]}
|
||||
rustls = { version = "0.21.7" }
|
||||
serde = { version = "1.0", features = [ "derive" ] }
|
||||
|
||||
|
||||
tls = { path = "../../libs/tls" }
|
||||
ureq = { version = "2.7.1", features = ["json"] }
|
||||
|
||||
[features]
|
||||
default = []
|
||||
|
|
|
|||
|
|
@ -8,10 +8,13 @@ use chat::{Chat, ChatOp};
|
|||
use locales::t;
|
||||
use modals::Modals;
|
||||
use pddb::Pddb;
|
||||
use std::sync::Arc;
|
||||
use std::fmt::Write as _;
|
||||
use std::io::{Error, ErrorKind, Read, Write as StdWrite};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use tls::Tls;
|
||||
use trng::*;
|
||||
use ureq::Agent;
|
||||
use xous_ipc::Buffer;
|
||||
|
||||
/// PDDB Dict for mtxchat keys
|
||||
|
|
@ -69,6 +72,7 @@ pub struct MtxChat<'a> {
|
|||
user_id: String,
|
||||
user_name: String,
|
||||
user_domain: String,
|
||||
agent: Agent,
|
||||
token: String,
|
||||
logged_in: bool,
|
||||
room_id: String,
|
||||
|
|
@ -88,6 +92,7 @@ impl<'a> MtxChat<'a> {
|
|||
let trng = Trng::new(&xns).unwrap();
|
||||
let pddb = pddb::Pddb::new();
|
||||
pddb.try_mount();
|
||||
let tls = Tls::new();
|
||||
MtxChat {
|
||||
chat: chat,
|
||||
trng: trng,
|
||||
|
|
@ -96,6 +101,9 @@ impl<'a> MtxChat<'a> {
|
|||
user_id: EMPTY.to_string(),
|
||||
user_name: EMPTY.to_string(),
|
||||
user_domain: DOMAIN_MATRIX.to_string(),
|
||||
agent: ureq::builder()
|
||||
.tls_config(Arc::new(tls.client_config()))
|
||||
.build(),
|
||||
token: EMPTY.to_string(),
|
||||
logged_in: false,
|
||||
room_id: EMPTY.to_string(),
|
||||
|
|
@ -290,7 +298,7 @@ impl<'a> MtxChat<'a> {
|
|||
)
|
||||
.expect("failed to write server");
|
||||
if self.token.len() > 0 {
|
||||
if let Some(user_id) = web::whoami(&server, &self.token) {
|
||||
if let Some(user_id) = web::whoami(&server, &self.token, &mut self.agent) {
|
||||
let i = match user_id.find('@') {
|
||||
Some(index) => index + 1,
|
||||
None => 0,
|
||||
|
|
@ -309,10 +317,10 @@ impl<'a> MtxChat<'a> {
|
|||
}
|
||||
}
|
||||
if !self.logged_in {
|
||||
if web::get_login_type(&server) {
|
||||
if web::get_login_type(&server, &mut self.agent) {
|
||||
self.login_modal();
|
||||
let password = self.get_or(PASSWORD_KEY, EMPTY);
|
||||
if let Some(new_token) = web::authenticate_user(&server, &self.user_id, &password) {
|
||||
if let Some(new_token) = web::authenticate_user(&server, &self.user_id, &password, &mut self.agent) {
|
||||
self.set_debug(TOKEN_KEY, &new_token);
|
||||
self.logged_in = true;
|
||||
} else {
|
||||
|
|
@ -403,7 +411,7 @@ impl<'a> MtxChat<'a> {
|
|||
write!(room, "#{}:{}", &self.room_name, &self.room_domain).expect("failed to write room");
|
||||
let mut server = String::new();
|
||||
write!(server, "{}{}", HTTPS, &self.user_domain).expect("failed to write server");
|
||||
if let Some(room_id) = web::get_room_id(&server, &room, &self.token) {
|
||||
if let Some(room_id) = web::get_room_id(&server, &room, &self.token, &mut self.agent) {
|
||||
self.set_debug(ROOM_ID_KEY, &room_id);
|
||||
Some(room)
|
||||
} else {
|
||||
|
|
@ -480,7 +488,7 @@ impl<'a> MtxChat<'a> {
|
|||
&self.token
|
||||
);
|
||||
if let Some(new_filter) =
|
||||
web::get_filter(&self.user_id, &server, &self.room_id, &self.token)
|
||||
web::get_filter(&self.user_id, &server, &self.room_id, &self.token, &mut self.agent)
|
||||
{
|
||||
self.set_debug(FILTER_KEY, &new_filter);
|
||||
true
|
||||
|
|
@ -519,6 +527,7 @@ impl<'a> MtxChat<'a> {
|
|||
&self.get_or(ROOM_DOMAIN_KEY, DOMAIN_MATRIX)
|
||||
)
|
||||
.expect("failed to write server");
|
||||
let mut agent = self.agent.clone();
|
||||
let filter = self.filter.clone();
|
||||
let since = self.since.clone();
|
||||
let room_id = self.room_id.clone();
|
||||
|
|
@ -529,7 +538,7 @@ impl<'a> MtxChat<'a> {
|
|||
let modals = Modals::new(&xns).expect("can't connect to Modals server");
|
||||
log::info!("client_sync for {} ms...", MTX_LONG_TIMEOUT);
|
||||
if let Some((_since, events)) =
|
||||
web::client_sync(&server, &filter, &since, MTX_LONG_TIMEOUT, &room_id, &token)
|
||||
web::client_sync(&server, &filter, &since, MTX_LONG_TIMEOUT, &room_id, &token, &mut agent)
|
||||
{
|
||||
// TODO send "since" to mtxchat server
|
||||
// and you probably want to have a look at Dialogue::MAX_BYTES
|
||||
|
|
@ -607,7 +616,7 @@ impl<'a> MtxChat<'a> {
|
|||
log::info!("txn_id = {}", txn_id);
|
||||
let mut server = String::new();
|
||||
write!(server, "{}{}", HTTPS, &self.user_domain).expect("failed to write server");
|
||||
if web::send_message(&server, &self.room_id, &text, &txn_id, &self.token) {
|
||||
if web::send_message(&server, &self.room_id, &text, &txn_id, &self.token, &mut self.agent) {
|
||||
log::info!("SENT: {}", text);
|
||||
} else {
|
||||
log::info!("FAILED TO SEND");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use ureq;
|
||||
use ureq::Agent;
|
||||
use ureq::serde_json::{Map, Value};
|
||||
|
||||
use crate::{url, Msg};
|
||||
|
|
@ -58,21 +58,21 @@ pub fn handle_response(maybe_response: Result<ureq::Response, ureq::Error>) -> O
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_json(url: &str) -> Result<ureq::Response, ureq::Error> {
|
||||
ureq::get(&url).set(ACCEPT, ACCEPT_JSON).call()
|
||||
pub fn get_json(url: &str, agent: &mut Agent) -> Result<ureq::Response, ureq::Error> {
|
||||
agent.get(&url).set(ACCEPT, ACCEPT_JSON).call()
|
||||
}
|
||||
|
||||
pub fn get_json_auth(url: &str, token: &str) -> Result<ureq::Response, ureq::Error> {
|
||||
pub fn get_json_auth(url: &str, token: &str, agent: &mut Agent) -> Result<ureq::Response, ureq::Error> {
|
||||
let mut authorization = String::from(BEARER);
|
||||
authorization.push_str(token);
|
||||
ureq::get(&url)
|
||||
agent.get(&url)
|
||||
.set(ACCEPT, ACCEPT_JSON)
|
||||
.set(AUTHORIZATION, &authorization)
|
||||
.call()
|
||||
}
|
||||
|
||||
pub fn post_string(url: &str, request_body: &str) -> Result<ureq::Response, ureq::Error> {
|
||||
ureq::post(&url)
|
||||
pub fn post_string(url: &str, request_body: &str, agent: &mut Agent) -> Result<ureq::Response, ureq::Error> {
|
||||
agent.post(&url)
|
||||
.set(ACCEPT, ACCEPT_JSON)
|
||||
.send_string(request_body)
|
||||
}
|
||||
|
|
@ -81,10 +81,11 @@ pub fn post_string_auth(
|
|||
url: &str,
|
||||
request_body: &str,
|
||||
token: &str,
|
||||
agent: &mut Agent,
|
||||
) -> Result<ureq::Response, ureq::Error> {
|
||||
let mut authorization = String::from(BEARER);
|
||||
authorization.push_str(token);
|
||||
ureq::post(&url)
|
||||
agent.post(&url)
|
||||
.set(ACCEPT, ACCEPT_JSON)
|
||||
.set(AUTHORIZATION, &authorization)
|
||||
.send_string(request_body)
|
||||
|
|
@ -94,10 +95,11 @@ pub fn put_string_auth(
|
|||
url: &str,
|
||||
request_body: &str,
|
||||
token: &str,
|
||||
agent: &mut Agent,
|
||||
) -> Result<ureq::Response, ureq::Error> {
|
||||
let mut authorization = String::from(BEARER);
|
||||
authorization.push_str(token);
|
||||
ureq::put(&url)
|
||||
agent.put(&url)
|
||||
.set(ACCEPT, ACCEPT_JSON)
|
||||
.set(AUTHORIZATION, &authorization)
|
||||
.send_string(request_body)
|
||||
|
|
@ -105,10 +107,10 @@ pub fn put_string_auth(
|
|||
|
||||
// --------------------------------
|
||||
|
||||
pub fn whoami(server: &str, token: &str) -> Option<String> {
|
||||
pub fn whoami(server: &str, token: &str, agent: &mut Agent) -> Option<String> {
|
||||
let mut url = String::from(server);
|
||||
url.push_str("/_matrix/client/r0/account/whoami");
|
||||
if let Some(value) = handle_response(get_json_auth(&url, token)) {
|
||||
if let Some(value) = handle_response(get_json_auth(&url, token, agent)) {
|
||||
if let Value::Object(body) = value {
|
||||
if let Some(Value::String(device_id)) = body.get("device_id") {
|
||||
log::info!("device_id = {}", device_id);
|
||||
|
|
@ -122,11 +124,11 @@ pub fn whoami(server: &str, token: &str) -> Option<String> {
|
|||
None
|
||||
}
|
||||
|
||||
pub fn get_login_type(server: &str) -> bool {
|
||||
pub fn get_login_type(server: &str, agent: &mut Agent) -> bool {
|
||||
let mut url = String::from(server);
|
||||
url.push_str("/_matrix/client/r0/login");
|
||||
let mut found = false;
|
||||
if let Some(value) = handle_response(get_json(&url)) {
|
||||
if let Some(value) = handle_response(get_json(&url, agent)) {
|
||||
if let Value::Object(body) = value {
|
||||
if let Some(Value::Array(flows)) = body.get("flows") {
|
||||
for flow in flows.iter() {
|
||||
|
|
@ -173,13 +175,13 @@ impl AuthRequest {
|
|||
}
|
||||
|
||||
// fn authenticate_user() -> Result<String, ureq::Error> {
|
||||
pub fn authenticate_user(server: &str, user: &str, password: &str) -> Option<String> {
|
||||
pub fn authenticate_user(server: &str, user: &str, password: &str, agent: &mut Agent) -> Option<String> {
|
||||
let mut maybe_token: Option<String> = None;
|
||||
let mut url = String::from(server);
|
||||
url.push_str("/_matrix/client/r0/login");
|
||||
let auth_request = AuthRequest::new(user, password);
|
||||
if let Some(request_body) = serialize(&auth_request) {
|
||||
if let Some(value) = handle_response(post_string(&url, &request_body)) {
|
||||
if let Some(value) = handle_response(post_string(&url, &request_body, agent)) {
|
||||
if let Value::Object(body) = value {
|
||||
if let Some(Value::String(access_token)) = body.get("access_token") {
|
||||
maybe_token = Some(access_token.to_string())
|
||||
|
|
@ -190,13 +192,13 @@ pub fn authenticate_user(server: &str, user: &str, password: &str) -> Option<Str
|
|||
maybe_token
|
||||
}
|
||||
|
||||
pub fn get_room_id(server: &str, room_server: &str, token: &str) -> Option<String> {
|
||||
pub fn get_room_id(server: &str, room_server: &str, token: &str, agent: &mut Agent) -> Option<String> {
|
||||
let room_encoded = url::encode(room_server);
|
||||
let mut url = String::from(server);
|
||||
url.push_str("/_matrix/client/v3/directory/room/");
|
||||
url.push_str(&room_encoded);
|
||||
log::info!("get_room_id = {}", url);
|
||||
if let Some(value) = handle_response(get_json_auth(&url, token)) {
|
||||
if let Some(value) = handle_response(get_json_auth(&url, token, agent)) {
|
||||
if let Value::Object(body) = value {
|
||||
if let Some(Value::String(room_id)) = body.get("room_id") {
|
||||
Some(room_id.to_string())
|
||||
|
|
@ -303,7 +305,7 @@ impl FilterRequest {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_filter(user: &str, server: &str, room_id: &str, token: &str) -> Option<String> {
|
||||
pub fn get_filter(user: &str, server: &str, room_id: &str, token: &str, agent: &mut Agent) -> Option<String> {
|
||||
let user_encoded = url::encode(user);
|
||||
let mut url = String::from(server);
|
||||
url.push_str("/_matrix/client/v3/user/");
|
||||
|
|
@ -312,7 +314,7 @@ pub fn get_filter(user: &str, server: &str, room_id: &str, token: &str) -> Optio
|
|||
log::info!("get_filter = {}", url);
|
||||
let filter_request = FilterRequest::new(room_id);
|
||||
if let Some(request_body) = serialize(&filter_request) {
|
||||
if let Some(value) = handle_response(post_string_auth(&url, &request_body, token)) {
|
||||
if let Some(value) = handle_response(post_string_auth(&url, &request_body, token, agent)) {
|
||||
if let Value::Object(body) = value {
|
||||
if let Some(Value::String(filter_id)) = body.get("filter_id") {
|
||||
log::info!("filter_id = {}", filter_id);
|
||||
|
|
@ -377,6 +379,7 @@ pub fn client_sync(
|
|||
timeout: i32,
|
||||
room_id: &str,
|
||||
token: &str,
|
||||
agent: &mut Agent,
|
||||
) -> Option<(String, Vec<Msg>)> {
|
||||
log::info!("heap usage: {}", crate::heap_usage());
|
||||
let mut url = String::from(server);
|
||||
|
|
@ -389,7 +392,7 @@ pub fn client_sync(
|
|||
url.push_str(since);
|
||||
}
|
||||
log::info!("client_sync = {}", url);
|
||||
if let Some(value) = handle_response(get_json_auth(&url, token)) {
|
||||
if let Some(value) = handle_response(get_json_auth(&url, token, agent)) {
|
||||
if let Value::Object(body) = value {
|
||||
if let Some(Value::String(next_batch)) = body.get("next_batch") {
|
||||
Some((next_batch.to_string(), get_messages(body, room_id)))
|
||||
|
|
@ -420,7 +423,7 @@ impl MessageRequest {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token: &str) -> bool {
|
||||
pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token: &str, agent: &mut Agent) -> bool {
|
||||
log::info!("heap usage: {}", crate::heap_usage());
|
||||
let room_id_encoded = url::encode(room_id);
|
||||
let mut url = String::from(server);
|
||||
|
|
@ -431,7 +434,7 @@ pub fn send_message(server: &str, room_id: &str, text: &str, txn_id: &str, token
|
|||
log::info!("send_message = {}", url);
|
||||
let message_request = MessageRequest::new(text);
|
||||
if let Some(request_body) = serialize(&message_request) {
|
||||
if let Some(value) = handle_response(put_string_auth(&url, &request_body, token)) {
|
||||
if let Some(value) = handle_response(put_string_auth(&url, &request_body, token, agent)) {
|
||||
if let Value::Object(_body) = value {
|
||||
true
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use rkyv::{
|
|||
ser::{serializers::WriteSerializer, Serializer},
|
||||
Deserialize,
|
||||
};
|
||||
use rustls::{Certificate, RootCertStore};
|
||||
use rustls::{Certificate, ClientConfig, RootCertStore};
|
||||
use sha2::Digest;
|
||||
use std::convert::{Into, TryFrom};
|
||||
use std::io::{Error, Read, Write};
|
||||
|
|
@ -199,7 +199,8 @@ impl Tls {
|
|||
rkyv::archived_value::<RustlsOwnedTrustAnchor>(&bytes, pos)
|
||||
};
|
||||
let ta = archive.deserialize(&mut AllocDeserializer {}).ok();
|
||||
log::info!("get '{}' = '{:?}'", key, &ta);
|
||||
log::info!("get {}", key);
|
||||
log::trace!("get '{}' = '{:?}'", key, &ta);
|
||||
ta
|
||||
}
|
||||
Err(e) => {
|
||||
|
|
@ -244,6 +245,13 @@ impl Tls {
|
|||
}
|
||||
root_store
|
||||
}
|
||||
|
||||
pub fn client_config(&self) -> ClientConfig {
|
||||
rustls::ClientConfig::builder()
|
||||
.with_safe_defaults()
|
||||
.with_root_certificates(self.root_store())
|
||||
.with_no_client_auth()
|
||||
}
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/57029974/how-to-split-string-into-chunks-in-rust-to-insert-spaces
|
||||
|
|
|
|||
Loading…
Reference in a new issue