Skip to content

Commit 1a5119e

Browse files
wojcik91Maciej Wójcik
andauthored
disable auto startup for Windows tunnel service (#88)
* add nix setup * disable auto restart for tunnel service * lint fixes * handle command errors * handle interface removal errors * remove unnecessary error from enum * fix typo * fix typo * lint fixes * lint fix --------- Co-authored-by: Maciej Wójcik <[email protected]>
1 parent 6538ef7 commit 1a5119e

File tree

12 files changed

+192
-44
lines changed

12 files changed

+192
-44
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
/target
22
.idea/
33
.vscode/
4+
.direnv
5+
.envrc

examples/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{net::SocketAddr, str::FromStr};
22

33
use defguard_wireguard_rs::{
4-
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, Kernel, Userspace, WGApi,
4+
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, Kernel, WGApi,
55
WireguardInterfaceApi,
66
};
77
use x25519_dalek::{EphemeralSecret, PublicKey};

examples/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::str::FromStr;
22

33
use defguard_wireguard_rs::{
4-
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, Kernel, Userspace, WGApi,
4+
host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration, Kernel, WGApi,
55
WireguardInterfaceApi,
66
};
77
use x25519_dalek::{EphemeralSecret, PublicKey};

examples/userspace.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
use std::{
2-
io::{stdin, stdout, Read, Write},
3-
net::SocketAddr,
4-
str::FromStr,
5-
};
1+
#[cfg(target_os = "macos")]
2+
use std::io::{stdin, stdout, Read, Write};
63

7-
use defguard_wireguard_rs::{host::Peer, key::Key, net::IpAddrMask, InterfaceConfiguration};
84
#[cfg(target_os = "macos")]
95
use defguard_wireguard_rs::{Userspace, WGApi, WireguardInterfaceApi};
10-
use x25519_dalek::{EphemeralSecret, PublicKey};
116

7+
#[cfg(target_os = "macos")]
128
fn pause() {
139
let mut stdout = stdout();
1410
stdout.write_all(b"Press Enter to continue...").unwrap();

flake.lock

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

flake.nix

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
description = "Rust development flake";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
rust-overlay = {
8+
url = "github:oxalica/rust-overlay";
9+
inputs = {
10+
nixpkgs.follows = "nixpkgs";
11+
};
12+
};
13+
};
14+
15+
outputs = {
16+
nixpkgs,
17+
flake-utils,
18+
rust-overlay,
19+
...
20+
}:
21+
flake-utils.lib.eachDefaultSystem (system: let
22+
overlays = [(import rust-overlay)];
23+
pkgs = import nixpkgs {
24+
inherit system overlays;
25+
};
26+
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
27+
extensions = ["rust-analyzer" "rust-src" "rustfmt" "clippy"];
28+
};
29+
# define shared build inputs
30+
nativeBuildInputs = with pkgs; [rustToolchain pkg-config];
31+
in {
32+
devShells.default = pkgs.mkShell {
33+
inherit nativeBuildInputs;
34+
35+
# Specify the rust-src path (many editors rely on this)
36+
RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
37+
};
38+
});
39+
}

src/error.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ pub enum WireguardInterfaceError {
3333
KernelNotSupported,
3434
#[error("DNS error: {0}")]
3535
DnsError(String),
36-
#[error("Service installation failed: `{message}`")]
37-
ServiceInstallationFailed {
38-
err: std::io::Error,
39-
message: String,
40-
},
36+
#[cfg(target_os = "windows")]
37+
#[error("Service installation failed: `{0}`")]
38+
ServiceInstallationFailed(String),
39+
#[cfg(target_os = "windows")]
40+
#[error("Tunnel service removal failed: `{0}`")]
41+
ServiceRemovalFailed(String),
4142
#[error("Socket is closed: {0}")]
4243
SocketClosed(String),
4344
}

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ mod wireguard_interface;
7676
#[macro_use]
7777
extern crate log;
7878

79-
use std::{fmt, process::Output};
79+
use std::fmt;
80+
#[cfg(not(target_os = "windows"))]
81+
use std::process::Output;
8082

8183
#[cfg(feature = "serde")]
8284
use serde::{Deserialize, Serialize};
@@ -139,6 +141,7 @@ impl TryFrom<&InterfaceConfiguration> for Host {
139141
}
140142
}
141143

144+
#[cfg(not(target_os = "windows"))]
142145
/// Utility function which checks external command output status.
143146
fn check_command_output_status(output: Output) -> Result<(), WireguardInterfaceError> {
144147
if !output.status.success() {

src/netlink.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ fn get_interface_index(ifname: &str) -> NetlinkResult<Option<u32>> {
467467
Ok(None)
468468
}
469469

470+
#[cfg(test)]
470471
/// Get default route for a given address family.
471472
pub(crate) fn get_gateway(address_family: AddressFamily) -> NetlinkResult<Option<IpAddr>> {
472473
let header = RouteHeader {
@@ -493,16 +494,13 @@ pub(crate) fn get_gateway(address_family: AddressFamily) -> NetlinkResult<Option
493494
// Because messages can't be properly filtered, find the first `Gateway`.
494495
if let RouteNetlinkMessage::NewRoute(RouteMessage { attributes, .. }) = message {
495496
for nla in attributes {
496-
match nla {
497-
RouteAttribute::Gateway(address) => {
498-
debug!("Found gateway {address:?}");
499-
match address {
500-
RouteAddress::Inet(ipv4) => return Ok(Some(IpAddr::V4(ipv4))),
501-
RouteAddress::Inet6(ipv6) => return Ok(Some(IpAddr::V6(ipv6))),
502-
_ => (),
503-
}
497+
if let RouteAttribute::Gateway(address) = nla {
498+
debug!("Found gateway {address:?}");
499+
match address {
500+
RouteAddress::Inet(ipv4) => return Ok(Some(IpAddr::V4(ipv4))),
501+
RouteAddress::Inet6(ipv6) => return Ok(Some(IpAddr::V6(ipv6))),
502+
_ => (),
504503
}
505-
_ => (),
506504
}
507505
}
508506
}

src/utils.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
use std::io::{BufRead, BufReader, Cursor, Error as IoError};
33
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "netbsd"))]
44
use std::net::{Ipv4Addr, Ipv6Addr};
5+
use std::net::{SocketAddr, ToSocketAddrs};
56
#[cfg(target_os = "linux")]
67
use std::{collections::HashSet, fs::OpenOptions};
78
#[cfg(any(target_os = "freebsd", target_os = "linux", target_os = "netbsd"))]
89
use std::{io::Write, process::Stdio};
9-
use std::{
10-
net::{IpAddr, SocketAddr, ToSocketAddrs},
11-
process::Command,
12-
};
10+
#[cfg(not(target_os = "windows"))]
11+
use std::{net::IpAddr, process::Command};
1312

1413
#[cfg(target_os = "freebsd")]
1514
use crate::check_command_output_status;
15+
#[cfg(not(target_os = "windows"))]
16+
use crate::Peer;
17+
use crate::WireguardInterfaceError;
1618
#[cfg(any(target_os = "freebsd", target_os = "macos", target_os = "netbsd"))]
1719
use crate::{
1820
bsd::{add_gateway, add_linked_route, get_gateway},
@@ -21,7 +23,6 @@ use crate::{
2123
};
2224
#[cfg(target_os = "linux")]
2325
use crate::{check_command_output_status, netlink, IpVersion};
24-
use crate::{Peer, WireguardInterfaceError};
2526

2627
#[cfg(any(target_os = "freebsd", target_os = "linux", target_os = "netbsd"))]
2728
pub(crate) fn configure_dns(

0 commit comments

Comments
 (0)