Added option to check for no password (#319)

This commit is contained in:
Michael
2021-04-01 12:40:46 -07:00
committed by GitHub
parent fe0995319a
commit 4521fe28d3
7 changed files with 50 additions and 33 deletions

View File

@@ -35,7 +35,7 @@ ENV PUID=1000 \
NAME="Valheim Docker" \
WORLD="Dedicated" \
PUBLIC="1" \
PASSWORD="12345" \
PASSWORD="" \
# Auto Update Configs
AUTO_UPDATE="0" \
AUTO_UPDATE_SCHEDULE="0 1 * * *" \

View File

@@ -49,7 +49,7 @@
| NAME | `Valheim Docker` | TRUE | The name of your server! Make it fun and unique! |
| WORLD | `Dedicated` | TRUE | This is used to generate the name of your world. |
| PUBLIC | `1` | FALSE | Sets whether or not your server is public on the server list. |
| PASSWORD | `12345` | TRUE | Set this to something unique! |
| PASSWORD | ` ` | TRUE | Set this to something unique! |
| TYPE | `Vanilla` | FALSE | This can be set to `ValheimPlus`, `BepInEx`, `BepInExFull` or `Vanilla` |
| MODS | ` ` | FALSE | This is an array of mods separated by comma and a new line. [Click Here for Examples](./docs/getting_started_with_mods.md) Supported files are `zip`, `dll`, and `cfg`. |
| AUTO_UPDATE | `0` | FALSE | Set to `1` if you want your container to auto update! This means at the times indicated by `AUTO_UPDATE_SCHEDULE` it will check for server updates. If there is an update then the server will be shut down, updated, and brought back online if the server was running before. |

View File

@@ -4,8 +4,8 @@ use clap::ArgMatches;
use log::{error, info};
pub fn invoke(args: &ArgMatches) {
let message = parse_arg_variable(&args, "MESSAGE", String::from("Test Notification"));
let webhook_url = parse_arg_variable(&args, "webhook_url", "".to_string());
let message = parse_arg_variable(&args, "MESSAGE", "Test Notification");
let webhook_url = parse_arg_variable(&args, "webhook_url", "");
if !webhook_url.is_empty() {
info!("Sending Broadcast: {}", message);
NotificationEvent::Broadcast.send_custom_notification(webhook_url.as_str(), message.as_str())

View File

@@ -22,7 +22,7 @@ pub fn fetch_public_address() -> Result<SocketAddrV4, AddrParseError> {
fn parse_address(args: &ArgMatches) -> Result<SocketAddrV4, AddrParseError> {
let has_address = args.is_present("address");
if has_address {
SocketAddrV4::from_str(&parse_arg_variable(args, "address", "".to_string()))
SocketAddrV4::from_str(&parse_arg_variable(args, "address", ""))
} else {
fetch_public_address()
}

View File

@@ -16,11 +16,10 @@ pub fn load_config() -> ValheimArguments {
let config = read_config(file);
debug!("Checking password compliance...");
if config.password.len() < 5 {
if config.password.len() < 5 && !config.password.is_empty() {
error!("The supplied password is too short! It must be 5 characters or greater!");
exit(1);
}
config
}
@@ -47,7 +46,7 @@ pub fn write_config(config: ManagedFile, args: &ArgMatches) -> bool {
let command = match fs::canonicalize(PathBuf::from(parse_arg_variable(
args,
"server_executable",
server_executable.to_string(),
server_executable,
))) {
Ok(command_path) => command_path.to_str().unwrap().to_string(),
Err(_) => {
@@ -57,11 +56,11 @@ pub fn write_config(config: ManagedFile, args: &ArgMatches) -> bool {
};
let content = &ValheimArguments {
port: parse_arg_variable(args, "port", "2456".to_string()),
name: parse_arg_variable(args, "name", "Valheim powered by Odin".to_string()),
world: parse_arg_variable(args, "world", "Dedicated".to_string()),
public: parse_arg_variable(args, "public", "1".to_string()),
password: parse_arg_variable(args, "password", "12345".to_string()),
port: parse_arg_variable(args, "port", "2456"),
name: parse_arg_variable(args, "name", "Valheim powered by Odin"),
world: parse_arg_variable(args, "world", "Dedicated"),
public: parse_arg_variable(args, "public", "1"),
password: parse_arg_variable(args, "password", ""),
command,
};
let content_to_write = serde_json::to_string(content).unwrap();

View File

@@ -1,25 +1,27 @@
use daemonize::{Daemonize, DaemonizeError};
use log::{debug, info};
use log::{debug, error, info};
use std::{io, process::Child};
use crate::mods::bepinex::BepInExEnvironment;
use crate::utils::common_paths::saves_directory;
use crate::utils::common_paths::{game_directory, saves_directory};
use crate::utils::environment::fetch_var;
use crate::{
constants,
executable::create_execution,
files::{create_file, ValheimArguments},
messages,
utils::{environment, get_working_dir},
utils::environment,
};
use std::process::exit;
type CommandResult = io::Result<Child>;
pub fn start_daemonized(config: ValheimArguments) -> Result<CommandResult, DaemonizeError> {
let stdout = create_file(format!("{}/logs/valheim_server.log", get_working_dir()).as_str());
let stderr = create_file(format!("{}/logs/valheim_server.err", get_working_dir()).as_str());
let stdout = create_file(format!("{}/logs/valheim_server.log", game_directory()).as_str());
let stderr = create_file(format!("{}/logs/valheim_server.err", game_directory()).as_str());
Daemonize::new()
.working_directory(get_working_dir())
.working_directory(game_directory())
.user("steam")
.group("steam")
.stdout(stdout)
@@ -44,28 +46,47 @@ pub fn start(config: &ValheimArguments) -> CommandResult {
info!("--------------------------------------------------------------------------------------------------------------");
let ld_library_path_value = environment::fetch_multiple_var(
constants::LD_LIBRARY_PATH_VAR,
format!("{}/linux64", get_working_dir()).as_str(),
format!("{}/linux64", game_directory()).as_str(),
);
debug!("Setting up base command");
let base_command = command
let mut base_command = command
// Extra launch arguments
.arg(fetch_var(
"SERVER_EXTRA_LAUNCH_ARGS",
"-nographics -batchmode",
))
// Required vars
.args(&[
"-nographics",
"-batchmode",
"-port",
&config.port.as_str(),
"-name",
&config.name.as_str(),
"-world",
&config.world.as_str(),
"-password",
&config.password.as_str(),
"-public",
&config.public.as_str(),
"-savedir",
&saves_directory(),
])
.env("SteamAppId", environment::fetch_var("APPID", "892970"))
.current_dir(get_working_dir());
.current_dir(game_directory());
let is_public = config.public.eq("1");
let is_vanilla = fetch_var("TYPE", "vanilla").eq_ignore_ascii_case("vanilla");
let no_password = config.password.is_empty();
// If no password env variable
if !is_public && !is_vanilla && no_password {
debug!("No password found, skipping password flag.")
} else if no_password && (is_public || is_vanilla) {
error!("Cannot run you server with no password! PUBLIC must be 0 and cannot be a Vanilla type server.");
exit(1)
} else {
debug!("Password found, adding password flag.");
base_command = base_command.args(&["-password", &config.password.as_str()]);
}
// Tack on save dir at the end.
base_command = base_command.args(&["-savedir", &saves_directory()]);
info!("Executable: {}", &config.command);
info!("Launching Command...");
let bepinex_env = BepInExEnvironment::new();

View File

@@ -16,7 +16,7 @@ pub fn get_working_dir() -> String {
)
}
pub fn parse_arg_variable(args: &ArgMatches, name: &str, default: String) -> String {
pub fn parse_arg_variable(args: &ArgMatches, name: &str, default: &str) -> String {
debug!("Checking env for {}", name);
if let Ok(env_val) = env::var(name.to_uppercase()) {
if !env_val.is_empty() {
@@ -28,10 +28,7 @@ pub fn parse_arg_variable(args: &ArgMatches, name: &str, default: String) -> Str
debug!("Env variable found {}={}", name, env_val);
return env_val;
}
args
.value_of(name)
.unwrap_or_else(|| default.as_str())
.to_string()
args.value_of(name).unwrap_or(default).to_string()
}
pub fn path_exists(path: &str) -> bool {