wifi settings added and hostapd service

This commit is contained in:
wt committed 2024-07-09 12:12:44 +07:00
1 parent d5cbaa8d38
commit 9990e60387
26 files changed
+337 -60

No files matched your search

+3
View File
@@ -39,8 +39,11 @@ int main() {
srv.Post("/api/settings/ftp/set", set_ftp);
srv.Post("/api/settings/wifi/status/get", get_wifi_status);
srv.Post("/api/settings/wifi/status/set", set_wifi_status);
srv.Post("/api/settings/wifi/name/get", get_wifi_name);
srv.Post("/api/settings/wifi/name/set", set_wifi_name);
srv.Post("/api/settings/wifi/password/get", get_wifi_password);
srv.Post("/api/settings/wifi/password/set", set_wifi_password);
srv.Post("/api/reboot", reboot);
+69
View File
@@ -10,6 +10,7 @@
#include <syslog.h>
#define WIFI_SETTINGS_PATH "/etc/hostapd/hostapd.conf"
#define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})"
void get_wifi_name(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
@@ -53,3 +54,71 @@ void get_wifi_name(const httplib::Request &request, httplib::Response &response)
response.set_content(responsedata.str(), JSON_TYPE);
}
}
void set_wifi_name(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["name"] == nullptr) {
response.set_content(WIFI_NAME_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string old_wifi_name;
std::string wifisettingsbuffer;
while (std::getline(wifisettingsfile, wifisettingsbuffer)) {
if (wifisettingsbuffer.find("ssid=") == 0) {
old_wifi_name = wifisettingsbuffer;
old_wifi_name.erase(0, 5);
break;
}
}
wifisettingsfile.close();
std::string new_wifi_name = json_body["name"];
std::stringstream systemrequest;
systemrequest << "sed -i \"/ssid=" << old_wifi_name << "/s//ssid=" << new_wifi_name << "/\" /etc/hostapd/hostapd.conf";
std::cout << systemrequest.str() << std::endl;
system(systemrequest.str().c_str());
system("rc restart hostapd");
std::string syslogstring = "WIFI name changed to " + new_wifi_name;
syslog(LOG_INFO, syslogstring.c_str());
std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH);
std::string wifi_name;
std::string changedwifisettingsbuffer;
while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) {
if (changedwifisettingsbuffer.find("ssid=") == 0) {
wifi_name = changedwifisettingsbuffer;
wifi_name.erase(0, 5);
break;
}
}
changedwifisettingsfile.close();
std::stringstream responsedata;
responsedata << R"({"name":")" << wifi_name << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
+66
View File
@@ -10,6 +10,7 @@
#include <syslog.h>
#define WIFI_SETTINGS_PATH "/etc/hostapd/hostapd.conf"
#define WIFI_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})"
void get_wifi_password(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
@@ -53,3 +54,68 @@ void get_wifi_password(const httplib::Request &request, httplib::Response &respo
response.set_content(responsedata.str(), JSON_TYPE);
}
}
void set_wifi_password(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["password"] == nullptr) {
response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["newpassword"] == nullptr) {
response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string old_wifi_password;
std::string wifisettingsbuffer;
while (std::getline(wifisettingsfile, wifisettingsbuffer)) {
if (wifisettingsbuffer.find("wpa_passphrase=") == 0) {
old_wifi_password = wifisettingsbuffer;
old_wifi_password.erase(0, 15);
break;
}
}
wifisettingsfile.close();
std::string req_old_password = json_body["password"];
if (old_wifi_password != req_old_password) {
response.set_content(R"({"status":"reject"})", JSON_TYPE);
return;
}
std::string new_wifi_password = json_body["newpassword"];
std::stringstream systemrequest;
systemrequest << "sed -i \"/wpa_passphrase=" << old_wifi_password << "/s//wpa_passphrase=" << new_wifi_password << "/\" /etc/hostapd/hostapd.conf";
system(systemrequest.str().c_str());
system("rc restart hostapd");
std::string syslogstring = "WIFI password changed";
syslog(LOG_INFO, syslogstring.c_str());
std::stringstream responsedata;
responsedata << R"({"success":"ok"})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
+56
View File
@@ -5,6 +5,8 @@
#include "../../types.hpp"
#include <syslog.h>
#define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})"
void get_wifi_status(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
@@ -40,3 +42,57 @@ void get_wifi_status(const httplib::Request &request, httplib::Response &respons
response.set_content(responsedata.str(), JSON_TYPE);
}
}
void set_wifi_status(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["status"] == nullptr) {
response.set_content(WIFI_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ofstream wififileout("/etc/openrouter/wifi/status");
std::string new_wifi_status = json_body["status"];
if (new_wifi_status == "0") {
system("rc stop hostapd");
system("rc del hostapd");
syslog(LOG_INFO, "WIFI is disabled");
} else {
system("rc start hostapd");
system("rc add hostapd");
syslog(LOG_INFO, "WIFI is enabled");
}
wififileout << stoi(new_wifi_status);
wififileout.close();
std::ifstream wififile("/etc/openrouter/wifi/status");
int wifi_status;
wififile >> wifi_status;
wififile.close();
std::stringstream responsedata;
responsedata << R"({"status":")" << wifi_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
+3
View File
@@ -4,9 +4,12 @@
#include <httplib.h>
void get_wifi_status(const httplib::Request &request, httplib::Response &response);
void set_wifi_status(const httplib::Request &request, httplib::Response &response);
void get_wifi_name(const httplib::Request &request, httplib::Response &response);
void set_wifi_name(const httplib::Request &request, httplib::Response &response);
void get_wifi_password(const httplib::Request &request, httplib::Response &response);
void set_wifi_password(const httplib::Request &request, httplib::Response &response);
#endif // WIFI_HPP
+9 -4
View File
@@ -144,13 +144,18 @@
<input type="text" id="SSID" class="forminput">
</div>
<div style="display:flex;align-items:center;justify-content:space-between;">
<label for="wifipassword" class="forminputlabel">Password</label>
<input type="text" id="wifipassword" class="forminput">
<label for="wifipassword" class="forminputlabel">Old password</label>
<button id="showpasswordbutton">Show</button>
<input type="password" id="wifipassword" class="forminput">
</div>
<div style="display:flex;align-items:center;justify-content:space-between;">
<label for="newwifipassword" class="forminputlabel">New password</label>
<button id="shownewpasswordbutton">Show</button>
<input type="password" id="newwifipassword" class="forminput">
</div>
</div>
<div class="settingrectfooter onebutton">
<button id="add_ssh_key" class="hiddenbutton settingrectfooterbutton">Add SSH key</button>
<button class="settingrectfooterbutton" id="set_ssh_settings">Confirm</button>
<button class="settingrectfooterbutton" id="setwifisettings">Confirm</button>
</div>
</div>
<!-- wifi settings -->
+1 -1
View File
@@ -14,5 +14,5 @@ const get_all_settings = () => {
get_wifi_status()
get_wifi_name()
get_wifi_password()
// get_wifi_password()
}
+91 -2
View File
@@ -2,13 +2,18 @@ const wifistatus = document.querySelector('#wifistatus')
const wifisettingsbody = document.querySelector('#wifisettingsbody')
const wifissid = document.querySelector('#SSID')
const wifipassword = document.querySelector('#wifipassword')
const showpasswordbutton = document.querySelector('#showpasswordbutton')
const setwifisettings = document.querySelector('#setwifisettings')
const shownewpasswordbutton = document.querySelector('#shownewpasswordbutton')
const newwifipassword = document.querySelector('#newwifipassword')
let old_wifi_name = ""
const get_wifi_status = () => {
fetch(routes.wifi_status_get(), {
method: 'POST',
body: `{"token":"${localStorage.getItem('token')}"}`
}).then(data => data.json()).then(jsondata => {
console.log(jsondata)
if (jsondata.status == "1") {
wifistatus.checked = true
wifisettingsbody.classList.add('active')
@@ -20,13 +25,47 @@ const get_wifi_status = () => {
})
}
const set_wifi_status = () => {
fetch(routes.wifi_status_set(), {
method: 'POST',
body: `{"token":"${localStorage.getItem('token')}","status":"${wifistatus.checked?"1":"0"}"}`
}).then(data => data.json()).then(jsondata => {
console.log(jsondata)
if (jsondata.status === "1") {
wifistatus.checked = true
wifisettingsbody.classList.add('active')
notification("Настройки WIFI оспешно сорранены", "success")
return
}
wifistatus.checked = false
wifisettingsbody.classList.remove('active')
notification("Настройки WIFI оспешно сорранены", "success")
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
}
const get_wifi_name = () => {
fetch(routes.wifi_name_get(), {
method: 'POST',
body: `{"token":"${localStorage.getItem('token')}"}`
}).then(data => data.json()).then(jsondata => {
wifissid.value = jsondata.name
old_wifi_name = jsondata.name
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
}
const set_wifi_name = () => {
fetch(routes.wifi_name_set(), {
method: 'POST',
body: `{"token":"${localStorage.getItem('token')}","name":"${wifissid.value}"}`
}).then(data => data.json()).then(jsondata => {
console.log(jsondata)
wifissid.value = jsondata.name
old_wifi_name = jsondata.name
notification("Имя WIFI оспешно сорранено", "success")
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
@@ -37,13 +76,47 @@ const get_wifi_password = () => {
method: 'POST',
body: `{"token":"${localStorage.getItem('token')}"}`
}).then(data => data.json()).then(jsondata => {
console.log(jsondata)
wifipassword.value = jsondata.password
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
}
const set_wifi_password = () => {
fetch(routes.wifi_password_set(), {
method: 'POST',
body: `{"token":"${localStorage.getItem('token')}","password":"${wifipassword.value}","newpassword":"${newwifipassword.value}"}`
}).then(data => data.json()).then(jsondata => {
if (jsondata.status === "reject") {
notification("Неверный пароль", "error")
return
}
notification("Пароль WIFI оспешно сорранен", "success")
wifipassword.value = ""
newwifipassword.value = ""
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
}
const set_wifi_settings = () => {
if (confirm("Confirm wifi settings?")) {
start_loader()
set_wifi_status()
if (wifistatus.checked) {
if (old_wifi_name !== wifissid.value) {
set_wifi_name()
}
if (wifipassword.value !== "" && newwifipassword.value !== "") {
set_wifi_password()
}
}
stop_loader()
}
}
setwifisettings.addEventListener('click', set_wifi_settings)
wifistatus.addEventListener('click', () => {
if (wifistatus.checked) {
wifisettingsbody.classList.add('active')
@@ -51,3 +124,19 @@ wifistatus.addEventListener('click', () => {
}
wifisettingsbody.classList.remove('active')
})
showpasswordbutton.addEventListener('click', () => {
if (wifipassword.type == "password") {
wifipassword.type = "text"
return
}
wifipassword.type = "password"
})
shownewpasswordbutton.addEventListener('click', () => {
if (newwifipassword.type == "password") {
newwifipassword.type = "text"
return
}
newwifipassword.type = "password"
})
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
+1 -4
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
@@ -20,7 +17,7 @@ case "$1" in
check $?
rm /var/run/dnsmasq &> /dev/null
else
notify "[$Red Dnsmasq is not running $Color_off]\n"
notify "[$Red Dnsmasq is not running $Color_Off]\n"
fi
;;
restart|reload)
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_Off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
+33
View File
@@ -0,0 +1,33 @@
#!/bin/sh
. /etc/init.d/init-functions
case "$1" in
start)
notify "Starting hostapd "
if [[ "$2" == "" ]]; then
/usr/bin/hostapd -P /var/run/hostapd.pid /etc/hostapd/hostapd
check $?
else
/usr/bin/hostapd -P /var/run/hostapd.pid $2
check $?
fi
;;
stop)
notify "Stopping hostapd "
if [ ! -f /var/run/hostapd.pid ]; then
notify "[$Red Dnsmasq is not running $Color_Off]\n"
exit 1
fi
kill $(cat /var/run/hostapd.pid)
check $?
;;
restart|reload)
"$0" stop
"$0" start
;;
*)
echo "hostapd init script usage: $0 {start|stop|restart}"
return 1
esac
return $?
-4
View File
@@ -1,9 +1,5 @@
#!/bin/sh
Green='\033[0;32m'
Color_Off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
case "$1" in
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_Off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
+1 -1
View File
@@ -68,7 +68,7 @@ check() {
if [[ "$1" == "0" ]];then
printf "[$Green OK $Color_Off]\n"
else
printf "[$Red ERR $Cpolor_Off]\n"
printf "[$Red ERR $Color_Off]\n"
fi
}
+1 -5
View File
@@ -1,9 +1,5 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
DAEMON="iptables"
IPTABLES_ARGS=""
@@ -37,7 +33,7 @@ save() {
iptables-save -f /etc/iptables.conf
status=$?
if [ "$status" -eq 0 ]; then
notify "[$Green OK $Color_off]\n"
notify "[$Green OK $Color_Off]\n"
else
echo "SKIP (read-only file system detected)"
fi
+1 -4
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
@@ -18,7 +15,7 @@ case "$1" in
check $?
rm /var/run/klogd.pid &> /dev/null
else
notify "[$Red Klogd is not running $Color_off]\n"
notify "[$Red Klogd is not running $Color_Off]\n"
fi
;;
restart|reload)
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_Off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_Off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
-4
View File
@@ -1,9 +1,5 @@
#!/bin/sh
Green='\033[0;32m'
Color_Off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
case "$1" in
+1 -4
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
@@ -18,7 +15,7 @@ case "$1" in
check $?
rm /var/run/syslogd.pid &> /dev/null
else
notify "[$Red Syslogd is not running $Color_off]\n"
notify "[$Red Syslogd is not running $Color_Off]\n"
fi
;;
restart|reload)
-3
View File
@@ -1,7 +1,4 @@
#!/bin/sh
Green='\033[0;32m'
Color_off='\033[0m'
Red='\033[0;31m'
. /etc/init.d/init-functions
+1
View File
@@ -11,4 +11,5 @@
80iptables
85openrouterinit
90ftp
92hostapd
95nginx