support CIDR for whitelist

Signed-off-by: Xerxes-2 <dspxue@gmail.com>
This commit is contained in:
Xerxes-2 2022-11-23 02:42:36 +11:00
parent 1591e0cfe7
commit cf721e9bb3
5 changed files with 39 additions and 3 deletions

31
Cargo.lock generated
View File

@ -633,6 +633,19 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "cidr-utils"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "355d5b5df67e58b523953d0c1a8d3d2c05f5af51f1332b0199b9c92263614ed0"
dependencies = [
"debug-helper",
"num-bigint",
"num-traits 0.2.15",
"once_cell",
"regex",
]
[[package]] [[package]]
name = "clang-sys" name = "clang-sys"
version = "1.3.3" version = "1.3.3"
@ -1244,6 +1257,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7046468a81e6a002061c01e6a7c83139daf91b11c30e66795b13217c2d885c8b" checksum = "7046468a81e6a002061c01e6a7c83139daf91b11c30e66795b13217c2d885c8b"
[[package]]
name = "debug-helper"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f578e8e2c440e7297e008bb5486a3a8a194775224bbc23729b0dbdfaeebf162e"
[[package]] [[package]]
name = "default-net" name = "default-net"
version = "0.11.0" version = "0.11.0"
@ -3291,6 +3310,17 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "num-bigint"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
dependencies = [
"autocfg 1.1.0",
"num-integer",
"num-traits 0.2.15",
]
[[package]] [[package]]
name = "num-complex" name = "num-complex"
version = "0.4.2" version = "0.4.2"
@ -4375,6 +4405,7 @@ dependencies = [
"cc", "cc",
"cfg-if 1.0.0", "cfg-if 1.0.0",
"chrono", "chrono",
"cidr-utils",
"clap 3.2.17", "clap 3.2.17",
"clipboard", "clipboard",
"cocoa", "cocoa",

View File

@ -68,6 +68,7 @@ url = { version = "2.1", features = ["serde"] }
reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features=false } reqwest = { version = "0.11", features = ["blocking", "json", "rustls-tls"], default-features=false }
chrono = "0.4.23" chrono = "0.4.23"
cidr-utils = "0.5.9"
[target.'cfg(not(any(target_os = "android", target_os = "linux")))'.dependencies] [target.'cfg(not(any(target_os = "android", target_os = "linux")))'.dependencies]
cpal = "0.13.5" cpal = "0.13.5"

View File

@ -133,7 +133,7 @@ void changeWhiteList({Function()? callback}) async {
final ips = final ips =
newWhiteListField.trim().split(RegExp(r"[\s,;\n]+")); newWhiteListField.trim().split(RegExp(r"[\s,;\n]+"));
// test ip // test ip
final ipMatch = RegExp(r"^\d+\.\d+\.\d+\.\d+$"); final ipMatch = RegExp(r"^\d+\.\d+\.\d+\.\d+(\/\d+)?$");
for (final ip in ips) { for (final ip in ips) {
if (!ipMatch.hasMatch(ip)) { if (!ipMatch.hasMatch(ip)) {
msg = "${translate("Invalid IP")} $ip"; msg = "${translate("Invalid IP")} $ip";

View File

@ -7,6 +7,7 @@ use crate::video_service;
#[cfg(any(target_os = "android", target_os = "ios"))] #[cfg(any(target_os = "android", target_os = "ios"))]
use crate::{common::DEVICE_NAME, flutter::connection_manager::start_channel}; use crate::{common::DEVICE_NAME, flutter::connection_manager::start_channel};
use crate::{ipc, VERSION}; use crate::{ipc, VERSION};
use cidr_utils::cidr::IpCidr;
use hbb_common::{ use hbb_common::{
config::Config, config::Config,
fs, fs,
@ -631,7 +632,10 @@ impl Connection {
.is_none() .is_none()
&& whitelist && whitelist
.iter() .iter()
.filter(|x| x.parse() == Ok(addr.ip())) .filter(|x| match IpCidr::from_str(x) {
Ok(cidr) => cidr.contains(addr.ip()),
Err(_) => false,
})
.next() .next()
.is_none() .is_none()
{ {

View File

@ -395,7 +395,7 @@ class MyIdMenu: Reactor.Component {
if (value) { if (value) {
var values = value.split(/[\s,;\n]+/g); var values = value.split(/[\s,;\n]+/g);
for (var ip in values) { for (var ip in values) {
if (!ip.match(/^\d+\.\d+\.\d+\.\d+$/)) { if (!ip.match(/^\d+\.\d+\.\d+\.\d+(\/\d+)?$/)) {
return translate("Invalid IP") + ": " + ip; return translate("Invalid IP") + ": " + ip;
} }
} }