Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose $VOPONO_NS_IP environment variable to the PostUp and PreDown scripts, and the application to run #154

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ The current network namespace name is provided to the PostUp and PreDown
scripts in the environment variable `$VOPONO_NS`. It is temporarily set
when running these scripts only.

Similarly, the network namespace IP address is provided via `$VOPONO_NS_IP`,
and is available to the PostUp and PreDown scripts, and the application to
run itself. `$VOPONO_NS_IP` is useful if you'd like to configure a server
running within the network namespace to listen on its local IP address only
(see below, for more information on that).


### Host scripts

Host scripts to run just after a network namespace is created and just before it is destroyed,
Expand Down Expand Up @@ -266,13 +273,20 @@ Note in the case of `transmission-daemon` the `-a *.*.*.*` argument is
required to allow external connections to the daemon's web portal (your
host machine will now count as external to the network namespace).

Instead of listening on `*.*.*.*` you also can listen on `$VOPONO_NS_IP`,
to listen on an IP address that is only reachable from the same machine,
the network namespace runs on.

When finished with vopono, you must manually kill the
`transmission-daemon` since the PID changes (i.e. use `killall`).

By default, vopono runs a small TCP proxy to proxy the ports on your
host machine to the ports on the network namespace - if you do not want
this to run use the `--no-proxy` flag.

In this case, you can read the IP of the network namespace from the
terminal, or use `$VOPONO_NS_IP` to get it (e.g. to use it in a script).

#### systemd service

For the above you may want to run vopono as a systemd service. If your
Expand Down
9 changes: 9 additions & 0 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,13 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {
}
}

// Temporarily set env var referring to this network namespace IP
// for the PostUp script and the application:
std::env::set_var(
"VOPONO_NS_IP",
&ns.veth_pair_ips.as_ref().unwrap().namespace_ip.to_string(),
);

// Run PostUp script (if any)
// Temporarily set env var referring to this network namespace name
if let Some(pucmd) = postup {
Expand All @@ -389,6 +396,8 @@ pub fn exec(command: ExecCommand) -> anyhow::Result<()> {

let application = ApplicationWrapper::new(&ns, &command.application, user)?;

std::env::remove_var("VOPONO_NS_IP");

// Launch TCP proxy server on other threads if forwarding ports
// TODO: Fix when running as root
let mut proxy = Vec::new();
Expand Down
10 changes: 10 additions & 0 deletions src/netns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ impl Drop for NetworkNamespace {
// Run PreDown script (if any)
if let Some(pdcmd) = self.predown.as_ref() {
std::env::set_var("VOPONO_NS", &self.name);
std::env::set_var(
"VOPONO_NS_IP",
&self
.veth_pair_ips
.as_ref()
.unwrap()
.namespace_ip
.to_string(),
);
if self.predown_user.is_some() {
std::process::Command::new("sudo")
.args(&["-Eu", self.predown_user.as_ref().unwrap(), pdcmd])
Expand All @@ -471,6 +480,7 @@ impl Drop for NetworkNamespace {
std::process::Command::new(&pdcmd).spawn().ok();
}
std::env::remove_var("VOPONO_NS");
std::env::remove_var("VOPONO_NS_IP");
}

self.openvpn = None;
Expand Down