Skip to content

Commit

Permalink
teliod cgi: Add base api to control teliod daemon
Browse files Browse the repository at this point in the history
Original work done in #1012.
This is restructure and reabase on common base.
  • Loading branch information
packgron committed Dec 18, 2024
1 parent b86960d commit 6a8b7f6
Show file tree
Hide file tree
Showing 12 changed files with 809 additions and 16 deletions.
Empty file added .unreleased/LLT-5831
Empty file.
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 134 additions & 0 deletions clis/teliod/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# QNAP Build

To build Teliod package for QNAP devices there are two alternatives:

- Run cargo build directly:
```cargo build --verbose --target x86_64-unknown-linux-musl --package teliod --features qnap```

- Run build script [Recommended]:
```../../ci/build_libtelio.py build qnap x86_64 [--debug]```

The build script has an additional stage at the end of the build where it creates the QNAP package.

## REST API

This REST API allows interaction with the Teliod daemon. It provides endpoints for managing the daemon, updating its configuration, and retrieving logs and status information.

### Endpoints

#### 1. **Start the Daemon**
- **Endpoint**: `/`
- **Method**: `POST`
- **Description**: Starts the Teliod Daemon in the background.
- **Request Body**: None
- **Responses**:
- **201 OK**: Daemon started successfully.
- **400 Bad Request**: Daemon is already running.
- **500 Internal Server Error**: Failed to start the daemon.

#### 2. **Stop the Daemon**
- **Endpoint**: `/`
- **Method**: `DELETE`
- **Description**: Stops the running Teliod Daemon.
- **Request Body**: None
- **Responses**:
- **200 OK**: Daemon stopped successfully.
- **410 Bad Request**: Daemon is not running.

#### 3. **Update Configuration**
- **Endpoint**: `/`
- **Method**: `PATCH`
- **Description**: Updates the daemon configuration with provided settings.
- **Request Body**: JSON object containing the configuration updates. Only specified fields will be updated; others remain unchanged.
- **Example Request Body**:
```json
{
"log_level": "info",
"log_file_path": "/new/path/to/log.log"
}
```
- **Responses**:
- **200 OK**: Configuration updated successfully
- **400 Bad Request**: Invalid JSON payload or configuration fields.
- **500 Internal Server Error**: Failed to update configuration.

#### 4. **Get Meshnet Status**
- **Endpoint**: `/?info=get-status`
- **Method**: `GET`
- **Description**: Retrieves the current status of the Meshnet from Teliod daemon.
- **Request Body**: None
- **Responses**:
- **200 OK**: Status information in JSON format.
```json
{
"telio_is_running": true,
"meshnet_ip": null,
"external_nodes": []
...
}
```
- **502 Internal Server Error**: Failed to retrieve status (Bad daemon response).
- **410 Gone**: Failed to communicate with the daemon (Couldn't send command/Daemon not accessible).
- **502 Gateway Timeout**: Failed to communicate with the daemon (Timeout while waiting daemon).

#### 5. **Get Meshnet Logs**
- **Endpoint**: `/?info=get-meshnet-logs`
- **Method**: `GET`
- **Description**: Retrieves the latest logs of the Meshnet.
- **Request Body**: None
- **Responses**:
- **200 OK**: Log content in text format.
```
{
"Log line 1\nLog line 2\nLog line 3\n..."
}
```
- **502 Bad Gateway**: Error reading log file.

#### 6. **Get Teliod Logs**
- **Endpoint**: `/?info=get-teliod-logs`
- **Method**: `GET`
- **Description**: Retrieves the latest logs of the Teliod Daemon.
- **Request Body**: None
- **Responses**:
- **200 OK**: Log content in text format.
```
{
"Log line 1\nLog line 2\nLog line 3\n..."
}
```
- **502 Bad Gateway**: Error reading log file.

### Error Handling

For all endpoints, the following error codes may be returned:
- **400 Bad Request**: The request was malformed or invalid.
- **404 Not Found**: Uri path is invalid.

### Example usage with curl

Launch web using:
```
just web
```
#### Start Teliod daemon:
```bash
curl -X POST http://127.0.0.1:8080/cgi-bin/teliod.cgi
```

#### Stop Teliod daemon:
```bash
curl -X DELETE http://127.0.0.1:8080/cgi-bin/teliod.cgi
```

#### Get Meshnet logs:
```bash
curl -X GET "http://127.0.0.1:8080/cgi-bin/?info=get-meshnet-logs"
```

#### Update Config:
```bash
curl -X PATCH -H "Content-Type: application/json" -d '{"log_level":"info", authentication_token": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}' http://<NAS-IP>:8080/cgi-bin/qpkg/teliod.cgi
```
7 changes: 6 additions & 1 deletion clis/teliod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ smart-default = "0.7.1"
base64 = "0.22.1"
dirs = "4.0.0"
rust-cgi = { version = "0.7.1", optional = true }
lazy_static.workspace = true
const_format = { version = "0.2.33", optional = true }

[dev-dependencies]
rand = "0.8.5"

[features]
cgi = ["rust-cgi"]
# TODO: remove this
default = ["cgi"]

cgi = ["rust-cgi", "const_format"]
qnap = ["cgi"]
Loading

0 comments on commit 6a8b7f6

Please sign in to comment.