1 // Copyright 2021-2022 Ian Jackson and contributors to Hippotat
2 // SPDX-License-Identifier: GPL-3.0-or-later WITH LicenseRef-Hippotat-OpenSSL-Exception
3 // There is NO WARRANTY.
7 #[derive(hippotat_macros::ResolveConfig)]
9 pub struct InstanceConfig {
10 // Exceptional settings
11 #[special(special_link, SKL::None)] pub link: LinkName,
12 #[per_client] pub secret: Secret,
13 #[global] #[special(special_ipif, SKL::PerClient)] pub ipif: String,
16 #[limited] pub max_batch_down: u32,
17 #[limited] pub max_queue_time: Duration,
18 #[limited] pub http_timeout: Duration,
19 #[limited] pub target_requests_outstanding: u32,
20 #[special(special_max_up, SKL::Limited)] pub max_batch_up: u32,
22 // Ordinary settings, used by both, not client-specifi:
23 #[global] pub addrs: Vec<IpAddr>,
24 #[global] pub vnetwork: Vec<IpNet>,
25 #[global] pub vaddr: IpAddr,
26 #[global] pub vrelay: IpAddr,
27 #[global] pub port: u16,
28 #[global] pub mtu: u32,
30 // Ordinary settings, used by server only:
31 #[server] #[per_client] pub max_clock_skew: Duration,
32 #[server] #[global] pub ifname_server: String,
34 // Ordinary settings, used by client only:
35 #[client] pub http_timeout_grace: Duration,
36 #[client] pub max_requests_outstanding: u32,
37 #[client] pub http_retry: Duration,
38 #[client] pub success_report_interval: Duration,
39 #[client] pub url: Url,
40 #[client] pub vroutes: Vec<IpNet>,
41 #[client] pub ifname_client: String,
43 // Computed, rather than looked up. Client only:
44 #[computed] pub effective_http_timeout: Duration,
47 static DEFAULT_CONFIG: &str = r#"
49 max_batch_down = 65536
51 target_requests_outstanding = 3
53 http_timeout_grace = 5
54 max_requests_outstanding = 6
59 ifname_client = hippo%d
60 ifname_server = shippo%d
62 success_report_interval = 3600
64 ipif = userv root ipif %{local},%{peer},%{mtu},slip,%{ifname} '%{rnets}'
68 vnetwork = 172.24.230.192
72 max_batch_down = 262144
75 target_requests_outstanding = 10
78 #[derive(clap::Args,Debug)]
79 pub struct CommonOpts {
80 /// Top-level config file or directory
82 /// Look for `main.cfg`, `config.d` and `secrets.d` here.
84 /// Or if this is a file, just read that file.
85 #[clap(long, default_value="/etc/hippotat")]
88 /// Additional config files or dirs, which can override the others
89 #[clap(long, action=clap::ArgAction::Append)]
90 pub extra_config: Vec<PathBuf>,
93 pub trait InspectableConfigAuto {
94 fn inspect_key_auto(&self, field: &'_ str)
95 -> Option<&dyn InspectableConfigValue>;
97 pub trait InspectableConfig: Debug {
98 fn inspect_key(&self, field: &'_ str)
99 -> Option<&dyn InspectableConfigValue>;
102 impl InspectableConfig for (&ServerName, &InstanceConfigGlobal) {
103 fn inspect_key(&self, field: &'_ str)
104 -> Option<&dyn InspectableConfigValue> {
107 k => return self.1.inspect_key_auto(k),
112 impl InspectableConfig for InstanceConfig {
113 fn inspect_key(&self, field: &'_ str)
114 -> Option<&dyn InspectableConfigValue> {
116 "link" => &self.link,
117 "server" => &self.link.server,
118 "client" => &self.link.client,
119 k => return self.inspect_key_auto(k),
124 #[derive(Debug,Clone,Copy)]
125 pub struct PrintConfigOpt<'a>(pub &'a Option<String>);
127 impl PrintConfigOpt<'_> {
129 pub fn implement<'c, C: InspectableConfig + 'c>(
131 configs: impl IntoIterator<Item=&'c C>,
133 if let Some(arg) = self.0 {
134 for config in configs {
135 Self::print_one_config(arg, config)?;
141 pub fn keys(&self) -> impl Iterator<Item=&str> {
142 self.0.as_ref().map(|arg| Self::split(arg)).into_iter().flatten()
145 fn split(arg: &str) -> impl Iterator<Item=&str> { arg.split(',') }
150 config: &dyn InspectableConfig,
152 let output = Self::split(arg)
155 return Ok(format!("{:#?}", &config));
157 let insp = config.inspect_key(key)
158 .ok_or_else(|| anyhow!("unknown config key {:?}", key))?;
159 Ok::<_,AE>(DisplayInspectable(insp).to_string())
161 .collect::<Result<Vec<_>,_>>()?
163 println!("{}", output);
167 pub trait InspectableConfigValue {
168 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result;
171 macro_rules! impl_inspectable_config_value {
172 { $t:ty as $trait:path } => {
173 impl InspectableConfigValue for $t {
174 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
175 <Self as $trait>::fmt(self, f)
181 impl InspectableConfigValue for Vec<$t> {
182 #[throws(fmt::Error)]
183 fn fmt(&self, f: &mut fmt::Formatter) {
184 let mut first = Some(());
185 for v in self.iter() {
186 if first.take().is_none() { write!(f, " ")?; }
187 InspectableConfigValue::fmt(v, f)?;
194 pub struct DisplayInspectable<'i>(pub &'i dyn InspectableConfigValue);
195 impl<'i> Display for DisplayInspectable<'i> {
196 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
197 InspectableConfigValue::fmt(self.0, f)
201 impl_inspectable_config_value!{ String as Display }
202 impl_inspectable_config_value!{ ServerName as Display }
203 impl_inspectable_config_value!{ ClientName as Display }
204 impl_inspectable_config_value!{ u16 as Display }
205 impl_inspectable_config_value!{ u32 as Display }
206 impl_inspectable_config_value!{ reqwest::Url as Display }
208 impl_inspectable_config_value!{ IpAddr as Display }
209 impl_inspectable_config_value!{ ipnet::IpNet as Display }
210 impl_inspectable_config_value!{ Vec<IpAddr> }
211 impl_inspectable_config_value!{ Vec<ipnet::IpNet> }
213 impl InspectableConfigValue for Duration {
214 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
215 let v = self.as_secs_f64();
222 fn sat(self) -> usize { self.try_into().unwrap_or(usize::MAX) }
226 impl<'s> Option<&'s str> {
228 fn value(self) -> &'s str {
229 self.ok_or_else(|| anyhow!("value needed"))?
234 pub struct Secret(pub String);
235 impl Parseable for Secret {
237 fn parse(s: Option<&str>) -> Self {
239 if s.is_empty() { throw!(anyhow!("secret value cannot be empty")) }
243 fn default_for_ordinary() -> Self { Parseable::unspecified() }
244 fn unspecified() -> Self { Secret(default()) }
246 impl Debug for Secret {
247 #[throws(fmt::Error)]
248 fn fmt(&self, f: &mut fmt::Formatter) { write!(f, "Secret(***)")? }
250 impl_inspectable_config_value!{ Secret as Debug }
252 #[derive(Debug,Clone,Hash,Eq,PartialEq)]
253 pub enum SectionName {
256 Server(ServerName), // includes SERVER, which is slightly special
257 ServerLimit(ServerName),
261 pub use SectionName as SN;
264 struct RawValRef<'v,'l,'s> {
265 raw: Option<&'v str>, // todo: not Option any more
268 section: &'s SectionName,
271 impl<'v> RawValRef<'v,'_,'_> {
273 fn try_map<F,T>(&self, f: F) -> T
274 where F: FnOnce(Option<&'v str>) -> Result<T, AE> {
276 .with_context(|| format!(r#"file {:?}, section {}, key "{}""#,
277 self.loc, self.section, self.key))?
282 pub opts: CommonOpts,
285 static OUTSIDE_SECTION: &str = "[";
286 static SPECIAL_SERVER_SECTION: &str = "SERVER";
291 keys_allowed: HashMap<&'static str, SectionKindList>,
292 sections: HashMap<SectionName, ini::Section>,
295 type OkAnyway<'f,A> = &'f dyn Fn(&io::Error) -> Option<A>;
297 impl<'f,A> OkAnyway<'f,A> {
298 fn ok<T>(self, r: &Result<T, io::Error>) -> Option<A> {
299 let e = r.as_ref().err()?;
305 impl FromStr for SectionName {
308 fn from_str(s: &str) -> Self {
310 "COMMON" => return SN::Common,
311 "LIMIT" => return SN::GlobalLimit,
314 if let Ok(n@ ServerName(_)) = s.parse() { return SN::Server(n) }
315 if let Ok(n@ ClientName(_)) = s.parse() { return SN::Client(n) }
316 let (server, client) = s.split_ascii_whitespace().collect_tuple()
317 .ok_or_else(|| anyhow!(
318 "bad section name {:?} \
319 (must be COMMON, <server>, <client>, or <server> <client>",
322 let server = server.parse().context("server name in link section name")?;
323 if client == "LIMIT" { return SN::ServerLimit(server) }
324 let client = client.parse().context("client name in link section name")?;
325 SN::Link(LinkName { server, client })
328 impl Display for InstanceConfig {
329 #[throws(fmt::Error)]
330 fn fmt(&self, f: &mut fmt::Formatter) { Display::fmt(&self.link, f)? }
333 impl Display for SectionName {
334 #[throws(fmt::Error)]
335 fn fmt(&self, f: &mut fmt::Formatter) {
337 SN::Link (ref l) => Display::fmt(l, f)?,
338 SN::Client(ref c) => write!(f, "[{}]" , c)?,
339 SN::Server(ref s) => write!(f, "[{}]" , s)?,
340 SN::ServerLimit(ref s) => write!(f, "[{} LIMIT] ", s)?,
341 SN::GlobalLimit => write!(f, "[LIMIT]" )?,
342 SN::Common => write!(f, "[COMMON]" )?,
350 keys_allowed: HashMap<&'static str, SectionKindList>
351 ) -> Self { Aggregate {
356 #[throws(AE)] // AE does not include path
357 fn read_file<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
359 let f = fs::File::open(path);
360 if let Some(anyway) = anyway.ok(&f) { return Some(anyway) }
361 let mut f = f.context("open")?;
363 let mut s = String::new();
364 let y = f.read_to_string(&mut s);
365 if let Some(anyway) = anyway.ok(&y) { return Some(anyway) }
368 self.read_string(s, path)?;
372 #[throws(AE)] // AE does not include path
373 fn read_string(&mut self, s: String, path_for_loc: &Path) {
374 let mut map: ini::Parsed = default();
375 ini::read(&mut map, &mut s.as_bytes(), path_for_loc)
376 .context("parse as INI")?;
377 if map.get(OUTSIDE_SECTION).is_some() {
378 throw!(anyhow!("INI file contains settings outside a section"));
381 for (sn, section) in map {
382 let sn = sn.parse().dcontext(&sn)?;
383 let vars = §ion.values;
385 for (key, val) in vars {
387 let skl = if key == "server" {
390 *self.keys_allowed.get(key.as_str()).ok_or_else(
391 || anyhow!("unknown configuration key")
394 if ! skl.contains(&sn, self.end) {
395 throw!(anyhow!("key not applicable in this kind of section"))
399 .with_context(|| format!("key {:?}", key))
400 .with_context(|| val.loc.to_string())?
403 let ent = self.sections.entry(sn)
404 .or_insert_with(|| ini::Section {
405 loc: section.loc.clone(),
409 for (key, ini::Val { val: raw, loc }) in vars {
410 let val = if raw.starts_with('\'') || raw.starts_with('"') {
412 if raw.contains('\\') {
414 anyhow!("quoted value contains backslash, not supported")
417 let quote = &raw[0..1];
419 let unq = raw[1..].strip_suffix(quote)
421 || anyhow!("mismatched quotes around quoted value")
424 if unq.contains(quote) {
426 "quoted value contains quote (escaping not supported)"
432 .with_context(|| format!("key {:?}", key))
433 .with_context(|| loc.to_string())?
437 let key = key.replace('-',"_");
438 ent.values.insert(key, ini::Val { val, loc: loc.clone() });
443 #[throws(AE)] // AE includes path
444 fn read_dir_d<A>(&mut self, path: &Path, anyway: OkAnyway<A>) -> Option<A>
446 let dir = fs::read_dir(path);
447 if let Some(anyway) = anyway.ok(&dir) { return Some(anyway) }
448 let dir = dir.context("open directory").dcontext(path)?;
450 let ent = ent.context("read directory").dcontext(path)?;
451 let leaf = ent.file_name();
452 let leaf = leaf.to_str();
453 let leaf = if let Some(leaf) = leaf { leaf } else { continue }; //utf8?
454 if leaf.len() == 0 { continue }
455 if ! leaf.chars().all(
456 |c| c=='-' || c=='_' || c.is_ascii_alphanumeric()
459 // OK we want this one
460 let ent = ent.path();
461 self.read_file(&ent, &|_| None::<Void>).dcontext(&ent)?;
466 #[throws(AE)] // AE includes everything
467 fn read_toplevel(&mut self, toplevel: &Path) {
468 enum Anyway { None, Dir }
469 match self.read_file(toplevel, &|e| match e {
470 e if e.kind() == EK::NotFound => Some(Anyway::None),
471 e if e.is_is_a_directory() => Some(Anyway::Dir),
474 .dcontext(toplevel).context("top-level config directory (or file)")?
476 None | Some(Anyway::None) => { },
478 Some(Anyway::Dir) => {
480 let anyway_none = |e: &io::Error| match e {
481 e if e.kind() == EK::NotFound => Some(AnywayNone),
485 let mk = |leaf: &str| {
486 [ toplevel, &PathBuf::from(leaf) ]
487 .iter().collect::<PathBuf>()
490 for &(try_main, desc) in &[
491 ("main.cfg", "main config file"),
492 ("master.cfg", "obsolete-named main config file"),
494 let main = mk(try_main);
496 match self.read_file(&main, &anyway_none)
497 .dcontext(main).context(desc)?
500 Some(AnywayNone) => { },
504 for &(try_dir, desc) in &[
505 ("config.d", "per-link config directory"),
506 ("secrets.d", "per-link secrets directory"),
508 let dir = mk(try_dir);
509 match self.read_dir_d(&dir, &anyway_none).context(desc)? {
511 Some(AnywayNone) => { },
518 #[throws(AE)] // AE includes extra, but does that this is extra
519 fn read_extra(&mut self, extra: &Path) {
522 match self.read_file(extra, &|e| match e {
523 e if e.is_is_a_directory() => Some(AnywayDir),
530 self.read_dir_d(extra, &|_| None::<Void>)?;
538 fn instances(&self, only_server: Option<&ServerName>) -> BTreeSet<LinkName> {
539 let mut links: BTreeSet<LinkName> = default();
541 let mut secrets_anyserver: BTreeSet<&ClientName> = default();
542 let mut secrets_anyclient: BTreeSet<&ServerName> = default();
543 let mut secret_global = false;
545 let mut putative_servers = BTreeSet::new();
546 let mut putative_clients = BTreeSet::new();
548 let mut note_server = |s| {
549 if let Some(only) = only_server { if s != only { return false } }
550 putative_servers.insert(s);
553 let mut note_client = |c| {
554 putative_clients.insert(c);
557 for (section, vars) in &self.sections {
558 let has_secret = || vars.values.contains_key("secret");
559 //dbg!(§ion, has_secret());
563 if ! note_server(&l.server) { continue }
564 note_client(&l.client);
565 if has_secret() { links.insert(l.clone()); }
567 SN::Server(ref s) => {
568 if ! note_server(s) { continue }
569 if has_secret() { secrets_anyclient.insert(s); }
571 SN::Client(ref c) => {
573 if has_secret() { secrets_anyserver.insert(c); }
576 if has_secret() { secret_global = true; }
582 //dbg!(&putative_servers, &putative_clients);
583 //dbg!(&secrets_anyserver, &secrets_anyclient, &secret_global);
585 // Add links which are justified by blanket secrets
586 for (client, server) in iproduct!(
587 putative_clients.into_iter().filter(
589 || secrets_anyserver.contains(c)
590 || ! secrets_anyclient.is_empty()
592 putative_servers.iter().cloned().filter(
594 || secrets_anyclient.contains(s)
595 || ! secrets_anyserver.is_empty()
598 links.insert(LinkName {
599 client: client.clone(),
600 server: server.clone(),
608 struct ResolveContext<'c> {
612 all_sections: Vec<SectionName>,
615 trait Parseable: Sized {
616 fn parse(s: Option<&str>) -> Result<Self, AE>;
618 /// Used for lookups with [`ResolveContest::ordinary`] etc.
620 /// Fails, if this setting ought to have been specified.
621 /// The caller will add a key name to the error.
622 fn default_for_ordinary() -> Result<Self, AE> {
623 Err(anyhow!("setting must be specified"))
626 /// Placeholder (infalliable)
628 /// Used (sometimes) for lookups with
629 /// [`ResolveContest::client`],
630 /// [`server`](`ResolveContest::server`) and
631 /// [`computed`](`ResolveContest::server`).
633 /// Ie, when the value need not be specified because
634 /// it may not be applicable, or could be computed.
636 /// We could use `Default::default` but
637 /// not all the types we want to use implement that.
638 fn unspecified() -> Self;
641 impl Parseable for Duration {
643 fn parse(s: Option<&str>) -> Duration {
644 // todo: would be nice to parse with humantime maybe
645 Duration::from_secs( s.value()?.parse()? )
647 fn unspecified() -> Duration { Duration::ZERO }
649 macro_rules! parseable_from_str { ($t:ty, $def:expr) => {
650 impl Parseable for $t {
652 fn parse(s: Option<&str>) -> $t { s.value()?.parse()? }
654 fn default_for_ordinary() -> Self { Parseable::unspecified() }
655 fn unspecified() -> Self { $def }
658 parseable_from_str!{u16, default() }
659 parseable_from_str!{u32, default() }
660 parseable_from_str!{String, default() }
661 parseable_from_str!{IpNet, default() }
662 parseable_from_str!{IpAddr, Ipv4Addr::UNSPECIFIED.into() }
666 "hippotat-unspecified:".parse()
667 .expect("failed to parse `hippotat-unspecified:` as a url")
670 impl<T:Parseable> Parseable for Vec<T> {
672 fn parse(s: Option<&str>) -> Vec<T> {
674 .split_ascii_whitespace()
675 .map(|s| Parseable::parse(Some(s)))
676 .collect::<Result<Vec<_>,_>>()?
679 fn default_for_ordinary() -> Self { Parseable::unspecified() }
680 fn unspecified() -> Self { default() }
684 #[derive(Debug,Copy,Clone,Eq,PartialEq)]
685 enum SectionKindList {
693 use SectionKindList as SKL;
696 fn special_server_section() -> Self { SN::Server(ServerName(
697 SPECIAL_SERVER_SECTION.into()
701 impl SectionKindList {
702 fn contains(self, s: &SectionName, end: LinkEnd) -> bool {
705 (SKL::Global, LinkEnd::Client) => matches!(s, SN::Link(_)
710 (SKL::Limits,_) => matches!(s, SN::ServerLimit(_)
713 (SKL::Global, LinkEnd::Server) => matches!(s, SN::Common
716 (SKL::Limited,_) => SKL::PerClient.contains(s, end)
717 | SKL::Limits .contains(s, end),
719 (SKL::ServerName,_) => matches!(s, SN::Common)
720 | matches!(s, SN::Server(ServerName(name))
721 if name == SPECIAL_SERVER_SECTION),
722 (SKL::None,_) => false,
728 fn lookup_raw<'a,'s,S>(&'a self, key: &'static str, sections: S)
729 -> Option<RawValRef<'a,'a,'s>>
730 where S: Iterator<Item=&'s SectionName>
732 for section in sections {
733 if let Some(val) = self.sections
735 .and_then(|s: &ini::Section| s.values.get(key))
737 return Some(RawValRef {
748 pub fn establish_server_name(&self) -> ServerName {
750 let raw = match self.lookup_raw(
752 [ &SectionName::Common, &SN::special_server_section() ].iter().cloned()
754 Some(raw) => raw.try_map(|os| os.value())?,
755 None => SPECIAL_SERVER_SECTION,
757 ServerName(raw.into())
761 impl<'c> ResolveContext<'c> {
762 fn first_of_raw(&'c self, key: &'static str, sections: SectionKindList)
763 -> Option<RawValRef<'c,'c,'c>> {
766 self.all_sections.iter()
767 .filter(|s| sections.contains(s, self.end))
772 fn first_of<T>(&self, key: &'static str, sections: SectionKindList)
776 match self.first_of_raw(key, sections) {
778 Some(raw) => Some(raw.try_map(Parseable::parse)?),
783 pub fn ordinary<T>(&self, key: &'static str, skl: SKL) -> T
786 match self.first_of(key, skl)? {
788 None => Parseable::default_for_ordinary()
789 .with_context(|| key.to_string())?,
794 pub fn limited<T>(&self, key: &'static str, skl: SKL) -> T
795 where T: Parseable + Ord
797 assert_eq!(skl, SKL::Limited);
798 let val = self.ordinary(key, SKL::PerClient)?;
799 if let Some(limit) = self.first_of(key, SKL::Limits)? {
807 pub fn client<T>(&self, key: &'static str, skl: SKL) -> T
810 LinkEnd::Client => self.ordinary(key, skl)?,
811 LinkEnd::Server => Parseable::unspecified(),
815 pub fn server<T>(&self, key: &'static str, skl: SKL) -> T
818 LinkEnd::Server => self.ordinary(key, skl)?,
819 LinkEnd::Client => Parseable::unspecified(),
824 pub fn computed<T>(&self, _key: &'static str, skl: SKL) -> T
827 assert_eq!(skl, SKL::None);
828 Parseable::unspecified()
832 pub fn special_ipif(&self, key: &'static str, skl: SKL) -> String {
833 assert_eq!(skl, SKL::PerClient); // we tolerate it in per-client sections
835 LinkEnd::Client => self.ordinary(key, SKL::PerClient)?,
836 LinkEnd::Server => self.ordinary(key, SKL::Global)?,
841 pub fn special_link(&self, _key: &'static str, skl: SKL) -> LinkName {
842 assert_eq!(skl, SKL::None);
847 pub fn special_max_up(&self, key: &'static str, skl: SKL) -> u32 {
848 assert_eq!(skl, SKL::Limited);
850 LinkEnd::Client => self.ordinary(key, SKL::Limited)?,
851 LinkEnd::Server => self.ordinary(key, SKL::Limits)?,
856 impl InstanceConfig {
858 fn complete(&mut self, end: LinkEnd) {
859 let mut vhosts = self.vnetwork.iter()
860 .map(|n| n.hosts()).flatten()
861 .filter({ let vaddr = self.vaddr; move |v| v != &vaddr });
863 if self.vaddr.is_unspecified() {
864 self.vaddr = vhosts.next().ok_or_else(
865 || anyhow!("vnetwork too small to generate vaddrr")
868 if self.vrelay.is_unspecified() {
869 self.vrelay = vhosts.next().ok_or_else(
870 || anyhow!("vnetwork too small to generate vrelay")
876 move |max_batch, key| {
877 if max_batch/2 < mtu {
878 throw!(anyhow!("max batch {:?} ({}) must be >= 2 x mtu ({}) \
879 (to allow for SLIP ESC-encoding)",
880 key, max_batch, mtu))
888 if self.url == Url::unspecified() {
889 let addr = self.addrs.get(0).ok_or_else(
890 || anyhow!("client needs addrs or url set")
895 IpAddr::V4(a) => format!("{}", a),
896 IpAddr::V6(a) => format!("[{}]", a),
900 p => format!(":{}", p),
905 self.effective_http_timeout = {
906 let a = self.http_timeout;
907 let b = self.http_timeout_grace;
908 a.checked_add(b).ok_or_else(
909 || anyhow!("calculate effective http timeout ({:?} + {:?})", a, b)
914 let t = self.target_requests_outstanding;
915 let m = self.max_requests_outstanding;
916 if t > m { throw!(anyhow!(
917 "target_requests_outstanding ({}) > max_requests_outstanding ({})",
922 check_batch(self.max_batch_up, "max_batch_up")?;
926 if self.addrs.is_empty() {
927 throw!(anyhow!("missing 'addrs' setting"))
929 check_batch(self.max_batch_down, "max_batch_down")?;
934 fn subst(var: &mut String,
935 kv: &mut dyn Iterator<Item=(&'static str, &dyn Display)>
938 .map(|(k,v)| (k.to_string(), v.to_string()))
939 .collect::<HashMap<String, String>>();
940 let bad = parking_lot::Mutex::new(vec![]);
941 *var = regex_replace_all!(
942 r#"%(?:%|\((\w+)\)s|\{(\w+)\}|.)"#,
944 |whole, k1, k2| (|| Ok::<_,String>({
945 if whole == "%%" { "%" }
946 else if let Some(&k) = [k1,k2].iter().find(|&&s| s != "") {
947 substs.get(k).ok_or_else(
948 || format!("unknown key %({})s", k)
951 throw!(format!("bad percent escape {:?}", &whole));
953 }))().unwrap_or_else(|e| { bad.lock().push(e); "" })
955 let bad = bad.into_inner();
956 if ! bad.is_empty() {
957 throw!(anyhow!("substitution failed: {}", bad.iter().format("; ")));
963 type DD<'d> = &'d dyn Display;
964 fn dv<T:Display>(v: &[T]) -> String {
965 format!("{}", v.iter().format(" "))
967 let mut ipif = mem::take(&mut self.ipif); // lets us borrow all of self
968 let s = &self; // just for abbreviation, below
969 let vnetwork = dv(&s.vnetwork);
970 let vroutes = dv(&s.vroutes);
972 let keys = &["local", "peer", "rnets", "ifname"];
973 let values = match end {
974 Server => [&s.vaddr as DD , &s.vrelay, &vnetwork, &s.ifname_server],
975 Client => [&s.link.client as DD, &s.vaddr, &vroutes, &s.ifname_client],
978 ( "mtu", &s.mtu as DD ),
983 &mut keys.iter().cloned()
985 .chain(always.iter().cloned()),
992 trait ResolveGlobal<'i> where Self: 'i {
993 fn resolve<I>(it: I) -> Self
994 where I: Iterator<Item=&'i Self>;
996 impl<'i,T> ResolveGlobal<'i> for T where T: Eq + Clone + Debug + 'i {
997 fn resolve<I>(mut it: I) -> Self
998 where I: Iterator<Item=&'i Self>
1000 let first = it.next().expect("empty instances no global!");
1001 for x in it { assert_eq!(x, first); }
1007 pub fn read(opts: &CommonOpts, end: LinkEnd)
1008 -> (Option<ServerName>, Vec<InstanceConfig>)
1011 let mut agg = Aggregate::new(
1013 InstanceConfig::FIELDS.iter().cloned().collect(),
1016 agg.read_string(DEFAULT_CONFIG.into(),
1017 "<build-in defaults>".as_ref())
1018 .expect("builtin configuration is broken");
1020 agg.read_toplevel(&opts.config)?;
1021 for extra in &opts.extra_config {
1022 agg.read_extra(extra).context("extra config")?;
1025 //eprintln!("GOT {:#?}", agg);
1028 })().context("read configuration")?;
1030 let server_name = match end {
1031 LinkEnd::Server => Some(agg.establish_server_name()?),
1032 LinkEnd::Client => None,
1035 let instances = agg.instances(server_name.as_ref());
1036 let mut ics = vec![];
1039 for link in instances {
1040 let rctx = ResolveContext {
1045 SN::Link(link.clone()),
1046 SN::Client(link.client.clone()),
1047 SN::Server(link.server.clone()),
1049 SN::ServerLimit(link.server.clone()),
1054 if rctx.first_of_raw("secret", SKL::PerClient).is_none() { continue }
1056 let mut ic = InstanceConfig::resolve_instance(&rctx)
1057 .with_context(|| format!("resolve config for {}", &link))?;
1060 .with_context(|| format!("complete config for {}", &link))?;
1068 pub async fn startup<F,T,G,GFut,U>(progname: &str, end: LinkEnd,
1069 opts: &CommonOpts, logopts: &LogOpts,
1071 where F: FnOnce(Option<ServerName>, &[InstanceConfig]) -> Result<T,AE>,
1072 G: FnOnce(T, Vec<InstanceConfig>) -> GFut,
1073 GFut: Future<Output=Result<U,AE>>,
1076 dedup_eyre_setup()?;
1077 let (server_name, ics) = config::read(opts, end)?;
1079 let t = f(server_name, &ics)?;
1080 if ics.is_empty() { throw!(anyhow!("no associations, quitting")); }
1082 logopts.log_init()?;
1083 let u = g(t, ics).await?;
1086 }.await.unwrap_or_else(|e| {
1087 eprintln!("{}: startup error: {}", progname, &e);