Compare commits

...

10 Commits
v0.1 ... HEAD

14 changed files with 324 additions and 120 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
go/jilo-agent
jilo-agent

View File

@ -4,6 +4,24 @@ All notable changes to this project will be documented in this file.
---
## Unreleased
#### Links
- upstream: https://code.lindeas.com/lindeas/jilo-agent/compare/v0.1...HEAD
- codeberg: https://codeberg.org/lindeas/jilo-agent/compare/v0.1...HEAD
- github: https://github.com/lindeas/jilo-agent/compare/v0.1...HEAD
- gitlab: https://gitlab.com/lindeas/jilo-agent/-/compare/v0.1...HEAD
### Added
- Removed PHP version
- Added support for HTTPS
- Added SysV init script
- Added Systemd service file
- Added installation script
- Added command line option for config file
---
## 0.1 - 2024-10-02
#### Links
@ -17,5 +35,5 @@ All notable changes to this project will be documented in this file.
- New version in folder "go", written in Go
- Added endpoints for /nginx, /prosody, /jicofo, /jvb, /jibri
- Added a config file and default values
- Initial vesion of a build script
- Initial version of a build script
- Works with JWT tokens

View File

@ -2,11 +2,7 @@
## overview
Jilo Agent - a remote agent for Jilo Web
Initial version is in PHP.
The current version is in "go" folder and is in Go.
Jilo Agent - a remote agent for Jilo Web written in Go.
## license
@ -32,6 +28,8 @@ go build -o jilo-agent main.go
The config file is "jilo-agent.json", in the same folder as the "jilo-agent" binary.
If you add the SSL config options, HTTPS will be used.
You can run the agent without a config file - then default vales are used.
## usage

View File

@ -1,7 +0,0 @@
<?php
$jicofoStatsURL = 'http://localhost:8888/stats';
$jvbStatsURL = 'http://localhost:8080/colibri/stats';
$nginxPort = '80';
?>

View File

@ -15,6 +15,6 @@
# - upx
###
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o jilo-agent main.go
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o jilo-agent ../main.go
upx --best --lzma -o jilo-agent-upx jilo-agent
mv jilo-agent-upx jilo-agent

79
doc/install.sh 100755
View File

@ -0,0 +1,79 @@
#!/usr/bin/env bash
###
# Jilo Agent installation script
#
# Description: Installation script for Jilo Agent
# Author: Yasen Pramatarov
# License: GPLv2
# Project URL: https://lindeas.com/jilo
# Year: 2024
# Version: 0.1
#
###
# Paths to init and systemd service files
SYSVINIT_SCRIPT="./jilo-agent.init"
SYSTEMD_SERVICE="./jilo-agent.service"
UPSTART_CONF="./jilo-agent.conf"
# Function to install the SysVinit script
install_sysvinit() {
echo "Detected SysVinit. Installing init script..."
cp "$SYSVINIT_SCRIPT" /etc/init.d/jilo-agent
chmod +x /etc/init.d/jilo-agent
# for Debian/Ubuntu
if command -v update-rc.d >/dev/null 2>&1; then
update-rc.d jilo-agent defaults
# for RedHat/CentOS/Fedora
elif command -v chkconfig >/dev/null 2>&1; then
chkconfig --add jilo-agent
fi
echo "SysVinit script installed."
}
# Function to install the systemd service file
install_systemd() {
echo "Detected systemd. Installing systemd service file..."
cp "$SYSTEMD_SERVICE" /etc/systemd/system/jilo-agent.service
systemctl daemon-reload
systemctl enable jilo-agent.service
# compatibility with sysV
sudo ln -s /etc/systemd/system/jilo-agent.service /etc/init.d/jilo-agent
# starting the agent
systemctl start jilo-agent.service
echo "Systemd service file installed."
}
# Function to install the Upstart configuration
install_upstart() {
echo "Detected Upstart. Installing Upstart configuration..."
cp "$UPSTART_CONF" /etc/init/jilo-agent.conf
initctl reload-configuration
echo "Upstart configuration installed."
}
# Detect the init system
if [[ `readlink /proc/1/exe` == */systemd ]]; then
install_systemd
elif [[ -f /sbin/init && `/sbin/init --version 2>/dev/null` =~ upstart ]]; then
install_upstart
else
install_sysvinit
fi
exit 0

View File

@ -8,6 +8,12 @@ agent_port: 8081
# secret for checking JWT (same as in Jilo Web agent config)
secret_key: "mysecretkey"
# SSL certificate file (relative or full path)
ssl_cert: "jilo-agent.crt"
# SSL key file (relative or full path)
ssl_key: "jilo-agent.key"
# the port we check for nginx
nginx_port: 80

View File

@ -0,0 +1,76 @@
#!/bin/bash
# /etc/init.d/jilo-agent
# Init script for Jilo Agent
### BEGIN INIT INFO
# Provides: jilo-agent
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the Jilo Agent service
# Description: This script starts and stops the Jilo Agent service.
### END INIT INFO
AGENT_PATH="/usr/local/bin/jilo-agent"
CONFIG_FILE="/usr/local/etc/jilo-agent.conf"
AGENT_NAME="Jilo Agent"
AGENT_PID="/var/run/jilo-agent.pid"
LOG_FILE="/var/log/jilo-agent.log"
# Function to start the jilo agent
start_agent() {
if [ -f "$AGENT_PID" ]; then
echo "$AGENT_NAME is already running."
else
echo "Starting $AGENT_NAME..."
nohup $AGENT_PATH -c $CONFIG_FILE > $LOG_FILE 2>&1 &
echo $! > "$AGENT_PID"
echo "$AGENT_NAME started."
fi
}
# Function to stop the jilo agent
stop_agent() {
if [ ! -f "$AGENT_PID" ]; then
echo "$AGENT_NAME is not running."
else
echo "Stopping $AGENT_NAME..."
kill -9 $(cat "$AGENT_PID") && rm -f "$AGENT_PID"
echo "$AGENT_NAME stopped."
fi
}
# Function to restart the jilo agent
restart_agent() {
echo "Restarting $AGENT_NAME..."
stop_agent
sleep 1
start_agent
}
# Check for the first argument
case "$1" in
start)
start_agent
;;
stop)
stop_agent
;;
restart)
restart_agent
;;
status)
if [ -f "$AGENT_PID" ]; then
echo "$AGENT_NAME is running with PID $(cat $AGENT_PID)."
else
echo "$AGENT_NAME is not running."
fi
;;
*)
echo "Usage: /etc/init.d/jilo-agent {start|stop|restart|status}"
exit 1
;;
esac
exit 0

View File

@ -0,0 +1,14 @@
[Unit]
Description=Jilo Agent Service
After=network.target
[Service]
ExecStart=/usr/local/bin/jilo-agent -c /usr/local/etc/jilo-agent.conf
PIDFile=/run/jilo-agent.pid
Restart=on-failure
SyslogIdentifier=jilo-agent
User=root
Group=root
[Install]
WantedBy=multi-user.target

View File

@ -1,50 +0,0 @@
<?php
// get nginx data
function getNginxStatus() {
$status = trim(shell_exec('systemctl is-active nginx'));
return ($status === 'active') ? 'running' : 'not running';
}
function getNginxConnections() {
$connections = shell_exec("netstat -an | grep ':$nginxPort' | wc -l");
return intval(trim($connections));
}
// get prosody data
function getProsodyStatus() {
$status = trim(shell_exec('systemctl is-active prosody'));
return ($status === 'active') ? 'running' : 'not running';
}
// get jicofo data
function getJicofoStatus() {
$status = trim(shell_exec('systemctl is-active jicofo'));
return ($status === 'active') ? 'running' : 'not running';
}
function getJicofoStats($command) {
$data = shell_exec($command);
$decodedData = json_decode($data, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['error' => 'Failed to decode the JSON reply from the service.'];
}
return $decodedData;
}
// get JVB data
function getJVBStatus() {
$status = trim(shell_exec('systemctl is-active jitsi-videobridge2'));
return ($status === 'active') ? 'running' : 'not running';
}
function getJVBStats($command) {
$data = shell_exec($command);
$decodedData = json_decode($data, true);
if (json_last_error() !== JSON_ERROR_NONE) {
return ['error' => 'Failed to decode the JSON reply from the service.'];
}
return $decodedData;
}
?>

View File

View File

View File

@ -1,52 +0,0 @@
<?php
require 'config.php';
include 'functions.php';
$scriptname = basename($_SERVER['SCRIPT_NAME']);
$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// the response is in JSON
header('Content-Type: application/json');
// nginx status
if ($request === '/nginx' || $request === "/$scriptname/nginx") {
$data = [
'nginx_status' => getNginxStatus(),
'nginx_connections' => getNginxConnections(),
];
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
// prosody status
} elseif ($request === '/prosody' || $request === "/$scriptname/prosody") {
$data = [
'prosody_status' => getProsodyStatus(),
];
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
// jicofo status
} elseif ($request === '/jicofo' || $request === "/$scriptname/jicofo") {
$jicofoStatsCommand = "curl -s $jicofoStatsURL";
$jicofoStatsData = getJicofoStats($jicofoStatsCommand);
$data = [
'jicofo_status' => getJicofoStatus(),
'jicofo_API_stats' => $jicofoStatsData,
];
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
// jvb status
} elseif ($request === '/jvb' || $request === "/$scriptname/jvb") {
$jvbStatsCommand = "curl -s $jvbStatsURL";
$jvbStatsData = getJVBStats($jvbStatsCommand);
$data = [
'jvb_status' => getJVBStatus(),
'jvb_API_stats' => $jvbStatsData,
];
echo json_encode($data, JSON_PRETTY_PRINT) . "\n";
// default response - error
} else {
echo json_encode(['error' => 'Endpoint not found']) . "\n";
}
?>

View File

@ -12,6 +12,7 @@ Version: 0.1
package main
import (
"flag"
"fmt"
"encoding/json"
"gopkg.in/yaml.v2"
@ -19,6 +20,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strconv"
@ -28,6 +30,8 @@ import (
// Config holds the structure of the configuration file
type Config struct {
AgentPort int `yaml:"agent_port"`
SSLcert string `yaml:"ssl_cert"`
SSLkey string `yaml:"ssl_key"`
SecretKey string `yaml:"secret_key"`
NginxPort int `yaml:"nginx_port"`
ProsodyPort int `yaml:"prosody_port"`
@ -43,6 +47,12 @@ type Claims struct {
jwt.StandardClaims
}
// StatusData holds the status of the agent and its endpoints
type StatusData struct {
AgentStatus string `json:"agent_status"`
Endpoints map[string]string `json:"endpoints"`
}
// NginxData holds the nginx data structure for the API response to /nginx
type NginxData struct {
NginxState string `json:"nginx_state"`
@ -214,6 +224,102 @@ func authenticationJWT(next http.Handler) http.Handler {
})
}
// statusHandler handles the /status endpoint
func statusHandler(config Config, w http.ResponseWriter, r *http.Request) {
// Check if the agent is running
// FIXME add logic here to check if the agent is running OK, with no errors
agentStatus := "running"
// Prepare the endpoint status map
endpointStatuses := make(map[string]string)
// Determine protocol based on SSL config
protocol := "http"
if config.SSLcert != "" && config.SSLkey != "" {
protocol = "https"
}
// Check if each endpoint is available or not
endpoints := []string{"nginx", "prosody", "jicofo", "jvb", "jibri"}
for _, endpoint := range endpoints {
endpointURL := fmt.Sprintf("%s://localhost:%d/%s", protocol, config.AgentPort, endpoint)
req, err := http.NewRequest(http.MethodGet, endpointURL, nil)
if err != nil {
endpointStatuses[endpoint] = "not available"
continue
}
// Copy the JWT token from the original request
req.Header.Set("Authorization", r.Header.Get("Authorization"))
// Create a response recorder to capture the response
rr := httptest.NewRecorder()
// Call the respective handler with the new request
switch endpoint {
case "nginx":
nginxHandler(config, rr, req)
case "prosody":
prosodyHandler(config, rr, req)
case "jicofo":
jicofoHandler(config, rr, req)
case "jvb":
jvbHandler(config, rr, req)
case "jibri":
jibriHandler(config, rr, req)
}
// Check the status code from the response recorder
if rr.Result().StatusCode == http.StatusOK {
// Check if it's json
if rr.Header().Get("Content-Type") == "application/json" {
var result map[string]interface{}
if err := json.NewDecoder(rr.Body).Decode(&result); err == nil {
available := true
for key, value := range result {
if strings.HasSuffix(key, "_state") {
// If there is "*_state": "error" - it's accessible, but unavailable
if valueStr, ok := value.(string); ok && valueStr == "error" {
available = false
break
}
// If there is "*_state": "running" - it's OK
if valueStr, ok := value.(string); ok && valueStr == "running" {
available = true
break
}
}
}
if available {
endpointStatuses[endpoint] = "available"
} else {
endpointStatuses[endpoint] = "not available"
}
} else {
// Invalid JSON
endpointStatuses[endpoint] = "not available"
}
} else {
// It's not a JSON response
endpointStatuses[endpoint] = "not available"
}
} else {
// Reply not 200 OK
endpointStatuses[endpoint] = "not available"
}
}
// Prepare the response data and send back the JSON
statusData := StatusData{
AgentStatus: agentStatus,
Endpoints: endpointStatuses,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(statusData)
}
// nginxHandler handles the /nginx endpoint
func nginxHandler(config Config, w http.ResponseWriter, r *http.Request) {
data := NginxData {
@ -288,13 +394,28 @@ func corsMiddleware(next http.Handler) http.Handler {
// main sets up the http server and the routes
func main() {
// load the configuration
config := loadConfig("jilo-agent.conf")
// Define a flag for the config file
configFile := flag.String("c", "./jilo-agent.conf", "Specify the agent config file")
// Parse the flags
flag.Parse()
// Check if the file exists, fallback to default config file if not
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
fmt.Println("Config file not found, using default values")
}
// Load the configuration from the specified file (option -c) or the default config file name
config := loadConfig(*configFile)
secretKey = []byte(config.SecretKey)
mux := http.NewServeMux()
// endpoints
mux.Handle("/status", authenticationJWT(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
statusHandler(config, w, r)
})))
mux.Handle("/nginx", authenticationJWT(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nginxHandler(config, w, r)
})))
@ -317,7 +438,8 @@ func main() {
// start the http server
agentPortStr := fmt.Sprintf(":%d", config.AgentPort)
fmt.Printf("Starting Jilo agent on port %d.\n", config.AgentPort)
if err := http.ListenAndServe(agentPortStr, corsHandler); err != nil {
// if err := http.ListenAndServe(agentPortStr, corsHandler); err != nil {
if err := http.ListenAndServeTLS(agentPortStr, config.SSLcert, config.SSLkey, corsHandler); err != nil {
log.Fatalf("Could not start the agent: %v\n", err)
}
}