Gemini bridge control

This commit is contained in:
Elizabeth Cray 2023-10-06 20:01:24 -04:00
parent 09b2f7a24f
commit 7c811a546e
7 changed files with 264 additions and 24 deletions

View File

@ -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 {

View File

@ -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:
"<ul><li>Is heavier than gopher</li><li>Is lighter than the web</li><li>Will not replace either</li></ul>",
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,
});
}
});
}
});
})
});
};

9
auth/gem.html Normal file
View File

@ -0,0 +1,9 @@
<div class="row">
<div class="col">Enable Web Proxy</div>
<div class="col">
<label class="switch">
<input type="checkbox" id="gemini-proxy" checked>
<span class="slider round"></span>
</label>
</div>
</div>

View File

@ -50,6 +50,8 @@ const failMsg = (msg) => {
...SwalConfig,
title: "Error!",
text: msg,
}).then(() => {
window.location.replace("/");
});
}

View File

@ -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."\"");
}
?>

View File

@ -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%;
}

69
tools/toggleProxy Executable file
View File

@ -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