From: Bram Matthys Date: Mon, 24 Apr 2023 09:48:10 +0000 (+0200) Subject: Add Settings - RPC Servers (start of multi-server work). X-Git-Tag: 0.9~102 X-Git-Url: https://jfr.im/git/irc/unrealircd/unrealircd-webpanel.git/commitdiff_plain/41aad10cbe0755aee50f597cb2cadd3029de7e1d?hp=630261974fd350964443aed92704938d4a8bd858 Add Settings - RPC Servers (start of multi-server work). This also makes $config["unrealircd"] an array now. --- diff --git a/api/installation2.php b/api/installation2.php index e9dde10..288f4be 100644 --- a/api/installation2.php +++ b/api/installation2.php @@ -2,36 +2,45 @@ require_once "../inc/common.php"; -/* only let this happen */ -if (isset($config['unrealircd']) && empty($config['unrealircd']['host'])) - die(json_encode(["error" => "Already configured."])); - if (!isset($_POST) || empty($_POST)) die(json_encode(["error" => "Incorrect parameters"])); -foreach($_POST as $key => $str) - ${$key} = $str; +foreach(array("tls_verify","host","port","user","password","edit_existing") as $k) +{ + if (!isset($_POST[$k])) + die("MISSING: $k"); + ${$k} = $_POST[$k]; +} -if ($method == "rpc") +if ($tls_verify == "false") + $tls_verify = false; +elseif ($tls_verify == "true") + $tls_verify = true; + +if (($edit_existing) && ($password == "****************")) { - if (isset($tls_verify)) - { - if ($tls_verify == "false") - $tls_verify = false; - elseif ($tls_verify == "true") - $tls_verify = true; - } - try { - $rpc = new UnrealIRCd\Connection - ( - "wss://$host:$port", - "$user:$password", - ["tls_verify" => $tls_verify] - ); - } - catch (Exception $e) - { - die(json_encode(["error" => "Unable to connect to UnrealIRCd: ".$e->getMessage()])); - } - die(json_encode(["success" => "Successfully connected"])); + /* If editing existing and password unchanged, + * try to look up existing password. + */ + if (isset($config["unrealircd"][$edit_existing])) + { + $password = $config["unrealircd"][$edit_existing]["rpc_password"]; + if (str_starts_with($password, "secret:")) + $password = secret_decrypt($password); + } } + +try { + $rpc = new UnrealIRCd\Connection + ( + "wss://$host:$port", + "$user:$password", + ["tls_verify" => $tls_verify] + ); +} +catch (Exception $e) +{ + die(json_encode(["error" => "Unable to connect to UnrealIRCd: ".$e->getMessage()])); +} + +die(json_encode(["success" => "Successfully connected"])); diff --git a/inc/common.php b/inc/common.php index d2dab10..8a485ae 100644 --- a/inc/common.php +++ b/inc/common.php @@ -264,6 +264,14 @@ function upgrade_check() write_config('unrealircd'); } } + /* $config["unrealircd"] should be an array now.. */ + if (isset($config['unrealircd']) && isset($config['unrealircd']['rpc_password'])) + { + $config["unrealircd"]["default"] = true; + $config['unrealircd'] = [ + "Primary" => $config['unrealircd']]; + write_config("unrealircd"); + } $version = get_version(); if (!isset($config['webpanel_version'])) @@ -370,6 +378,7 @@ $pages = [ ], "Settings" => [ "Plugins" => "settings/plugins.php", + "RPC Servers" => "settings/rpc-servers.php", ], "News" => "news.php", diff --git a/inc/connection.php b/inc/connection.php index 44ec034..fd264b7 100644 --- a/inc/connection.php +++ b/inc/connection.php @@ -3,24 +3,42 @@ if (!defined('UPATH')) die("Access denied"); +function get_active_rpc_server() +{ + // TODO: make user able to override this - either in user or in session + + foreach (get_config("unrealircd") as $displayname=>$e) + { + if (isset($e["default"]) && $e["default"]) + return $displayname; + } + return null; +} + function connect_to_ircd() { GLOBAL $rpc; + GLOBAL $config; - $host = get_config("unrealircd::host"); - $port = get_config("unrealircd::port"); - $rpc_user = get_config("unrealircd::rpc_user"); - $rpc_password = get_config("unrealircd::rpc_password"); + $server = get_active_rpc_server(); + if (!$server) + die("No RPC server configured as primary"); + $host = $config["unrealircd"][$server]["host"]; + $port = $config["unrealircd"][$server]["port"]; + $rpc_user = $config["unrealircd"][$server]["rpc_user"]; + $rpc_password = $config["unrealircd"][$server]["rpc_password"]; if (str_starts_with($rpc_password, "secret:")) $rpc_password = secret_decrypt($rpc_password); + $tls_verify = $config["unrealircd"][$server]["tls_verify_cert"]; if (!$host || !$port || !$rpc_user) - die("Unable to find RPC credentials in your config.php"); + die("RPC Server is missing credentials"); + if ($rpc_password === null) + { die("Your RPC password in the DB was encrypted with a different key than config/config.php contains.
\n". "Either restore your previous config/config.php or start with a fresh database.
\n"); - - $tls_verify = get_config("unrealircd::tls_verify_cert"); + } /* Connect now */ try { diff --git a/index.php b/index.php index 219c143..b41cdad 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ rpc_displayname]) && + !($opts->rpc_displayname == $opts->edit_existing)) + { + die("Server with that name already exists"); // TODO: pretier :D + } + + $new_properties = [ + "rpc_user" => $opts->rpc_user, + "rpc_password" => $opts->rpc_password, + "host"=>$opts->rpc_iphost, + "port"=>$opts->rpc_port, + "tls_verify_cert"=>isset($opts->rpc_tls_verify_cert)?true:false, + "default"=>isset($opts->rpc_default)?true:false, + ]; + + if (!empty($opts->edit_existing)) + { + // Change existing server + if (!isset($config["unrealircd"][$opts->edit_existing])) + die("Editing a server that does not exist!?"); // not very graceful, isn't it? + if ($new_properties["rpc_password"] == "****************") + $new_properties["rpc_password"] = $config["unrealircd"][$opts->edit_existing]["rpc_password"]; + // we unset because there could be a rename + unset($config["unrealircd"][$opts->edit_existing]); + $config["unrealircd"][$opts->rpc_displayname] = $new_properties; + } else { + // Add new server + $config["unrealircd"][$opts->rpc_displayname] = $new_properties; + // TODO: encrypt pwd ;) + } + + if ($new_properties["default"]) + { + /* Mark all other servers as non-default */ + foreach ($config["unrealircd"] as $name=>$e) + if ($name != $opts->rpc_displayname) + $config["unrealircd"][$name]["default"] = false; + } else { + /* Ensure at least 1 server is default */ + $has_default_server = false; + foreach ($config["unrealircd"] as $name=>$e) + if ($e["default"]) + $has_default_server = true; + if (!$has_default_server) + { + /* Make first server in the list the default */ + foreach ($config["unrealircd"] as $name=>$e) + { + $config["unrealircd"][$name]["default"] = true; + break; + } + } + } + + /* And write the new config */ + write_config(); + // TODO: change this message if it's the first server added? tell them to browse around? + Message::Success("RPC Server successfully added."); +} + +?> + +

RPC Servers

+You can configure which JSON-RPC server(s) the panel can connect to.

+You normally only need one server, but it can be useful to have multiple servers, so +you can switch to a secondary server in case the primary server goes down.
+
+ + +
+
+ +
+
+
Add Server
+
+
+ +
+
+ + + + + + + + +
+ + + + + + + + + $e) + { + $primary = ""; + if (isset($e["default"]) && $e["default"]) + $primary = " Default"; + $name = htmlspecialchars($name); + $default_server = $e["default"] ? "true" : "false"; + $host = htmlspecialchars($e["host"]); + $port = htmlspecialchars($e["port"]); + $rpc_user = htmlspecialchars($e["rpc_user"]); + $tls_verify_cert = $e["tls_verify_cert"] ? "true" : "false"; + $name = "$name"; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + ?> + +
Display nameHostnamePortRPC User
".$name.$primary."".$host."".$port."".$rpc_user."
+ + + +