Only use wake whitelist if enabled in server.properties

This commit is contained in:
timvisee
2021-11-28 22:00:59 +01:00
parent c477e45553
commit 5b5a2bf8ae
2 changed files with 49 additions and 5 deletions

View File

@@ -145,3 +145,37 @@ fn rewrite_contents(contents: String, mut changes: HashMap<&str, String>) -> Opt
None
}
}
/// Read the given property from the given server.properties file.o
///
/// Returns `None` if file does not contain the property.
pub fn read_property<P: AsRef<Path>>(file: P, property: &str) -> Option<String> {
// File must exist
if !file.as_ref().is_file() {
warn!(target: "lazymc",
"Failed to read property from {} file, it does not exist",
FILE,
);
return None;
}
// Read contents
let contents = match fs::read_to_string(&file) {
Ok(contents) => contents,
Err(err) => {
error!(target: "lazymc",
"Failed to read property from {} file, could not load: {}",
FILE,
err,
);
return None;
}
};
// Find property, return value
contents
.lines()
.filter_map(|line| line.split_once('='))
.find(|(p, _)| p.trim().to_lowercase() == property.to_lowercase())
.map(|(_, v)| v.trim().to_string())
}

View File

@@ -7,7 +7,7 @@ use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
use crate::config::{Config, Server as ConfigServer};
use crate::mc::ban::{self, BannedIps};
use crate::mc::whitelist;
use crate::mc::{server_properties, whitelist};
use crate::server::Server;
/// File watcher debounce time.
@@ -103,11 +103,12 @@ fn update(config: &Config, server: &Server, dir: &Path, path: &Path) {
}
// Update whitelist
if path.ends_with(whitelist::WHITELIST_FILE) || path.ends_with(whitelist::OPS_FILE) {
if path.ends_with(whitelist::WHITELIST_FILE)
|| path.ends_with(whitelist::OPS_FILE)
|| path.ends_with(server_properties::FILE)
{
reload_whitelist(config, server, dir);
}
// TODO: update on server.properties change
}
/// Reload banned IPs.
@@ -149,7 +150,16 @@ fn reload_whitelist(config: &Config, server: &Server, dir: &Path) {
return;
}
// TODO: whitelist must be enabled in server.properties
// Must be enabled in server.properties
let enabled =
server_properties::read_property(&dir.join(server_properties::FILE), "white-list")
.map(|v| v.trim() == "true")
.unwrap_or(false);
if !enabled {
server.set_whitelist_blocking(None);
debug!(target: "lazymc", "Not using whitelist, not enabled in {}", server_properties::FILE);
return;
}
trace!(target: "lazymc", "Reloading whitelisted users...");