Files
NetworkAuth/controllers/admin/api.go

382 lines
11 KiB
Go
Raw Normal View History

package admin
import (
2026-03-18 21:51:17 +08:00
"NetworkAuth/controllers"
"NetworkAuth/models"
"NetworkAuth/services"
2026-03-18 21:51:17 +08:00
"NetworkAuth/utils/encrypt"
"encoding/hex"
"net/http"
"strconv"
"strings"
2025-10-26 14:48:02 +08:00
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
2025-10-27 23:12:15 +08:00
// ============================================================================
// 全局变量
// ============================================================================
2025-10-26 14:48:02 +08:00
// 创建基础控制器实例
var apiBaseController = controllers.NewBaseController()
2025-10-27 23:12:15 +08:00
// ============================================================================
// API处理器
// ============================================================================
// APIListHandler 接口列表API处理器
2025-10-26 14:48:02 +08:00
func APIListHandler(c *gin.Context) {
// 获取分页参数
page, limit := apiBaseController.GetPaginationParams(c)
// 获取应用UUID参数用于按应用筛选接口
2025-10-26 14:48:02 +08:00
appUUID := strings.TrimSpace(c.Query("app_uuid"))
2025-10-25 20:52:50 +08:00
// 获取接口类型参数(用于按接口类型筛选)
2025-10-26 14:48:02 +08:00
apiTypeStr := strings.TrimSpace(c.Query("api_type"))
2025-10-25 20:52:50 +08:00
var apiType int
if apiTypeStr != "" {
apiType, _ = strconv.Atoi(apiTypeStr)
}
// 构建查询
2025-10-26 14:48:02 +08:00
db, ok := apiBaseController.GetDB(c)
if !ok {
return
}
// 构建基础查询
query := db.Model(&models.API{})
// 如果指定了应用UUID则按应用筛选
if appUUID != "" {
query = query.Where("app_uuid = ?", appUUID)
}
2025-10-25 20:52:50 +08:00
// 如果指定了接口类型,则按接口类型筛选
if apiType > 0 {
query = query.Where("api_type = ?", apiType)
}
// 泛型分页查询
apis, total, err := services.Paginate[models.API](query, page, limit, "created_at DESC")
if err != nil {
logrus.WithError(err).Error("Failed to fetch APIs")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "获取接口列表失败", err)
return
}
// 获取关联的应用信息
var appUUIDs []string
for _, api := range apis {
appUUIDs = append(appUUIDs, api.AppUUID)
}
var apps []models.App
if len(appUUIDs) > 0 {
if err := db.Where("uuid IN ?", appUUIDs).Find(&apps).Error; err != nil {
logrus.WithError(err).Error("Failed to fetch related apps")
}
}
// 创建应用UUID到应用名称的映射
appMap := make(map[string]string)
for _, app := range apps {
appMap[app.UUID] = app.Name + "(ID:" + strconv.Itoa(int(app.ID)) + ")"
}
// 构建响应数据
type APIResponse struct {
models.API
AppName string `json:"app_name"`
APITypeName string `json:"api_type_name"`
StatusName string `json:"status_name"`
AlgorithmNames struct {
Submit string `json:"submit"`
Return string `json:"return"`
} `json:"algorithm_names"`
}
var responseAPIs []APIResponse
for _, api := range apis {
responseAPI := APIResponse{
API: api,
AppName: appMap[api.AppUUID],
APITypeName: models.GetAPITypeName(api.APIType),
StatusName: getAPIStatusName(api.Status),
}
responseAPI.AlgorithmNames.Submit = models.GetAlgorithmName(api.SubmitAlgorithm)
responseAPI.AlgorithmNames.Return = models.GetAlgorithmName(api.ReturnAlgorithm)
responseAPIs = append(responseAPIs, responseAPI)
}
2026-03-28 23:30:02 +08:00
// 返回结果
2025-10-26 14:48:02 +08:00
response := gin.H{
2026-03-28 23:30:02 +08:00
"code": 0,
"msg": "success",
"count": total,
"data": responseAPIs,
}
2025-10-26 14:48:02 +08:00
c.JSON(http.StatusOK, response)
}
2025-10-27 23:12:15 +08:00
// ============================================================================
// 辅助函数
// ============================================================================
// getAPIStatusName 获取API状态名称
func getAPIStatusName(status int) string {
switch status {
case 1:
return "启用"
case 0:
return "禁用"
default:
return "未知"
}
}
// APIUpdateHandler 更新接口处理器
2025-10-26 14:48:02 +08:00
func APIUpdateHandler(c *gin.Context) {
var req struct {
2025-10-26 00:08:55 +08:00
UUID string `json:"uuid"`
Status int `json:"status"`
SubmitAlgorithm int `json:"submit_algorithm"`
ReturnAlgorithm int `json:"return_algorithm"`
SubmitPublicKey string `json:"submit_public_key"`
SubmitPrivateKey string `json:"submit_private_key"`
ReturnPublicKey string `json:"return_public_key"`
ReturnPrivateKey string `json:"return_private_key"`
}
2025-10-26 14:48:02 +08:00
if !apiBaseController.BindJSON(c, &req) {
return
}
// 验证必填字段
2025-10-26 00:08:55 +08:00
if strings.TrimSpace(req.UUID) == "" {
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "接口UUID不能为空")
return
}
if req.Status != 0 && req.Status != 1 {
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "无效的状态值")
return
}
if !models.IsValidAlgorithm(req.SubmitAlgorithm) || !models.IsValidAlgorithm(req.ReturnAlgorithm) {
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "无效的算法类型")
return
}
// 获取数据库连接
2025-10-26 14:48:02 +08:00
db, ok := apiBaseController.GetDB(c)
if !ok {
return
}
// 查找并更新API记录
var api models.API
2025-10-26 00:08:55 +08:00
if err := db.Where("uuid = ?", strings.TrimSpace(req.UUID)).First(&api).Error; err != nil {
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "接口不存在")
return
}
// 更新字段(不允许修改 APIType
api.Status = req.Status
api.SubmitAlgorithm = req.SubmitAlgorithm
api.ReturnAlgorithm = req.ReturnAlgorithm
// 可选更新密钥/证书(当提供时)
if req.SubmitPublicKey != "" || req.SubmitPrivateKey != "" {
api.SubmitPublicKey = req.SubmitPublicKey
api.SubmitPrivateKey = req.SubmitPrivateKey
}
if req.ReturnPublicKey != "" || req.ReturnPrivateKey != "" {
api.ReturnPublicKey = req.ReturnPublicKey
api.ReturnPrivateKey = req.ReturnPrivateKey
}
if err := db.Save(&api).Error; err != nil {
logrus.WithError(err).Error("Failed to update API")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "更新接口失败", err)
return
}
2025-10-26 14:48:02 +08:00
apiBaseController.HandleSuccess(c, "接口更新成功", api)
}
2025-10-25 20:52:50 +08:00
// APIGetTypesHandler 获取接口类型列表API处理器
2025-10-26 14:48:02 +08:00
func APIGetTypesHandler(c *gin.Context) {
2025-10-25 20:52:50 +08:00
// 构建接口类型列表
type APITypeItem struct {
Value int `json:"value"`
Name string `json:"name"`
}
var apiTypes []APITypeItem
2025-10-25 20:52:50 +08:00
// 获取所有有效的API类型
validTypes := []int{
models.APITypeGetBulletin, models.APITypeGetUpdateUrl, models.APITypeCheckAppVersion, models.APITypeGetCardInfo,
models.APITypeSingleLogin,
2025-10-27 23:12:15 +08:00
models.APITypeUserLogin, models.APITypeUserRegin, models.APITypeUserRecharge,
2025-10-25 20:52:50 +08:00
models.APITypeLogOut,
models.APITypeGetExpired, models.APITypeCheckUserStatus, models.APITypeGetAppData, models.APITypeGetVariable,
models.APITypeUpdatePwd, models.APITypeMacChangeBind, models.APITypeIPChangeBind,
models.APITypeDisableUser, models.APITypeBlackUser, models.APITypeUserDeductedTime,
}
for _, apiType := range validTypes {
apiTypes = append(apiTypes, APITypeItem{
Value: apiType,
Name: models.GetAPITypeName(apiType),
})
}
2025-10-26 14:48:02 +08:00
apiBaseController.HandleSuccess(c, "获取接口类型列表成功", apiTypes)
2025-10-25 20:52:50 +08:00
}
// APIUpdateStatusHandler 更新单个接口状态处理器
func APIUpdateStatusHandler(c *gin.Context) {
var req struct {
ID uint `json:"id"`
Status int `json:"status"`
}
if !apiBaseController.BindJSON(c, &req) {
return
}
if req.ID == 0 {
apiBaseController.HandleValidationError(c, "接口ID不能为空")
return
}
if req.Status != 0 && req.Status != 1 {
apiBaseController.HandleValidationError(c, "状态值无效")
return
}
// 获取数据库连接
db, ok := apiBaseController.GetDB(c)
if !ok {
return
}
// 检查接口是否存在
var api models.API
if err := db.Where("id = ?", req.ID).First(&api).Error; err != nil {
apiBaseController.HandleValidationError(c, "接口不存在")
return
}
// 更新状态
if err := db.Model(&api).Update("status", req.Status).Error; err != nil {
logrus.WithError(err).Error("Failed to update API status")
apiBaseController.HandleInternalError(c, "更新状态失败", err)
return
}
statusText := "禁用"
if req.Status == 1 {
statusText = "启用"
}
apiBaseController.HandleSuccess(c, "接口"+statusText+"成功", nil)
}
2025-10-26 14:48:02 +08:00
func APIGenerateKeysHandler(c *gin.Context) {
var req struct {
2025-10-25 02:59:57 +08:00
Side string `json:"side"` // submit | return
Algorithm int `json:"algorithm"` // 与 models.Algorithm* 对应
}
2025-10-26 14:48:02 +08:00
if !apiBaseController.BindJSON(c, &req) {
return
}
if req.Side != "submit" && req.Side != "return" {
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "side参数必须为submit或return")
return
}
if !models.IsValidAlgorithm(req.Algorithm) {
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "无效的算法类型")
return
}
// 根据算法生成密钥/证书
result := map[string]interface{}{}
switch req.Algorithm {
case models.AlgorithmNone:
// 不加密不生成任何密钥
result["public_key"] = ""
result["private_key"] = ""
case models.AlgorithmRC4:
// 生成16字节随机密钥并返回16位十六进制大写
2025-10-25 02:59:11 +08:00
key, err := encrypt.GenerateRC4Key(8) // 生成8字节密钥
if err != nil {
logrus.WithError(err).Error("Failed to generate RC4 key")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "生成RC4密钥失败", err)
return
}
result["public_key"] = ""
2025-10-25 02:59:11 +08:00
result["private_key"] = strings.ToUpper(hex.EncodeToString(key))
case models.AlgorithmRSA:
// 生成标准RSA 2048密钥对返回PEM明文字符串
publicKey, privateKey, err := encrypt.GenerateRSAKeyPair(2048)
if err != nil {
logrus.WithError(err).Error("Failed to generate RSA key pair")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "生成RSA密钥失败", err)
return
}
2025-10-25 02:59:57 +08:00
2025-10-25 02:59:11 +08:00
// 转换为PEM格式
publicKeyPEM, err := encrypt.PublicKeyToPEM(publicKey)
if err != nil {
logrus.WithError(err).Error("Failed to convert public key to PEM")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "转换公钥格式失败", err)
2025-10-25 02:59:11 +08:00
return
}
2025-10-25 02:59:57 +08:00
2025-10-25 02:59:11 +08:00
privateKeyPEM, err := encrypt.PrivateKeyToPEM(privateKey)
if err != nil {
logrus.WithError(err).Error("Failed to convert private key to PEM")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "转换私钥格式失败", err)
2025-10-25 02:59:11 +08:00
return
}
2025-10-25 02:59:57 +08:00
2025-10-25 02:59:11 +08:00
result["public_key"] = publicKeyPEM
result["private_key"] = privateKeyPEM
case models.AlgorithmRSADynamic:
// 生成RSA动态加密密钥对返回PEM明文字符串
publicKeyPEM, privateKeyPEM, err := encrypt.GenerateRSADynamicKeyPair(2048)
if err != nil {
logrus.WithError(err).Error("Failed to generate RSA dynamic key pair")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "生成RSA动态密钥失败", err)
2025-10-25 02:59:11 +08:00
return
}
2025-10-25 02:59:57 +08:00
2025-10-25 02:59:11 +08:00
result["public_key"] = publicKeyPEM
result["private_key"] = privateKeyPEM
case models.AlgorithmEasy:
// 生成易加密密钥对,返回逗号分隔的整数数组字符串
encryptKey, _, err := encrypt.GenerateEasyKey()
if err != nil {
logrus.WithError(err).Error("Failed to generate Easy encryption key")
2025-10-26 14:48:02 +08:00
apiBaseController.HandleInternalError(c, "生成易加密密钥失败", err)
return
}
result["public_key"] = ""
result["private_key"] = encrypt.FormatKeyAsString(encryptKey)
default:
2025-10-26 14:48:02 +08:00
apiBaseController.HandleValidationError(c, "不支持的算法类型")
return
}
2025-10-26 14:48:02 +08:00
apiBaseController.HandleSuccess(c, "生成成功", result)
}