Skip to main content

kdeconnect_proto/
config.rs

1//! Define the configuration of the host device.
2//!
3//! The constants in this module define the maximum sizes of the various buffers used during
4//! communication in `kdeconnect-proto`. Each of these constants have a default value but can be
5//! set using the correct environment variable.
6use crate::device::DeviceType;
7
8#[cfg(not(feature = "std"))]
9use alloc::{string::String, vec::Vec};
10
11pub(crate) const MIN_TCP_PORT: u16 = 1716;
12pub(crate) const MAX_TCP_PORT: u16 = 1764;
13pub(crate) const UDP_PORT: u16 = 1716;
14pub(crate) const KDECONNECT_PROTOCOL_VERSION: u16 = 8;
15
16/// The maximum size of the buffer used by UDP connections (this setting also applies for
17/// MDNS buffers, although they are not shared with UDP-only connections).
18///
19/// Default value: 2500 bytes
20/// Environment variable to set: KDECONNECT_UDP_BUFFER_SIZE
21pub const UDP_BUFFER_SIZE: usize = const {
22    if let Some(str_value) = option_env!("KDECONNECT_UDP_BUFFER_SIZE") {
23        if let Ok(value) = usize::from_str_radix(str_value, 10) {
24            value
25        } else {
26            panic!("Failed to parse environment variable `KDECONNECT_UDP_BUFFER_SIZE`");
27        }
28    } else {
29        2500
30    }
31};
32
33/// The maximum size of the buffer used by TCP connections.
34///
35/// Default value: 2500 bytes
36/// Environment variable to set: KDECONNECT_TCP_BUFFER_SIZE
37pub const TCP_BUFFER_SIZE: usize = const {
38    if let Some(str_value) = option_env!("KDECONNECT_TCP_BUFFER_SIZE") {
39        if let Ok(value) = usize::from_str_radix(str_value, 10) {
40            value
41        } else {
42            panic!("Failed to parse environment variable `KDECONNECT_TCP_BUFFER_SIZE`");
43        }
44    } else {
45        2500
46    }
47};
48
49/// The maximum size of the buffer used by TLS connections.
50///
51/// Default value: 2500 bytes
52/// Environment variable to set: KDECONNECT_TLS_BUFFER_SIZE
53pub const TLS_BUFFER_SIZE: usize = const {
54    if let Some(str_value) = option_env!("KDECONNECT_TLS_BUFFER_SIZE") {
55        if let Ok(value) = usize::from_str_radix(str_value, 10) {
56            value
57        } else {
58            panic!("Failed to parse environment variable `KDECONNECT_TLS_BUFFER_SIZE`");
59        }
60    } else {
61        2500
62    }
63};
64
65/// The maximum size of the buffer used by TLS connections of the main application.
66///
67/// Default value: 3000 bytes
68/// Environment variable to set: KDECONNECT_TLS_APP_BUFFER_SIZE
69pub const TLS_APP_BUFFER_SIZE: usize = const {
70    if let Some(str_value) = option_env!("KDECONNECT_TLS_APP_BUFFER_SIZE") {
71        if let Ok(value) = usize::from_str_radix(str_value, 10) {
72            value
73        } else {
74            panic!("Failed to parse environment variable `KDECONNECT_TLS_APP_BUFFER_SIZE`");
75        }
76    } else {
77        3000
78    }
79};
80
81/// The configuration of the host device.
82///
83/// You must generate a self-signed certificate with the device ID as CommonName in order to start
84/// a device and connect to other peers.
85///
86/// You can either use `openssl` to generate a certificate using:
87///
88/// ```bash
89/// openssl req -x509 \
90///     -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -keyout private_key.pem \
91///     -addext basicConstraints=critical,CA:FALSE \
92///     -days 3650 -nodes -subj "/O=KDE/OU=KDE Connect/CN=<your host device ID>" -out cert.pem
93/// ```
94///
95/// Or use the `rcgen` crate:
96///
97/// ```ignore
98/// use std::fs;
99/// use rcgen::{CertificateParams, DistinguishedName, DnType, IsCa, KeyPair};
100///
101/// pub fn gen_certificate(uuid: &str) {
102///     let mut params = CertificateParams::default();
103///     params.is_ca = IsCa::ExplicitNoCa;
104///
105///     let mut dn = DistinguishedName::new();
106///     dn.push(DnType::CommonName, uuid);
107///     dn.push(DnType::OrganizationName, "KDE");
108///     dn.push(DnType::OrganizationalUnitName, "KDE Connect");
109///     params.distinguished_name = dn;
110///
111///     let key_pair = KeyPair::generate().unwrap();
112///     let cert = params.self_signed(&key_pair).unwrap();
113///
114///     fs::write("private_key.pem", key_pair.serialize_pem()).unwrap();
115///     fs::write("cert.pem", cert.pem()).unwrap();
116/// }
117/// ```
118///
119/// You can then make a [`DeviceConfig`] using the content of the files `cert.pem` and
120/// `private_key.pem`, respectively in the [`DeviceConfig::cert`] and [`DeviceConfig::private_key`]
121/// fields.
122#[derive(Debug, Clone)]
123pub struct DeviceConfig {
124    /// The name of the host device.
125    ///
126    /// It must be 1-32 characters in length and shouldn't contain any of the following punctuation marks `"',;:.!?()[]<>`.
127    pub name: String,
128
129    /// The physical type of the host device.
130    pub device_type: DeviceType,
131
132    /// A PEM-encoded X.509 certificate.
133    pub cert: Vec<u8>,
134
135    /// A PEM-encoded X.509 private signing key.
136    pub private_key: Vec<u8>,
137}