Skip to content

Commit

Permalink
Only use wake whitelist if enabled in server.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
timvisee committed Nov 28, 2021
1 parent c477e45 commit 5b5a2bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/mc/server_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
20 changes: 15 additions & 5 deletions src/service/file_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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...");

Expand Down

0 comments on commit 5b5a2bf

Please sign in to comment.