@@ -5,6 +5,22 @@ mod rules;
55use crate :: cli:: verbosity:: Verbosity ;
66use anyhow:: { Context , Result , bail} ;
77
8+ /// Check if an IP address is in a private/reserved range (RFC 1918, loopback, link-local)
9+ #[ must_use]
10+ pub fn is_private_ip ( ip : & std:: net:: Ipv4Addr ) -> bool {
11+ let o = ip. octets ( ) ;
12+ // 10.0.0.0/8
13+ o[ 0 ] == 10
14+ // 172.16.0.0/12
15+ || ( o[ 0 ] == 172 && ( 16 ..=31 ) . contains ( & o[ 1 ] ) )
16+ // 192.168.0.0/16
17+ || ( o[ 0 ] == 192 && o[ 1 ] == 168 )
18+ // 127.0.0.0/8 (loopback)
19+ || o[ 0 ] == 127
20+ // 169.254.0.0/16 (link-local)
21+ || ( o[ 0 ] == 169 && o[ 1 ] == 254 )
22+ }
23+
824fn check_root ( ) -> Result < ( ) > {
925 let euid = unsafe { libc:: geteuid ( ) } ;
1026 if euid != 0 {
@@ -19,13 +35,7 @@ fn validate_ipv4(ip: &str) -> Result<()> {
1935 let IpAddr :: V4 ( v4) = addr else {
2036 bail ! ( "IPv6 addresses are not supported: {ip}" ) ;
2137 } ;
22- let o = v4. octets ( ) ;
23- if o[ 0 ] == 10
24- || ( o[ 0 ] == 172 && ( 16 ..=31 ) . contains ( & o[ 1 ] ) )
25- || ( o[ 0 ] == 192 && o[ 1 ] == 168 )
26- || o[ 0 ] == 127
27- || ( o[ 0 ] == 169 && o[ 1 ] == 254 )
28- {
38+ if is_private_ip ( & v4) {
2939 bail ! ( "{ip} is a private/reserved IP address. VPN peer must be a public IP" ) ;
3040 }
3141 Ok ( ( ) )
@@ -91,6 +101,7 @@ pub fn disable(verbose: Verbosity) -> Result<()> {
91101///
92102/// # Errors
93103/// Returns an error if the firewall status cannot be queried
104+ #[ must_use = "status returns the current state which should be displayed or checked" ]
94105pub fn status ( ) -> Result < String > {
95106 pf:: status ( )
96107}
@@ -129,10 +140,16 @@ pub fn show_interfaces(verbose: Verbosity) -> Result<String> {
129140 let mut out = String :: new ( ) ;
130141 let _ = writeln ! ( out, "Interface MAC address IP" ) ;
131142
132- let has_vpn = interfaces. iter ( ) . any ( |i| i . is_p2p ) ;
143+ let has_vpn = interfaces. iter ( ) . any ( network :: InterfaceInfo :: is_p2p) ;
133144
134145 for iface in & interfaces {
135- let _ = writeln ! ( out, "{:<10} {:<19} {}" , iface. name, iface. mac, iface. ip) ;
146+ let _ = writeln ! (
147+ out,
148+ "{:<10} {:<19} {}" ,
149+ iface. name( ) ,
150+ iface. mac( ) ,
151+ iface. ip( )
152+ ) ;
136153 }
137154
138155 // Show public IP
0 commit comments