Config Update and PIN POST Method

This commit is contained in:
Elia Zammuto
2021-05-30 16:42:40 +02:00
parent bc261fddf2
commit b848db8f2b
5 changed files with 173 additions and 105 deletions

View File

@@ -3,62 +3,90 @@
<h1>Applications</h1> <h1>Applications</h1>
<div>Applications are refreshed only when Client is restarted</div> <div>Applications are refreshed only when Client is restarted</div>
</div> </div>
<table class="table"> <div class="card p-4">
<thead> <table class="table">
<tr> <thead>
<th scope="col">Name</th> <tr>
<th scope="col">Actions</th> <th scope="col">Name</th>
</tr> <th scope="col">Actions</th>
</thead> </tr>
<tbody> </thead>
<tr v-for="(app,i) in apps" :key="i"> <tbody>
<td>{{app.name}}</td> <tr v-for="(app,i) in apps" :key="i">
<td><button class="btn btn-primary" @click="editApp(i)">Edit</button> <td>{{app.name}}</td>
<button class="btn btn-danger" @click="showDeleteForm(i)">Delete</button></td> <td><button class="btn btn-primary" @click="editApp(i)">Edit</button>
</tr> <button class="btn btn-danger" @click="showDeleteForm(i)">Delete</button></td>
</tbody> </tr>
</table> </tbody>
<div class="edit-form" v-if="showEditForm"> </table>
<div class="mb-3"> </div>
<label for="appName" class="form-label">Application Name</label> <div class="edit-form card mt-2" v-if="showEditForm">
<input type="text" class="form-control" id="appName" aria-describedby="appNameHelp" v-model="editForm.name"> <div class="p-4">
<div id="appNameHelp" class="form-text">Application Name, as shown on Moonlight</div> <!--name-->
</div> <div class="mb-3">
<div class="mb-3"> <label for="appName" class="form-label">Application Name</label>
<label for="appName" class="form-label">Output</label> <input type="text" class="form-control" id="appName" aria-describedby="appNameHelp" v-model="editForm.name">
<input type="text" class="form-control" id="appOutput" aria-describedby="appOutputHelp" v-model="editForm.output"> <div id="appNameHelp" class="form-text">Application Name, as shown on Moonlight</div>
<div id="appOutputHelp" class="form-text">The file where the output of the command is stored, if it is not specified, the output is ignored</div> </div>
</div> <!--output-->
<div class="mb-3"> <div class="mb-3">
<label for="appName" class="form-label">Command Preparations</label> <label for="appOutput" class="form-label">Output</label>
<div class="form-text">A list of commands to be run before/after the application. <br> If any of the prep-commands fail, starting the application is aborted</div> <input type="text" class="form-control monospace" id="appOutput" aria-describedby="appOutputHelp"
<table> v-model="editForm.output">
<thead> <div id="appOutputHelp" class="form-text">The file where the output of the command is stored, if it is not
<th class="precmd-head">Do</th> specified, the output is ignored</div>
<th class="precmd-head">Undo</th> </div>
<th></th> <!--prep-cmd-->
</thead> <div class="mb-3 d-flex flex-column">
<tbody> <label for="appName" class="form-label">Command Preparations</label>
<tr v-for="(c,i) in editForm['prep-cmd']"> <div class="form-text">A list of commands to be run before/after the application. <br> If any of the
<td><input type="text" class="form-control" v-model="c.do"></td> prep-commands fail, starting the application is aborted</div>
<td><input type="text" class="form-control" v-model="c.undo"></td> <table v-if="editForm['prep-cmd'].length > 0">
<td><button class="btn btn-danger" @click="editForm['prep-cmd'].splice(i,1)">&times;</button></td> <thead>
</tr> <th class="precmd-head">Do</th>
</tbody> <th class="precmd-head">Undo</th>
</table> <th style="width: 48px;"></th>
<button class="btn btn-success" @click="addPrepCmd">&plus;</button> </thead>
</div> <tbody>
<div class="mb-3"> <tr v-for="(c,i) in editForm['prep-cmd']">
<label for="appCmd" class="form-label">Command</label> <td><input type="text" class="form-control monospace" v-model="c.do"></td>
<input type="text" class="form-control" id="appCmd" aria-describedby="appCmdHelp" v-model="editForm.cmd"> <td><input type="text" class="form-control monospace" v-model="c.undo"></td>
<div id="appCmdHelp" class="form-text">The main application, if it is not specified, a processs is started that sleeps indefinitely</div> <td><button class="btn btn-danger" @click="editForm['prep-cmd'].splice(i,1)">&times;</button></td>
</div> </tr>
<div class="d-flex"> </tbody>
<button @click="showEditForm = false" class="btn btn-secondary m-2">Cancel</button> </table>
<button class="btn btn-primary m-2" @click="save">Save</button> <button class="mt-2 btn btn-success" style="margin: 0 auto;" @click="addPrepCmd">&plus; Add</button>
</div>
<!--detatched-->
<div class="mb-3">
<label for="appName" class="form-label">Detached Commands</label>
<div v-for="(c,i) in editForm.detached" class="d-flex justify-content-between my-2">
<pre>{{c}}</pre>
<button class="btn btn-danger mx-2" @click="editForm.detached.splice(i,1)">&times;</button>
</div>
<div class="d-flex justify-content-between">
<input type="text" class="form-control monospace" v-model="detachedCmd">
<button class="btn btn-success mx-2" @click="editForm.detached.push(detachedCmd);detachedCmd = '';">+</button>
</div>
<div class="form-text">A list of commands to be run and forgotten about</div>
</div>
<!--command-->
<div class="mb-3">
<label for="appCmd" class="form-label">Command</label>
<input type="text" class="form-control monospace" id="appCmd" aria-describedby="appCmdHelp" v-model="editForm.cmd">
<div id="appCmdHelp" class="form-text">The main application, if it is not specified, a processs is started that
sleeps indefinitely</div>
</div>
<!--buttons-->
<div class="d-flex">
<button @click="showEditForm = false" class="btn btn-secondary m-2">Cancel</button>
<button class="btn btn-primary m-2" @click="save">Save</button>
</div>
</div> </div>
</div> </div>
<button class="btn btn-primary" v-else @click="newApp">+ Add New</button> <div class="mt-2" v-else >
<button class="btn btn-primary" @click="newApp">+ Add New</button>
</div>
</div> </div>
<script> <script>
@@ -69,6 +97,7 @@
apps: [], apps: [],
showEditForm: false, showEditForm: false,
editForm: null, editForm: null,
detachedCmd: '',
} }
}, },
created() { created() {
@@ -78,7 +107,7 @@
}) })
}, },
methods: { methods: {
newApp(){ newApp() {
this.editForm = { this.editForm = {
name: '', name: '',
output: '', output: '',
@@ -89,30 +118,30 @@
this.editForm.index = -1; this.editForm.index = -1;
this.showEditForm = true; this.showEditForm = true;
}, },
editApp(id){ editApp(id) {
this.editForm = JSON.parse(JSON.stringify(this.apps[id])); this.editForm = JSON.parse(JSON.stringify(this.apps[id]));
this.$set(this.editForm,"index",id); this.$set(this.editForm, "index", id);
if(this.editForm["prep-cmd"] === undefined)this.$set(this.editForm,"prep-cmd",[]); if (this.editForm["prep-cmd"] === undefined) this.$set(this.editForm, "prep-cmd", []);
if (this.editForm["detached"] === undefined) this.$set(this.editForm, "detached", []);
this.showEditForm = true; this.showEditForm = true;
}, },
showDeleteForm(id){ showDeleteForm(id) {
let resp = confirm("Are you sure to delete " + this.apps[id].name + "?"); let resp = confirm("Are you sure to delete " + this.apps[id].name + "?");
if(resp){ if (resp) {
fetch("/api/apps/" + id,{method: "DELETE"}).then((r)=>{ fetch("/api/apps/" + id, { method: "DELETE" }).then((r) => {
if(r.status == 200)document.location.reload(); if (r.status == 200) document.location.reload();
}); });
} }
}, },
addPrepCmd(){ addPrepCmd() {
this.editForm['prep-cmd'].push({ this.editForm['prep-cmd'].push({
do: '', do: '',
undo: '', undo: '',
}); });
}, },
save(){ save() {
console.log("AAAAAAA") fetch("/api/apps", { method: "POST", body: JSON.stringify(this.editForm) }).then((r) => {
fetch("/api/apps",{method: "POST",body: JSON.stringify(this.editForm)}).then((r)=>{ if (r.status == 200) document.location.reload();
if(r.status == 200)document.location.reload();
}); });
} }
} }
@@ -123,4 +152,9 @@
.precmd-head { .precmd-head {
width: 200px; width: 200px;
} }
.monospace {
font-family: monospace;
}
</style> </style>

View File

@@ -19,10 +19,11 @@
e.preventDefault(); e.preventDefault();
let pin = document.querySelector("#pin-input").value; let pin = document.querySelector("#pin-input").value;
document.querySelector("#status").innerHTML = ""; document.querySelector("#status").innerHTML = "";
fetch("/pin/" + pin).then((response)=>{ let b = JSON.stringify({pin: pin});
if(response.status == 200){ fetch("/api/pin",{method: "POST",body: b}).then((response) => response.json()).then((response)=>{
if(response.status){
document.querySelector("#status").innerHTML = `<div class="alert alert-success" role="alert">Success! Please check Moonlight to continue</div>`; document.querySelector("#status").innerHTML = `<div class="alert alert-success" role="alert">Success! Please check Moonlight to continue</div>`;
} else if(response.status == 418){ } else {
document.querySelector("#status").innerHTML = `<div class="alert alert-danger" role="alert">PIN does not match, please check if it's typed correctly</div>`; document.querySelector("#status").innerHTML = `<div class="alert alert-danger" role="alert">PIN does not match, please check if it's typed correctly</div>`;
} }
}) })

View File

@@ -170,10 +170,12 @@ void saveApp(resp_https_t response, req_https_t request) {
pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
auto &apps_node = fileTree.get_child("apps"s); auto &apps_node = fileTree.get_child("apps"s);
int index = inputTree.get<int>("index"); int index = inputTree.get<int>("index");
BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty();
if(inputTree.get_child("prep-cmd").empty()) if(inputTree.get_child("prep-cmd").empty())
inputTree.erase("prep-cmd"); inputTree.erase("prep-cmd");
if(inputTree.get_child("detached").empty())
inputTree.erase("detached");
inputTree.erase("index"); inputTree.erase("index");
if(index == -1) { if(index == -1) {
apps_node.push_back(std::make_pair("", inputTree)); apps_node.push_back(std::make_pair("", inputTree));
@@ -372,6 +374,34 @@ void savePassword(resp_https_t response, req_https_t request) {
} }
} }
void savePin(resp_https_t response, req_https_t request){
if(!authenticate(response, request)) return;
std::stringstream ss;
ss << request->content.rdbuf();
pt::ptree inputTree,outputTree;
auto g = util::fail_guard([&]() {
std::ostringstream data;
pt::write_json(data, outputTree);
response->write(data.str());
});
try {
//TODO: Input Validation
pt::read_json(ss, inputTree);
std::string pin = inputTree.get<std::string>("pin");
outputTree.put("status",nvhttp::pin(pin));
}
catch(std::exception &e) {
BOOST_LOG(warning) << e.what();
outputTree.put("status", false);
outputTree.put("error", e.what());
return;
}
}
void start(std::shared_ptr<safe::signal_t> shutdown_event) { void start(std::shared_ptr<safe::signal_t> shutdown_event) {
auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls); auto ctx = std::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tls);
ctx->use_certificate_chain_file(config::nvhttp.cert); ctx->use_certificate_chain_file(config::nvhttp.cert);
@@ -384,7 +414,7 @@ void start(std::shared_ptr<safe::signal_t> shutdown_event) {
server.resource["^/clients$"]["GET"] = getClientsPage; server.resource["^/clients$"]["GET"] = getClientsPage;
server.resource["^/config$"]["GET"] = getConfigPage; server.resource["^/config$"]["GET"] = getConfigPage;
server.resource["^/password$"]["GET"] = getPasswordPage; server.resource["^/password$"]["GET"] = getPasswordPage;
server.resource["^/pin/([0-9]+)$"]["GET"] = nvhttp::pin<SimpleWeb::HTTPS>; server.resource["^/api/pin"]["POST"] = savePin;
server.resource["^/api/apps$"]["GET"] = getApps; server.resource["^/api/apps$"]["GET"] = getApps;
server.resource["^/api/apps$"]["POST"] = saveApp; server.resource["^/api/apps$"]["POST"] = saveApp;
server.resource["^/api/config$"]["GET"] = getConfig; server.resource["^/api/config$"]["GET"] = getConfig;

View File

@@ -420,6 +420,36 @@ void pair(std::shared_ptr<safe::queue_t<crypto::x509_t>> &add_cert, std::shared_
response->write(data.str()); response->write(data.str());
} }
bool pin(std::string pin){
pt::ptree tree;
if(map_id_sess.empty()) {
return false;
}
auto &sess = std::begin(map_id_sess)->second;
getservercert(sess, tree, pin);
// response to the request for pin
std::ostringstream data;
pt::write_xml(data, tree);
auto &async_response = sess.async_insert_pin.response;
if(async_response.has_left() && async_response.left()) {
async_response.left()->write(data.str());
}
else if(async_response.has_right() && async_response.right()) {
async_response.right()->write(data.str());
}
else {
return false;
}
// reset async_response
async_response = std::decay_t<decltype(async_response.left())>();
// response to the current request
return true;
}
template<class T> template<class T>
void pin(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) { void pin(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request) {
print_req<T>(request); print_req<T>(request);
@@ -434,38 +464,12 @@ void pin(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response,
return; return;
} }
pt::ptree tree; bool pinResponse = pin(request->path_match[1]);
if(pinResponse){
if(map_id_sess.empty()) { response->write(SimpleWeb::StatusCode::success_ok);
} else {
response->write(SimpleWeb::StatusCode::client_error_im_a_teapot); response->write(SimpleWeb::StatusCode::client_error_im_a_teapot);
return;
} }
auto &sess = std::begin(map_id_sess)->second;
getservercert(sess, tree, request->path_match[1]);
// response to the request for pin
std::ostringstream data;
pt::write_xml(data, tree);
auto &async_response = sess.async_insert_pin.response;
if(async_response.has_left() && async_response.left()) {
async_response.left()->write(data.str());
}
else if(async_response.has_right() && async_response.right()) {
async_response.right()->write(data.str());
}
else {
response->write(SimpleWeb::StatusCode::client_error_im_a_teapot);
return;
}
// reset async_response
async_response = std::decay_t<decltype(async_response.left())>();
// response to the current request
response->write(SimpleWeb::StatusCode::success_ok);
} }
template<class T> template<class T>

View File

@@ -17,8 +17,7 @@
namespace nvhttp { namespace nvhttp {
void start(std::shared_ptr<safe::signal_t> shutdown_event); void start(std::shared_ptr<safe::signal_t> shutdown_event);
template<class T> bool pin(std::string pin);
void pin(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> response, std::shared_ptr<typename SimpleWeb::ServerBase<T>::Request> request);
} // namespace nvhttp } // namespace nvhttp
#endif //SUNSHINE_NVHTTP_H #endif //SUNSHINE_NVHTTP_H