Files
terminal/server/api/website_api.go
T
admin 1f7c491048 feat: 完善日志审计功能
- 实现文件系统日志(FilesystemLog)记录文件管理器操作
- 实现操作日志(OperationLog)记录用户操作行为
- 实现数据库SQL日志(DatabaseSQLLog)模型和API
- 实现SSH会话命令记录(SessionCommand)含命令输出和风险等级
- 添加IP提取服务支持X-Real-IP和X-Forwarded-For
- 添加日志自动清理功能
- 修复ProFormSwitch required验证问题
- 修复设置页面默认值问题
- 修复文件上传错误检测逻辑
- 修复资产树key前缀问题
- 添加VNC/RDP设置默认值
- 修复文件管理标题翻译
2026-04-19 06:57:42 +08:00

288 lines
6.9 KiB
Go

package api
import (
"context"
"encoding/json"
"strconv"
"next-terminal/server/common"
"next-terminal/server/common/maps"
"next-terminal/server/dto"
"next-terminal/server/model"
"next-terminal/server/repository"
"next-terminal/server/utils"
"github.com/labstack/echo/v4"
)
type WebsiteApi struct{}
func (api WebsiteApi) AllEndpoint(c echo.Context) error {
items, err := repository.WebsiteRepository.FindAll(context.TODO())
if err != nil {
return err
}
return Success(c, items)
}
func (api WebsiteApi) PagingEndpoint(c echo.Context) error {
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
keyword := c.QueryParam("keyword")
items, total, err := repository.WebsiteRepository.Find(context.TODO(), pageIndex, pageSize, keyword)
if err != nil {
return err
}
return Success(c, maps.Map{
"total": total,
"items": items,
})
}
func serializeJSON(v interface{}) string {
if v == nil {
return ""
}
switch val := v.(type) {
case string:
return val
case []interface{}, map[string]interface{}:
bytes, err := json.Marshal(val)
if err != nil {
return ""
}
return string(bytes)
default:
bytes, err := json.Marshal(val)
if err != nil {
return ""
}
return string(bytes)
}
}
func (api WebsiteApi) CreateEndpoint(c echo.Context) error {
var req dto.WebsiteDTO
if err := c.Bind(&req); err != nil {
return err
}
item := &model.Website{
ID: utils.UUID(),
Name: req.Name,
Enabled: req.Enabled,
TargetUrl: req.TargetUrl,
TargetHost: req.TargetHost,
TargetPort: req.TargetPort,
Domain: req.Domain,
AsciiDomain: req.AsciiDomain,
Entrance: req.Entrance,
Description: req.Description,
Status: req.Status,
StatusText: req.StatusText,
GatewayType: req.GatewayType,
GatewayId: req.GatewayId,
BasicAuth: serializeJSON(req.BasicAuth),
Headers: serializeJSON(req.Headers),
Cert: serializeJSON(req.Cert),
Public: serializeJSON(req.Public),
TempAllow: serializeJSON(req.TempAllow),
GroupId: req.GroupId,
Sort: req.Sort,
}
if err := repository.WebsiteRepository.Create(context.TODO(), item); err != nil {
return err
}
return Success(c, "")
}
func (api WebsiteApi) UpdateEndpoint(c echo.Context) error {
id := c.Param("id")
var req dto.WebsiteDTO
if err := c.Bind(&req); err != nil {
return err
}
item := &model.Website{
ID: id,
Name: req.Name,
Enabled: req.Enabled,
TargetUrl: req.TargetUrl,
TargetHost: req.TargetHost,
TargetPort: req.TargetPort,
Domain: req.Domain,
AsciiDomain: req.AsciiDomain,
Entrance: req.Entrance,
Description: req.Description,
Status: req.Status,
StatusText: req.StatusText,
GatewayType: req.GatewayType,
GatewayId: req.GatewayId,
BasicAuth: serializeJSON(req.BasicAuth),
Headers: serializeJSON(req.Headers),
Cert: serializeJSON(req.Cert),
Public: serializeJSON(req.Public),
TempAllow: serializeJSON(req.TempAllow),
GroupId: req.GroupId,
Sort: req.Sort,
}
if err := repository.WebsiteRepository.UpdateById(context.TODO(), item, id); err != nil {
return err
}
return Success(c, nil)
}
func (api WebsiteApi) DeleteEndpoint(c echo.Context) error {
id := c.Param("id")
if err := repository.WebsiteRepository.DeleteById(context.TODO(), id); err != nil {
return err
}
return Success(c, nil)
}
func (api WebsiteApi) GetEndpoint(c echo.Context) error {
id := c.Param("id")
item, err := repository.WebsiteRepository.FindById(context.TODO(), id)
if err != nil {
return err
}
return Success(c, item)
}
func (api WebsiteApi) GroupsGetEndpoint(c echo.Context) error {
groups, err := repository.WebsiteGroupRepository.FindAll(context.TODO())
if err != nil {
return err
}
tree := buildWebsiteGroupTree(groups, "")
return Success(c, tree)
}
func buildWebsiteGroupTree(groups []model.WebsiteGroup, parentId string) []maps.Map {
var tree []maps.Map
for _, g := range groups {
if g.ParentId == parentId {
node := maps.Map{
"id": g.ID,
"name": g.Name,
"title": g.Name,
"key": "wgroup_" + g.ID,
"value": g.ID,
}
children := buildWebsiteGroupTree(groups, g.ID)
if len(children) > 0 {
node["children"] = children
}
tree = append(tree, node)
}
}
return tree
}
func (api WebsiteApi) GroupsSetEndpoint(c echo.Context) error {
var req []map[string]interface{}
if err := c.Bind(&req); err != nil {
return err
}
ctx := context.TODO()
repository.WebsiteGroupRepository.DeleteAll(ctx)
for i, item := range req {
name := ""
if v, ok := item["name"].(string); ok {
name = v
} else if v, ok := item["title"].(string); ok {
name = v
}
group := model.WebsiteGroup{
ID: utils.UUID(),
Name: name,
ParentId: "",
Sort: i,
Created: common.NowJsonTime(),
}
repository.WebsiteGroupRepository.Create(ctx, &group)
if subChildren, ok := item["children"].([]interface{}); ok {
saveWebsiteGroupChildren(ctx, subChildren, group.ID)
}
}
return Success(c, nil)
}
func saveWebsiteGroupChildren(ctx context.Context, children []interface{}, parentId string) {
for i, child := range children {
m, ok := child.(map[string]interface{})
if !ok {
continue
}
name := ""
if v, ok := m["name"].(string); ok {
name = v
} else if v, ok := m["title"].(string); ok {
name = v
}
group := model.WebsiteGroup{
ID: utils.UUID(),
Name: name,
ParentId: parentId,
Sort: i,
Created: common.NowJsonTime(),
}
repository.WebsiteGroupRepository.Create(ctx, &group)
if subChildren, ok := m["children"].([]interface{}); ok {
saveWebsiteGroupChildren(ctx, subChildren, group.ID)
}
}
}
func (api WebsiteApi) GroupsDeleteEndpoint(c echo.Context) error {
id := c.Param("id")
count, err := repository.WebsiteGroupRepository.CountByParentId(context.TODO(), id)
if err != nil {
return err
}
if count > 0 {
return Fail(c, -1, "该分组下存在子分组,无法删除")
}
if err := repository.WebsiteGroupRepository.DeleteById(context.TODO(), id); err != nil {
return err
}
return Success(c, nil)
}
func (api WebsiteApi) ChangeGroupEndpoint(c echo.Context) error {
var req struct {
WebsiteIds []string `json:"websiteIds"`
GroupId string `json:"groupId"`
}
if err := c.Bind(&req); err != nil {
return err
}
for _, websiteId := range req.WebsiteIds {
website, err := repository.WebsiteRepository.FindById(context.TODO(), websiteId)
if err != nil {
continue
}
website.GroupId = req.GroupId
repository.WebsiteRepository.UpdateById(context.TODO(), &website, websiteId)
}
return Success(c, nil)
}
func (api WebsiteApi) ChangeGatewayEndpoint(c echo.Context) error {
return Success(c, nil)
}
func (api WebsiteApi) SortEndpoint(c echo.Context) error {
return Success(c, nil)
}
func (api WebsiteApi) FaviconEndpoint(c echo.Context) error {
return Success(c, "")
}