| @@ -9,9 +9,11 @@ use std::hash::{Hash, Hasher}; |
| 9 | 9 | use std::io::{BufReader, BufWriter, Write}; |
| 10 | 10 | use std::path::{Path, PathBuf}; |
| 11 | 11 | use std::process; |
| 12 | use std::str::FromStr; |
| 12 | 13 | |
| 13 | 14 | use clap::Parser; |
| 14 | 15 | use matrix_sdk::authentication::matrix::MatrixSession; |
| 16 | use matrix_sdk::reqwest::header::{HeaderMap, HeaderValue}; |
| 15 | 17 | use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId}; |
| 16 | 18 | use matrix_sdk::EncryptionState; |
| 17 | 19 | use ratatui::style::{Color, Modifier as StyleModifier, Style}; |
| @@ -123,6 +125,20 @@ fn validate_profile_names(names: &BTreeMap<String, ProfileConfig>) { |
| 123 | 125 | } |
| 124 | 126 | } |
| 125 | 127 | |
| 128 | fn deserialize_from_str_opt<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error> |
| 129 | where |
| 130 | D: serde::de::Deserializer<'de>, |
| 131 | T: FromStr, |
| 132 | <T as FromStr>::Err: fmt::Display, |
| 133 | { |
| 134 | <Option<&'de str>>::deserialize(deserializer)? |
| 135 | .map(|s| { |
| 136 | let t = T::from_str(s); |
| 137 | t.map_err(|e| D::Error::custom(format!("failed to parse string: {e}"))) |
| 138 | }) |
| 139 | .transpose() |
| 140 | } |
| 141 | |
| 126 | 142 | const VERSION: &str = match option_env!("VERGEN_GIT_SHA") { |
| 127 | 143 | None => env!("CARGO_PKG_VERSION"), |
| 128 | 144 | Some(_) => concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")"), |
| @@ -154,6 +170,26 @@ pub enum ConfigError { |
| 154 | 170 | InvalidJSON(#[from] serde_json::Error), |
| 155 | 171 | } |
| 156 | 172 | |
| 173 | macro_rules! deserialize_str_with_visitor { |
| 174 | ($t: ident, $v: ident) => { |
| 175 | impl<'de> Deserialize<'de> for $t { |
| 176 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 177 | where |
| 178 | D: Deserializer<'de>, |
| 179 | { |
| 180 | deserializer.deserialize_str($v) |
| 181 | } |
| 182 | } |
| 183 | }; |
| 184 | } |
| 185 | |
| 186 | deserialize_str_with_visitor!(Keys, KeysVisitor); |
| 187 | deserialize_str_with_visitor!(VimModes, VimModesVisitor); |
| 188 | deserialize_str_with_visitor!(UserColor, UserColorVisitor); |
| 189 | deserialize_str_with_visitor!(EncryptionIndicatorLocation, EncryptionIndicatorLocationVisitor); |
| 190 | deserialize_str_with_visitor!(NotifyVia, NotifyViaVisitor); |
| 191 | deserialize_str_with_visitor!(ProxyUrl, ProxyUrlVisitor); |
| 192 | |
| 157 | 193 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 158 | 194 | pub struct Keys(pub Vec<TerminalKey>, pub String); |
| 159 | 195 | pub struct KeysVisitor; |
| @@ -176,15 +212,6 @@ impl Visitor<'_> for KeysVisitor { |
| 176 | 212 | } |
| 177 | 213 | } |
| 178 | 214 | |
| 179 | | impl<'de> Deserialize<'de> for Keys { |
| 180 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 181 | | where |
| 182 | | D: Deserializer<'de>, |
| 183 | | { |
| 184 | | deserializer.deserialize_str(KeysVisitor) |
| 185 | | } |
| 186 | | } |
| 187 | | |
| 188 | 215 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
| 189 | 216 | pub struct VimModes(pub Vec<VimMode>); |
| 190 | 217 | pub struct VimModesVisitor; |
| @@ -220,15 +247,6 @@ impl Visitor<'_> for VimModesVisitor { |
| 220 | 247 | } |
| 221 | 248 | } |
| 222 | 249 | |
| 223 | | impl<'de> Deserialize<'de> for VimModes { |
| 224 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 225 | | where |
| 226 | | D: Deserializer<'de>, |
| 227 | | { |
| 228 | | deserializer.deserialize_str(VimModesVisitor) |
| 229 | | } |
| 230 | | } |
| 231 | | |
| 232 | 250 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 233 | 251 | pub struct UserColor(pub Color); |
| 234 | 252 | pub struct UserColorVisitor; |
| @@ -267,15 +285,6 @@ impl Visitor<'_> for UserColorVisitor { |
| 267 | 285 | } |
| 268 | 286 | } |
| 269 | 287 | |
| 270 | | impl<'de> Deserialize<'de> for UserColor { |
| 271 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 272 | | where |
| 273 | | D: Deserializer<'de>, |
| 274 | | { |
| 275 | | deserializer.deserialize_str(UserColorVisitor) |
| 276 | | } |
| 277 | | } |
| 278 | | |
| 279 | 288 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 280 | 289 | pub struct Session { |
| 281 | 290 | access_token: String, |
| @@ -392,15 +401,6 @@ impl Visitor<'_> for EncryptionIndicatorLocationVisitor { |
| 392 | 401 | } |
| 393 | 402 | } |
| 394 | 403 | |
| 395 | | impl<'de> Deserialize<'de> for EncryptionIndicatorLocation { |
| 396 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 397 | | where |
| 398 | | D: Deserializer<'de>, |
| 399 | | { |
| 400 | | deserializer.deserialize_str(EncryptionIndicatorLocationVisitor) |
| 401 | | } |
| 402 | | } |
| 403 | | |
| 404 | 404 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
| 405 | 405 | #[serde(rename_all = "lowercase")] |
| 406 | 406 | pub enum UserDisplayStyle { |
| @@ -485,15 +485,6 @@ impl Visitor<'_> for NotifyViaVisitor { |
| 485 | 485 | } |
| 486 | 486 | } |
| 487 | 487 | |
| 488 | | impl<'de> Deserialize<'de> for NotifyVia { |
| 489 | | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 490 | | where |
| 491 | | D: Deserializer<'de>, |
| 492 | | { |
| 493 | | deserializer.deserialize_str(NotifyViaVisitor) |
| 494 | | } |
| 495 | | } |
| 496 | | |
| 497 | 488 | #[derive(Clone, Debug, Default, Deserialize)] |
| 498 | 489 | pub struct Encryption { |
| 499 | 490 | indicator: Option<EncryptionIndicator>, |
| @@ -564,6 +555,69 @@ impl EncryptionValues { |
| 564 | 555 | } |
| 565 | 556 | } |
| 566 | 557 | |
| 558 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 559 | pub enum ProxyUrl { |
| 560 | Disabled, |
| 561 | Endpoint(Url), |
| 562 | #[default] |
| 563 | System, |
| 564 | } |
| 565 | |
| 566 | pub struct ProxyUrlVisitor; |
| 567 | |
| 568 | impl Visitor<'_> for ProxyUrlVisitor { |
| 569 | type Value = ProxyUrl; |
| 570 | |
| 571 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 572 | formatter.write_str("a valid proxy URL (e.g. \"socks5://localhost:9050\")") |
| 573 | } |
| 574 | |
| 575 | fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> |
| 576 | where |
| 577 | E: SerdeError, |
| 578 | { |
| 579 | if value.is_empty() { |
| 580 | return Ok(ProxyUrl::Disabled); |
| 581 | } |
| 582 | |
| 583 | match Url::from_str(value) { |
| 584 | Ok(uri) => Ok(ProxyUrl::Endpoint(uri)), |
| 585 | Err(e) => Err(E::custom(format!("could not parse {value:?}: {e}"))), |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | #[derive(Clone, Debug, Default, Deserialize)] |
| 591 | pub struct Proxy { |
| 592 | /// How and where to proxy the client's requests to the homeserver. |
| 593 | url: Option<ProxyUrl>, |
| 594 | |
| 595 | /// An optional value to include in the `Proxy-Authorization` header. |
| 596 | #[serde(default, deserialize_with = "deserialize_from_str_opt")] |
| 597 | auth: Option<HeaderValue>, |
| 598 | |
| 599 | /// Optional headers to include in requests sent to the proxy. |
| 600 | #[serde(default, with = "http_serde::header_map")] |
| 601 | headers: HeaderMap, |
| 602 | } |
| 603 | |
| 604 | impl Proxy { |
| 605 | pub fn values(self) -> ProxyValues { |
| 606 | ProxyValues { |
| 607 | url: self.url.unwrap_or_default(), |
| 608 | auth: self.auth, |
| 609 | headers: self.headers, |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | #[derive(Clone, Debug, Default)] |
| 615 | pub struct ProxyValues { |
| 616 | pub url: ProxyUrl, |
| 617 | pub auth: Option<HeaderValue>, |
| 618 | pub headers: HeaderMap, |
| 619 | } |
| 620 | |
| 567 | 621 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
| 568 | 622 | pub struct Mouse { |
| 569 | 623 | #[serde(default)] |
| @@ -589,7 +643,7 @@ pub struct ImagePreviewValues { |
| 589 | 643 | pub protocol: Option<ImagePreviewProtocolValues>, |
| 590 | 644 | } |
| 591 | 645 | |
| 592 | | #[derive(Clone, Default, Deserialize)] |
| 646 | #[derive(Clone, Debug, Default, Deserialize)] |
| 593 | 647 | pub struct ImagePreview { |
| 594 | 648 | pub lazy_load: Option<bool>, |
| 595 | 649 | pub size: Option<ImagePreviewSize>, |
| @@ -618,7 +672,7 @@ impl Default for ImagePreviewSize { |
| 618 | 672 | } |
| 619 | 673 | } |
| 620 | 674 | |
| 621 | | #[derive(Clone, Deserialize)] |
| 675 | #[derive(Clone, Debug, Deserialize)] |
| 622 | 676 | pub struct ImagePreviewProtocolValues { |
| 623 | 677 | pub r#type: Option<ProtocolType>, |
| 624 | 678 | pub font_size: Option<(u16, u16)>, |
| @@ -633,7 +687,7 @@ pub struct SortValues { |
| 633 | 687 | pub members: Vec<SortColumn<SortFieldUser>>, |
| 634 | 688 | } |
| 635 | 689 | |
| 636 | | #[derive(Clone, Default, Deserialize)] |
| 690 | #[derive(Clone, Debug, Default, Deserialize)] |
| 637 | 691 | pub struct SortOverrides { |
| 638 | 692 | pub chats: Option<Vec<SortColumn<SortFieldRoom>>>, |
| 639 | 693 | pub dms: Option<Vec<SortColumn<SortFieldRoom>>>, |
| @@ -696,6 +750,8 @@ pub struct TerminalValues { |
| 696 | 750 | pub enable_title: bool, |
| 697 | 751 | } |
| 698 | 752 | |
| 753 | /// The configuration settings to run with, after merging the |
| 754 | /// per-profile overrides on top of the global settings. |
| 699 | 755 | #[derive(Clone)] |
| 700 | 756 | pub struct TunableValues { |
| 701 | 757 | pub encryption: EncryptionValues, |
| @@ -703,6 +759,7 @@ pub struct TunableValues { |
| 703 | 759 | pub max_log_files: usize, |
| 704 | 760 | pub message_shortcode_display: bool, |
| 705 | 761 | pub normal_after_send: bool, |
| 762 | pub proxy: ProxyValues, |
| 706 | 763 | pub reaction_display: bool, |
| 707 | 764 | pub reaction_shortcode_display: bool, |
| 708 | 765 | pub read_receipt_send: bool, |
| @@ -728,12 +785,15 @@ pub struct TunableValues { |
| 728 | 785 | pub ssl_verify: bool, |
| 729 | 786 | } |
| 730 | 787 | |
| 731 | | #[derive(Clone, Default, Deserialize)] |
| 788 | #[derive(Clone, Debug, Default, Deserialize)] |
| 732 | 789 | pub struct Tunables { |
| 733 | 790 | /// Subsection for overriding encryption-related settings. |
| 734 | 791 | #[serde(default)] |
| 735 | 792 | pub encryption: Encryption, |
| 736 | 793 | |
| 794 | /// Subsection for configuring an HTTP(S) proxy. |
| 795 | pub proxy: Option<Proxy>, |
| 796 | |
| 737 | 797 | /// Subsection for overriding sort orders in UI lists. |
| 738 | 798 | #[serde(default)] |
| 739 | 799 | pub sort: SortOverrides, |
| @@ -779,6 +839,11 @@ impl Tunables { |
| 779 | 839 | terminal: Terminal::merge(self.terminal, other.terminal), |
| 780 | 840 | users: merge_maps(self.users, other.users), |
| 781 | 841 | |
| 842 | // Proxy configuration sub-field do *not* get merged, so that a |
| 843 | // per-profile override won't inherit auth or headers from the |
| 844 | // global settings. |
| 845 | proxy: self.proxy.or(other.proxy), |
| 846 | |
| 782 | 847 | log_level: self.log_level.or(other.log_level), |
| 783 | 848 | max_log_files: self.max_log_files.or(other.max_log_files), |
| 784 | 849 | message_shortcode_display: self |
| @@ -815,8 +880,10 @@ impl Tunables { |
| 815 | 880 | fn values(self) -> TunableValues { |
| 816 | 881 | TunableValues { |
| 817 | 882 | encryption: self.encryption.values(), |
| 883 | proxy: self.proxy.unwrap_or_default().values(), |
| 818 | 884 | sort: self.sort.values(), |
| 819 | 885 | terminal: self.terminal.values(), |
| 886 | users: self.users.unwrap_or_default(), |
| 820 | 887 | |
| 821 | 888 | log_level: self.log_level.unwrap_or_else(|| "warn".to_string()), |
| 822 | 889 | max_log_files: self.max_log_files.unwrap_or(7), |
| @@ -830,7 +897,6 @@ impl Tunables { |
| 830 | 897 | state_event_display: self.state_event_display.unwrap_or(true), |
| 831 | 898 | typing_notice_send: self.typing_notice_send.unwrap_or(true), |
| 832 | 899 | typing_notice_display: self.typing_notice_display.unwrap_or(true), |
| 833 | | users: self.users.unwrap_or_default(), |
| 834 | 900 | username_display: self.username_display.unwrap_or_default(), |
| 835 | 901 | message_user_color: self.message_user_color.unwrap_or(false), |
| 836 | 902 | default_room: self.default_room, |
| @@ -1424,6 +1490,82 @@ mod tests { |
| 1424 | 1490 | assert_eq!(res.sort.dms, Vec::from(DEFAULT_ROOM_SORT)); |
| 1425 | 1491 | } |
| 1426 | 1492 | |
| 1493 | #[test] |
| 1494 | fn test_parse_tunables_proxy_invalid() { |
| 1495 | let res = |
| 1496 | serde_json::from_str::<Tunables>(r#"{"proxy": {"url": "localhost"}}"#).unwrap_err(); |
| 1497 | |
| 1498 | // Should result in a validation error: |
| 1499 | assert_eq!(res.classify(), serde_json::error::Category::Data); |
| 1500 | } |
| 1501 | |
| 1502 | #[test] |
| 1503 | fn test_parse_tunables_proxy_empty() { |
| 1504 | let res: Tunables = serde_json::from_str(r#"{"proxy": {"url": ""}}"#).unwrap(); |
| 1505 | let proxy = res.proxy.unwrap(); |
| 1506 | assert_eq!(proxy.url.unwrap(), ProxyUrl::Disabled); |
| 1507 | } |
| 1508 | |
| 1509 | #[test] |
| 1510 | fn test_parse_tunables_proxy_socks5() { |
| 1511 | let res: Tunables = |
| 1512 | serde_json::from_str(r#"{"proxy": {"url": "socks5://localhost:1080"}}"#).unwrap(); |
| 1513 | let proxy = res.proxy.unwrap(); |
| 1514 | let ProxyUrl::Endpoint(url) = proxy.url.unwrap() else { |
| 1515 | panic!("should parse ProxyUrl::Endpoint") |
| 1516 | }; |
| 1517 | |
| 1518 | assert_eq!(url.scheme(), "socks5"); |
| 1519 | assert_eq!(url.host_str().unwrap(), "localhost"); |
| 1520 | assert_eq!(url.port().unwrap(), 1080); |
| 1521 | assert_eq!(url.authority(), "localhost:1080"); |
| 1522 | } |
| 1523 | |
| 1524 | #[test] |
| 1525 | fn test_parse_tunables_proxy_https() { |
| 1526 | let res: Tunables = serde_json::from_str( |
| 1527 | r#"{"proxy": {"url": "https://localhost:8080","auth": "Bearer abcd1234","headers":{"User-Agent": "iamb"}}}"# |
| 1528 | ).unwrap(); |
| 1529 | let proxy = res.proxy.unwrap(); |
| 1530 | let ProxyUrl::Endpoint(url) = proxy.url.unwrap() else { |
| 1531 | panic!("should parse ProxyUrl::Endpoint") |
| 1532 | }; |
| 1533 | |
| 1534 | // Verify URL fields: |
| 1535 | assert_eq!(url.scheme(), "https"); |
| 1536 | assert_eq!(url.host_str().unwrap(), "localhost"); |
| 1537 | assert_eq!(url.port().unwrap(), 8080); |
| 1538 | assert_eq!(url.authority(), "localhost:8080"); |
| 1539 | |
| 1540 | // Verify our `Proxy-Authorization` value: |
| 1541 | assert_eq!(proxy.auth.unwrap(), "Bearer abcd1234"); |
| 1542 | |
| 1543 | // Verify our custom header is present: |
| 1544 | assert_eq!(proxy.headers.len(), 1); |
| 1545 | assert_eq!(proxy.headers.get("user-agent").unwrap(), "iamb"); |
| 1546 | } |
| 1547 | |
| 1548 | #[test] |
| 1549 | fn test_parse_tunables_proxy_merge() { |
| 1550 | let global: Tunables = serde_json::from_str( |
| 1551 | r#"{"proxy": {"url": "https://localhost:8080","auth": "Bearer abcd1234","headers":{"User-Agent": "iamb"}}}"# |
| 1552 | ).unwrap(); |
| 1553 | let profile: Tunables = |
| 1554 | serde_json::from_str(r#"{"proxy": {"url": "socks5://localhost:1080"}}"#).unwrap(); |
| 1555 | |
| 1556 | // The configuration merge should select the entirety of the profile proxy config, |
| 1557 | // and not merge subfields, to ensure that things like `auth` and `headers` are |
| 1558 | // not ever sent to a `url` they were meant for. |
| 1559 | let merged = profile.merge(global).values(); |
| 1560 | let ProxyUrl::Endpoint(url) = merged.proxy.url else { |
| 1561 | panic!("should parse ProxyUrl::Endpoint") |
| 1562 | }; |
| 1563 | assert_eq!(url.scheme(), "socks5"); |
| 1564 | assert_eq!(url.authority(), "localhost:1080"); |
| 1565 | assert_eq!(merged.proxy.auth, None); |
| 1566 | assert_eq!(merged.proxy.headers.is_empty(), true); |
| 1567 | } |
| 1568 | |
| 1427 | 1569 | #[test] |
| 1428 | 1570 | fn test_parse_layout() { |
| 1429 | 1571 | let user = WindowPath::UserId(user_id!("@user:example.com").to_owned()); |