- Introduce FrpcProxyInfo struct to support proxy configuration - Add logging statements to instance creation, deletion, and modification handlers - Implement addFrpcProxy function for generating proxy configuration
835 lines
26 KiB
Go
835 lines
26 KiB
Go
package main
|
||
|
||
import (
|
||
"database/sql"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"os"
|
||
"os/exec"
|
||
"path/filepath"
|
||
"strings"
|
||
"super-frpc/postLog"
|
||
"time"
|
||
)
|
||
|
||
type InstanceInfo struct {
|
||
Name string `json:"name"`
|
||
ServerAddr string `json:"serverAddr"`
|
||
ServerPort string `json:"serverPort"`
|
||
AuthMethod string `json:"auth_method"`
|
||
// BootAtStart bool `json:"bootAtStart"`
|
||
RunUser string `json:"runUser"`
|
||
Additional map[string]interface{} `json:"additionalProperties"`
|
||
}
|
||
|
||
type FrpcProxyInfo struct {
|
||
Name string `json:"name"`
|
||
Type string `json:"type"`
|
||
LocalIP string `json:"local_ip"`
|
||
LocalPort string `json:"local_port"`
|
||
RemotePort string `json:"remote_port"`
|
||
}
|
||
|
||
type CreateInstanceRequest struct {
|
||
InstanceInfo InstanceInfo `json:"instanceInfo"`
|
||
BootAtStart bool `json:"bootAtStart"`
|
||
RunUser string `json:"runUser"`
|
||
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 {
|
||
if frpcDB != nil {
|
||
return frpcDB.Close()
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||
if r.Method != http.MethodPost {
|
||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||
postLog.Debug(fmt.Sprintf("[CreateInstanceHandler] Invalid request method: %s", r.Method))
|
||
return
|
||
}
|
||
|
||
body, err := io.ReadAll(r.Body)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to read request body: %v", err))
|
||
SendErrorResponse(w, http.StatusBadRequest, "Failed to read request body")
|
||
return
|
||
}
|
||
defer r.Body.Close()
|
||
|
||
// 先解析为map,处理类型不匹配的情况
|
||
var reqMap map[string]interface{}
|
||
if err := json.Unmarshal(body, &reqMap); err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to unmarshal request body: %v", err))
|
||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request format")
|
||
return
|
||
}
|
||
|
||
// 处理bootAtStart字段
|
||
bootAtStart := false
|
||
if bas, ok := reqMap["bootAtStart"]; ok {
|
||
switch v := bas.(type) {
|
||
case bool:
|
||
bootAtStart = v
|
||
case string:
|
||
if v == "true" {
|
||
bootAtStart = true
|
||
}
|
||
}
|
||
}
|
||
|
||
// 处理instanceInfo字段
|
||
instanceInfoMap, ok := reqMap["instanceInfo"].(map[string]interface{})
|
||
if !ok {
|
||
SendErrorResponse(w, http.StatusBadRequest, "Invalid instanceInfo format")
|
||
return
|
||
}
|
||
|
||
instanceInfo := InstanceInfo{
|
||
Name: getStringFromMap(instanceInfoMap, "name"),
|
||
ServerAddr: getStringFromMap(instanceInfoMap, "serverAddr"),
|
||
ServerPort: getStringFromMap(instanceInfoMap, "serverPort"),
|
||
AuthMethod: getStringFromMap(instanceInfoMap, "auth_method"),
|
||
RunUser: getStringFromMap(reqMap, "runUser"),
|
||
}
|
||
|
||
// 处理additionalProperties字段
|
||
if additional, ok := reqMap["additionalProperties"].(map[string]interface{}); ok {
|
||
instanceInfo.Additional = additional
|
||
}
|
||
|
||
// 从Header中验证token和timeStamp
|
||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to validate request header: %v", err))
|
||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||
return
|
||
}
|
||
|
||
// 构建请求结构体
|
||
req := CreateInstanceRequest{
|
||
InstanceInfo: instanceInfo,
|
||
BootAtStart: bootAtStart,
|
||
RunUser: instanceInfo.RunUser,
|
||
Additional: instanceInfo.Additional,
|
||
}
|
||
|
||
if err := CheckPermission(userID, "superuser", "admin"); err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to check permission: %v", err))
|
||
SendErrorResponse(w, http.StatusForbidden, err.Error())
|
||
return
|
||
}
|
||
|
||
if req.InstanceInfo.Name == "" || req.InstanceInfo.ServerAddr == "" ||
|
||
req.InstanceInfo.ServerPort == "" || req.InstanceInfo.AuthMethod == "" {
|
||
SendErrorResponse(w, http.StatusBadRequest, "Missing required fields in instanceInfo")
|
||
return
|
||
}
|
||
|
||
runUser := req.RunUser
|
||
if runUser == "" {
|
||
runUser = "root"
|
||
}
|
||
|
||
user, err := GetUserByID(userID)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to get user info: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user info")
|
||
return
|
||
}
|
||
|
||
configDir, err := GetConfigDir()
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to get config directory: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get config directory")
|
||
return
|
||
}
|
||
|
||
configFileName := fmt.Sprintf("superfrpc_%s_%s.toml", user.Username, req.InstanceInfo.Name)
|
||
configPath := filepath.Join(configDir, configFileName)
|
||
|
||
configContent := generateFrpcConfig(req.InstanceInfo)
|
||
if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil {
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to create config file %s: %v", configPath, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to create config file")
|
||
return
|
||
}
|
||
|
||
_, err = frpcDB.Exec(`
|
||
INSERT INTO frpcInstances (userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||
`, userID, req.InstanceInfo.Name, req.InstanceInfo.ServerAddr, req.InstanceInfo.ServerPort,
|
||
req.InstanceInfo.AuthMethod, req.BootAtStart, runUser, configPath, time.Now().Format(time.RFC3339))
|
||
|
||
if err != nil {
|
||
os.Remove(configPath)
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to save instance %s to database: %v", req.InstanceInfo.Name, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to save instance to database")
|
||
return
|
||
}
|
||
|
||
if req.BootAtStart {
|
||
if err := createBootService(user.Username, req.InstanceInfo.Name, configPath, runUser); err != nil {
|
||
frpcDB.Exec("DELETE FROM frpcInstances WHERE userID = ? AND name = ?", userID, req.InstanceInfo.Name)
|
||
os.Remove(configPath)
|
||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to create boot service for instance %s: %v", req.InstanceInfo.Name, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to create boot service")
|
||
return
|
||
}
|
||
}
|
||
|
||
SendSuccessResponse(w, "Instance created successfully", map[string]interface{}{
|
||
"name": req.InstanceInfo.Name,
|
||
"configPath": configPath,
|
||
"bootAtStart": req.BootAtStart,
|
||
})
|
||
postLog.Info(fmt.Sprintf("[CreateInstanceHandler] Instance %s created successfully: configPath=%s, bootAtStart=%v, runUser=%s, additionalProperties=%v", req.InstanceInfo.Name, configPath, req.BootAtStart, runUser, req.Additional))
|
||
}
|
||
|
||
func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||
if r.Method != http.MethodPost {
|
||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||
return
|
||
}
|
||
|
||
body, err := io.ReadAll(r.Body)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to read request body: %v", err))
|
||
SendErrorResponse(w, http.StatusBadRequest, "Failed to read request body")
|
||
return
|
||
}
|
||
defer r.Body.Close()
|
||
|
||
var reqMap map[string]interface{}
|
||
if err := json.Unmarshal(body, &reqMap); err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to unmarshal request body: %v", err))
|
||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request format")
|
||
return
|
||
}
|
||
|
||
instanceName := getStringFromMap(reqMap, "instanceName")
|
||
if instanceName == "" {
|
||
SendErrorResponse(w, http.StatusBadRequest, "instanceName is required")
|
||
return
|
||
}
|
||
|
||
// 从Header中验证token和timeStamp
|
||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to validate request header: %v", err))
|
||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||
return
|
||
}
|
||
|
||
if err := CheckPermission(userID, "superuser", "admin"); err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to check permission: %v", err))
|
||
SendErrorResponse(w, http.StatusForbidden, err.Error())
|
||
return
|
||
}
|
||
|
||
user, err := GetUserByID(userID)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to get user info: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user info")
|
||
return
|
||
}
|
||
|
||
var instance FrpcInstance
|
||
err = frpcDB.QueryRow(`
|
||
SELECT id, userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt
|
||
FROM frpcInstances WHERE userID = ? AND name = ?
|
||
`, userID, instanceName).Scan(
|
||
&instance.ID, &instance.UserID, &instance.Name, &instance.ServerAddr, &instance.ServerPort,
|
||
&instance.AuthMethod, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &instance.CreatedAt)
|
||
|
||
if err == sql.ErrNoRows {
|
||
SendErrorResponse(w, http.StatusNotFound, "instance not found")
|
||
return
|
||
}
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to query instance: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to query instance")
|
||
return
|
||
}
|
||
|
||
if instance.BootAtStart {
|
||
if err := removeBootService(user.Username, instanceName); err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to remove boot service for instance %s: %v", instanceName, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to remove boot service")
|
||
return
|
||
}
|
||
}
|
||
|
||
if _, err := os.Stat(instance.ConfigPath); err == nil {
|
||
if err := os.Remove(instance.ConfigPath); err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to remove config file %s: %v", instance.ConfigPath, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to remove config file")
|
||
return
|
||
}
|
||
}
|
||
|
||
_, err = frpcDB.Exec("DELETE FROM frpcInstances WHERE id = ?", instance.ID)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to delete instance %s from database: %v", instanceName, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to delete instance from database")
|
||
return
|
||
}
|
||
|
||
SendSuccessResponse(w, "Instance deleted successfully", map[string]interface{}{
|
||
"name": instanceName,
|
||
})
|
||
postLog.Info(fmt.Sprintf("[DeleteInstanceHandler] Instance %s deleted successfully", instanceName))
|
||
}
|
||
|
||
func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request, field string) {
|
||
if r.Method != http.MethodPost {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Invalid request method: %s", r.Method))
|
||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||
return
|
||
}
|
||
|
||
body, err := io.ReadAll(r.Body)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to read request body: %v", err))
|
||
SendErrorResponse(w, http.StatusBadRequest, "Failed to read request body")
|
||
return
|
||
}
|
||
defer r.Body.Close()
|
||
|
||
var reqMap map[string]interface{}
|
||
if err := json.Unmarshal(body, &reqMap); err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to unmarshal request body: %v", err))
|
||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request format")
|
||
return
|
||
}
|
||
|
||
instanceName := getStringFromMap(reqMap, "instanceName")
|
||
if instanceName == "" {
|
||
SendErrorResponse(w, http.StatusBadRequest, "instanceName is required")
|
||
return
|
||
}
|
||
|
||
// 从Header中验证token和timeStamp
|
||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to validate request header: %v", err))
|
||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||
return
|
||
}
|
||
|
||
if err := CheckPermission(userID, "superuser", "admin"); err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to check permission: %v", err))
|
||
SendErrorResponse(w, http.StatusForbidden, err.Error())
|
||
return
|
||
}
|
||
|
||
user, err := GetUserByID(userID)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to get user info: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user info")
|
||
return
|
||
}
|
||
|
||
var instance FrpcInstance
|
||
err = frpcDB.QueryRow(`
|
||
SELECT id, userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt
|
||
FROM frpcInstances WHERE userID = ? AND name = ?
|
||
`, userID, instanceName).Scan(
|
||
&instance.ID, &instance.UserID, &instance.Name, &instance.ServerAddr, &instance.ServerPort,
|
||
&instance.AuthMethod, &instance.BootAtStart, &instance.RunUser, &instance.ConfigPath, &instance.CreatedAt)
|
||
|
||
if err == sql.ErrNoRows {
|
||
SendErrorResponse(w, http.StatusNotFound, "instance not found")
|
||
return
|
||
}
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to query instance: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to query instance")
|
||
return
|
||
}
|
||
|
||
newName := instance.Name
|
||
newServerAddr := instance.ServerAddr
|
||
newServerPort := instance.ServerPort
|
||
newAuthMethod := instance.AuthMethod
|
||
newRunUser := instance.RunUser
|
||
newBootAtStart := instance.BootAtStart
|
||
|
||
if v, ok := reqMap["name"].(string); ok && v != "" {
|
||
newName = v
|
||
}
|
||
if v, ok := reqMap["serverAddr"].(string); ok && v != "" {
|
||
newServerAddr = v
|
||
}
|
||
if v, ok := reqMap["serverPort"].(string); ok && v != "" {
|
||
newServerPort = v
|
||
}
|
||
if v, ok := reqMap["auth_method"].(string); ok && v != "" {
|
||
newAuthMethod = v
|
||
}
|
||
if v, ok := reqMap["runUser"].(string); ok && v != "" {
|
||
newRunUser = v
|
||
}
|
||
if v, ok := reqMap["bootAtStart"].(bool); ok {
|
||
newBootAtStart = v
|
||
}
|
||
|
||
oldConfigPath := instance.ConfigPath
|
||
var newConfigPath string
|
||
if newName != instance.Name || newRunUser != instance.RunUser {
|
||
configDir, err := GetConfigDir()
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to get config directory: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get config directory")
|
||
return
|
||
}
|
||
|
||
newConfigFileName := fmt.Sprintf("superfrpc_%s_%s.toml", user.Username, newName)
|
||
newConfigPath = filepath.Join(configDir, newConfigFileName)
|
||
|
||
if oldConfigPath != newConfigPath {
|
||
if _, err := os.Stat(oldConfigPath); err == nil {
|
||
if err := os.Rename(oldConfigPath, newConfigPath); err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to rename config file %s to %s: %v", oldConfigPath, newConfigPath, err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to rename config file")
|
||
return
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
newConfigPath = oldConfigPath
|
||
}
|
||
|
||
info := InstanceInfo{
|
||
Name: newName,
|
||
ServerAddr: newServerAddr,
|
||
ServerPort: newServerPort,
|
||
AuthMethod: newAuthMethod,
|
||
RunUser: newRunUser,
|
||
}
|
||
|
||
configContent := generateFrpcConfig(info)
|
||
if err := os.WriteFile(newConfigPath, []byte(configContent), 0644); err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to update config file: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to update config file")
|
||
return
|
||
}
|
||
|
||
_, err = frpcDB.Exec(`
|
||
UPDATE frpcInstances
|
||
SET name = ?, serverAddr = ?, serverPort = ?, auth_method = ?, bootAtStart = ?, runUser = ?, configPath = ?
|
||
WHERE id = ?
|
||
`, newName, newServerAddr, newServerPort, newAuthMethod, newBootAtStart, newRunUser, newConfigPath, instance.ID)
|
||
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to update instance in database: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to update instance in database")
|
||
return
|
||
}
|
||
|
||
if instance.BootAtStart && !newBootAtStart {
|
||
removeBootService(user.Username, instanceName)
|
||
} else if !instance.BootAtStart && newBootAtStart {
|
||
createBootService(user.Username, newName, newConfigPath, newRunUser)
|
||
} else if instance.BootAtStart && newBootAtStart && (instance.Name != newName || instance.RunUser != newRunUser) {
|
||
removeBootService(user.Username, instanceName)
|
||
createBootService(user.Username, newName, newConfigPath, newRunUser)
|
||
}
|
||
|
||
SendSuccessResponse(w, "Instance modified successfully", map[string]interface{}{
|
||
"name": newName,
|
||
"configPath": newConfigPath,
|
||
})
|
||
postLog.Info(fmt.Sprintf("[ModifyInstanceHandler] Instance %s modified successfully: configPath=%s, bootAtStart=%v, runUser=%s", newName, newConfigPath, newBootAtStart, newRunUser))
|
||
}
|
||
|
||
func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
|
||
if r.Method != http.MethodGet {
|
||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||
return
|
||
}
|
||
|
||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to validate request: %v", err))
|
||
SendErrorResponse(w, http.StatusUnauthorized, "Failed to validate request")
|
||
return
|
||
}
|
||
|
||
userType, err := GetUserType(userID)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to get user type: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user type")
|
||
return
|
||
}
|
||
|
||
rows, err := frpcDB.Query(`
|
||
SELECT id, userID, name, serverAddr, serverPort, auth_method, bootAtStart, runUser, configPath, createdAt
|
||
FROM frpcInstances WHERE userID = ?
|
||
`, userID)
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to query instances: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to query instances")
|
||
return
|
||
}
|
||
defer rows.Close()
|
||
|
||
var instances []map[string]interface{}
|
||
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 {
|
||
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to scan instance: %v", err))
|
||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to scan instance")
|
||
return
|
||
}
|
||
|
||
instanceData := map[string]interface{}{
|
||
"name": instance.Name,
|
||
"serverAddr": instance.ServerAddr,
|
||
"serverPort": instance.ServerPort,
|
||
"auth_method": instance.AuthMethod,
|
||
"bootAtStart": instance.BootAtStart,
|
||
"runUser": instance.RunUser,
|
||
"configPath": instance.ConfigPath,
|
||
"createdAt": createdAtStr,
|
||
}
|
||
|
||
if userType == "visitor" {
|
||
delete(instanceData, "serverAddr")
|
||
delete(instanceData, "serverPort")
|
||
delete(instanceData, "auth_method")
|
||
}
|
||
|
||
instances = append(instances, instanceData)
|
||
}
|
||
|
||
if instances == nil {
|
||
instances = []map[string]interface{}{}
|
||
}
|
||
|
||
SendSuccessResponse(w, "Instances retrieved successfully", instances)
|
||
}
|
||
|
||
func generateFrpcConfig(info InstanceInfo) string {
|
||
var sb strings.Builder
|
||
sb.WriteString("[common]\n")
|
||
sb.WriteString(fmt.Sprintf("server_addr = %s\n", info.ServerAddr))
|
||
sb.WriteString(fmt.Sprintf("server_port = %s\n", info.ServerPort))
|
||
sb.WriteString(fmt.Sprintf("auth_method = %s\n", info.AuthMethod))
|
||
|
||
for key, value := range info.Additional {
|
||
sb.WriteString(fmt.Sprintf("%s = %v\n", key, value))
|
||
}
|
||
|
||
return sb.String()
|
||
}
|
||
|
||
func addFrpcProxy(info FrpcProxyInfo) string {
|
||
var sb strings.Builder
|
||
sb.WriteString("[[proxies]]")
|
||
sb.WriteString(fmt.Sprintf("name = %s\n", info.Name))
|
||
sb.WriteString(fmt.Sprintf("type = %s\n", info.Type))
|
||
sb.WriteString(fmt.Sprintf("local_ip = %s\n", info.LocalIP))
|
||
sb.WriteString(fmt.Sprintf("local_port = %s\n", info.LocalPort))
|
||
sb.WriteString(fmt.Sprintf("remote_port = %s\n", info.RemotePort))
|
||
|
||
return sb.String()
|
||
}
|
||
|
||
func GetConfigDir() (string, error) {
|
||
config, err := GetConfig()
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[GetConfigDir] Failed to get config: %v", err))
|
||
return "", err
|
||
}
|
||
return config.InstancePath, nil
|
||
}
|
||
|
||
func GetFrpcPath() (string, error) {
|
||
config, err := GetConfig()
|
||
if err != nil {
|
||
postLog.Error(fmt.Sprintf("[GetFrpcPath] Failed to get config: %v", err))
|
||
return "", err
|
||
}
|
||
return config.FrpcPath, nil
|
||
}
|
||
|
||
func detectInitSystem() string {
|
||
if _, err := os.Stat("/run/systemd/system"); err == nil {
|
||
return "systemd"
|
||
}
|
||
if _, err := os.Stat("/etc/init.d"); err == nil {
|
||
return "init.d"
|
||
}
|
||
return "unknown"
|
||
}
|
||
|
||
func createBootService(username, instanceName, configPath, runUser string) error {
|
||
initSystem := detectInitSystem()
|
||
|
||
switch initSystem {
|
||
case "systemd":
|
||
return createSystemdService(username, instanceName, configPath, runUser)
|
||
case "init.d":
|
||
return createInitDService(username, instanceName, configPath, runUser)
|
||
default:
|
||
return errors.New("unsupported init system")
|
||
}
|
||
}
|
||
|
||
func createSystemdService(username, instanceName, configPath, runUser string) error {
|
||
frpcPath, err := GetFrpcPath()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
serviceName := fmt.Sprintf("superfrpc_%s_%s", username, instanceName)
|
||
serviceContent := fmt.Sprintf(`[Unit]
|
||
Description=superfrpc_%s_%s
|
||
After=network.target
|
||
|
||
[Service]
|
||
Type=simple
|
||
ExecStart=%s -c %s
|
||
User=%s
|
||
Restart=on-failure
|
||
RestartSec=5
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
`, username, instanceName, frpcPath, configPath, runUser)
|
||
|
||
servicePath := filepath.Join("/etc/systemd/system", serviceName+".service")
|
||
if err := os.WriteFile(servicePath, []byte(serviceContent), 0644); err != nil {
|
||
return fmt.Errorf("failed to create systemd service file: %w", err)
|
||
}
|
||
|
||
cmd := exec.Command("systemctl", "enable", serviceName)
|
||
if output, err := cmd.CombinedOutput(); err != nil {
|
||
os.Remove(servicePath)
|
||
return fmt.Errorf("failed to enable service: %s, output: %s", err, output)
|
||
}
|
||
|
||
cmd = exec.Command("systemctl", "daemon-reload")
|
||
if output, err := cmd.CombinedOutput(); err != nil {
|
||
return fmt.Errorf("failed to reload daemon: %s, output: %s", err, output)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func createInitDService(username, instanceName, configPath, runUser string) error {
|
||
frpcPath, err := GetFrpcPath()
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
serviceName := fmt.Sprintf("superfrpc_%s_%s", username, instanceName)
|
||
|
||
var serviceContent string
|
||
runUserArg := ""
|
||
if runUser != "" && runUser != "root" {
|
||
runUserArg = runUser
|
||
serviceContent = fmt.Sprintf(`#!/bin/sh
|
||
|
||
### BEGIN INIT INFO
|
||
# Provides: %s
|
||
# Required-Start: $network $remote_fs $syslog
|
||
# Required-Stop: $network $remote_fs $syslog
|
||
# Default-Start: 2 3 4 5
|
||
# Default-Stop: 0 1 6
|
||
# Description: superfrpc %s %s
|
||
### END INIT INFO
|
||
|
||
NAME="%s"
|
||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
||
DAEMON=%s
|
||
DAEMON_ARGS="-c %s"
|
||
SCRIPTNAME=/etc/init.d/$NAME
|
||
USER=%s
|
||
|
||
[ -x "$DAEMON" ] || exit 0
|
||
|
||
case "$1" in
|
||
start)
|
||
echo -n "Starting $NAME: "
|
||
start-stop-daemon -S -c $USER -b -m -p /var/run/$NAME.pid --start --exec $DAEMON -- $DAEMON_ARGS
|
||
echo "$NAME."
|
||
;;
|
||
stop)
|
||
echo -n "Stopping $NAME: "
|
||
start-stop-daemon -K -p /var/run/$NAME.pid
|
||
rm -f /var/run/$NAME.pid
|
||
echo "$NAME."
|
||
;;
|
||
restart)
|
||
$0 stop
|
||
$0 start
|
||
;;
|
||
status)
|
||
if [ -f /var/run/$NAME.pid ]; then
|
||
echo "$NAME is running (pid $(cat /var/run/$NAME.pid))"
|
||
else
|
||
echo "$NAME is not running"
|
||
fi
|
||
;;
|
||
*)
|
||
echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
exit 0
|
||
`, serviceName, username, instanceName, serviceName, frpcPath, configPath, runUserArg)
|
||
} else {
|
||
serviceContent = fmt.Sprintf(`#!/bin/sh
|
||
|
||
### BEGIN INIT INFO
|
||
# Provides: %s
|
||
# Required-Start: $network $remote_fs $syslog
|
||
# Required-Stop: $network $remote_fs $syslog
|
||
# Default-Start: 2 3 4 5
|
||
# Default-Stop: 0 1 6
|
||
# Description: superfrpc %s %s
|
||
### END INIT INFO
|
||
|
||
NAME="%s"
|
||
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
||
DAEMON=%s
|
||
DAEMON_ARGS="-c %s"
|
||
SCRIPTNAME=/etc/init.d/$NAME
|
||
|
||
[ -x "$DAEMON" ] || exit 0
|
||
|
||
case "$1" in
|
||
start)
|
||
echo -n "Starting $NAME: "
|
||
start-stop-daemon -S -b -m -p /var/run/$NAME.pid --start --exec $DAEMON -- $DAEMON_ARGS
|
||
echo "$NAME."
|
||
;;
|
||
stop)
|
||
echo -n "Stopping $NAME: "
|
||
start-stop-daemon -K -p /var/run/$NAME.pid
|
||
rm -f /var/run/$NAME.pid
|
||
echo "$NAME."
|
||
;;
|
||
restart)
|
||
$0 stop
|
||
$0 start
|
||
;;
|
||
status)
|
||
if [ -f /var/run/$NAME.pid ]; then
|
||
echo "$NAME is running (pid $(cat /var/run/$NAME.pid))"
|
||
else
|
||
echo "$NAME is not running"
|
||
fi
|
||
;;
|
||
*)
|
||
echo "Usage: $SCRIPTNAME {start|stop|restart|status}"
|
||
exit 1
|
||
;;
|
||
esac
|
||
|
||
exit 0
|
||
`, serviceName, username, instanceName, serviceName, frpcPath, configPath)
|
||
}
|
||
|
||
servicePath := filepath.Join("/etc/init.d", serviceName)
|
||
if err := os.WriteFile(servicePath, []byte(serviceContent), 0755); err != nil {
|
||
return fmt.Errorf("failed to create init.d service file: %w", err)
|
||
}
|
||
|
||
cmd := exec.Command("/etc/init.d", serviceName, "enable")
|
||
if output, err := cmd.CombinedOutput(); err != nil {
|
||
os.Remove(servicePath)
|
||
return fmt.Errorf("failed to enable service: %s, output: %s", err, output)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func removeBootService(username, instanceName string) error {
|
||
initSystem := detectInitSystem()
|
||
|
||
serviceName := fmt.Sprintf("superfrpc_%s_%s", username, instanceName)
|
||
|
||
switch initSystem {
|
||
case "systemd":
|
||
cmd := exec.Command("systemctl", "disable", serviceName)
|
||
cmd.CombinedOutput()
|
||
|
||
servicePath := filepath.Join("/etc/systemd/system", serviceName+".service")
|
||
os.Remove(servicePath)
|
||
|
||
cmd = exec.Command("systemctl", "daemon-reload")
|
||
cmd.CombinedOutput()
|
||
|
||
case "init.d":
|
||
cmd := exec.Command("/etc/init.d", serviceName, "disable")
|
||
cmd.CombinedOutput()
|
||
|
||
servicePath := filepath.Join("/etc/init.d", serviceName)
|
||
os.Remove(servicePath)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
func GetUserInstances(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)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer rows.Close()
|
||
|
||
var instances []FrpcInstance
|
||
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 {
|
||
return nil, err
|
||
}
|
||
instance.CreatedAt, _ = time.Parse(time.RFC3339, createdAtStr)
|
||
instances = append(instances, instance)
|
||
}
|
||
|
||
return instances, nil
|
||
}
|
||
|
||
func getStringFromMap(m map[string]interface{}, key string) string {
|
||
if v, ok := m[key].(string); ok {
|
||
return v
|
||
}
|
||
return ""
|
||
}
|