diff --git a/database.db b/database.db index ed4881b..8d54ebd 100644 Binary files a/database.db and b/database.db differ diff --git a/database.go b/database.go index 31f9b20..03470b8 100644 --- a/database.go +++ b/database.go @@ -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) diff --git a/docs/api.md b/docs/api.md index 7c78a26..f6a273b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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). --- diff --git a/frpAct.go b/frpAct.go index 259e864..342a6aa 100644 --- a/frpAct.go +++ b/frpAct.go @@ -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" {