-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qnap: Add cgi script handling REST API
- Loading branch information
Showing
6 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,3 +46,4 @@ beta-events.db | |
gamma-events.db | ||
nat-lab/**/report.html | ||
events-moose.log | ||
qnap/**/teliod |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
{ | ||
"log_level": "debug", | ||
"log_file_path": "/tmp/nordsecuritymeshnet/teliod.log", | ||
"log_file_path": "/tmp/nordsecuritymeshnet/meshnet.log", | ||
"authentication_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
"app_user_uid": "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", | ||
"interface_name": "nlx" | ||
"interface": { | ||
"name": "nlx", | ||
"config_provider": "manual" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/bin/bash | ||
|
||
echo "Content-Type: application/json" | ||
echo "" | ||
|
||
# Varibles set by the HTTP web server | ||
# | ||
# $REQUEST_METHOD | ||
# $QUERY_STRING | ||
# $CONTENT_LENGTH | ||
|
||
POST_DATA="" | ||
|
||
if [[ "$REQUEST_METHOD" == "POST" || "$REQUEST_METHOD" == "PATCH" ]]; then | ||
read -n $CONTENT_LENGTH POST_DATA | ||
fi | ||
|
||
QPKG_DIR="/share/CACHEDEV1_DATA/.qpkg/NordSecurityMeshnet" | ||
APP_TMP_DIR="/tmp/nordsecuritymeshnet" | ||
APP_CMD="$QPKG_DIR/teliod" | ||
CONFIG_FILE="$QPKG_DIR/teliod.cfg" | ||
LOG_FILE="$APP_TMP_DIR/teliod.log" | ||
PID_FILE="$APP_TMP_DIR/teliod.pid" | ||
|
||
is_application_running() { | ||
if [ -f ${PID_FILE} ]; then | ||
PID=$(cat ${PID_FILE}) | ||
if [ -d /proc/${PID}/ ]; then | ||
# application is running | ||
return 0 | ||
fi | ||
fi | ||
# application is NOT running | ||
return 1 | ||
} | ||
|
||
send_response() { | ||
local code=$1 | ||
local message=$2 | ||
echo "{\"code\": $code, \"message\": \"$message\"}" | ||
exit $code | ||
} | ||
|
||
start_daemon() { | ||
if is_application_running; then | ||
send_response 400 "Application is already running." | ||
fi | ||
$APP_CMD daemon $CONFIG_FILE > $LOG_FILE 2>&1 & | ||
if [[ $? -eq 0 ]]; then | ||
echo $! > ${PID_FILE} | ||
send_response 200 "Application started successfully." | ||
else | ||
echo $! > ${PID_FILE} | ||
send_response 500 "Failed to start the application." | ||
fi | ||
} | ||
|
||
stop_daemon() { | ||
if [ -f ${PID_FILE} ]; then | ||
PID=$(cat ${PID_FILE}) | ||
kill ${PID} || true | ||
rm -f ${PID_FILE} | ||
send_response 200 "Application stopped successfully." | ||
else | ||
send_response 400 "Application PID not found." | ||
fi | ||
} | ||
|
||
update_config() { | ||
NEW_CONFIG=$(echo "$POST_DATA" | jq -r '.config') | ||
if [[ -z "$NEW_CONFIG" ]]; then | ||
send_response 400 "No configuration provided." | ||
fi | ||
echo "$NEW_CONFIG" > $CONFIG_FILE | ||
if [[ $? -eq 0 ]]; then | ||
send_response 200 "Configuration updated successfully." | ||
else | ||
send_response 500 "Failed to update configuration." | ||
fi | ||
} | ||
|
||
get_status() { | ||
if is_application_running; then | ||
STATUS=$($APP_CMD get-status) | ||
echo "{\"code\": 200, \"status-report\": $STATUS}" | ||
exit 0 | ||
else | ||
send_response 400 "Application is not running." | ||
fi | ||
} | ||
|
||
get_logs() { | ||
if [[ -f $LOG_FILE ]]; then | ||
LOGS=$(tail -n 100 $LOG_FILE | jq -R -s '.') | ||
echo "{\"code\": 200, \"logs\": $LOGS}" | ||
exit 0 | ||
else | ||
send_response 404 "Log file not found." | ||
fi | ||
} | ||
|
||
case "$REQUEST_METHOD" in | ||
POST) | ||
if [[ "$QUERY_STRING" == "action=start" ]]; then | ||
start_daemon | ||
elif [[ "$QUERY_STRING" == "action=stop" ]]; then | ||
stop_daemon | ||
else | ||
send_response 400 "Invalid action for POST." | ||
fi | ||
;; | ||
PATCH) | ||
if [[ "$QUERY_STRING" == "action=update-config" ]]; then | ||
update_config | ||
else | ||
send_response 400 "Invalid action for PATCH." | ||
fi | ||
;; | ||
GET) | ||
if [[ "$QUERY_STRING" == "action=get-status" ]]; then | ||
get_status | ||
elif [[ "$QUERY_STRING" == "action=get-logs" ]]; then | ||
get_logs | ||
else | ||
send_response 400 "Invalid action for GET." | ||
fi | ||
;; | ||
*) | ||
send_response 405 "Method not allowed." | ||
;; | ||
esac |