From 7c811a546e76864b732ef3d26328201833d6dfdc Mon Sep 17 00:00:00 2001 From: Elizabeth Cray Date: Fri, 6 Oct 2023 20:01:24 -0400 Subject: [PATCH] Gemini bridge control --- auth/api/index.php | 57 ++++++++++++++++++++++++++--------- auth/auth.js | 65 +++++++++++++++++++++++++++++++++++----- auth/gem.html | 9 ++++++ index.js | 2 ++ secret/helpers.php | 12 ++++++++ style.css | 74 ++++++++++++++++++++++++++++++++++++++++++++-- tools/toggleProxy | 69 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 264 insertions(+), 24 deletions(-) create mode 100644 auth/gem.html create mode 100755 tools/toggleProxy diff --git a/auth/api/index.php b/auth/api/index.php index 6b179da..3ba1396 100644 --- a/auth/api/index.php +++ b/auth/api/index.php @@ -72,22 +72,51 @@ if (isset($_REQUEST["act"])) { }else{ // Valid Token if (isset($_REQUEST["enable"])){ - $userHomeDir = shell_exec("eval echo ~".$TokenData["MastodonData"]->username); - if (!file_exists($userHomeDir)){ + if (!userExists($TokenData["MastodonData"]->username)){ returnError("User Home Directory Not Found, try making a new SSH key."); } else { - if (file_exists($userHomeDir."/public_gemini")){ - mkdir($userHomeDir."/public_gemini", 0755); - } - $EnableFile = $userHomeDir."/public_gemini/.serve_ok"; - if ($_REQUEST["enable"] == 1){ - if (!file_exists($EnableFile)){ - file_put_contents($EnableFile, "web_gen"); - } - }else{ - if (file_exists($EnableFile)){ - unlink($EnableFile); - } + switch ($_REQUEST["enable"]){ + case "yes": + shell_exec("/usr/bin/sudo /etc/ttyserver/bin/toggleProxy enable \"".$TokenData["MastodonData"]->username."\""); + returnSuccess("Gemini Proxy Enabled", buildEncToken($TokenData["AuthToken"], + $TokenData["UserID"], + $_SERVER["REMOTE_ADDR"], + $_SERVER["HTTP_USER_AGENT"])); + break; + case "no": + $result = trim(shell_exec("/usr/bin/sudo /etc/ttyserver/bin/toggleProxy disable \"".$TokenData["MastodonData"]->username."\"")); + switch($result){ + case "done": + returnSuccess("Gemini Proxy Disabled", buildEncToken($TokenData["AuthToken"], + $TokenData["UserID"], + $_SERVER["REMOTE_ADDR"], + $_SERVER["HTTP_USER_AGENT"] + )); + break; + case "no_perm": + returnError("Gemini Proxy Disable Failed, error_p"); + break; + case "no_dir": + returnError("Gemini Directory Not Found"); + break; + default: + returnError("Gemini Proxy Disable Failed, error_u"); + break; + } + + + break; + case "get": + $result = trim(shell_exec("/usr/bin/sudo /etc/ttyserver/bin/toggleProxy get \"".$TokenData["MastodonData"]->username."\"")); + returnSuccess(($result == "enabled"), buildEncToken($TokenData["AuthToken"], + $TokenData["UserID"], + $_SERVER["REMOTE_ADDR"], + $_SERVER["HTTP_USER_AGENT"] + )); + break; + default: + returnError("Incorrect Gemini Proxy Query"); + break; } } }else { diff --git a/auth/auth.js b/auth/auth.js index bab707a..252c259 100644 --- a/auth/auth.js +++ b/auth/auth.js @@ -6,6 +6,14 @@ const SwalConfig = { color: "#79F257", background: "#022601", buttonsStyling: false, + showClass: { + backdrop: 'swal2-noanimation', + popup: '', + icon: '' + }, + hideClass: { + popup: '', + } }; const invalidChars = ["/", "\\", ">", "<", ":", "*", "|", '"', "'", "?", "\0"]; @@ -35,7 +43,12 @@ const post = (url, data, callback) => { }, data: data }; - $.ajax(settings).done(callback); + $.ajax(settings).done((data) => { + if (typeof data.token !== "undefined") { + localStorage.setItem("tty_token", data.token); + } + callback(data); + }); }; const saveFile = (name, type, data) => { @@ -241,12 +254,50 @@ const logout = () => { }; const gemini = () => { - Swal.fire({ - ...SwalConfig, - title: "Gemini", - text: "Gemini is a new internet protocol which:", - html: - "", + var payload = { + token: localStorage.getItem("tty_token"), + act: "gemproxy", + enable: "get" + }; + post(USE_ORIGIN + "/auth/api/index.php", payload, (response) => { + console.dir(response.data); + $.get(USE_ORIGIN + "/auth/gem.html", (ui) => { + ui = ui.replace("checked", response.data?"checked":""); + console.log(ui); + Swal.fire({ + ...SwalConfig, + title: "Gemini Settings", + html: ui, + willClose: (doc) => { + switch(doc.getElementsByTagName("input")[0].checked){ + case true: + payload.enable = "yes"; + break; + case false: + payload.enable = "no"; + break; + default: + break; + } + payload.token = localStorage.getItem("tty_token"); + post(USE_ORIGIN + "/auth/api/index.php", payload, (set_response) => { + if(set_response.error){ + Swal.fire({ + ...SwalConfig, + title: "Config Failed", + text: set_response.error, + }); + }else { + Swal.fire({ + ...SwalConfig, + title: "Success", + text: set_response.data, + }); + } + }); + } + }); + }) }); }; diff --git a/auth/gem.html b/auth/gem.html new file mode 100644 index 0000000..2c69d1b --- /dev/null +++ b/auth/gem.html @@ -0,0 +1,9 @@ +
+
Enable Web Proxy
+
+ +
+
diff --git a/index.js b/index.js index 72ad795..c95312e 100644 --- a/index.js +++ b/index.js @@ -50,6 +50,8 @@ const failMsg = (msg) => { ...SwalConfig, title: "Error!", text: msg, + }).then(() => { + window.location.replace("/"); }); } diff --git a/secret/helpers.php b/secret/helpers.php index 3f9da87..2945f49 100644 --- a/secret/helpers.php +++ b/secret/helpers.php @@ -37,4 +37,16 @@ function returnSuccess($success, $refreshToken = ""){ )); exit(); } + +function getUsers(){ + return explode("\n", shell_exec("cut -d: -f1 /etc/passwd")); +} + +function userExists($user){ + return in_array($user, getUsers()); +} + +function geminiProxy($user, $action){ + return shell_exec("/usr/bin/sudo /etc/ttyserver/bin/toggleProxy ".$action." \"".$user."\""); +} ?> diff --git a/style.css b/style.css index 96a945d..bbd1cf1 100644 --- a/style.css +++ b/style.css @@ -53,7 +53,8 @@ span { .button { margin: 8px; } -.button > button { + +.button>button { background-color: #377326; color: #79F257; border: 1px solid #5AA637; @@ -68,7 +69,7 @@ span { color: #377326; } -.copyright > a { +.copyright>a { color: #377326; text-decoration: none; } @@ -85,7 +86,9 @@ span { display: none; } -.swal2-confirm, .swal2-deny, .swal2-cancel { +.swal2-confirm, +.swal2-deny, +.swal2-cancel { border: 1px solid #79F257; background-color: #377326; color: #79F257; @@ -98,6 +101,10 @@ span { background-color: #022601; } +.swal2-html-container { + overflow: hidden !important; +} + .footerbutton { background: none; border: none; @@ -107,9 +114,11 @@ span { td { border: 1px solid #3a4c35; } + td:first-child { border-left: 2px solid #3a4c35; } + .fingerprintTable { width: 100%; border-collapse: collapse; @@ -132,3 +141,62 @@ td:first-child { .emoji { height: 2em; } + +.switch { + position: relative; + display: inline-block; + width: 60px; + height: 34px; +} + +.switch input { + opacity: 0; + width: 0; + height: 0; +} + +.slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: #3a4c35; + -webkit-transition: .4s; + transition: .4s; +} + +.slider:before { + position: absolute; + content: ""; + height: 26px; + width: 26px; + left: 4px; + bottom: 4px; + background-color: #79F257; + -webkit-transition: .4s; + transition: .4s; +} + +input:checked+.slider { + background-color: #377326; +} + +input:focus+.slider { + box-shadow: 0 0 1px #377326; +} + +input:checked+.slider:before { + -webkit-transform: translateX(26px); + -ms-transform: translateX(26px); + transform: translateX(26px); +} + +.slider.round { + border-radius: 34px; +} + +.slider.round:before { + border-radius: 50%; +} diff --git a/tools/toggleProxy b/tools/toggleProxy new file mode 100755 index 0000000..6e0f1af --- /dev/null +++ b/tools/toggleProxy @@ -0,0 +1,69 @@ +#!/bin/bash + +# $1 - get / enable / disable +# $2 - optional username + +ru=$USER +if [ "$EUID" != 0 ]; then + # Only for running user + if [ "$2" != "$USER" ]; then + echo "Cannot run for user other than yourself without superuser privileges." + exit 0 + fi +else + # Run for specified user + if [ "$2" != "" ]; then + ru=$2 + else + ru="root" + fi +fi + +hd="$(eval echo ~$ru)/public_gemini" + +if [ "$1" == "get" ]; then + if [ -d "$hd" ]; then + # gemini dir exists + if [ -r "$hd/.serve_ok" ]; then + echo "enabled" + else + echo "disabled" + fi + else + echo "no_exist" + fi +elif [ "$1" == "enable" ]; then + if [ -d "$hd" ]; then + if [ -r "$hd/.serve_ok" ]; then + echo "done" + else + touch "$hd/.serve_ok" + chown "$ru" "$hd/.serve_ok" + echo "done" + fi + else + mkdir "$hd" + touch "$hd/.serve_ok" + chown "$ru" -R "$hd" + echo "done" + fi +elif [ "$1" == "disable" ]; then + if [ -d "$hd" ]; then + if [ -w "$hd/.serve_ok" ]; then + rm "$hd/.serve_ok" + echo "done" + else + if [ -r "$hd/.serve_ok" ]; then + echo "no_perm" + else + echo "done" + fi + fi + else + echo "no_dir" + fi +else + # TODO: display command help guide + echo "TODO" +fi +