refactor(database): make DBListFrpcInstances able to list all users' frpc instances but not single user

feat(frpcAct): user will get createdBy when query frpc instances on current server
docs(api): update related api documentation
This commit is contained in:
2026-03-12 22:41:17 +08:00
parent f2b07a7732
commit 49048597f2
4 changed files with 45 additions and 25 deletions
BIN
View File
Binary file not shown.
+23 -4
View File
@@ -21,6 +21,20 @@ type User struct {
CreatedAt string
}
type FrpcInstance struct {
ID int
UserID int
Name string
ServerAddr string
ServerPort string
AuthMethod string
BootAtStart bool
RunUser string
ConfigPath string
CreatedAt time.Time
CreatedBy string
}
func InitDatabase(dbPath string) error {
InitUserDatabase(dbPath)
InitFrpcDatabase(dbPath)
@@ -362,10 +376,15 @@ func DBUpdateFrpcInstance(instance FrpcInstance) error {
return nil
}
func DBListFrpcInstances(userID int) ([]FrpcInstance, error) {
rows, err := frpcDB.Query("SELECT id, userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt FROM frpcInstances WHERE userID = ?", userID)
func DBListFrpcInstances() ([]FrpcInstance, error) {
rows, err := frpcDB.Query(`
SELECT fi.id, fi.userID, fi.name, fi.serverAddr, fi.serverPort, fi.auth_method,
fi.bootAtStart, fi.runUser, fi.configPath, fi.createdAt, u.username
FROM frpcInstances fi
JOIN userLogin u ON fi.userID = u.userID
`)
if err != nil {
return nil, fmt.Errorf("failed to query frpc instances: %w", err)
return nil, fmt.Errorf("failed to query all frpc instances: %w", err)
}
defer rows.Close()
@@ -373,7 +392,7 @@ func DBListFrpcInstances(userID int) ([]FrpcInstance, error) {
for rows.Next() {
var instance FrpcInstance
var createdAtStr string
if err := rows.Scan(&instance.ID, &instance.UserID, &instance.Name, &instance.ServerAddr, &instance.ServerPort, &instance.AuthMethod, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &createdAtStr); err != nil {
if err := rows.Scan(&instance.ID, &instance.UserID, &instance.Name, &instance.ServerAddr, &instance.ServerPort, &instance.AuthMethod, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &createdAtStr, &instance.CreatedBy); err != nil {
return nil, fmt.Errorf("failed to scan frpc instance: %w", err)
}
instance.CreatedAt, _ = time.Parse(time.RFC3339, createdAtStr)
+20 -3
View File
@@ -642,7 +642,7 @@ Modify system-level settings such as instance name, boot-at-start configuration,
**Auth Required:** Yes (token)
**Permission Level:** Admin
Retrieve a list of all frpc instances.
Retrieve a list of all frpc instances on the server.
**Request Headers:**
```
@@ -662,6 +662,7 @@ X-Timestamp: 1704067200000
"message": "instances retrieved successfully",
"data": [
{
"instanceID": 1,
"name": "my_frpc",
"serverAddr": "127.0.0.1",
"serverPort": "7000",
@@ -669,7 +670,8 @@ X-Timestamp: 1704067200000
"bootAtStart": true,
"runUser": "root",
"configPath": "./configs/superfrpc_user_my_frpc.toml",
"createdAt": "2024-01-01T00:00:00Z"
"createdAt": "2024-01-01T00:00:00Z",
"createdBy": "admin"
}
]
}
@@ -682,16 +684,31 @@ X-Timestamp: 1704067200000
"message": "instances retrieved successfully",
"data": [
{
"instanceID": 1,
"name": "my_frpc",
"bootAtStart": true,
"runUser": "root",
"configPath": "./configs/superfrpc_user_my_frpc.toml",
"createdAt": "2024-01-01T00:00:00Z"
"createdAt": "2024-01-01T00:00:00Z",
"createdBy": "admin"
}
]
}
```
| Field | Type | Description |
|-------|------|-------------|
| instanceID | int | Instance ID |
| name | string | Instance name |
| serverAddr | string | frps server address (admin/superuser only) |
| serverPort | string | frps server port (admin/superuser only) |
| auth_method | string | Authentication method (admin/superuser only) |
| bootAtStart | bool | Auto-start on system boot |
| runUser | string | User to run the frpc instance as |
| configPath | string | Path to the configuration file |
| createdAt | string | Instance creation time (ISO 8601 format) |
| createdBy | string | Username of the user who created this instance |
> Note: Visitor users do not see sensitive information (serverAddr, serverPort, auth_method).
---
+2 -18
View File
@@ -40,19 +40,6 @@ type CreateInstanceRequest struct {
Additional map[string]interface{} `json:"additionalProperties"`
}
type FrpcInstance struct {
ID int
UserID int
Name string
ServerAddr string
ServerPort string
AuthMethod string
BootAtStart bool
RunUser string
ConfigPath string
CreatedAt time.Time
}
var frpcDB *sql.DB
func CloseFrpcDatabase() error {
@@ -610,11 +597,7 @@ func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
return
}
// rows, err := frpcDB.Query(`
// SELECT id, userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt
// FROM frpcInstances WHERE userID = ?
// `, userID)
instanceList, err := DBListFrpcInstances(userID)
instanceList, err := DBListFrpcInstances()
if err != nil {
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to query instances: %v", err))
SendErrorResponse(w, http.StatusInternalServerError, "Failed to query instances")
@@ -633,6 +616,7 @@ func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
"runUser": instance.RunUser,
"configPath": instance.ConfigPath,
"createdAt": instance.CreatedAt,
"createdBy": instance.CreatedBy,
}
if userType == "visitor" {