Skip to content

Commit 912ea96

Browse files
authored
Merge pull request #58 from dvdsk/print-local-ip
adds local machine ip as note to some errors
2 parents a49f219 + cd39608 commit 912ea96

File tree

9 files changed

+91
-9
lines changed

9 files changed

+91
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.3.1] - 2023-06-11
9+
10+
### Added
11+
- Note the current machines local IP when the challenge server is not reachable
12+
813
## [0.3.0] - 2023-06-11
914

1015
### Added

Cargo.lock

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "renewc"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["David Kleingeld <[email protected]>"]
55
edition = "2021"
66
rust-version = "1.70"
@@ -43,6 +43,7 @@ async-trait = "0.1"
4343
data-encoding = "2.4"
4444
pem = "2"
4545
strum = { version = "0.24", features = ["derive"] }
46+
local-ip-address = "0.5.3"
4647

4748
[dev-dependencies]
4849
libc = "0.2"

main/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub struct Config {
118118
pub(crate) domains: Vec<String>,
119119
pub(crate) email: Vec<String>,
120120
pub production: bool,
121-
pub(crate) port: u16,
121+
pub port: u16,
122122
pub output_config: OutputConfig,
123123
pub reload: Option<String>,
124124
pub(crate) renew_early: bool,

main/src/diagnostics/reachable.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::time::Duration;
44
use color_eyre::{eyre, Help};
55
use hyper::{body, Body, Response, StatusCode, Uri};
66
use tokio::time::timeout;
7-
use tracing::debug;
7+
use tracing::{debug, instrument};
88

99
use crate::config::Config;
1010
use crate::renew::server::Http01Challenge;
@@ -21,14 +21,15 @@ async fn check_response(resp: Response<Body>, key_auth: &str, domain: &str) -> e
2121
StatusCode::SERVICE_UNAVAILABLE | StatusCode::NOT_FOUND => {
2222
Err(eyre::eyre!(
2323
"Could not reach {APP} via {domain}"
24-
)
24+
))
2525
.note("Another server is getting traffic for external port 80")
26-
.suggestion(format!("Check if port 80 is forwarded to a port on this machine. If it is configure {APP} to use that port with the `--port` option. If not forward port 80 to this machine")))
26+
.suggestion(format!("Check if port 80 is forwarded to a port on this machine. If it is configure {APP} to use that port with the `--port` option. If not forward port 80 to this machine")).with_local_ip_note()
2727
}
2828
_ => unreachable!("got incorrect status code: {resp:?}"),
2929
}
3030
}
3131

32+
#[instrument(ret, skip(key_auth, path))]
3233
async fn check(path: &str, domain: &str, key_auth: &str) -> eyre::Result<()> {
3334
let url = format!("http://{domain}{path}");
3435
debug!("checking: {url}");
@@ -40,9 +41,11 @@ async fn check(path: &str, domain: &str, key_auth: &str) -> eyre::Result<()> {
4041
Ok(Err(e)) if e.is_timeout() || e.is_connect() => {
4142
Err(eyre::eyre!("Could not reach {APP} via {domain}"))
4243
.suggestion("Forward port 80 to this machine")
44+
.with_local_ip_note()
4345
}
4446
Err(_) => Err(eyre::eyre!("Could not reach {APP} via {domain}"))
45-
.suggestion("Forward port 80 to this machine"),
47+
.suggestion("Forward port 80 to this machine")
48+
.with_local_ip_note(),
4649
Ok(Err(e)) => unreachable!("reqwest error: {e:?}"),
4750
}
4851
}
@@ -62,3 +65,18 @@ pub async fn server(config: &Config, challanges: &[Http01Challenge]) -> eyre::Re
6265

6366
Ok(())
6467
}
68+
69+
trait WithLocalIp {
70+
fn with_local_ip_note(self) -> Self;
71+
}
72+
73+
impl<T> WithLocalIp for eyre::Result<T> {
74+
fn with_local_ip_note(self) -> Self {
75+
match local_ip_address::local_ip() {
76+
Ok(ip) => self.with_note(|| format!("This machines local IP adress: {ip:?}")),
77+
Err(e) => self.with_warning(|| {
78+
format!("Failed to be helpfull and find this machines local IP error: {e:?}")
79+
}),
80+
}
81+
}
82+
}

main/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ async fn main() -> eyre::Result<()> {
3838
match cli.command {
3939
Commands::Run(args) => {
4040
let config = Config::try_from(args)?;
41-
let Some(certs): Option<Signed<pem::Pem>> = run(&mut InstantAcme {}, &mut stdout, &config, debug).await? else {
41+
let Some(certs): Option<Signed<pem::Pem>> =
42+
run(&mut InstantAcme {}, &mut stdout, &config, debug).await? else {
4243
return Ok(());
4344
};
4445
cert::store::on_disk(&config, certs, &mut stdout)

main/tests/behaviour.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use shared::TestAcme;
1010
use shared::TestPrinter;
1111
use tracing::info;
1212

13-
1413
#[tokio::test]
1514
async fn production_does_not_overwrite_valid_production() {
1615
shared::setup_color_eyre();

main/tests/diagnostics.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,23 @@ async fn insufficent_permissions() {
5454
assert!(test.contains("You normally need sudo to attach to ports below 1025"));
5555
assert!(test.contains("port: 42"));
5656
}
57+
58+
#[tokio::test]
59+
async fn port_forward_suggestion_includes_ip() {
60+
shared::setup_color_eyre();
61+
shared::setup_tracing();
62+
63+
let dir = tempfile::tempdir().unwrap();
64+
// port 1119 is assigned to a use by the IANA
65+
// and should not route to the current machine
66+
let config = Config::test(1119, &dir.path());
67+
let err = run::<Pem>(&mut InstantAcme {}, &mut TestPrinter, &config, true)
68+
.await
69+
.unwrap_err();
70+
71+
let test = format!("{err:?}");
72+
assert!(
73+
test.contains("This machines local IP adress:"),
74+
"\n\n***********error was:\n\n {test}\n\n************\n"
75+
);
76+
}

main/tests/shared/gen_cert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn ca_cert(is_staging: bool) -> Certificate {
2323
pub fn client_cert(valid_till: OffsetDateTime) -> Certificate {
2424
let subject_alt_names = vec!["example.org".to_string()];
2525
let mut params = CertificateParams::new(subject_alt_names);
26-
params.not_after = dbg!(valid_till);
26+
params.not_after = valid_till;
2727
Certificate::from_params(params).unwrap()
2828
}
2929

0 commit comments

Comments
 (0)