feat: 添加数据库资产、命令拦截器、授权资产等功能,修复GitHub Actions工作流
This commit is contained in:
@@ -10,13 +10,13 @@ type AccessSettingApi struct{}
|
||||
|
||||
func (api AccessSettingApi) GetEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"fontSize": "14",
|
||||
"lineHeight": "1.5",
|
||||
"fontFamily": "JetBrains Mono, Consolas, monospace",
|
||||
"selectionCopy": "true",
|
||||
"rightClickPaste": "true",
|
||||
"treeExpandedKeys": "",
|
||||
"useSnippets": "true",
|
||||
"fontSize": "14",
|
||||
"lineHeight": "1.5",
|
||||
"fontFamily": "JetBrains Mono, Consolas, monospace",
|
||||
"selectionCopy": "true",
|
||||
"rightClickPaste": "true",
|
||||
"treeExpandedKeys": "",
|
||||
"useSnippets": "true",
|
||||
"interceptSearchShortcut": "false",
|
||||
})
|
||||
}
|
||||
|
||||
+45
-30
@@ -118,13 +118,18 @@ func (api AccountApi) LoginEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
info := AccountInfo{
|
||||
Id: user.ID,
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Type: user.Type,
|
||||
EnableTotp: user.TOTPSecret != "" && user.TOTPSecret != "-",
|
||||
Roles: user.Roles,
|
||||
Menus: menus,
|
||||
Id: user.ID,
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Type: user.Type,
|
||||
EnabledTotp: user.TOTPSecret != "" && user.TOTPSecret != "-",
|
||||
MfaEnabled: user.TOTPSecret != "" && user.TOTPSecret != "-",
|
||||
Roles: user.Roles,
|
||||
Menus: menus,
|
||||
Language: "zh-CN",
|
||||
ForceTotpEnabled: false,
|
||||
NeedChangePassword: false,
|
||||
Dev: config.GlobalCfg.Debug,
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
@@ -269,13 +274,18 @@ func (api AccountApi) ChangePasswordEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
type AccountInfo struct {
|
||||
Id string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Type string `json:"type"`
|
||||
EnableTotp bool `json:"enableTotp"`
|
||||
Roles []string `json:"roles"`
|
||||
Menus []UserMenu `json:"menus"`
|
||||
Id string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Type string `json:"type"`
|
||||
EnabledTotp bool `json:"enabledTotp"`
|
||||
MfaEnabled bool `json:"mfaEnabled"`
|
||||
Roles []string `json:"roles"`
|
||||
Menus []UserMenu `json:"menus"`
|
||||
Language string `json:"language"`
|
||||
ForceTotpEnabled bool `json:"forceTotpEnabled"`
|
||||
NeedChangePassword bool `json:"needChangePassword"`
|
||||
Dev bool `json:"dev"`
|
||||
}
|
||||
|
||||
type UserMenu struct {
|
||||
@@ -317,13 +327,18 @@ func (api AccountApi) InfoEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
info := AccountInfo{
|
||||
Id: user.ID,
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Type: user.Type,
|
||||
EnableTotp: user.TOTPSecret != "" && user.TOTPSecret != "-",
|
||||
Roles: user.Roles,
|
||||
Menus: menus,
|
||||
Id: user.ID,
|
||||
Username: user.Username,
|
||||
Nickname: user.Nickname,
|
||||
Type: user.Type,
|
||||
EnabledTotp: user.TOTPSecret != "" && user.TOTPSecret != "-",
|
||||
MfaEnabled: user.TOTPSecret != "" && user.TOTPSecret != "-",
|
||||
Roles: user.Roles,
|
||||
Menus: menus,
|
||||
Language: "zh-CN",
|
||||
ForceTotpEnabled: false,
|
||||
NeedChangePassword: false,
|
||||
Dev: config.GlobalCfg.Debug,
|
||||
}
|
||||
return Success(c, info)
|
||||
}
|
||||
@@ -409,39 +424,39 @@ func (api AccountApi) SecurityTokenSupportTypesEndpoint(c echo.Context) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
var types []string
|
||||
if user.TOTPSecret != "" && user.TOTPSecret != "-" {
|
||||
types = append(types, "otp")
|
||||
}
|
||||
|
||||
|
||||
if len(types) == 0 {
|
||||
types = []string{}
|
||||
}
|
||||
|
||||
|
||||
return Success(c, types)
|
||||
}
|
||||
|
||||
func (api AccountApi) SecurityTokenMfaEndpoint(c echo.Context) error {
|
||||
account, _ := GetCurrentAccount(c)
|
||||
passcode := c.QueryParam("passcode")
|
||||
|
||||
|
||||
user, err := repository.UserRepository.FindById(context.TODO(), account.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
if user.TOTPSecret == "" || user.TOTPSecret == "-" {
|
||||
return Fail(c, -1, "MFA not enabled")
|
||||
}
|
||||
|
||||
|
||||
if !common.Validate(passcode, user.TOTPSecret) {
|
||||
return Fail(c, -1, "Invalid passcode")
|
||||
}
|
||||
|
||||
|
||||
token := utils.UUID()
|
||||
cache.TokenManager.Set(token, account.ID, cache.NotRememberExpiration)
|
||||
|
||||
|
||||
return Success(c, maps.Map{"token": token})
|
||||
}
|
||||
|
||||
@@ -450,7 +465,7 @@ func (api AccountApi) SecurityTokenValidateEndpoint(c echo.Context) error {
|
||||
if token == "" {
|
||||
return Success(c, maps.Map{"ok": false})
|
||||
}
|
||||
|
||||
|
||||
_, ok := cache.TokenManager.Get(token)
|
||||
return Success(c, maps.Map{"ok": ok})
|
||||
}
|
||||
|
||||
@@ -100,24 +100,24 @@ func (api AgentGatewayApi) GetStatEndpoint(c echo.Context) error {
|
||||
return err
|
||||
}
|
||||
return Success(c, maps.Map{
|
||||
"ping": 0,
|
||||
"cpu": maps.Map{"percent": 0},
|
||||
"memory": maps.Map{"percent": 0},
|
||||
"disk": maps.Map{"percent": 0},
|
||||
"network": maps.Map{},
|
||||
"load": maps.Map{},
|
||||
"host": maps.Map{},
|
||||
"process": maps.Map{"total": 0},
|
||||
"connections": 0,
|
||||
"tcp_states": []string{},
|
||||
"errors": maps.Map{},
|
||||
"ping": 0,
|
||||
"cpu": maps.Map{"percent": 0},
|
||||
"memory": maps.Map{"percent": 0},
|
||||
"disk": maps.Map{"percent": 0},
|
||||
"network": maps.Map{},
|
||||
"load": maps.Map{},
|
||||
"host": maps.Map{},
|
||||
"process": maps.Map{"total": 0},
|
||||
"connections": 0,
|
||||
"tcp_states": []string{},
|
||||
"errors": maps.Map{},
|
||||
})
|
||||
}
|
||||
|
||||
func (api AgentGatewayApi) UpdateSortEndpoint(c echo.Context) error {
|
||||
var items []struct {
|
||||
ID string `json:"id"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
ID string `json:"id"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
}
|
||||
if err := c.Bind(&items); err != nil {
|
||||
return err
|
||||
|
||||
+40
-21
@@ -32,9 +32,15 @@ func (api AssetGroupApi) GroupsSetEndpoint(c echo.Context) error {
|
||||
ctx := context.TODO()
|
||||
repository.AssetGroupRepository.DeleteByParentId(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.AssetGroup{
|
||||
ID: utils.UUID(),
|
||||
Name: item["name"].(string),
|
||||
Name: name,
|
||||
ParentId: "",
|
||||
Sort: i,
|
||||
Created: time.Now().UnixMilli(),
|
||||
@@ -58,9 +64,15 @@ func (api AssetGroupApi) GroupsDeleteEndpoint(c echo.Context) error {
|
||||
func saveChildren(ctx context.Context, children []interface{}, parentId string) {
|
||||
for i, item := range children {
|
||||
m := item.(map[string]interface{})
|
||||
name := ""
|
||||
if v, ok := m["name"].(string); ok {
|
||||
name = v
|
||||
} else if v, ok := m["title"].(string); ok {
|
||||
name = v
|
||||
}
|
||||
group := model.AssetGroup{
|
||||
ID: utils.UUID(),
|
||||
Name: m["name"].(string),
|
||||
Name: name,
|
||||
ParentId: parentId,
|
||||
Sort: i,
|
||||
Created: time.Now().UnixMilli(),
|
||||
@@ -109,27 +121,15 @@ func (api AssetGroupApi) TreeEndpoint(c echo.Context) error {
|
||||
|
||||
func buildAssetTree(assets []model.Asset, groups []model.AssetGroup, groupId string) []maps.Map {
|
||||
var nodes []maps.Map
|
||||
var groupAssets []model.Asset
|
||||
for _, a := range assets {
|
||||
groupAssets = append(groupAssets, a)
|
||||
}
|
||||
for _, a := range groupAssets {
|
||||
nodes = append(nodes, maps.Map{
|
||||
"id": a.ID,
|
||||
"name": a.Name,
|
||||
"key": a.ID,
|
||||
"isLeaf": true,
|
||||
"protocol": a.Protocol,
|
||||
"ip": a.IP,
|
||||
"port": a.Port,
|
||||
})
|
||||
}
|
||||
|
||||
for _, g := range groups {
|
||||
if g.ParentId == groupId {
|
||||
node := maps.Map{
|
||||
"id": g.ID,
|
||||
"name": g.Name,
|
||||
"key": g.ID,
|
||||
"id": g.ID,
|
||||
"name": g.Name,
|
||||
"key": g.ID,
|
||||
"title": g.Name,
|
||||
"value": g.ID,
|
||||
}
|
||||
children := buildAssetTree(assets, groups, g.ID)
|
||||
if len(children) > 0 {
|
||||
@@ -138,6 +138,25 @@ func buildAssetTree(assets []model.Asset, groups []model.AssetGroup, groupId str
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
}
|
||||
|
||||
if groupId == "" {
|
||||
for _, a := range assets {
|
||||
nodes = append(nodes, maps.Map{
|
||||
"id": a.ID,
|
||||
"name": a.Name,
|
||||
"key": a.ID,
|
||||
"title": a.Name,
|
||||
"value": a.ID,
|
||||
"isLeaf": true,
|
||||
"protocol": a.Protocol,
|
||||
"ip": a.IP,
|
||||
"port": a.Port,
|
||||
"extra": maps.Map{
|
||||
"network": a.IP + ":" + strconv.Itoa(a.Port),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
@@ -186,7 +205,7 @@ func (api AssetGroupApi) ChangeGatewayEndpoint(c echo.Context) error {
|
||||
|
||||
func (api AssetGroupApi) SortEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
Id string `json:"id"`
|
||||
Id string `json:"id"`
|
||||
BeforeId string `json:"beforeId"`
|
||||
AfterId string `json:"afterId"`
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"math/big"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -65,12 +69,49 @@ func parseCertificate(certPEM string) (commonName, subject, issuer string, notBe
|
||||
return commonName, subject, issuer, notBefore, notAfter, nil
|
||||
}
|
||||
|
||||
func generateSelfSignedCertificate(commonName string) (certPEM, keyPEM string, notBefore, notAfter time.Time, err error) {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return "", "", time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
||||
notBefore = time.Now()
|
||||
notAfter = notBefore.Add(365 * 24 * time.Hour)
|
||||
|
||||
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||||
if err != nil {
|
||||
return "", "", time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
CommonName: commonName,
|
||||
},
|
||||
NotBefore: notBefore,
|
||||
NotAfter: notAfter,
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
|
||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||
if err != nil {
|
||||
return "", "", time.Time{}, time.Time{}, err
|
||||
}
|
||||
|
||||
certPEM = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}))
|
||||
keyPEM = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}))
|
||||
|
||||
return certPEM, keyPEM, notBefore, notAfter, nil
|
||||
}
|
||||
|
||||
func (api CertificateApi) CreateEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
CommonName string `json:"commonName"`
|
||||
Certificate string `json:"certificate"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Type string `json:"type"`
|
||||
CommonName string `json:"commonName"`
|
||||
Certificate string `json:"certificate"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Type string `json:"type"`
|
||||
RequireClientAuth bool `json:"requireClientAuth"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
@@ -78,18 +119,32 @@ func (api CertificateApi) CreateEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
item := &model.Certificate{
|
||||
ID: utils.UUID(),
|
||||
CommonName: req.CommonName,
|
||||
Certificate: req.Certificate,
|
||||
PrivateKey: req.PrivateKey,
|
||||
Type: req.Type,
|
||||
ID: utils.UUID(),
|
||||
CommonName: req.CommonName,
|
||||
Certificate: req.Certificate,
|
||||
PrivateKey: req.PrivateKey,
|
||||
Type: req.Type,
|
||||
RequireClientAuth: req.RequireClientAuth,
|
||||
IssuedStatus: "success",
|
||||
Created: common.NowJsonTime(),
|
||||
UpdatedAt: common.NowJsonTime(),
|
||||
IssuedStatus: "success",
|
||||
Created: common.NowJsonTime(),
|
||||
UpdatedAt: common.NowJsonTime(),
|
||||
}
|
||||
|
||||
if req.Certificate != "" {
|
||||
if item.Type == "" {
|
||||
item.Type = "imported"
|
||||
}
|
||||
|
||||
if req.Type == "self-signed" && req.Certificate == "" {
|
||||
certPEM, keyPEM, notBefore, notAfter, err := generateSelfSignedCertificate(req.CommonName)
|
||||
if err == nil {
|
||||
item.Certificate = certPEM
|
||||
item.PrivateKey = keyPEM
|
||||
item.NotBefore = common.NewJsonTime(notBefore)
|
||||
item.NotAfter = common.NewJsonTime(notAfter)
|
||||
item.Subject = "CN=" + req.CommonName
|
||||
item.Issuer = "CN=" + req.CommonName
|
||||
}
|
||||
} else if req.Certificate != "" {
|
||||
commonName, subject, issuer, notBefore, notAfter, err := parseCertificate(req.Certificate)
|
||||
if err == nil {
|
||||
if item.CommonName == "" {
|
||||
@@ -102,10 +157,6 @@ func (api CertificateApi) CreateEndpoint(c echo.Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
if item.Type == "" {
|
||||
item.Type = "imported"
|
||||
}
|
||||
|
||||
if err := repository.CertificateRepository.Create(context.TODO(), item); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -115,9 +166,9 @@ func (api CertificateApi) CreateEndpoint(c echo.Context) error {
|
||||
func (api CertificateApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var req struct {
|
||||
CommonName string `json:"commonName"`
|
||||
Certificate string `json:"certificate"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
CommonName string `json:"commonName"`
|
||||
Certificate string `json:"certificate"`
|
||||
PrivateKey string `json:"privateKey"`
|
||||
RequireClientAuth bool `json:"requireClientAuth"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
@@ -125,12 +176,12 @@ func (api CertificateApi) UpdateEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
item := &model.Certificate{
|
||||
ID: id,
|
||||
CommonName: req.CommonName,
|
||||
Certificate: req.Certificate,
|
||||
PrivateKey: req.PrivateKey,
|
||||
ID: id,
|
||||
CommonName: req.CommonName,
|
||||
Certificate: req.Certificate,
|
||||
PrivateKey: req.PrivateKey,
|
||||
RequireClientAuth: req.RequireClientAuth,
|
||||
UpdatedAt: common.NowJsonTime(),
|
||||
UpdatedAt: common.NowJsonTime(),
|
||||
}
|
||||
|
||||
if req.Certificate != "" {
|
||||
|
||||
+114
-18
@@ -1,7 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -10,59 +15,150 @@ import (
|
||||
type CommandFilterApi struct{}
|
||||
|
||||
func (api CommandFilterApi) AllEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
items, err := repository.CommandFilterRepository.FindAll(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, items)
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
name := c.QueryParam("name")
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
items, total, err := repository.CommandFilterRepository.Find(context.TODO(), pageIndex, pageSize, name, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"items": []interface{}{},
|
||||
"total": 0,
|
||||
"items": items,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) CreateEndpoint(c echo.Context) error {
|
||||
var item map[string]interface{}
|
||||
var item model.CommandFilter
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = utils.LongUUID()
|
||||
item.ID = utils.UUID()
|
||||
|
||||
if err := repository.CommandFilterRepository.Create(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var item map[string]interface{}
|
||||
var item model.CommandFilter
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = id
|
||||
item.ID = id
|
||||
|
||||
if err := repository.CommandFilterRepository.UpdateById(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
if err := repository.CommandFilterRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) GetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"name": "Default Filter",
|
||||
"createdAt": 1700000000000,
|
||||
})
|
||||
item, err := repository.CommandFilterRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) BindEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api CommandFilterApi) UnbindEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
type CommandFilterRuleApi struct{}
|
||||
|
||||
func (api CommandFilterRuleApi) AllEndpoint(c echo.Context) error {
|
||||
commandFilterId := c.QueryParam("commandFilterId")
|
||||
items, err := repository.CommandFilterRuleRepository.FindByCommandFilterId(context.TODO(), commandFilterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, items)
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) PagingEndpoint(c echo.Context) error {
|
||||
commandFilterId := c.QueryParam("commandFilterId")
|
||||
items, err := repository.CommandFilterRuleRepository.FindByCommandFilterId(context.TODO(), commandFilterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, maps.Map{
|
||||
"items": items,
|
||||
"total": len(items),
|
||||
})
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) CreateEndpoint(c echo.Context) error {
|
||||
var item model.CommandFilterRule
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item.ID = utils.UUID()
|
||||
|
||||
if err := repository.CommandFilterRuleRepository.Create(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var item model.CommandFilterRule
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item.ID = id
|
||||
|
||||
if err := repository.CommandFilterRuleRepository.UpdateById(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if err := repository.CommandFilterRuleRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) GetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
item, err := repository.CommandFilterRuleRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
+10
-8
@@ -13,12 +13,13 @@ type DashboardApi struct{}
|
||||
|
||||
func (api DashboardApi) GetTimeCounterEndpoint(c echo.Context) error {
|
||||
var (
|
||||
totalUser int64
|
||||
onlineUser int64
|
||||
countOnlineSession int64
|
||||
totalAsset int64
|
||||
activeAsset int64
|
||||
failLoginCount int64
|
||||
totalUser int64
|
||||
onlineUser int64
|
||||
countOnlineSession int64
|
||||
totalAsset int64
|
||||
activeAsset int64
|
||||
failLoginCount int64
|
||||
totalWebsite int64
|
||||
)
|
||||
|
||||
totalUser, _ = repository.UserRepository.Count(context.TODO())
|
||||
@@ -29,6 +30,7 @@ func (api DashboardApi) GetTimeCounterEndpoint(c echo.Context) error {
|
||||
failLoginCount, _ = repository.LoginLogRepository.CountByState(context.TODO(), "0")
|
||||
gatewayList, _ := repository.GatewayRepository.FindAll(context.TODO())
|
||||
gatewayActiveCount := int64(len(gatewayList))
|
||||
totalWebsite, _ = repository.WebsiteRepository.Count(context.TODO())
|
||||
|
||||
counter := map[string]interface{}{
|
||||
"loginFailedTimes": failLoginCount,
|
||||
@@ -39,7 +41,7 @@ func (api DashboardApi) GetTimeCounterEndpoint(c echo.Context) error {
|
||||
"assetActiveCount": activeAsset,
|
||||
"assetTotalCount": totalAsset,
|
||||
"websiteActiveCount": 0,
|
||||
"websiteTotalCount": 0,
|
||||
"websiteTotalCount": totalWebsite,
|
||||
"gatewayActiveCount": gatewayActiveCount,
|
||||
"gatewayTotalCount": gatewayActiveCount,
|
||||
}
|
||||
@@ -74,7 +76,7 @@ func (api DashboardApi) GetDateCounterV2Endpoint(c echo.Context) error {
|
||||
day := lastDate.AddDate(0, 0, i).Format("2006-01-02")
|
||||
|
||||
item := map[string]interface{}{
|
||||
"date": day,
|
||||
"date": day,
|
||||
"login": int64(0),
|
||||
"user": int64(0),
|
||||
"asset": int64(0),
|
||||
|
||||
+131
-28
@@ -1,7 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -10,65 +17,161 @@ import (
|
||||
type DatabaseAssetApi struct{}
|
||||
|
||||
func (api DatabaseAssetApi) AllEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
dbType := c.QueryParam("type")
|
||||
items, err := repository.DatabaseAssetRepository.FindByType(context.TODO(), dbType)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, items)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
name := c.QueryParam("keyword")
|
||||
dbType := c.QueryParam("type")
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
items, total, err := repository.DatabaseAssetRepository.Find(context.TODO(), pageIndex, pageSize, name, dbType, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"items": []interface{}{},
|
||||
"total": 0,
|
||||
"items": items,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) CreateEndpoint(c echo.Context) error {
|
||||
var item map[string]interface{}
|
||||
if err := c.Bind(&item); err != nil {
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Description string `json:"description"`
|
||||
GatewayType string `json:"gatewayType"`
|
||||
GatewayId string `json:"gatewayId"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = utils.LongUUID()
|
||||
|
||||
account, _ := GetCurrentAccount(c)
|
||||
|
||||
item := &model.DatabaseAsset{
|
||||
ID: utils.UUID(),
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Host: req.Host,
|
||||
Port: req.Port,
|
||||
Database: req.Database,
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
Description: req.Description,
|
||||
GatewayType: req.GatewayType,
|
||||
GatewayId: req.GatewayId,
|
||||
Tags: strings.Join(req.Tags, ","),
|
||||
Owner: account.ID,
|
||||
Created: common.NowJsonTime(),
|
||||
Updated: common.NowJsonTime(),
|
||||
}
|
||||
|
||||
if item.Port == 0 {
|
||||
item.Port = repository.DatabaseAssetRepository.GetDBPort(item.Type)
|
||||
}
|
||||
|
||||
if err := repository.DatabaseAssetRepository.Create(context.TODO(), item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var item map[string]interface{}
|
||||
if err := c.Bind(&item); err != nil {
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Description string `json:"description"`
|
||||
GatewayType string `json:"gatewayType"`
|
||||
GatewayId string `json:"gatewayId"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = id
|
||||
return Success(c, item)
|
||||
|
||||
existing, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existing.Name = req.Name
|
||||
existing.Type = req.Type
|
||||
existing.Host = req.Host
|
||||
existing.Port = req.Port
|
||||
existing.Database = req.Database
|
||||
existing.Username = req.Username
|
||||
if req.Password != "" {
|
||||
existing.Password = req.Password
|
||||
}
|
||||
existing.Description = req.Description
|
||||
existing.GatewayType = req.GatewayType
|
||||
existing.GatewayId = req.GatewayId
|
||||
existing.Tags = strings.Join(req.Tags, ",")
|
||||
existing.Updated = common.NowJsonTime()
|
||||
|
||||
if err := repository.DatabaseAssetRepository.UpdateById(context.TODO(), &existing); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, existing)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
if err := repository.DatabaseAssetRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) GetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"name": "MySQL Server",
|
||||
"type": "mysql",
|
||||
"host": "localhost",
|
||||
"port": 3306,
|
||||
"database": "test_db",
|
||||
"username": "root",
|
||||
"description": "Test database",
|
||||
"status": "active",
|
||||
"statusText": "Active",
|
||||
"createdAt": 1700000000000,
|
||||
"updatedAt": 1700000000000,
|
||||
})
|
||||
item, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) DecryptEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
item, err := repository.DatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"password": "decrypted_password",
|
||||
"password": item.Password,
|
||||
})
|
||||
}
|
||||
|
||||
func (api DatabaseAssetApi) TagsEndpoint(c echo.Context) error {
|
||||
tags, err := repository.DatabaseAssetRepository.FindAllTags(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, tags)
|
||||
}
|
||||
|
||||
+103
-25
@@ -1,7 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -10,77 +15,150 @@ import (
|
||||
type DepartmentApi struct{}
|
||||
|
||||
func (api DepartmentApi) AllEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
items, err := repository.DepartmentRepository.FindAll(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, items)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
name := c.QueryParam("keyword")
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
items, total, err := repository.DepartmentRepository.Find(context.TODO(), pageIndex, pageSize, name, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
departmentMap := make(map[string]model.Department)
|
||||
allDepts, _ := repository.DepartmentRepository.FindAll(context.TODO())
|
||||
for _, dept := range allDepts {
|
||||
departmentMap[dept.ID] = dept
|
||||
}
|
||||
|
||||
for i := range items {
|
||||
if items[i].ParentId != "" {
|
||||
if parent, ok := departmentMap[items[i].ParentId]; ok {
|
||||
items[i].ParentName = parent.Name
|
||||
}
|
||||
}
|
||||
userCount, _ := repository.UserDepartmentRepository.CountByDepartmentId(context.TODO(), items[i].ID)
|
||||
items[i].UserCount = userCount
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"items": []interface{}{},
|
||||
"total": 0,
|
||||
"items": items,
|
||||
"total": total,
|
||||
})
|
||||
}
|
||||
|
||||
func (api DepartmentApi) CreateEndpoint(c echo.Context) error {
|
||||
var item map[string]interface{}
|
||||
var item model.Department
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = utils.LongUUID()
|
||||
item.ID = utils.UUID()
|
||||
|
||||
if err := repository.DepartmentRepository.Create(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var item map[string]interface{}
|
||||
var item model.Department
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item["id"] = id
|
||||
item.ID = id
|
||||
|
||||
if err := repository.DepartmentRepository.UpdateById(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
|
||||
count, err := repository.DepartmentRepository.CountByParentId(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return Fail(c, -1, "该部门下存在子部门,无法删除")
|
||||
}
|
||||
|
||||
userCount, err := repository.UserDepartmentRepository.CountByDepartmentId(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if userCount > 0 {
|
||||
return Fail(c, -1, "该部门下存在用户,无法删除")
|
||||
}
|
||||
|
||||
if err := repository.DepartmentRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) GetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
_ = id
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"name": "Default",
|
||||
"parentId": "",
|
||||
})
|
||||
item, err := repository.DepartmentRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) GetTreeEndpoint(c echo.Context) error {
|
||||
tree := []map[string]interface{}{
|
||||
{
|
||||
"title": "Default",
|
||||
"key": "default",
|
||||
"value": "default",
|
||||
"children": []interface{}{},
|
||||
},
|
||||
departments, err := repository.DepartmentRepository.FindAll(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tree := repository.DepartmentRepository.BuildTree(departments)
|
||||
return Success(c, tree)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) GetDepartmentUsersEndpoint(c echo.Context) error {
|
||||
departmentId := c.Param("id")
|
||||
_ = departmentId
|
||||
return Success(c, []string{})
|
||||
users, err := repository.UserDepartmentRepository.FindUsersByDepartmentId(context.TODO(), departmentId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, users)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) SetDepartmentUsersEndpoint(c echo.Context) error {
|
||||
departmentId := c.Param("id")
|
||||
_ = departmentId
|
||||
var userIds []string
|
||||
if err := c.Bind(&userIds); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := repository.UserDepartmentRepository.SaveDepartmentUsers(context.TODO(), departmentId, userIds); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api DepartmentApi) RemoveUsersFromDepartmentEndpoint(c echo.Context) error {
|
||||
departmentId := c.Param("id")
|
||||
_ = departmentId
|
||||
var userIds []string
|
||||
if err := c.Bind(&userIds); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := repository.UserDepartmentRepository.RemoveUsersFromDepartment(context.TODO(), departmentId, userIds); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
@@ -43,6 +44,8 @@ func (api GatewayGroupApi) CreateEndpoint(c echo.Context) error {
|
||||
return err
|
||||
}
|
||||
item.ID = utils.UUID()
|
||||
item.Created = common.NowJsonTime()
|
||||
item.Updated = common.NowJsonTime()
|
||||
|
||||
if err := repository.GatewayGroupRepository.Create(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
@@ -56,6 +59,7 @@ func (api GatewayGroupApi) UpdateEndpoint(c echo.Context) error {
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item.Updated = common.NowJsonTime()
|
||||
if err := repository.GatewayGroupRepository.UpdateById(context.TODO(), &item, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ func (api LoginPolicyApi) GetUserPageEndpoint(c echo.Context) error {
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
items, total, err := repository.UserRepository.Find(context.TODO(), pageIndex, pageSize, username, nickname, mail, "", id, order, field)
|
||||
items, total, err := repository.UserRepository.Find(context.TODO(), pageIndex, pageSize, username, nickname, mail, "", "", id, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+2
-2
@@ -5,10 +5,10 @@ import (
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
)
|
||||
|
||||
type LogoApi struct{}
|
||||
|
||||
+58
-58
@@ -65,9 +65,9 @@ func (api PortalApi) AssetsTreeEndpoint(c echo.Context) error {
|
||||
tree := make([]maps.Map, 0)
|
||||
for _, g := range groups {
|
||||
node := maps.Map{
|
||||
"key": g.ID,
|
||||
"title": g.Name,
|
||||
"isLeaf": false,
|
||||
"key": g.ID,
|
||||
"title": g.Name,
|
||||
"isLeaf": false,
|
||||
"children": buildPortalAssetChildren(items, g.ID, keyword),
|
||||
}
|
||||
tree = append(tree, node)
|
||||
@@ -159,9 +159,9 @@ func (api PortalApi) AssetsGroupTreeEndpoint(c echo.Context) error {
|
||||
tree := make([]maps.Map, 0)
|
||||
for _, g := range groups {
|
||||
node := maps.Map{
|
||||
"key": g.ID,
|
||||
"title": g.Name,
|
||||
"isLeaf": false,
|
||||
"key": g.ID,
|
||||
"title": g.Name,
|
||||
"isLeaf": false,
|
||||
"children": []interface{}{},
|
||||
}
|
||||
tree = append(tree, node)
|
||||
@@ -198,34 +198,34 @@ func (api PortalApi) CreateSessionEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"id": s.ID,
|
||||
"protocol": s.Protocol,
|
||||
"assetName": assetName,
|
||||
"strategy": maps.Map{},
|
||||
"url": "",
|
||||
"watermark": maps.Map{},
|
||||
"readonly": false,
|
||||
"idle": 3600,
|
||||
"fileSystem": s.FileSystem == "1",
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
"id": s.ID,
|
||||
"protocol": s.Protocol,
|
||||
"assetName": assetName,
|
||||
"strategy": maps.Map{},
|
||||
"url": "",
|
||||
"watermark": maps.Map{},
|
||||
"readonly": false,
|
||||
"idle": 3600,
|
||||
"fileSystem": s.FileSystem == "1",
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
})
|
||||
}
|
||||
|
||||
func (api PortalApi) GetSessionEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
return Success(c, maps.Map{
|
||||
"id": id,
|
||||
"protocol": "ssh",
|
||||
"assetName": id,
|
||||
"strategy": maps.Map{},
|
||||
"url": "",
|
||||
"watermark": maps.Map{},
|
||||
"readonly": false,
|
||||
"idle": 3600,
|
||||
"fileSystem": false,
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
"id": id,
|
||||
"protocol": "ssh",
|
||||
"assetName": id,
|
||||
"strategy": maps.Map{},
|
||||
"url": "",
|
||||
"watermark": maps.Map{},
|
||||
"readonly": false,
|
||||
"idle": 3600,
|
||||
"fileSystem": false,
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -233,7 +233,7 @@ func (api PortalApi) GetShareEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"enabled": false,
|
||||
"passcode": "",
|
||||
"url": "",
|
||||
"url": "",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ func (api PortalApi) CreateShareEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"enabled": true,
|
||||
"passcode": "",
|
||||
"url": "",
|
||||
"url": "",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -277,16 +277,16 @@ func (api PortalApi) PingAssetEndpoint(c echo.Context) error {
|
||||
assetId := c.Param("id")
|
||||
item, _ := repository.AssetRepository.FindById(context.TODO(), assetId)
|
||||
return Success(c, maps.Map{
|
||||
"name": item.Name,
|
||||
"active": true,
|
||||
"usedTime": 10,
|
||||
"name": item.Name,
|
||||
"active": true,
|
||||
"usedTime": 10,
|
||||
"usedTimeStr": "10ms",
|
||||
})
|
||||
}
|
||||
|
||||
func (api PortalApi) TerminalStatsEndpoint(c echo.Context) error {
|
||||
sessionId := c.Param("id")
|
||||
|
||||
|
||||
sess := session.GlobalSessionManager.GetById(sessionId)
|
||||
if sess == nil || sess.NextTerminal == nil || sess.NextTerminal.SshClient == nil {
|
||||
return Success(c, maps.Map{
|
||||
@@ -321,7 +321,7 @@ func (api PortalApi) TerminalStatsEndpoint(c echo.Context) error {
|
||||
"cpu": []interface{}{},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
stats, err := getSystemStats(sess.NextTerminal.SshClient)
|
||||
if err != nil {
|
||||
log.Warn("Failed to get system stats", log.NamedError("error", err))
|
||||
@@ -357,30 +357,30 @@ func (api PortalApi) TerminalStatsEndpoint(c echo.Context) error {
|
||||
"cpu": []interface{}{},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return Success(c, stats)
|
||||
}
|
||||
|
||||
func getSystemStats(client *ssh.Client) (maps.Map, error) {
|
||||
result := maps.Map{}
|
||||
|
||||
|
||||
// Get hostname
|
||||
hostname, _ := sshExec(client, "hostname")
|
||||
|
||||
|
||||
// Get OS info
|
||||
osId, _ := sshExec(client, "cat /etc/os-release 2>/dev/null | grep '^ID=' | cut -d'=' -f2 | tr -d '\"'")
|
||||
osName, _ := sshExec(client, "cat /etc/os-release 2>/dev/null | grep '^NAME=' | cut -d'=' -f2 | tr -d '\"'")
|
||||
osVersion, _ := sshExec(client, "cat /etc/os-release 2>/dev/null | grep '^VERSION_ID=' | cut -d'=' -f2 | tr -d '\"'")
|
||||
|
||||
|
||||
// Get arch
|
||||
arch, _ := sshExec(client, "uname -m")
|
||||
|
||||
|
||||
// Get uptime
|
||||
uptimeStr, _ := sshExec(client, "cat /proc/uptime | awk '{print $1}'")
|
||||
uptimeFloat, _ := strconv.ParseFloat(strings.TrimSpace(uptimeStr), 64)
|
||||
uptime := int64(uptimeFloat)
|
||||
upDays := uptime / 86400
|
||||
|
||||
|
||||
// Get load average
|
||||
loadStr, _ := sshExec(client, "cat /proc/loadavg")
|
||||
loadParts := strings.Fields(loadStr)
|
||||
@@ -390,28 +390,28 @@ func getSystemStats(client *ssh.Client) (maps.Map, error) {
|
||||
load5 = loadParts[1]
|
||||
load15 = loadParts[2]
|
||||
}
|
||||
|
||||
|
||||
// Get process count
|
||||
processStr, _ := sshExec(client, "ps aux | wc -l")
|
||||
totalProcess := strings.TrimSpace(processStr)
|
||||
|
||||
|
||||
// Get memory info
|
||||
memInfo, _ := sshExec(client, "cat /proc/meminfo")
|
||||
memTotal, memFree, memAvailable, memBuffers, memCached, swapTotal, swapFree := parseMemInfo(memInfo)
|
||||
memUsed := memTotal - memAvailable
|
||||
|
||||
|
||||
// Get CPU info
|
||||
cpuInfo, _ := sshExec(client, "cat /proc/stat | head -1")
|
||||
cpuStats := parseCpuStat(cpuInfo)
|
||||
|
||||
|
||||
// Get filesystem info
|
||||
dfOutput, _ := sshExec(client, "df -B1 | tail -n +2")
|
||||
fileSystems := parseDfOutput(dfOutput)
|
||||
|
||||
|
||||
// Get network info
|
||||
netDev, _ := sshExec(client, "cat /proc/net/dev | tail -n +3")
|
||||
network := parseNetDev(netDev)
|
||||
|
||||
|
||||
result["info"] = maps.Map{
|
||||
"id": strings.TrimSpace(osId),
|
||||
"name": strings.TrimSpace(osName),
|
||||
@@ -441,7 +441,7 @@ func getSystemStats(client *ssh.Client) (maps.Map, error) {
|
||||
result["cpu"] = cpuStats
|
||||
result["fileSystems"] = fileSystems
|
||||
result["network"] = network
|
||||
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -451,7 +451,7 @@ func sshExec(client *ssh.Client, cmd string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
|
||||
output, err := session.CombinedOutput(cmd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -468,7 +468,7 @@ func parseMemInfo(info string) (total, free, available, buffers, cached, swapTot
|
||||
}
|
||||
key := strings.TrimSuffix(parts[0], ":")
|
||||
value, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||
|
||||
|
||||
switch key {
|
||||
case "MemTotal":
|
||||
total = value
|
||||
@@ -494,11 +494,11 @@ func parseCpuStat(stat string) []interface{} {
|
||||
if len(parts) < 5 {
|
||||
return []interface{}{maps.Map{"user": 0.0, "nice": 0.0, "system": 0.0}}
|
||||
}
|
||||
|
||||
|
||||
user, _ := strconv.ParseFloat(parts[1], 64)
|
||||
nice, _ := strconv.ParseFloat(parts[2], 64)
|
||||
system, _ := strconv.ParseFloat(parts[3], 64)
|
||||
|
||||
|
||||
return []interface{}{
|
||||
maps.Map{
|
||||
"user": user,
|
||||
@@ -519,16 +519,16 @@ func parseDfOutput(output string) []interface{} {
|
||||
if len(parts) < 6 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
total, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||
used, _ := strconv.ParseInt(parts[2], 10, 64)
|
||||
free, _ := strconv.ParseInt(parts[3], 10, 64)
|
||||
|
||||
|
||||
var percent float64 = 0
|
||||
if total > 0 {
|
||||
percent = float64(used) / float64(total)
|
||||
}
|
||||
|
||||
|
||||
result = append(result, maps.Map{
|
||||
"mountPoint": parts[5],
|
||||
"total": total,
|
||||
@@ -551,15 +551,15 @@ func parseNetDev(output string) []interface{} {
|
||||
if len(parts) < 10 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
iface := strings.TrimSuffix(parts[0], ":")
|
||||
if strings.HasPrefix(iface, "lo") {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
rx, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||
tx, _ := strconv.ParseInt(parts[9], 10, 64)
|
||||
|
||||
|
||||
result = append(result, maps.Map{
|
||||
"iface": iface,
|
||||
"rx": rx,
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/pem"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/service"
|
||||
@@ -57,8 +57,8 @@ func (api PropertyApi) SendMailEndpoint(c echo.Context) error {
|
||||
|
||||
func (api PropertyApi) ClientIPsEndpoint(c echo.Context) error {
|
||||
return Success(c, map[string]string{
|
||||
"direct": c.RealIP(),
|
||||
"x-real-ip": c.Request().Header.Get("X-Real-IP"),
|
||||
"direct": c.RealIP(),
|
||||
"x-real-ip": c.Request().Header.Get("X-Real-IP"),
|
||||
"x-forwarded-for": c.Request().Header.Get("X-Forwarded-For"),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,339 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/log"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/service"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
type ScheduledTaskApi struct{}
|
||||
|
||||
type ScheduledTaskDTO struct {
|
||||
ID string `json:"id"`
|
||||
EntryId int `json:"entryId"`
|
||||
Name string `json:"name"`
|
||||
Spec string `json:"spec"`
|
||||
Type string `json:"type"`
|
||||
AssetIdList []string `json:"assetIdList"`
|
||||
Mode string `json:"mode"`
|
||||
Script string `json:"script"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt common.JsonTime `json:"createdAt"`
|
||||
UpdatedAt common.JsonTime `json:"updatedAt"`
|
||||
LastExecAt *common.JsonTime `json:"lastExecAt,omitempty"`
|
||||
}
|
||||
|
||||
type JobLogDTO struct {
|
||||
ID string `json:"id"`
|
||||
JobId string `json:"jobId"`
|
||||
JobType string `json:"jobType"`
|
||||
Results []interface{} `json:"results"`
|
||||
CreatedAt common.JsonTime `json:"createdAt"`
|
||||
}
|
||||
|
||||
func JobLogToDTO(log model.JobLog, jobType string) JobLogDTO {
|
||||
var results []interface{}
|
||||
if log.Results != "" {
|
||||
json.Unmarshal([]byte(log.Results), &results)
|
||||
}
|
||||
if results == nil {
|
||||
results = []interface{}{}
|
||||
}
|
||||
return JobLogDTO{
|
||||
ID: log.ID,
|
||||
JobId: log.JobId,
|
||||
JobType: jobType,
|
||||
Results: results,
|
||||
CreatedAt: log.Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
func JobToDTO(job model.Job) ScheduledTaskDTO {
|
||||
dto := ScheduledTaskDTO{
|
||||
ID: job.ID,
|
||||
EntryId: job.CronJobId,
|
||||
Name: job.Name,
|
||||
Spec: job.Cron,
|
||||
Type: job.Func,
|
||||
Mode: job.Mode,
|
||||
Enabled: job.Status == "enabled",
|
||||
CreatedAt: job.Created,
|
||||
UpdatedAt: job.Updated,
|
||||
}
|
||||
if job.ResourceIds != "" {
|
||||
dto.AssetIdList = strings.Split(job.ResourceIds, ",")
|
||||
}
|
||||
if job.Metadata != "" && job.Func == "asset-exec-command" {
|
||||
var metadataShell struct {
|
||||
Shell string `json:"shell"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(job.Metadata), &metadataShell); err == nil {
|
||||
dto.Script = metadataShell.Shell
|
||||
} else {
|
||||
dto.Script = job.Metadata
|
||||
}
|
||||
} else {
|
||||
dto.Script = job.Metadata
|
||||
}
|
||||
if !job.LastExecAt.IsZero() {
|
||||
dto.LastExecAt = &job.LastExecAt
|
||||
}
|
||||
return dto
|
||||
}
|
||||
|
||||
func DTOToJob(dto ScheduledTaskDTO) model.Job {
|
||||
status := "disabled"
|
||||
if dto.Enabled {
|
||||
status = "enabled"
|
||||
}
|
||||
metadata := dto.Script
|
||||
if dto.Type == "asset-exec-command" && dto.Script != "" {
|
||||
metadataJSON, _ := json.Marshal(map[string]string{"shell": dto.Script})
|
||||
metadata = string(metadataJSON)
|
||||
}
|
||||
return model.Job{
|
||||
ID: dto.ID,
|
||||
CronJobId: dto.EntryId,
|
||||
Name: dto.Name,
|
||||
Cron: dto.Spec,
|
||||
Func: dto.Type,
|
||||
ResourceIds: strings.Join(dto.AssetIdList, ","),
|
||||
Mode: dto.Mode,
|
||||
Metadata: metadata,
|
||||
Status: status,
|
||||
}
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) AllEndpoint(c echo.Context) error {
|
||||
items, err := repository.JobRepository.FindAll(context.TODO())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var dtos []ScheduledTaskDTO
|
||||
for _, item := range items {
|
||||
dtos = append(dtos, JobToDTO(item))
|
||||
}
|
||||
if dtos == nil {
|
||||
dtos = []ScheduledTaskDTO{}
|
||||
}
|
||||
return Success(c, dtos)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
name := c.QueryParam("name")
|
||||
status := c.QueryParam("status")
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
items, total, err := repository.JobRepository.Find(context.TODO(), pageIndex, pageSize, name, status, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var dtos []ScheduledTaskDTO
|
||||
for _, item := range items {
|
||||
dtos = append(dtos, JobToDTO(item))
|
||||
}
|
||||
if dtos == nil {
|
||||
dtos = []ScheduledTaskDTO{}
|
||||
}
|
||||
return Success(c, maps.Map{
|
||||
"total": total,
|
||||
"items": dtos,
|
||||
})
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) CreateEndpoint(c echo.Context) error {
|
||||
var dto ScheduledTaskDTO
|
||||
if err := c.Bind(&dto); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("Create ScheduledTask", log.Any("dto", dto))
|
||||
job := DTOToJob(dto)
|
||||
job.ID = utils.UUID()
|
||||
job.Created = common.NowJsonTime()
|
||||
job.Updated = common.NowJsonTime()
|
||||
if job.Status == "" {
|
||||
job.Status = "disabled"
|
||||
}
|
||||
|
||||
if err := repository.JobRepository.Create(context.TODO(), &job); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, job.ID)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) UpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
var dto ScheduledTaskDTO
|
||||
if err := c.Bind(&dto); err != nil {
|
||||
return err
|
||||
}
|
||||
job := DTOToJob(dto)
|
||||
job.ID = id
|
||||
job.Updated = common.NowJsonTime()
|
||||
if err := repository.JobRepository.UpdateById(context.TODO(), &job); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if err := repository.JobRepository.DeleteJobById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) GetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
item, err := repository.JobRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dto := JobToDTO(item)
|
||||
log.Info("Get ScheduledTask", log.Any("dto", dto))
|
||||
return Success(c, dto)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) ChangeStatusEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
enabled := c.QueryParam("enabled") == "true"
|
||||
var status string
|
||||
if enabled {
|
||||
status = "enabled"
|
||||
} else {
|
||||
status = "disabled"
|
||||
}
|
||||
job := model.Job{
|
||||
ID: id,
|
||||
Status: status,
|
||||
}
|
||||
if err := repository.JobRepository.UpdateById(context.TODO(), &job); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) ExecEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
job, err := repository.JobRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := common.NowJsonTime()
|
||||
jobUpdate := model.Job{
|
||||
ID: id,
|
||||
LastExecAt: now,
|
||||
}
|
||||
if err := repository.JobRepository.UpdateById(context.TODO(), &jobUpdate); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch job.Func {
|
||||
case "asset-exec-command":
|
||||
shellJob := service.ShellJob{
|
||||
ID: job.ID,
|
||||
Mode: job.Mode,
|
||||
ResourceIds: job.ResourceIds,
|
||||
Metadata: job.Metadata,
|
||||
}
|
||||
shellJob.Run()
|
||||
default:
|
||||
jobLog := &model.JobLog{
|
||||
ID: utils.UUID(),
|
||||
JobId: id,
|
||||
Timestamp: now,
|
||||
Message: "任务执行成功",
|
||||
}
|
||||
_ = repository.JobLogRepository.Create(context.TODO(), jobLog)
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) GetLogsEndpoint(c echo.Context) error {
|
||||
jobId := c.Param("id")
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
if pageIndex == 0 {
|
||||
pageIndex = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
job, err := repository.JobRepository.FindById(context.TODO(), jobId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
items, total, err := repository.JobLogRepository.FindByJobId(context.TODO(), jobId, pageIndex, pageSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var dtos []JobLogDTO
|
||||
for _, item := range items {
|
||||
dtos = append(dtos, JobLogToDTO(item, job.Func))
|
||||
}
|
||||
if dtos == nil {
|
||||
dtos = []JobLogDTO{}
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"total": total,
|
||||
"items": dtos,
|
||||
})
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) DeleteLogsEndpoint(c echo.Context) error {
|
||||
jobId := c.Param("id")
|
||||
if err := repository.JobLogRepository.DeleteByJobId(context.TODO(), jobId); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) NextTenRunsEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
Spec string `json:"spec"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var results []string
|
||||
if req.Spec == "" {
|
||||
return Success(c, results)
|
||||
}
|
||||
|
||||
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
|
||||
schedule, err := parser.Parse(req.Spec)
|
||||
if err != nil {
|
||||
return Success(c, results)
|
||||
}
|
||||
|
||||
now := common.NowJsonTime().Time
|
||||
for i := 0; i < 10; i++ {
|
||||
next := schedule.Next(now)
|
||||
results = append(results, next.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
return Success(c, results)
|
||||
}
|
||||
@@ -634,12 +634,12 @@ func (api SessionApi) SessionTriggerAuditEndpoint(c echo.Context) error {
|
||||
sessionId := c.Param("id")
|
||||
|
||||
audit := &model.SessionAudit{
|
||||
ID: utils.UUID(),
|
||||
ID: utils.UUID(),
|
||||
SessionId: sessionId,
|
||||
Status: "completed",
|
||||
Content: "Audit completed. No issues found.",
|
||||
Created: common.NowJsonTime(),
|
||||
Updated: common.NowJsonTime(),
|
||||
Status: "completed",
|
||||
Content: "Audit completed. No issues found.",
|
||||
Created: common.NowJsonTime(),
|
||||
Updated: common.NowJsonTime(),
|
||||
}
|
||||
|
||||
if err := repository.SessionAuditRepository.Upsert(context.TODO(), audit); err != nil {
|
||||
|
||||
+4
-4
@@ -131,11 +131,11 @@ func (api SetupApi) ValidateTOTPEndpoint(c echo.Context) error {
|
||||
|
||||
func (api SetupApi) PasswordPolicyEndpoint(c echo.Context) error {
|
||||
return Success(c, map[string]interface{}{
|
||||
"minLength": 6,
|
||||
"minCharacterType": 0,
|
||||
"minLength": 6,
|
||||
"minCharacterType": 0,
|
||||
"mustNotContainUsername": false,
|
||||
"mustNotBePalindrome": false,
|
||||
"mustNotWeek": false,
|
||||
"mustNotBePalindrome": false,
|
||||
"mustNotWeek": false,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+14
-104
@@ -3,71 +3,10 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"next-terminal/server/common/maps"
|
||||
"strconv"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type ScheduledTaskApi struct{}
|
||||
|
||||
func (api ScheduledTaskApi) AllEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
if pageIndex == 0 {
|
||||
pageIndex = 1
|
||||
}
|
||||
if pageSize == 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
return Success(c, maps.Map{
|
||||
"total": 0,
|
||||
"items": []interface{}{},
|
||||
})
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) CreateEndpoint(c echo.Context) error {
|
||||
return Success(c, "")
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) UpdateEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) DeleteEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) GetEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) ChangeStatusEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) ExecEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) GetLogsEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"total": 0,
|
||||
"items": []interface{}{},
|
||||
})
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) DeleteLogsEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api ScheduledTaskApi) NextTenRunsEndpoint(c echo.Context) error {
|
||||
return Success(c, []string{})
|
||||
}
|
||||
|
||||
type SessionCommandApi struct{}
|
||||
|
||||
func (api SessionCommandApi) AllEndpoint(c echo.Context) error {
|
||||
@@ -186,35 +125,6 @@ func (api FilesystemLogApi) ClearEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
type CommandFilterRuleApi struct{}
|
||||
|
||||
func (api CommandFilterRuleApi) AllEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) PagingEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"total": 0,
|
||||
"items": []interface{}{},
|
||||
})
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) CreateEndpoint(c echo.Context) error {
|
||||
return Success(c, "")
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) UpdateEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) DeleteEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api CommandFilterRuleApi) GetEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
type AgentGatewayTokenApi struct{}
|
||||
|
||||
func (api AgentGatewayTokenApi) AllEndpoint(c echo.Context) error {
|
||||
@@ -283,26 +193,26 @@ func (api AccessLogApi) GetHourlyStatsEndpoint(c echo.Context) error {
|
||||
|
||||
func (api AccessLogApi) GetTotalStatsEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"totalRequests": 0,
|
||||
"uniqueDomains": 0,
|
||||
"uniqueVisitors": 0,
|
||||
"avgResponseTime": 0,
|
||||
"totalRequests": 0,
|
||||
"uniqueDomains": 0,
|
||||
"uniqueVisitors": 0,
|
||||
"avgResponseTime": 0,
|
||||
})
|
||||
}
|
||||
|
||||
func (api AccessLogApi) GetWebsiteStatsEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"websiteId": "",
|
||||
"websiteName": "",
|
||||
"domain": "",
|
||||
"pv": 0,
|
||||
"uv": 0,
|
||||
"ip": 0,
|
||||
"traffic": 0,
|
||||
"requests": 0,
|
||||
"realtimeTraffic": 0,
|
||||
"websiteId": "",
|
||||
"websiteName": "",
|
||||
"domain": "",
|
||||
"pv": 0,
|
||||
"uv": 0,
|
||||
"ip": 0,
|
||||
"traffic": 0,
|
||||
"requests": 0,
|
||||
"realtimeTraffic": 0,
|
||||
"requestsPerSecond": 0,
|
||||
"avgResponseTime": 0,
|
||||
"avgResponseTime": 0,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+432
-20
@@ -1,7 +1,13 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
@@ -9,87 +15,493 @@ import (
|
||||
type AuthorisedAssetApi struct{}
|
||||
|
||||
func (api AuthorisedAssetApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
userId := c.QueryParam("userId")
|
||||
departmentId := c.QueryParam("departmentId")
|
||||
assetGroupId := c.QueryParam("assetGroupId")
|
||||
assetId := c.QueryParam("assetId")
|
||||
|
||||
items, total, err := repository.AuthorisedAssetRepository.FindWithDetails(context.TODO(), pageIndex, pageSize, userId, departmentId, assetGroupId, assetId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if items == nil {
|
||||
items = []map[string]interface{}{}
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"total": 0,
|
||||
"items": []interface{}{},
|
||||
"total": total,
|
||||
"items": items,
|
||||
})
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) AuthorisedAssetsEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
UserIds []string `json:"userIds"`
|
||||
DepartmentIds []string `json:"departmentIds"`
|
||||
AssetIds []string `json:"assetIds"`
|
||||
AssetGroupIds []string `json:"assetGroupIds"`
|
||||
CommandFilterId string `json:"commandFilterId"`
|
||||
StrategyId string `json:"strategyId"`
|
||||
ExpiredAt int64 `json:"expiredAt"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var items []model.AuthorisedAsset
|
||||
for _, userId := range req.UserIds {
|
||||
for _, assetId := range req.AssetIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
AssetId: assetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
for _, assetGroupId := range req.AssetGroupIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
AssetGroupId: assetGroupId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, departmentId := range req.DepartmentIds {
|
||||
for _, assetId := range req.AssetIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
AssetId: assetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
for _, assetGroupId := range req.AssetGroupIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
AssetGroupId: assetGroupId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(items) > 0 {
|
||||
if err := repository.AuthorisedAssetRepository.CreateInBatches(context.TODO(), items); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) AuthorisedUsersEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
AssetIds []string `json:"assetIds"`
|
||||
AssetGroupIds []string `json:"assetGroupIds"`
|
||||
UserIds []string `json:"userIds"`
|
||||
CommandFilterId string `json:"commandFilterId"`
|
||||
StrategyId string `json:"strategyId"`
|
||||
ExpiredAt int64 `json:"expiredAt"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var items []model.AuthorisedAsset
|
||||
for _, userId := range req.UserIds {
|
||||
for _, assetId := range req.AssetIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
AssetId: assetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
for _, assetGroupId := range req.AssetGroupIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
AssetGroupId: assetGroupId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(items) > 0 {
|
||||
if err := repository.AuthorisedAssetRepository.CreateInBatches(context.TODO(), items); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) AuthorisedDepartmentsEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
AssetIds []string `json:"assetIds"`
|
||||
AssetGroupIds []string `json:"assetGroupIds"`
|
||||
DepartmentIds []string `json:"departmentIds"`
|
||||
CommandFilterId string `json:"commandFilterId"`
|
||||
StrategyId string `json:"strategyId"`
|
||||
ExpiredAt int64 `json:"expiredAt"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var items []model.AuthorisedAsset
|
||||
for _, departmentId := range req.DepartmentIds {
|
||||
for _, assetId := range req.AssetIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
AssetId: assetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
for _, assetGroupId := range req.AssetGroupIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
AssetGroupId: assetGroupId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(items) > 0 {
|
||||
if err := repository.AuthorisedAssetRepository.CreateInBatches(context.TODO(), items); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) SelectedEndpoint(c echo.Context) error {
|
||||
return Success(c, []string{})
|
||||
expect := c.QueryParam("expect")
|
||||
userId := c.QueryParam("userId")
|
||||
departmentId := c.QueryParam("departmentId")
|
||||
assetId := c.QueryParam("assetId")
|
||||
|
||||
result, err := repository.AuthorisedAssetRepository.Selected(context.TODO(), expect, userId, departmentId, assetId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, result)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if err := repository.AuthorisedAssetRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) GetEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
id := c.Param("id")
|
||||
item, err := repository.AuthorisedAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) UpdateEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
id := c.Param("id")
|
||||
var item model.AuthorisedAsset
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item.ID = id
|
||||
if err := repository.AuthorisedAssetRepository.UpdateById(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api AuthorisedAssetApi) CreateEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
UserIds []string `json:"userIds"`
|
||||
DepartmentIds []string `json:"departmentIds"`
|
||||
AssetIds []string `json:"assetIds"`
|
||||
AssetGroupIds []string `json:"assetGroupIds"`
|
||||
CommandFilterId string `json:"commandFilterId"`
|
||||
StrategyId string `json:"strategyId"`
|
||||
ExpiredAt int64 `json:"expiredAt"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var items []model.AuthorisedAsset
|
||||
for _, userId := range req.UserIds {
|
||||
for _, assetId := range req.AssetIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
AssetId: assetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
for _, assetGroupId := range req.AssetGroupIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
AssetGroupId: assetGroupId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, departmentId := range req.DepartmentIds {
|
||||
for _, assetId := range req.AssetIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
AssetId: assetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
for _, assetGroupId := range req.AssetGroupIds {
|
||||
items = append(items, model.AuthorisedAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
AssetGroupId: assetGroupId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(items) > 0 {
|
||||
if err := repository.AuthorisedAssetRepository.CreateInBatches(context.TODO(), items); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
type AuthorisedDatabaseAssetApi struct{}
|
||||
|
||||
func (api AuthorisedDatabaseAssetApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
userId := c.QueryParam("userId")
|
||||
departmentId := c.QueryParam("departmentId")
|
||||
databaseAssetId := c.QueryParam("assetId")
|
||||
|
||||
items, total, err := repository.AuthorisedDatabaseAssetRepository.FindWithDetails(context.TODO(), pageIndex, pageSize, userId, departmentId, databaseAssetId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if items == nil {
|
||||
items = []map[string]interface{}{}
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"total": 0,
|
||||
"items": []interface{}{},
|
||||
"total": total,
|
||||
"items": items,
|
||||
})
|
||||
}
|
||||
|
||||
func (api AuthorisedDatabaseAssetApi) SelectedEndpoint(c echo.Context) error {
|
||||
return Success(c, []string{})
|
||||
expect := c.QueryParam("expect")
|
||||
userId := c.QueryParam("userId")
|
||||
departmentId := c.QueryParam("departmentId")
|
||||
databaseAssetId := c.QueryParam("assetId")
|
||||
|
||||
result, err := repository.AuthorisedDatabaseAssetRepository.Selected(context.TODO(), expect, userId, departmentId, databaseAssetId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, result)
|
||||
}
|
||||
|
||||
func (api AuthorisedDatabaseAssetApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if err := repository.AuthorisedDatabaseAssetRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api AuthorisedDatabaseAssetApi) GetEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
id := c.Param("id")
|
||||
item, err := repository.AuthorisedDatabaseAssetRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api AuthorisedDatabaseAssetApi) UpdateEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
id := c.Param("id")
|
||||
var item model.AuthorisedDatabaseAsset
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
item.ID = id
|
||||
if err := repository.AuthorisedDatabaseAssetRepository.UpdateById(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func (api AuthorisedDatabaseAssetApi) CreateEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
UserIds []string `json:"userIds"`
|
||||
DepartmentIds []string `json:"departmentIds"`
|
||||
DatabaseAssetIds []string `json:"assetIds"`
|
||||
CommandFilterId string `json:"commandFilterId"`
|
||||
StrategyId string `json:"strategyId"`
|
||||
ExpiredAt int64 `json:"expiredAt"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var items []model.AuthorisedDatabaseAsset
|
||||
for _, userId := range req.UserIds {
|
||||
for _, databaseAssetId := range req.DatabaseAssetIds {
|
||||
items = append(items, model.AuthorisedDatabaseAsset{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
DatabaseAssetId: databaseAssetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, departmentId := range req.DepartmentIds {
|
||||
for _, databaseAssetId := range req.DatabaseAssetIds {
|
||||
items = append(items, model.AuthorisedDatabaseAsset{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
DatabaseAssetId: databaseAssetId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(items) > 0 {
|
||||
if err := repository.AuthorisedDatabaseAssetRepository.CreateInBatches(context.TODO(), items); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
type AuthorisedWebsiteApi struct{}
|
||||
|
||||
func (api AuthorisedWebsiteApi) PagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
userId := c.QueryParam("userId")
|
||||
departmentId := c.QueryParam("departmentId")
|
||||
websiteId := c.QueryParam("websiteId")
|
||||
|
||||
items, total, err := repository.AuthorisedWebsiteRepository.FindWithDetails(context.TODO(), pageIndex, pageSize, userId, departmentId, websiteId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if items == nil {
|
||||
items = []map[string]interface{}{}
|
||||
}
|
||||
|
||||
return Success(c, maps.Map{
|
||||
"total": 0,
|
||||
"items": []interface{}{},
|
||||
"total": total,
|
||||
"items": items,
|
||||
})
|
||||
}
|
||||
|
||||
func (api AuthorisedWebsiteApi) DeleteEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
if err := repository.AuthorisedWebsiteRepository.DeleteById(context.TODO(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func (api AuthorisedWebsiteApi) CreateEndpoint(c echo.Context) error {
|
||||
var req struct {
|
||||
UserIds []string `json:"userIds"`
|
||||
DepartmentIds []string `json:"departmentIds"`
|
||||
WebsiteIds []string `json:"websiteIds"`
|
||||
CommandFilterId string `json:"commandFilterId"`
|
||||
StrategyId string `json:"strategyId"`
|
||||
ExpiredAt int64 `json:"expiredAt"`
|
||||
}
|
||||
if err := c.Bind(&req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var items []model.AuthorisedWebsite
|
||||
for _, userId := range req.UserIds {
|
||||
for _, websiteId := range req.WebsiteIds {
|
||||
items = append(items, model.AuthorisedWebsite{
|
||||
ID: utils.UUID(),
|
||||
UserId: userId,
|
||||
WebsiteId: websiteId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, departmentId := range req.DepartmentIds {
|
||||
for _, websiteId := range req.WebsiteIds {
|
||||
items = append(items, model.AuthorisedWebsite{
|
||||
ID: utils.UUID(),
|
||||
DepartmentId: departmentId,
|
||||
WebsiteId: websiteId,
|
||||
CommandFilterId: req.CommandFilterId,
|
||||
StrategyId: req.StrategyId,
|
||||
ExpiredAt: req.ExpiredAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(items) > 0 {
|
||||
if err := repository.AuthorisedWebsiteRepository.CreateInBatches(context.TODO(), items); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
@@ -139,9 +551,9 @@ type DnsProviderApi struct{}
|
||||
|
||||
func (api DnsProviderApi) GetConfigEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"ok": false,
|
||||
"email": "",
|
||||
"type": "",
|
||||
"ok": false,
|
||||
"email": "",
|
||||
"type": "",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -163,12 +575,12 @@ func (api LicenseApi) GetMachineIdEndpoint(c echo.Context) error {
|
||||
|
||||
func (api LicenseApi) GetLicenseEndpoint(c echo.Context) error {
|
||||
return Success(c, maps.Map{
|
||||
"type": "free",
|
||||
"machineId": "",
|
||||
"asset": 0,
|
||||
"type": "free",
|
||||
"machineId": "",
|
||||
"asset": 0,
|
||||
"concurrent": 0,
|
||||
"user": 0,
|
||||
"expired": 0,
|
||||
"user": 0,
|
||||
"expired": 0,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+15
-15
@@ -25,10 +25,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
Closed = 0
|
||||
Data = 1
|
||||
Resize = 2
|
||||
Ping = 9
|
||||
Closed = 0
|
||||
Data = 1
|
||||
Resize = 2
|
||||
Ping = 9
|
||||
)
|
||||
|
||||
type WebTerminalApi struct {
|
||||
@@ -119,11 +119,11 @@ func (api WebTerminalApi) SshEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
sessionForUpdate := model.Session{
|
||||
ConnectionId: sessionId,
|
||||
Width: cols,
|
||||
Height: rows,
|
||||
Status: nt.Connected,
|
||||
Recording: recording,
|
||||
ConnectionId: sessionId,
|
||||
Width: cols,
|
||||
Height: rows,
|
||||
Status: nt.Connected,
|
||||
Recording: recording,
|
||||
ConnectedTime: common.NowJsonTime(),
|
||||
}
|
||||
if sessionForUpdate.Recording == "" {
|
||||
@@ -247,7 +247,7 @@ func (api WebTerminalApi) AccessTerminalEndpoint(c echo.Context) error {
|
||||
}()
|
||||
|
||||
sessionId := c.QueryParam("sessionId")
|
||||
log.Debug("AccessTerminal: WebSocket connected, sessionId="+sessionId+", cols="+c.QueryParam("cols")+", rows="+c.QueryParam("rows"))
|
||||
log.Debug("AccessTerminal: WebSocket connected, sessionId=" + sessionId + ", cols=" + c.QueryParam("cols") + ", rows=" + c.QueryParam("rows"))
|
||||
if sessionId == "" {
|
||||
return WriteMessage(ws, dto.NewMessage(Closed, "sessionId is required"))
|
||||
}
|
||||
@@ -305,11 +305,11 @@ func (api WebTerminalApi) AccessTerminalEndpoint(c echo.Context) error {
|
||||
log.Debug("AccessTerminal: Shell OK, starting TermHandler...")
|
||||
|
||||
sessionForUpdate := model.Session{
|
||||
ConnectionId: sessionId,
|
||||
Width: cols,
|
||||
Height: rows,
|
||||
Status: nt.Connected,
|
||||
Recording: recording,
|
||||
ConnectionId: sessionId,
|
||||
Width: cols,
|
||||
Height: rows,
|
||||
Status: nt.Connected,
|
||||
Recording: recording,
|
||||
ConnectedTime: common.NowJsonTime(),
|
||||
}
|
||||
if sessionForUpdate.Recording == "" {
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
type ToolsApi struct{}
|
||||
|
||||
func (api ToolsApi) TcpingEndpoint(c echo.Context) error {
|
||||
host := c.QueryParam("host")
|
||||
port := c.QueryParam("port")
|
||||
attemptsStr := c.QueryParam("attempts")
|
||||
|
||||
attempts := 4
|
||||
if attemptsStr != "" {
|
||||
attempts, _ = strconv.Atoi(attemptsStr)
|
||||
}
|
||||
|
||||
c.Response().Header().Set("Content-Type", "text/event-stream")
|
||||
c.Response().Header().Set("Cache-Control", "no-cache")
|
||||
c.Response().Header().Set("Connection", "keep-alive")
|
||||
|
||||
flusher, ok := c.Response().Writer.(http.Flusher)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(500, "Streaming unsupported")
|
||||
}
|
||||
|
||||
for i := 0; i < attempts; i++ {
|
||||
start := time.Now()
|
||||
conn, err := net.DialTimeout("tcp", host+":"+port, 5*time.Second)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
var result string
|
||||
if err != nil {
|
||||
result = fmt.Sprintf("TCP ping %s:%s - Failed: %v", host, port, err)
|
||||
} else {
|
||||
conn.Close()
|
||||
result = fmt.Sprintf("TCP ping %s:%s - Connected, time=%v", host, port, elapsed.Round(time.Millisecond))
|
||||
}
|
||||
|
||||
fmt.Fprintf(c.Response(), "data: %s\n\n", result)
|
||||
flusher.Flush()
|
||||
|
||||
if i < attempts-1 {
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(c.Response(), "event: done\ndata: completed\n\n")
|
||||
flusher.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api ToolsApi) PingEndpoint(c echo.Context) error {
|
||||
host := c.QueryParam("host")
|
||||
attemptsStr := c.QueryParam("attempts")
|
||||
|
||||
attempts := 4
|
||||
if attemptsStr != "" {
|
||||
attempts, _ = strconv.Atoi(attemptsStr)
|
||||
}
|
||||
|
||||
c.Response().Header().Set("Content-Type", "text/event-stream")
|
||||
c.Response().Header().Set("Cache-Control", "no-cache")
|
||||
c.Response().Header().Set("Connection", "keep-alive")
|
||||
|
||||
flusher, ok := c.Response().Writer.(http.Flusher)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(500, "Streaming unsupported")
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd = exec.Command("ping", "-n", strconv.Itoa(attempts), host)
|
||||
} else {
|
||||
cmd = exec.Command("ping", "-c", strconv.Itoa(attempts), host)
|
||||
}
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
fmt.Fprintf(c.Response(), "data: Error: %v\n\n", err)
|
||||
flusher.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
fmt.Fprintf(c.Response(), "data: Error: %v\n\n", err)
|
||||
flusher.Flush()
|
||||
return nil
|
||||
}
|
||||
|
||||
var reader io.Reader = stdout
|
||||
if runtime.GOOS == "windows" {
|
||||
reader = transform.NewReader(stdout, simplifiedchinese.GBK.NewDecoder())
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.TrimSpace(line) != "" {
|
||||
fmt.Fprintf(c.Response(), "data: %s\n\n", line)
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
cmd.Wait()
|
||||
|
||||
fmt.Fprintf(c.Response(), "event: done\ndata: completed\n\n")
|
||||
flusher.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api ToolsApi) TelnetEndpoint(c echo.Context) error {
|
||||
host := c.QueryParam("host")
|
||||
port := c.QueryParam("port")
|
||||
|
||||
c.Response().Header().Set("Content-Type", "text/event-stream")
|
||||
c.Response().Header().Set("Cache-Control", "no-cache")
|
||||
c.Response().Header().Set("Connection", "keep-alive")
|
||||
|
||||
flusher, ok := c.Response().Writer.(http.Flusher)
|
||||
if !ok {
|
||||
return echo.NewHTTPError(500, "Streaming unsupported")
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
conn, err := net.DialTimeout("tcp", host+":"+port, 10*time.Second)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
var result bytes.Buffer
|
||||
if err != nil {
|
||||
result.WriteString(fmt.Sprintf("Telnet %s:%s - Connection failed: %v", host, port, err))
|
||||
} else {
|
||||
conn.Close()
|
||||
result.WriteString(fmt.Sprintf("Telnet %s:%s - Connected successfully in %v", host, port, elapsed.Round(time.Millisecond)))
|
||||
}
|
||||
|
||||
fmt.Fprintf(c.Response(), "data: %s\n\n", result.String())
|
||||
flusher.Flush()
|
||||
|
||||
fmt.Fprintf(c.Response(), "event: done\ndata: completed\n\n")
|
||||
flusher.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
+2
-1
@@ -41,8 +41,9 @@ func (userApi UserApi) PagingEndpoint(c echo.Context) error {
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
online := c.QueryParam("online")
|
||||
userType := c.QueryParam("type")
|
||||
|
||||
items, total, err := repository.UserRepository.Find(context.TODO(), pageIndex, pageSize, username, nickname, mail, online, "", order, field)
|
||||
items, total, err := repository.UserRepository.Find(context.TODO(), pageIndex, pageSize, username, nickname, mail, online, userType, "", order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+150
-43
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/common/maps"
|
||||
"next-terminal/server/dto"
|
||||
"next-terminal/server/model"
|
||||
@@ -68,27 +69,27 @@ func (api WebsiteApi) CreateEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
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,
|
||||
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 {
|
||||
@@ -105,27 +106,27 @@ func (api WebsiteApi) UpdateEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
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,
|
||||
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 {
|
||||
@@ -152,18 +153,124 @@ func (api WebsiteApi) GetEndpoint(c echo.Context) error {
|
||||
}
|
||||
|
||||
func (api WebsiteApi) GroupsGetEndpoint(c echo.Context) error {
|
||||
return Success(c, []interface{}{})
|
||||
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": 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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user