Node API Reference

Tài liệu chi tiết về các REST API endpoints của OV-Node

Base URL

URL cơ sở cho tất cả API requests

http://YOUR_NODE_IP:9090

Thay thế YOUR_NODE_IP bằng IP hoặc domain của Node.

Get Status

Lấy trạng thái hiện tại của Node và cập nhật cấu hình OpenVPN

POST

Endpoint

POST /sync/get-status

Headers

{
  "key": "YOUR_API_KEY",
  "Content-Type": "application/json"
}

Request Body

{
  "set_new_setting": false,
  "protocol": "udp",        // optional: "udp" or "tcp"
  "port": 1194,             // optional: OpenVPN port
  "dns": "1.1.1.1"          // optional: DNS server
}

set_new_setting: true để cập nhật cấu hình OpenVPN, false để chỉ lấy status

Response

{
  "success": true,
  "msg": "Node status retrieved successfully",
  "data": {
    "status": "running",
    "cpu_usage": 15.2,
    "memory_usage": 45.8
  }
}

cURL Example

curl -X POST http://YOUR_NODE_IP:9090/sync/get-status \
  -H "key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "set_new_setting": false
  }'

Create User

Tạo người dùng OpenVPN mới trên Node

POST

Endpoint

POST /sync/create-user

Headers

{
  "key": "YOUR_API_KEY",
  "Content-Type": "application/json"
}

Request Body

{
  "name": "username"
}

Response (Success)

{
  "success": true,
  "msg": "User created successfully",
  "data": {
    "client_name": "username"
  }
}

Response (Error)

{
  "success": false,
  "msg": "Failed to create user",
  "data": null
}

cURL Example

curl -X POST http://YOUR_NODE_IP:9090/sync/create-user \
  -H "key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "testuser"
  }'

Delete User

Xóa người dùng OpenVPN khỏi Node

POST

Endpoint

POST /sync/delete-user

Headers

{
  "key": "YOUR_API_KEY",
  "Content-Type": "application/json"
}

Request Body

{
  "name": "username"
}

Response

{
  "success": true,
  "msg": "User deleted successfully",
  "data": {
    "client_name": "username"
  }
}

cURL Example

curl -X POST http://YOUR_NODE_IP:9090/sync/delete-user \
  -H "key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "testuser"
  }'

Download OVPN File

Tải file cấu hình .ovpn của người dùng

GET

Endpoint

GET /sync/download/ovpn/{client_name}

Headers

{
  "key": "YOUR_API_KEY"
}

Path Parameters

client_name: Tên người dùng cần tải file cấu hình

Response

Trả về file .ovpn với header:

Content-Type: application/x-openvpn-profile
Content-Disposition: attachment; filename="username.ovpn"

cURL Example

curl -X GET http://YOUR_NODE_IP:9090/sync/download/ovpn/testuser \
  -H "key: YOUR_API_KEY" \
  --output testuser.ovpn

OpenVPN Health Check

Kiểm tra tình trạng hoạt động của OpenVPN service

GET

Endpoint

GET /sync/openvpn/health

Headers

{
  "key": "YOUR_API_KEY"
}

Response (Healthy)

{
  "success": true,
  "msg": "OpenVPN health check completed",
  "data": {
    "healthy": true,
    "service_running": true,
    "port_listening": true,
    "config_valid": true,
    "issues": []
  }
}

Response (Unhealthy)

{
  "success": false,
  "msg": "OpenVPN health check completed",
  "data": {
    "healthy": false,
    "service_running": false,
    "port_listening": false,
    "config_valid": true,
    "issues": [
      "OpenVPN service is not running",
      "Port 1194 is not listening"
    ]
  }
}

cURL Example

curl -X GET http://YOUR_NODE_IP:9090/sync/openvpn/health \
  -H "key: YOUR_API_KEY"

Auto-Fix OpenVPN

Tự động phát hiện và sửa các vấn đề của OpenVPN service

POST

Endpoint

POST /sync/openvpn/fix

Headers

{
  "key": "YOUR_API_KEY"
}

Response (Success)

{
  "success": true,
  "msg": "OpenVPN auto-fix completed",
  "data": {
    "success": true,
    "actions_taken": [
      "Fixed missing IP in config",
      "Restarted OpenVPN service"
    ],
    "healthy": true
  }
}

cURL Example

curl -X POST http://YOUR_NODE_IP:9090/sync/openvpn/fix \
  -H "key: YOUR_API_KEY"

HTTP Status Codes

Các mã trạng thái HTTP thường gặp

200

OK

Request thành công

401

Unauthorized

API key không hợp lệ hoặc thiếu

422

Unprocessable Entity

Request body không hợp lệ

500

Internal Server Error

Lỗi server, kiểm tra logs

Best Practices

Khuyến nghị khi sử dụng API

  • Luôn kiểm tra success field trong response
  • Xử lý lỗi 401 bằng cách kiểm tra lại API key
  • Sử dụng HTTPS trong production với reverse proxy (nginx)
  • Giữ API key bí mật và không commit vào git
  • Sử dụng health check endpoint để monitoring