完成重构数据库操作代码
This commit is contained in:
parent
25b8381a4f
commit
2eb4dc3969
22
main.go
22
main.go
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
|
||||||
"io"
|
"io"
|
||||||
"next-terminal/server/api"
|
"next-terminal/server/api"
|
||||||
"next-terminal/server/config"
|
"next-terminal/server/config"
|
||||||
@ -15,11 +14,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const Version = "v0.3.3"
|
const Version = "v0.3.4"
|
||||||
|
|
||||||
var (
|
|
||||||
db *gorm.DB
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := Run()
|
err := Run()
|
||||||
@ -56,27 +51,20 @@ func Run() error {
|
|||||||
logrus.SetOutput(io.MultiWriter(writer1, writer2, writer3))
|
logrus.SetOutput(io.MultiWriter(writer1, writer2, writer3))
|
||||||
|
|
||||||
global.Config = config.SetupConfig()
|
global.Config = config.SetupConfig()
|
||||||
db = config.SetupDB()
|
|
||||||
|
|
||||||
//if global.Config.ResetPassword != "" {
|
|
||||||
// return ResetPassword()
|
|
||||||
//}
|
|
||||||
|
|
||||||
if err := api.ReloadAccessSecurity(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
global.Store = global.NewStore()
|
global.Store = global.NewStore()
|
||||||
global.Cron = cron.New(cron.WithSeconds()) //精确到秒
|
global.Cron = cron.New(cron.WithSeconds()) //精确到秒
|
||||||
global.Cron.Start()
|
global.Cron.Start()
|
||||||
|
|
||||||
|
db := api.SetupDB()
|
||||||
e := api.SetupRoutes(db)
|
e := api.SetupRoutes(db)
|
||||||
global.Cache = api.SetupCache()
|
global.Cache = api.SetupCache()
|
||||||
|
if global.Config.ResetPassword != "" {
|
||||||
|
return api.ResetPassword()
|
||||||
|
}
|
||||||
|
|
||||||
api.SetupTicker()
|
api.SetupTicker()
|
||||||
|
|
||||||
go handle.RunDataFix()
|
|
||||||
|
|
||||||
if global.Config.Server.Cert != "" && global.Config.Server.Key != "" {
|
if global.Config.Server.Cert != "" && global.Config.Server.Key != "" {
|
||||||
return e.StartTLS(global.Config.Server.Addr, global.Config.Server.Cert, global.Config.Server.Key)
|
return e.StartTLS(global.Config.Server.Addr, global.Config.Server.Cert, global.Config.Server.Key)
|
||||||
} else {
|
} else {
|
||||||
|
@ -22,7 +22,7 @@ func CommandCreateEndpoint(c echo.Context) error {
|
|||||||
item.ID = utils.UUID()
|
item.ID = utils.UUID()
|
||||||
item.Created = utils.NowJsonTime()
|
item.Created = utils.NowJsonTime()
|
||||||
|
|
||||||
if err := model.CreateNewCommand(&item); err != nil {
|
if err := commandRepository.Create(&item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ func CommandPagingEndpoint(c echo.Context) error {
|
|||||||
order := c.QueryParam("order")
|
order := c.QueryParam("order")
|
||||||
field := c.QueryParam("field")
|
field := c.QueryParam("field")
|
||||||
|
|
||||||
items, total, err := model.FindPageCommand(pageIndex, pageSize, name, content, order, field, account)
|
items, total, err := commandRepository.Find(pageIndex, pageSize, name, content, order, field, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -61,7 +61,9 @@ func CommandUpdateEndpoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
model.UpdateCommandById(&item, id)
|
if err := commandRepository.UpdateById(&item, id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return Success(c, nil)
|
return Success(c, nil)
|
||||||
}
|
}
|
||||||
@ -73,11 +75,11 @@ func CommandDeleteEndpoint(c echo.Context) error {
|
|||||||
if err := PreCheckCommandPermission(c, split[i]); err != nil {
|
if err := PreCheckCommandPermission(c, split[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := model.DeleteCommandById(split[i]); err != nil {
|
if err := commandRepository.DeleteById(split[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 删除资产与用户的关系
|
// 删除资产与用户的关系
|
||||||
if err := model.DeleteResourceSharerByResourceId(split[i]); err != nil {
|
if err := resourceSharerRepository.DeleteResourceSharerByResourceId(split[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,7 +94,7 @@ func CommandGetEndpoint(c echo.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var item model.Command
|
var item model.Command
|
||||||
if item, err = model.FindCommandById(id); err != nil {
|
if item, err = commandRepository.FindById(id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, item)
|
return Success(c, item)
|
||||||
@ -106,12 +108,14 @@ func CommandChangeOwnerEndpoint(c echo.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
owner := c.QueryParam("owner")
|
owner := c.QueryParam("owner")
|
||||||
model.UpdateCommandById(&model.Command{Owner: owner}, id)
|
if err := commandRepository.UpdateById(&model.Command{Owner: owner}, id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func PreCheckCommandPermission(c echo.Context, id string) error {
|
func PreCheckCommandPermission(c echo.Context, id string) error {
|
||||||
item, err := model.FindCommandById(id)
|
item, err := commandRepository.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import (
|
|||||||
|
|
||||||
func CredentialAllEndpoint(c echo.Context) error {
|
func CredentialAllEndpoint(c echo.Context) error {
|
||||||
account, _ := GetCurrentAccount(c)
|
account, _ := GetCurrentAccount(c)
|
||||||
items, _ := model.FindAllCredential(account)
|
items, _ := credentialRepository.FindByUser(account)
|
||||||
return Success(c, items)
|
return Success(c, items)
|
||||||
}
|
}
|
||||||
func CredentialCreateEndpoint(c echo.Context) error {
|
func CredentialCreateEndpoint(c echo.Context) error {
|
||||||
@ -53,7 +53,7 @@ func CredentialCreateEndpoint(c echo.Context) error {
|
|||||||
return Fail(c, -1, "类型错误")
|
return Fail(c, -1, "类型错误")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.CreateNewCredential(&item); err != nil {
|
if err := credentialRepository.Create(&item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ func CredentialPagingEndpoint(c echo.Context) error {
|
|||||||
field := c.QueryParam("field")
|
field := c.QueryParam("field")
|
||||||
|
|
||||||
account, _ := GetCurrentAccount(c)
|
account, _ := GetCurrentAccount(c)
|
||||||
items, total, err := model.FindPageCredential(pageIndex, pageSize, name, order, field, account)
|
items, total, err := credentialRepository.Find(pageIndex, pageSize, name, order, field, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -117,7 +117,9 @@ func CredentialUpdateEndpoint(c echo.Context) error {
|
|||||||
return Fail(c, -1, "类型错误")
|
return Fail(c, -1, "类型错误")
|
||||||
}
|
}
|
||||||
|
|
||||||
model.UpdateCredentialById(&item, id)
|
if err := credentialRepository.UpdateById(&item, id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return Success(c, nil)
|
return Success(c, nil)
|
||||||
}
|
}
|
||||||
@ -129,11 +131,11 @@ func CredentialDeleteEndpoint(c echo.Context) error {
|
|||||||
if err := PreCheckCredentialPermission(c, split[i]); err != nil {
|
if err := PreCheckCredentialPermission(c, split[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := model.DeleteCredentialById(split[i]); err != nil {
|
if err := credentialRepository.DeleteById(split[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 删除资产与用户的关系
|
// 删除资产与用户的关系
|
||||||
if err := model.DeleteResourceSharerByResourceId(split[i]); err != nil {
|
if err := resourceSharerRepository.DeleteResourceSharerByResourceId(split[i]); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -147,7 +149,7 @@ func CredentialGetEndpoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
item, err := model.FindCredentialById(id)
|
item, err := credentialRepository.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -167,12 +169,14 @@ func CredentialChangeOwnerEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
owner := c.QueryParam("owner")
|
owner := c.QueryParam("owner")
|
||||||
model.UpdateCredentialById(&model.Credential{Owner: owner}, id)
|
if err := credentialRepository.UpdateById(&model.Credential{Owner: owner}, id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func PreCheckCredentialPermission(c echo.Context, id string) error {
|
func PreCheckCredentialPermission(c echo.Context, id string) error {
|
||||||
item, err := model.FindCredentialById(id)
|
item, err := credentialRepository.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ func JobCreateEndpoint(c echo.Context) error {
|
|||||||
item.ID = utils.UUID()
|
item.ID = utils.UUID()
|
||||||
item.Created = utils.NowJsonTime()
|
item.Created = utils.NowJsonTime()
|
||||||
|
|
||||||
if err := model.CreateNewJob(&item); err != nil {
|
if err := jobService.Create(&item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
@ -34,7 +34,7 @@ func JobPagingEndpoint(c echo.Context) error {
|
|||||||
order := c.QueryParam("order")
|
order := c.QueryParam("order")
|
||||||
field := c.QueryParam("field")
|
field := c.QueryParam("field")
|
||||||
|
|
||||||
items, total, err := model.FindPageJob(pageIndex, pageSize, name, status, order, field)
|
items, total, err := jobRepository.Find(pageIndex, pageSize, name, status, order, field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -52,8 +52,8 @@ func JobUpdateEndpoint(c echo.Context) error {
|
|||||||
if err := c.Bind(&item); err != nil {
|
if err := c.Bind(&item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
item.ID = id
|
||||||
if err := model.UpdateJobById(&item, id); err != nil {
|
if err := jobRepository.UpdateById(&item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ func JobUpdateEndpoint(c echo.Context) error {
|
|||||||
func JobChangeStatusEndpoint(c echo.Context) error {
|
func JobChangeStatusEndpoint(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
status := c.QueryParam("status")
|
status := c.QueryParam("status")
|
||||||
if err := model.ChangeJobStatusById(id, status); err != nil {
|
if err := jobService.ChangeStatusById(id, status); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
@ -71,7 +71,7 @@ func JobChangeStatusEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func JobExecEndpoint(c echo.Context) error {
|
func JobExecEndpoint(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
if err := model.ExecJobById(id); err != nil {
|
if err := jobService.ExecJobById(id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
@ -83,7 +83,7 @@ func JobDeleteEndpoint(c echo.Context) error {
|
|||||||
split := strings.Split(ids, ",")
|
split := strings.Split(ids, ",")
|
||||||
for i := range split {
|
for i := range split {
|
||||||
jobId := split[i]
|
jobId := split[i]
|
||||||
if err := model.DeleteJobById(jobId); err != nil {
|
if err := jobRepository.DeleteJobById(jobId); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ func JobDeleteEndpoint(c echo.Context) error {
|
|||||||
func JobGetEndpoint(c echo.Context) error {
|
func JobGetEndpoint(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
item, err := model.FindJobById(id)
|
item, err := jobRepository.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ func JobGetEndpoint(c echo.Context) error {
|
|||||||
func JobGetLogsEndpoint(c echo.Context) error {
|
func JobGetLogsEndpoint(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
items, err := model.FindJobLogs(id)
|
items, err := jobLogRepository.FindByJobId(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -115,7 +115,7 @@ func JobGetLogsEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func JobDeleteLogsEndpoint(c echo.Context) error {
|
func JobDeleteLogsEndpoint(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
if err := model.DeleteJobLogByJobId(id); err != nil {
|
if err := jobLogRepository.DeleteByJobId(id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
|
@ -2,7 +2,7 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"next-terminal/server/constant"
|
"next-terminal/server/constant"
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/repository"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
@ -25,14 +25,14 @@ func OverviewCounterEndPoint(c echo.Context) error {
|
|||||||
)
|
)
|
||||||
if constant.TypeUser == account.Type {
|
if constant.TypeUser == account.Type {
|
||||||
countUser, _ = userRepository.CountOnlineUser()
|
countUser, _ = userRepository.CountOnlineUser()
|
||||||
countOnlineSession, _ = model.CountOnlineSession()
|
countOnlineSession, _ = sessionRepository.CountOnlineSession()
|
||||||
credential, _ = model.CountCredentialByUserId(account.ID)
|
credential, _ = credentialRepository.CountByUserId(account.ID)
|
||||||
asset, _ = model.CountAssetByUserId(account.ID)
|
asset, _ = assetRepository.CountByUserId(account.ID)
|
||||||
} else {
|
} else {
|
||||||
countUser, _ = userRepository.CountOnlineUser()
|
countUser, _ = userRepository.CountOnlineUser()
|
||||||
countOnlineSession, _ = model.CountOnlineSession()
|
countOnlineSession, _ = sessionRepository.CountOnlineSession()
|
||||||
credential, _ = model.CountCredential()
|
credential, _ = credentialRepository.Count()
|
||||||
asset, _ = model.CountAsset()
|
asset, _ = assetRepository.Count()
|
||||||
}
|
}
|
||||||
counter := Counter{
|
counter := Counter{
|
||||||
User: countUser,
|
User: countUser,
|
||||||
@ -46,11 +46,11 @@ func OverviewCounterEndPoint(c echo.Context) error {
|
|||||||
|
|
||||||
func OverviewSessionPoint(c echo.Context) (err error) {
|
func OverviewSessionPoint(c echo.Context) (err error) {
|
||||||
d := c.QueryParam("d")
|
d := c.QueryParam("d")
|
||||||
var results []model.D
|
var results []repository.D
|
||||||
if d == "m" {
|
if d == "m" {
|
||||||
results, err = model.CountSessionByDay(30)
|
results, err = sessionRepository.CountSessionByDay(30)
|
||||||
} else {
|
} else {
|
||||||
results, err = model.CountSessionByDay(7)
|
results, err = sessionRepository.CountSessionByDay(7)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func PropertyGetEndpoint(c echo.Context) error {
|
func PropertyGetEndpoint(c echo.Context) error {
|
||||||
properties := model.FindAllPropertiesMap()
|
properties := propertyRepository.FindAllMap()
|
||||||
return Success(c, properties)
|
return Success(c, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,13 +32,15 @@ func PropertyUpdateEndpoint(c echo.Context) error {
|
|||||||
Value: value,
|
Value: value,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := model.FindPropertyByName(key)
|
_, err := propertyRepository.FindByName(key)
|
||||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
if err := model.CreateNewProperty(&property); err != nil {
|
if err := propertyRepository.Create(&property); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
model.UpdatePropertyByName(&property, key)
|
if err := propertyRepository.UpdateByName(&property, key); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Success(c, nil)
|
return Success(c, nil)
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"next-terminal/server/model"
|
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,7 +19,7 @@ type UR struct {
|
|||||||
|
|
||||||
func RSGetSharersEndPoint(c echo.Context) error {
|
func RSGetSharersEndPoint(c echo.Context) error {
|
||||||
resourceId := c.QueryParam("resourceId")
|
resourceId := c.QueryParam("resourceId")
|
||||||
userIds, err := model.FindUserIdsByResourceId(resourceId)
|
userIds, err := resourceSharerRepository.FindUserIdsByResourceId(resourceId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -34,7 +32,7 @@ func RSOverwriteSharersEndPoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.OverwriteUserIdsByResourceId(ur.ResourceId, ur.ResourceType, ur.UserIds); err != nil {
|
if err := resourceSharerRepository.OverwriteUserIdsByResourceId(ur.ResourceId, ur.ResourceType, ur.UserIds); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +45,7 @@ func ResourceRemoveByUserIdAssignEndPoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.DeleteByUserIdAndResourceTypeAndResourceIdIn(ru.UserGroupId, ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
|
if err := resourceSharerRepository.DeleteByUserIdAndResourceTypeAndResourceIdIn(ru.UserGroupId, ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +58,7 @@ func ResourceAddByUserIdAssignEndPoint(c echo.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.AddSharerResources(ru.UserGroupId, ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
|
if err := resourceSharerRepository.AddSharerResources(ru.UserGroupId, ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
"net/http"
|
"net/http"
|
||||||
"next-terminal/server/global"
|
"next-terminal/server/global"
|
||||||
"next-terminal/server/log"
|
"next-terminal/server/log"
|
||||||
@ -40,6 +44,7 @@ var (
|
|||||||
propertyService *service.PropertyService
|
propertyService *service.PropertyService
|
||||||
userService *service.UserService
|
userService *service.UserService
|
||||||
sessionService *service.SessionService
|
sessionService *service.SessionService
|
||||||
|
mailService *service.MailService
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetupRoutes(db *gorm.DB) *echo.Echo {
|
func SetupRoutes(db *gorm.DB) *echo.Echo {
|
||||||
@ -47,7 +52,9 @@ func SetupRoutes(db *gorm.DB) *echo.Echo {
|
|||||||
InitRepository(db)
|
InitRepository(db)
|
||||||
InitService()
|
InitService()
|
||||||
|
|
||||||
InitDBData()
|
if err := InitDBData(); err != nil {
|
||||||
|
logrus.WithError(err).Error("初始化数据异常")
|
||||||
|
}
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.HideBanner = true
|
e.HideBanner = true
|
||||||
@ -225,8 +232,9 @@ func InitRepository(db *gorm.DB) {
|
|||||||
func InitService() {
|
func InitService() {
|
||||||
jobService = service.NewJobService(jobRepository, jobLogRepository, assetRepository, credentialRepository)
|
jobService = service.NewJobService(jobRepository, jobLogRepository, assetRepository, credentialRepository)
|
||||||
propertyService = service.NewPropertyService(propertyRepository)
|
propertyService = service.NewPropertyService(propertyRepository)
|
||||||
userService = service.NewUserService(userRepository)
|
userService = service.NewUserService(userRepository, loginLogRepository)
|
||||||
sessionService = service.NewSessionService(sessionRepository)
|
sessionService = service.NewSessionService(sessionRepository)
|
||||||
|
mailService = service.NewMailService(propertyRepository)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDBData() (err error) {
|
func InitDBData() (err error) {
|
||||||
@ -244,8 +252,11 @@ func InitDBData() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sessionService.Fix()
|
sessionService.Fix()
|
||||||
|
if err := ReloadAccessSecurity(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
nums, _ := numRepository.FindAll()
|
nums, _ := numRepository.FindAll()
|
||||||
if nums == nil || len(nums) == 0 {
|
if nums == nil {
|
||||||
for i := 0; i <= 30; i++ {
|
for i := 0; i <= 30; i++ {
|
||||||
if err := numRepository.Create(&model.Num{I: strconv.Itoa(i)}); err != nil {
|
if err := numRepository.Create(&model.Num{I: strconv.Itoa(i)}); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -291,3 +302,44 @@ func SetupCache() *cache.Cache {
|
|||||||
})
|
})
|
||||||
return mCache
|
return mCache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetupDB() *gorm.DB {
|
||||||
|
|
||||||
|
var logMode logger.Interface
|
||||||
|
if global.Config.Debug {
|
||||||
|
logMode = logger.Default.LogMode(logger.Info)
|
||||||
|
} else {
|
||||||
|
logMode = logger.Default.LogMode(logger.Silent)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("当前数据库模式为:%v\n", global.Config.DB)
|
||||||
|
var err error
|
||||||
|
var db *gorm.DB
|
||||||
|
if global.Config.DB == "mysql" {
|
||||||
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
|
global.Config.Mysql.Username,
|
||||||
|
global.Config.Mysql.Password,
|
||||||
|
global.Config.Mysql.Hostname,
|
||||||
|
global.Config.Mysql.Port,
|
||||||
|
global.Config.Mysql.Database,
|
||||||
|
)
|
||||||
|
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
||||||
|
Logger: logMode,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
db, err = gorm.Open(sqlite.Open(global.Config.Sqlite.File), &gorm.Config{
|
||||||
|
Logger: logMode,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
logrus.WithError(err).Panic("连接数据库异常")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.AutoMigrate(&model.User{}, &model.Asset{}, &model.AssetAttribute{}, &model.Session{}, &model.Command{},
|
||||||
|
&model.Credential{}, &model.Property{}, &model.ResourceSharer{}, &model.UserGroup{}, &model.UserGroupMember{},
|
||||||
|
&model.LoginLog{}, &model.Num{}, &model.Job{}, &model.JobLog{}, &model.AccessSecurity{}); err != nil {
|
||||||
|
logrus.WithError(err).Panic("初始化数据库表结构异常")
|
||||||
|
}
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
@ -32,7 +32,7 @@ func SessionPagingEndpoint(c echo.Context) error {
|
|||||||
assetId := c.QueryParam("assetId")
|
assetId := c.QueryParam("assetId")
|
||||||
protocol := c.QueryParam("protocol")
|
protocol := c.QueryParam("protocol")
|
||||||
|
|
||||||
items, total, err := model.FindPageSession(pageIndex, pageSize, status, userId, clientIp, assetId, protocol)
|
items, total, err := sessionRepository.Find(pageIndex, pageSize, status, userId, clientIp, assetId, protocol)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -67,7 +67,7 @@ func SessionPagingEndpoint(c echo.Context) error {
|
|||||||
func SessionDeleteEndpoint(c echo.Context) error {
|
func SessionDeleteEndpoint(c echo.Context) error {
|
||||||
sessionIds := c.Param("id")
|
sessionIds := c.Param("id")
|
||||||
split := strings.Split(sessionIds, ",")
|
split := strings.Split(sessionIds, ",")
|
||||||
err := model.DeleteSessionByIds(split)
|
err := sessionRepository.DeleteByIds(split)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ func SessionConnectEndpoint(c echo.Context) error {
|
|||||||
session.Status = constant.Connected
|
session.Status = constant.Connected
|
||||||
session.ConnectedTime = utils.NowJsonTime()
|
session.ConnectedTime = utils.NowJsonTime()
|
||||||
|
|
||||||
if err := model.UpdateSessionById(&session, sessionId); err != nil {
|
if err := sessionRepository.UpdateById(&session, sessionId); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, nil)
|
return Success(c, nil)
|
||||||
@ -116,7 +116,7 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
|||||||
}
|
}
|
||||||
global.Store.Del(sessionId)
|
global.Store.Del(sessionId)
|
||||||
|
|
||||||
s, err := model.FindSessionById(sessionId)
|
s, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -127,7 +127,7 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
|||||||
|
|
||||||
if s.Status == constant.Connecting {
|
if s.Status == constant.Connecting {
|
||||||
// 会话还未建立成功,无需保留数据
|
// 会话还未建立成功,无需保留数据
|
||||||
_ = model.DeleteSessionById(sessionId)
|
_ = sessionRepository.DeleteById(sessionId)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ func CloseSessionById(sessionId string, code int, reason string) {
|
|||||||
session.Code = code
|
session.Code = code
|
||||||
session.Message = reason
|
session.Message = reason
|
||||||
|
|
||||||
_ = model.UpdateSessionById(&session, sessionId)
|
_ = sessionRepository.UpdateById(&session, sessionId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SessionResizeEndpoint(c echo.Context) error {
|
func SessionResizeEndpoint(c echo.Context) error {
|
||||||
@ -154,7 +154,7 @@ func SessionResizeEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
intHeight, _ := strconv.Atoi(height)
|
intHeight, _ := strconv.Atoi(height)
|
||||||
|
|
||||||
if err := model.UpdateSessionWindowSizeById(intWidth, intHeight, sessionId); err != nil {
|
if err := sessionRepository.UpdateWindowSizeById(intWidth, intHeight, sessionId); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
@ -174,7 +174,7 @@ func SessionCreateEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
if constant.TypeUser == user.Type {
|
if constant.TypeUser == user.Type {
|
||||||
// 检测是否有访问权限
|
// 检测是否有访问权限
|
||||||
assetIds, err := model.FindAssetIdsByUserId(user.ID)
|
assetIds, err := resourceSharerRepository.FindAssetIdsByUserId(user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -184,7 +184,7 @@ func SessionCreateEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
asset, err := model.FindAssetById(assetId)
|
asset, err := assetRepository.FindById(assetId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -206,7 +206,7 @@ func SessionCreateEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if asset.AccountType == "credential" {
|
if asset.AccountType == "credential" {
|
||||||
credential, err := model.FindCredentialById(asset.CredentialId)
|
credential, err := credentialRepository.FindById(asset.CredentialId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -221,7 +221,7 @@ func SessionCreateEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.CreateNewSession(session); err != nil {
|
if err := sessionRepository.Create(session); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ func SessionCreateEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func SessionUploadEndpoint(c echo.Context) error {
|
func SessionUploadEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -280,7 +280,7 @@ func SessionUploadEndpoint(c echo.Context) error {
|
|||||||
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
||||||
}
|
}
|
||||||
|
|
||||||
drivePath, err := model.GetDrivePath()
|
drivePath, err := propertyRepository.GetDrivePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -304,7 +304,7 @@ func SessionUploadEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func SessionDownloadEndpoint(c echo.Context) error {
|
func SessionDownloadEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -337,7 +337,7 @@ func SessionDownloadEndpoint(c echo.Context) error {
|
|||||||
SafetyRuleTrigger(c)
|
SafetyRuleTrigger(c)
|
||||||
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
||||||
}
|
}
|
||||||
drivePath, err := model.GetDrivePath()
|
drivePath, err := propertyRepository.GetDrivePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -359,7 +359,7 @@ type File struct {
|
|||||||
|
|
||||||
func SessionLsEndpoint(c echo.Context) error {
|
func SessionLsEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -419,7 +419,7 @@ func SessionLsEndpoint(c echo.Context) error {
|
|||||||
SafetyRuleTrigger(c)
|
SafetyRuleTrigger(c)
|
||||||
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
||||||
}
|
}
|
||||||
drivePath, err := model.GetDrivePath()
|
drivePath, err := propertyRepository.GetDrivePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -458,12 +458,12 @@ func SafetyRuleTrigger(c echo.Context) {
|
|||||||
Rule: constant.AccessRuleReject,
|
Rule: constant.AccessRuleReject,
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = model.CreateNewSecurity(&security)
|
_ = accessSecurityRepository.Create(&security)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SessionMkDirEndpoint(c echo.Context) error {
|
func SessionMkDirEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -482,7 +482,7 @@ func SessionMkDirEndpoint(c echo.Context) error {
|
|||||||
SafetyRuleTrigger(c)
|
SafetyRuleTrigger(c)
|
||||||
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
||||||
}
|
}
|
||||||
drivePath, err := model.GetDrivePath()
|
drivePath, err := propertyRepository.GetDrivePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -498,7 +498,7 @@ func SessionMkDirEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func SessionRmEndpoint(c echo.Context) error {
|
func SessionRmEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -543,7 +543,7 @@ func SessionRmEndpoint(c echo.Context) error {
|
|||||||
SafetyRuleTrigger(c)
|
SafetyRuleTrigger(c)
|
||||||
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
||||||
}
|
}
|
||||||
drivePath, err := model.GetDrivePath()
|
drivePath, err := propertyRepository.GetDrivePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -560,7 +560,7 @@ func SessionRmEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func SessionRenameEndpoint(c echo.Context) error {
|
func SessionRenameEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -584,7 +584,7 @@ func SessionRenameEndpoint(c echo.Context) error {
|
|||||||
SafetyRuleTrigger(c)
|
SafetyRuleTrigger(c)
|
||||||
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
return Fail(c, -1, ":) 您的IP已被记录,请去向管理员自首。")
|
||||||
}
|
}
|
||||||
drivePath, err := model.GetDrivePath()
|
drivePath, err := propertyRepository.GetDrivePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -600,7 +600,7 @@ func SessionRenameEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
func SessionRecordingEndpoint(c echo.Context) error {
|
func SessionRecordingEndpoint(c echo.Context) error {
|
||||||
sessionId := c.Param("id")
|
sessionId := c.Param("id")
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
|||||||
cols, _ := strconv.Atoi(c.QueryParam("cols"))
|
cols, _ := strconv.Atoi(c.QueryParam("cols"))
|
||||||
rows, _ := strconv.Atoi(c.QueryParam("rows"))
|
rows, _ := strconv.Atoi(c.QueryParam("rows"))
|
||||||
|
|
||||||
session, err := model.FindSessionById(sessionId)
|
session, err := sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg := Message{
|
msg := Message{
|
||||||
Type: Closed,
|
Type: Closed,
|
||||||
@ -67,7 +67,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
|||||||
user, _ := GetCurrentAccount(c)
|
user, _ := GetCurrentAccount(c)
|
||||||
if constant.TypeUser == user.Type {
|
if constant.TypeUser == user.Type {
|
||||||
// 检测是否有访问权限
|
// 检测是否有访问权限
|
||||||
assetIds, err := model.FindAssetIdsByUserId(user.ID)
|
assetIds, err := resourceSharerRepository.FindAssetIdsByUserId(user.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
recording := ""
|
recording := ""
|
||||||
propertyMap := model.FindAllPropertiesMap()
|
propertyMap := propertyRepository.FindAllMap()
|
||||||
if propertyMap[guacd.EnableRecording] == "true" {
|
if propertyMap[guacd.EnableRecording] == "true" {
|
||||||
recording = path.Join(propertyMap[guacd.RecordingPath], sessionId, "recording.cast")
|
recording = path.Join(propertyMap[guacd.RecordingPath], sessionId, "recording.cast")
|
||||||
}
|
}
|
||||||
@ -145,7 +145,7 @@ func SSHEndpoint(c echo.Context) (err error) {
|
|||||||
}
|
}
|
||||||
// 创建新会话
|
// 创建新会话
|
||||||
logrus.Debugf("创建新会话 %v", sess.ConnectionId)
|
logrus.Debugf("创建新会话 %v", sess.ConnectionId)
|
||||||
if err := model.UpdateSessionById(&sess, sessionId); err != nil {
|
if err := sessionRepository.UpdateById(&sess, sessionId); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,12 +42,12 @@ func TunEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
configuration := guacd.NewConfiguration()
|
configuration := guacd.NewConfiguration()
|
||||||
|
|
||||||
propertyMap := model.FindAllPropertiesMap()
|
propertyMap := propertyRepository.FindAllMap()
|
||||||
|
|
||||||
var session model.Session
|
var session model.Session
|
||||||
|
|
||||||
if len(connectionId) > 0 {
|
if len(connectionId) > 0 {
|
||||||
session, err = model.FindSessionByConnectionId(connectionId)
|
session, err = sessionRepository.FindByConnectionId(connectionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Warnf("会话不存在")
|
logrus.Warnf("会话不存在")
|
||||||
return err
|
return err
|
||||||
@ -65,7 +65,7 @@ func TunEndpoint(c echo.Context) error {
|
|||||||
configuration.SetParameter("width", width)
|
configuration.SetParameter("width", width)
|
||||||
configuration.SetParameter("height", height)
|
configuration.SetParameter("height", height)
|
||||||
configuration.SetParameter("dpi", dpi)
|
configuration.SetParameter("dpi", dpi)
|
||||||
session, err = model.FindSessionById(sessionId)
|
session, err = sessionRepository.FindById(sessionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
CloseSessionById(sessionId, NotFoundSession, "会话不存在")
|
CloseSessionById(sessionId, NotFoundSession, "会话不存在")
|
||||||
return err
|
return err
|
||||||
@ -143,7 +143,7 @@ func TunEndpoint(c echo.Context) error {
|
|||||||
configuration.SetParameter("port", strconv.Itoa(session.Port))
|
configuration.SetParameter("port", strconv.Itoa(session.Port))
|
||||||
|
|
||||||
// 加载资产配置的属性,优先级比全局配置的高,因此最后加载,覆盖掉全局配置
|
// 加载资产配置的属性,优先级比全局配置的高,因此最后加载,覆盖掉全局配置
|
||||||
attributes, _ := model.FindAssetAttributeByAssetId(session.AssetId)
|
attributes, _ := assetRepository.FindAttrById(session.AssetId)
|
||||||
if len(attributes) > 0 {
|
if len(attributes) > 0 {
|
||||||
for i := range attributes {
|
for i := range attributes {
|
||||||
attribute := attributes[i]
|
attribute := attributes[i]
|
||||||
@ -195,7 +195,7 @@ func TunEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
// 创建新会话
|
// 创建新会话
|
||||||
logrus.Debugf("创建新会话 %v", sess.ConnectionId)
|
logrus.Debugf("创建新会话 %v", sess.ConnectionId)
|
||||||
if err := model.UpdateSessionById(&sess, sessionId); err != nil {
|
if err := sessionRepository.UpdateById(&sess, sessionId); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
"next-terminal/server/utils"
|
"next-terminal/server/utils"
|
||||||
|
|
||||||
@ -29,7 +28,7 @@ func UserGroupCreateEndpoint(c echo.Context) error {
|
|||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.CreateNewUserGroup(&userGroup, item.Members); err != nil {
|
if err := userGroupRepository.Create(&userGroup, item.Members); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,7 +43,7 @@ func UserGroupPagingEndpoint(c echo.Context) error {
|
|||||||
order := c.QueryParam("order")
|
order := c.QueryParam("order")
|
||||||
field := c.QueryParam("field")
|
field := c.QueryParam("field")
|
||||||
|
|
||||||
items, total, err := model.FindPageUserGroup(pageIndex, pageSize, name, order, field)
|
items, total, err := userGroupRepository.Find(pageIndex, pageSize, name, order, field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -66,7 +65,7 @@ func UserGroupUpdateEndpoint(c echo.Context) error {
|
|||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := model.UpdateUserGroupById(&userGroup, item.Members, id); err != nil {
|
if err := userGroupRepository.Update(&userGroup, item.Members, id); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +77,9 @@ func UserGroupDeleteEndpoint(c echo.Context) error {
|
|||||||
split := strings.Split(ids, ",")
|
split := strings.Split(ids, ",")
|
||||||
for i := range split {
|
for i := range split {
|
||||||
userId := split[i]
|
userId := split[i]
|
||||||
model.DeleteUserGroupById(userId)
|
if err := userGroupRepository.DeleteById(userId); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Success(c, nil)
|
return Success(c, nil)
|
||||||
@ -87,12 +88,12 @@ func UserGroupDeleteEndpoint(c echo.Context) error {
|
|||||||
func UserGroupGetEndpoint(c echo.Context) error {
|
func UserGroupGetEndpoint(c echo.Context) error {
|
||||||
id := c.Param("id")
|
id := c.Param("id")
|
||||||
|
|
||||||
item, err := model.FindUserGroupById(id)
|
item, err := userGroupRepository.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
members, err := model.FindUserGroupMembersByUserGroupId(id)
|
members, err := userGroupRepository.FindMembersById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -105,32 +106,3 @@ func UserGroupGetEndpoint(c echo.Context) error {
|
|||||||
|
|
||||||
return Success(c, userGroup)
|
return Success(c, userGroup)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UserGroupAddMembersEndpoint(c echo.Context) error {
|
|
||||||
id := c.Param("id")
|
|
||||||
|
|
||||||
var items []string
|
|
||||||
if err := c.Bind(&items); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := model.AddUserGroupMembers(global.DB, items, id); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return Success(c, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func UserGroupDelMembersEndpoint(c echo.Context) (err error) {
|
|
||||||
id := c.Param("id")
|
|
||||||
memberIdsStr := c.Param("memberId")
|
|
||||||
memberIds := strings.Split(memberIdsStr, ",")
|
|
||||||
for i := range memberIds {
|
|
||||||
memberId := memberIds[i]
|
|
||||||
err = global.DB.Where("user_group_id = ? and user_id = ?", id, memberId).Delete(&model.UserGroupMember{}).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return Success(c, "")
|
|
||||||
}
|
|
||||||
|
@ -34,7 +34,7 @@ func UserCreateEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if item.Mail != "" {
|
if item.Mail != "" {
|
||||||
go model.SendMail(item.Mail, "[Next Terminal] 注册通知", "你好,"+item.Nickname+"。管理员为你注册了账号:"+item.Username+" 密码:"+password)
|
go mailService.SendMail(item.Mail, "[Next Terminal] 注册通知", "你好,"+item.Nickname+"。管理员为你注册了账号:"+item.Username+" 密码:"+password)
|
||||||
}
|
}
|
||||||
return Success(c, item)
|
return Success(c, item)
|
||||||
}
|
}
|
||||||
@ -89,14 +89,14 @@ func UserDeleteEndpoint(c echo.Context) error {
|
|||||||
return Fail(c, -1, "不允许删除自身账户")
|
return Fail(c, -1, "不允许删除自身账户")
|
||||||
}
|
}
|
||||||
// 将用户强制下线
|
// 将用户强制下线
|
||||||
loginLogs, err := model.FindAliveLoginLogsByUserId(userId)
|
loginLogs, err := loginLogRepository.FindAliveLoginLogsByUserId(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for j := range loginLogs {
|
for j := range loginLogs {
|
||||||
global.Cache.Delete(loginLogs[j].ID)
|
global.Cache.Delete(loginLogs[j].ID)
|
||||||
if err := model.Logout(loginLogs[j].ID); err != nil {
|
if err := userService.Logout(loginLogs[j].ID); err != nil {
|
||||||
logrus.WithError(err).WithField("id:", loginLogs[j].ID).Error("Cache Deleted Error")
|
logrus.WithError(err).WithField("id:", loginLogs[j].ID).Error("Cache Deleted Error")
|
||||||
return Fail(c, 500, "强制下线错误")
|
return Fail(c, 500, "强制下线错误")
|
||||||
}
|
}
|
||||||
@ -144,7 +144,7 @@ func UserChangePasswordEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if user.Mail != "" {
|
if user.Mail != "" {
|
||||||
go model.SendMail(user.Mail, "[Next Terminal] 密码修改通知", "你好,"+user.Nickname+"。管理员已将你的密码修改为:"+password)
|
go mailService.SendMail(user.Mail, "[Next Terminal] 密码修改通知", "你好,"+user.Nickname+"。管理员已将你的密码修改为:"+password)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
@ -161,3 +161,37 @@ func UserResetTotpEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
return Success(c, "")
|
return Success(c, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReloadToken() error {
|
||||||
|
loginLogs, err := loginLogRepository.FindAliveLoginLogs()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range loginLogs {
|
||||||
|
loginLog := loginLogs[i]
|
||||||
|
token := loginLog.ID
|
||||||
|
user, err := userRepository.FindById(loginLog.UserId)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Debugf("用户「%v」获取失败,忽略", loginLog.UserId)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
authorization := Authorization{
|
||||||
|
Token: token,
|
||||||
|
Remember: loginLog.Remember,
|
||||||
|
User: user,
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheKey := BuildCacheKeyByToken(token)
|
||||||
|
|
||||||
|
if authorization.Remember {
|
||||||
|
// 记住登录有效期两周
|
||||||
|
global.Cache.Set(cacheKey, authorization, RememberEffectiveTime)
|
||||||
|
} else {
|
||||||
|
global.Cache.Set(cacheKey, authorization, NotRememberEffectiveTime)
|
||||||
|
}
|
||||||
|
logrus.Debugf("重新加载用户「%v」授权Token「%v」到缓存", user.Nickname, token)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,53 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"gorm.io/driver/mysql"
|
|
||||||
"gorm.io/driver/sqlite"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"gorm.io/gorm/logger"
|
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
|
||||||
)
|
|
||||||
|
|
||||||
func SetupDB() *gorm.DB {
|
|
||||||
|
|
||||||
var logMode logger.Interface
|
|
||||||
if global.Config.Debug {
|
|
||||||
logMode = logger.Default.LogMode(logger.Info)
|
|
||||||
} else {
|
|
||||||
logMode = logger.Default.LogMode(logger.Silent)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("当前数据库模式为:%v\n", global.Config.DB)
|
|
||||||
var err error
|
|
||||||
var db *gorm.DB
|
|
||||||
if global.Config.DB == "mysql" {
|
|
||||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
||||||
global.Config.Mysql.Username,
|
|
||||||
global.Config.Mysql.Password,
|
|
||||||
global.Config.Mysql.Hostname,
|
|
||||||
global.Config.Mysql.Port,
|
|
||||||
global.Config.Mysql.Database,
|
|
||||||
)
|
|
||||||
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
|
||||||
Logger: logMode,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
db, err = gorm.Open(sqlite.Open(global.Config.Sqlite.File), &gorm.Config{
|
|
||||||
Logger: logMode,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
logrus.WithError(err).Panic("连接数据库异常")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := db.AutoMigrate(&model.User{}, &model.Asset{}, &model.AssetAttribute{}, &model.Session{}, &model.Command{},
|
|
||||||
&model.Credential{}, &model.Property{}, &model.ResourceSharer{}, &model.UserGroup{}, &model.UserGroupMember{},
|
|
||||||
&model.LoginLog{}, &model.Num{}, &model.Job{}, &model.JobLog{}, &model.AccessSecurity{}); err != nil {
|
|
||||||
logrus.WithError(err).Panic("初始化数据库表结构异常")
|
|
||||||
}
|
|
||||||
return db
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -63,19 +62,19 @@ func (r AccessSecurityRepository) Find(pageIndex, pageSize int, ip, rule, order,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r AccessSecurityRepository) Create(o *model.AccessSecurity) error {
|
func (r AccessSecurityRepository) Create(o *model.AccessSecurity) error {
|
||||||
return global.DB.Create(o).Error
|
return r.DB.Create(o).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r AccessSecurityRepository) UpdateById(o *model.AccessSecurity, id string) error {
|
func (r AccessSecurityRepository) UpdateById(o *model.AccessSecurity, id string) error {
|
||||||
o.ID = id
|
o.ID = id
|
||||||
return global.DB.Updates(o).Error
|
return r.DB.Updates(o).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r AccessSecurityRepository) DeleteById(id string) error {
|
func (r AccessSecurityRepository) DeleteById(id string) error {
|
||||||
return global.DB.Where("id = ?", id).Delete(model.AccessSecurity{}).Error
|
return r.DB.Where("id = ?", id).Delete(model.AccessSecurity{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r AccessSecurityRepository) FindById(id string) (o *model.AccessSecurity, err error) {
|
func (r AccessSecurityRepository) FindById(id string) (o *model.AccessSecurity, err error) {
|
||||||
err = global.DB.Where("id = ?", id).First(&o).Error
|
err = r.DB.Where("id = ?", id).First(&o).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ func (r AssetRepository) DeleteById(id string) error {
|
|||||||
return r.DB.Where("id = ?", id).Delete(&model.Asset{}).Error
|
return r.DB.Where("id = ?", id).Delete(&model.Asset{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r AssetRepository) CountAsset() (total int64, err error) {
|
func (r AssetRepository) Count() (total int64, err error) {
|
||||||
err = r.DB.Find(&model.Asset{}).Count(&total).Error
|
err = r.DB.Find(&model.Asset{}).Count(&total).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"next-terminal/server/constant"
|
"next-terminal/server/constant"
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -68,13 +67,13 @@ func (r CommandRepository) Create(o *model.Command) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r CommandRepository) FindById(id string) (o model.Command, err error) {
|
func (r CommandRepository) FindById(id string) (o model.Command, err error) {
|
||||||
err = global.DB.Where("id = ?", id).First(&o).Error
|
err = r.DB.Where("id = ?", id).First(&o).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r CommandRepository) UpdateById(o *model.Command, id string) {
|
func (r CommandRepository) UpdateById(o *model.Command, id string) error {
|
||||||
o.ID = id
|
o.ID = id
|
||||||
global.DB.Updates(o)
|
return r.DB.Updates(o).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r CommandRepository) DeleteById(id string) error {
|
func (r CommandRepository) DeleteById(id string) error {
|
||||||
|
@ -15,7 +15,7 @@ func NewCredentialRepository(db *gorm.DB) *CredentialRepository {
|
|||||||
return credentialRepository
|
return credentialRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r CredentialRepository) FindAllByUser(account model.User) (o []model.CredentialSimpleVo, err error) {
|
func (r CredentialRepository) FindByUser(account model.User) (o []model.CredentialSimpleVo, err error) {
|
||||||
db := r.DB.Table("credentials").Select("DISTINCT credentials.id,credentials.name").Joins("left join resource_sharers on credentials.id = resource_sharers.resource_id")
|
db := r.DB.Table("credentials").Select("DISTINCT credentials.id,credentials.name").Joins("left join resource_sharers on credentials.id = resource_sharers.resource_id")
|
||||||
if account.Type == constant.TypeUser {
|
if account.Type == constant.TypeUser {
|
||||||
db = db.Where("credentials.owner = ? or resource_sharers.user_id = ?", account.ID, account.ID)
|
db = db.Where("credentials.owner = ? or resource_sharers.user_id = ?", account.ID, account.ID)
|
||||||
|
@ -2,7 +2,6 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
"next-terminal/server/utils"
|
"next-terminal/server/utils"
|
||||||
)
|
)
|
||||||
@ -64,19 +63,6 @@ func (r JobRepository) FindByFunc(function string) (o []model.Job, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r JobRepository) Create(o *model.Job) (err error) {
|
func (r JobRepository) Create(o *model.Job) (err error) {
|
||||||
//
|
|
||||||
//if o.Status == constant.JobStatusRunning {
|
|
||||||
// j, err := getJob(o)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// jobId, err := global.Cron.AddJob(o.Cron, j)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// o.CronJobId = int(jobId)
|
|
||||||
//}
|
|
||||||
|
|
||||||
return r.DB.Create(o).Error
|
return r.DB.Create(o).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,5 +90,5 @@ func (r JobRepository) DeleteJobById(id string) error {
|
|||||||
// return err
|
// return err
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
return global.DB.Where("id = ?", id).Delete(model.Job{}).Error
|
return r.DB.Where("id = ?", id).Delete(model.Job{}).Error
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,6 +20,6 @@ func (r NumRepository) FindAll() (o []model.Num, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r NumRepository) Create(o *model.Num) (err error) {
|
func (r NumRepository) Create(o *model.Num) (err error) {
|
||||||
err = global.DB.Create(o).Error
|
err = r.DB.Create(o).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/jordan-wright/email"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"net/smtp"
|
|
||||||
"next-terminal/server/constant"
|
|
||||||
"next-terminal/server/guacd"
|
"next-terminal/server/guacd"
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
)
|
)
|
||||||
@ -31,7 +27,7 @@ func (r PropertyRepository) Create(o *model.Property) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r PropertyRepository) UpdatePropertyByName(o *model.Property, name string) error {
|
func (r PropertyRepository) UpdateByName(o *model.Property, name string) error {
|
||||||
o.Name = name
|
o.Name = name
|
||||||
return r.DB.Updates(o).Error
|
return r.DB.Updates(o).Error
|
||||||
}
|
}
|
||||||
@ -65,26 +61,3 @@ func (r PropertyRepository) GetRecordingPath() (string, error) {
|
|||||||
}
|
}
|
||||||
return property.Value, nil
|
return property.Value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r PropertyRepository) SendMail(to, subject, text string) {
|
|
||||||
propertiesMap := r.FindAllMap()
|
|
||||||
host := propertiesMap[constant.MailHost]
|
|
||||||
port := propertiesMap[constant.MailPort]
|
|
||||||
username := propertiesMap[constant.MailUsername]
|
|
||||||
password := propertiesMap[constant.MailPassword]
|
|
||||||
|
|
||||||
if host == "" || port == "" || username == "" || password == "" {
|
|
||||||
logrus.Debugf("邮箱信息不完整,跳过发送邮件。")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
e := email.NewEmail()
|
|
||||||
e.From = "Next Terminal <" + username + ">"
|
|
||||||
e.To = []string{to}
|
|
||||||
e.Subject = subject
|
|
||||||
e.Text = []byte(text)
|
|
||||||
err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("邮件发送失败: %v", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -2,7 +2,6 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"next-terminal/server/global"
|
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
"next-terminal/server/utils"
|
"next-terminal/server/utils"
|
||||||
)
|
)
|
||||||
@ -56,7 +55,7 @@ func (r UserGroupRepository) Find(pageIndex, pageSize int, name, order, field st
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r UserGroupRepository) FindById(id string) (o model.UserGroup, err error) {
|
func (r UserGroupRepository) FindById(id string) (o model.UserGroup, err error) {
|
||||||
err = global.DB.Where("id = ?", id).First(&o).Error
|
err = r.DB.Where("id = ?", id).First(&o).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +65,7 @@ func (r UserGroupRepository) FindUserGroupIdsByUserId(userId string) (o []string
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r UserGroupRepository) FindUserGroupMembersByUserGroupId(userGroupId string) (o []string, err error) {
|
func (r UserGroupRepository) FindMembersById(userGroupId string) (o []string, err error) {
|
||||||
err = r.DB.Table("user_group_members").Select("user_id").Where("user_group_id = ?", userGroupId).Find(&o).Error
|
err = r.DB.Table("user_group_members").Select("user_id").Where("user_group_id = ?", userGroupId).Find(&o).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -112,9 +111,12 @@ func (r UserGroupRepository) Update(o *model.UserGroup, members []string, id str
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r UserGroupRepository) DeleteById(id string) {
|
func (r UserGroupRepository) DeleteById(id string) (err error) {
|
||||||
r.DB.Where("id = ?", id).Delete(&model.UserGroup{})
|
err = r.DB.Where("id = ?", id).Delete(&model.UserGroup{}).Error
|
||||||
r.DB.Where("user_group_id = ?", id).Delete(&model.UserGroupMember{})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return r.DB.Where("user_group_id = ?", id).Delete(&model.UserGroupMember{}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddUserGroupMembers(tx *gorm.DB, userIds []string, userGroupId string) error {
|
func AddUserGroupMembers(tx *gorm.DB, userIds []string, userGroupId string) error {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"next-terminal/server/api"
|
|
||||||
"next-terminal/server/constant"
|
"next-terminal/server/constant"
|
||||||
"next-terminal/server/global"
|
"next-terminal/server/global"
|
||||||
"next-terminal/server/model"
|
"next-terminal/server/model"
|
||||||
@ -28,7 +27,7 @@ func NewJobService(jobRepository *repository.JobRepository, jobLogRepository *re
|
|||||||
return &JobService{jobRepository: jobRepository, jobLogRepository: jobLogRepository, assetRepository: assetRepository, credentialRepository: credentialRepository}
|
return &JobService{jobRepository: jobRepository, jobLogRepository: jobLogRepository, assetRepository: assetRepository, credentialRepository: credentialRepository}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r JobService) ChangeJobStatusById(id, status string) error {
|
func (r JobService) ChangeStatusById(id, status string) error {
|
||||||
job, err := r.jobRepository.FindById(id)
|
job, err := r.jobRepository.FindById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -259,7 +258,7 @@ func (r JobService) ExecJobById(id string) (err error) {
|
|||||||
|
|
||||||
func (r JobService) InitJob() error {
|
func (r JobService) InitJob() error {
|
||||||
jobs, _ := r.jobRepository.FindByFunc(constant.FuncCheckAssetStatusJob)
|
jobs, _ := r.jobRepository.FindByFunc(constant.FuncCheckAssetStatusJob)
|
||||||
if jobs == nil || len(jobs) == 0 {
|
if jobs == nil {
|
||||||
job := model.Job{
|
job := model.Job{
|
||||||
ID: utils.UUID(),
|
ID: utils.UUID(),
|
||||||
Name: "资产状态检测",
|
Name: "资产状态检测",
|
||||||
@ -277,7 +276,7 @@ func (r JobService) InitJob() error {
|
|||||||
} else {
|
} else {
|
||||||
for i := range jobs {
|
for i := range jobs {
|
||||||
if jobs[i].Status == constant.JobStatusRunning {
|
if jobs[i].Status == constant.JobStatusRunning {
|
||||||
err := r.ChangeJobStatusById(jobs[i].ID, constant.JobStatusRunning)
|
err := r.ChangeStatusById(jobs[i].ID, constant.JobStatusRunning)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -288,37 +287,32 @@ func (r JobService) InitJob() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO 可能存在循环引用
|
func (r JobService) Create(o *model.Job) (err error) {
|
||||||
func (r UserService) ReloadToken() error {
|
|
||||||
loginLogs, err := r.loginLogRepository.FindAliveLoginLogs()
|
if o.Status == constant.JobStatusRunning {
|
||||||
|
j, err := getJob(o, &r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
jobId, err := global.Cron.AddJob(o.Cron, j)
|
||||||
for i := range loginLogs {
|
|
||||||
loginLog := loginLogs[i]
|
|
||||||
token := loginLog.ID
|
|
||||||
user, err := r.userRepository.FindById(loginLog.UserId)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Debugf("用户「%v」获取失败,忽略", loginLog.UserId)
|
return err
|
||||||
continue
|
}
|
||||||
|
o.CronJobId = int(jobId)
|
||||||
}
|
}
|
||||||
|
|
||||||
authorization := api.Authorization{
|
return r.jobRepository.Create(o)
|
||||||
Token: token,
|
}
|
||||||
Remember: loginLog.Remember,
|
|
||||||
User: user,
|
func (r JobService) DeleteJobById(id string) error {
|
||||||
}
|
job, err := r.jobRepository.FindById(id)
|
||||||
|
if err != nil {
|
||||||
cacheKey := api.BuildCacheKeyByToken(token)
|
return err
|
||||||
|
}
|
||||||
if authorization.Remember {
|
if job.Status == constant.JobStatusRunning {
|
||||||
// 记住登录有效期两周
|
if err := r.ChangeStatusById(id, constant.JobStatusNotRunning); err != nil {
|
||||||
global.Cache.Set(cacheKey, authorization, api.RememberEffectiveTime)
|
return err
|
||||||
} else {
|
}
|
||||||
global.Cache.Set(cacheKey, authorization, api.NotRememberEffectiveTime)
|
}
|
||||||
}
|
return r.jobRepository.DeleteJobById(id)
|
||||||
logrus.Debugf("重新加载用户「%v」授权Token「%v」到缓存", user.Nickname, token)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
40
server/service/mail.go
Normal file
40
server/service/mail.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jordan-wright/email"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"net/smtp"
|
||||||
|
"next-terminal/server/constant"
|
||||||
|
"next-terminal/server/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MailService struct {
|
||||||
|
propertyRepository *repository.PropertyRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMailService(propertyRepository *repository.PropertyRepository) *MailService {
|
||||||
|
return &MailService{propertyRepository: propertyRepository}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r MailService) SendMail(to, subject, text string) {
|
||||||
|
propertiesMap := r.propertyRepository.FindAllMap()
|
||||||
|
host := propertiesMap[constant.MailHost]
|
||||||
|
port := propertiesMap[constant.MailPort]
|
||||||
|
username := propertiesMap[constant.MailUsername]
|
||||||
|
password := propertiesMap[constant.MailPassword]
|
||||||
|
|
||||||
|
if host == "" || port == "" || username == "" || password == "" {
|
||||||
|
logrus.Debugf("邮箱信息不完整,跳过发送邮件。")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
e := email.NewEmail()
|
||||||
|
e.From = "Next Terminal <" + username + ">"
|
||||||
|
e.To = []string{to}
|
||||||
|
e.Subject = subject
|
||||||
|
e.Text = []byte(text)
|
||||||
|
err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("邮件发送失败: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
@ -13,8 +13,8 @@ type UserService struct {
|
|||||||
loginLogRepository *repository.LoginLogRepository
|
loginLogRepository *repository.LoginLogRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserService(userRepository *repository.UserRepository) *UserService {
|
func NewUserService(userRepository *repository.UserRepository, loginLogRepository *repository.LoginLogRepository) *UserService {
|
||||||
return &UserService{userRepository: userRepository}
|
return &UserService{userRepository: userRepository, loginLogRepository: loginLogRepository}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r UserService) InitUser() (err error) {
|
func (r UserService) InitUser() (err error) {
|
||||||
|
@ -13,7 +13,6 @@ import {
|
|||||||
Menu,
|
Menu,
|
||||||
Modal,
|
Modal,
|
||||||
notification,
|
notification,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
@ -27,7 +26,7 @@ import qs from "qs";
|
|||||||
import AssetModal from "./AssetModal";
|
import AssetModal from "./AssetModal";
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {getHeaders, isEmpty, itemRender} from "../../utils/utils";
|
import {getHeaders, isEmpty} from "../../utils/utils";
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import {
|
import {
|
||||||
DeleteOutlined,
|
DeleteOutlined,
|
||||||
@ -881,7 +880,7 @@ class Asset extends Component {
|
|||||||
|
|
||||||
<Button type="primary" onClick={() => {
|
<Button type="primary" onClick={() => {
|
||||||
|
|
||||||
let csvString= 'name,ssh,127.0.0.1,22,username,password,privateKey,passphrase,description';
|
let csvString = 'name,ssh,127.0.0.1,22,username,password,privateKey,passphrase,description';
|
||||||
//前置的"\uFEFF"为“零宽不换行空格”,可处理中文乱码问题
|
//前置的"\uFEFF"为“零宽不换行空格”,可处理中文乱码问题
|
||||||
const blob = new Blob(["\uFEFF" + csvString], {type: 'text/csv;charset=gb2312;'});
|
const blob = new Blob(["\uFEFF" + csvString], {type: 'text/csv;charset=gb2312;'});
|
||||||
let a = document.createElement('a');
|
let a = document.createElement('a');
|
||||||
|
@ -12,7 +12,6 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
Menu,
|
Menu,
|
||||||
Modal,
|
Modal,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
@ -33,7 +32,7 @@ import {
|
|||||||
SyncOutlined,
|
SyncOutlined,
|
||||||
UndoOutlined
|
UndoOutlined
|
||||||
} from '@ant-design/icons';
|
} from '@ant-design/icons';
|
||||||
import {compare, itemRender} from "../../utils/utils";
|
import {compare} from "../../utils/utils";
|
||||||
|
|
||||||
import {hasPermission, isAdmin} from "../../service/permission";
|
import {hasPermission, isAdmin} from "../../service/permission";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {Card, Col, PageHeader, Radio, Row, Statistic} from "antd";
|
import {Card, Col, Radio, Row, Statistic} from "antd";
|
||||||
import {DesktopOutlined, IdcardOutlined, LinkOutlined, UserOutlined} from '@ant-design/icons';
|
import {DesktopOutlined, IdcardOutlined, LinkOutlined, UserOutlined} from '@ant-design/icons';
|
||||||
import {itemRender} from '../../utils/utils'
|
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import './Dashboard.css'
|
import './Dashboard.css'
|
||||||
import {Link} from "react-router-dom";
|
import {Link} from "react-router-dom";
|
||||||
|
@ -9,7 +9,6 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
Menu,
|
Menu,
|
||||||
Modal,
|
Modal,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Space,
|
Space,
|
||||||
Spin,
|
Spin,
|
||||||
@ -30,7 +29,6 @@ import {
|
|||||||
SyncOutlined,
|
SyncOutlined,
|
||||||
UndoOutlined
|
UndoOutlined
|
||||||
} from '@ant-design/icons';
|
} from '@ant-design/icons';
|
||||||
import {itemRender} from "../../utils/utils";
|
|
||||||
|
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import JobModal from "./JobModal";
|
import JobModal from "./JobModal";
|
||||||
|
@ -8,7 +8,6 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
Modal,
|
Modal,
|
||||||
notification,
|
notification,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
@ -18,7 +17,7 @@ import {
|
|||||||
} from "antd";
|
} from "antd";
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {formatDate, isEmpty, itemRender} from "../../utils/utils";
|
import {formatDate, isEmpty} from "../../utils/utils";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {DeleteOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons";
|
import {DeleteOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons";
|
||||||
|
|
||||||
|
@ -1,25 +1,10 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
|
|
||||||
import {
|
import {Button, Col, Divider, Input, Layout, Modal, Row, Space, Table, Tag, Tooltip, Typography} from "antd";
|
||||||
Button,
|
|
||||||
Col,
|
|
||||||
Divider,
|
|
||||||
Input,
|
|
||||||
Layout,
|
|
||||||
Modal,
|
|
||||||
PageHeader,
|
|
||||||
Row,
|
|
||||||
Space,
|
|
||||||
Table,
|
|
||||||
Tag,
|
|
||||||
Tooltip,
|
|
||||||
Typography
|
|
||||||
} from "antd";
|
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {DeleteOutlined, ExclamationCircleOutlined, PlusOutlined, SyncOutlined, UndoOutlined} from '@ant-design/icons';
|
import {DeleteOutlined, ExclamationCircleOutlined, PlusOutlined, SyncOutlined, UndoOutlined} from '@ant-design/icons';
|
||||||
import {itemRender} from "../../utils/utils";
|
|
||||||
import './Job.css'
|
import './Job.css'
|
||||||
import SecurityModal from "./SecurityModal";
|
import SecurityModal from "./SecurityModal";
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
Modal,
|
Modal,
|
||||||
notification,
|
notification,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
@ -18,7 +17,7 @@ import {
|
|||||||
} from "antd";
|
} from "antd";
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {differTime, itemRender} from "../../utils/utils";
|
import {differTime} from "../../utils/utils";
|
||||||
import Playback from "./Playback";
|
import Playback from "./Playback";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {DeleteOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons";
|
import {DeleteOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons";
|
||||||
|
@ -8,7 +8,6 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
Modal,
|
Modal,
|
||||||
notification,
|
notification,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
@ -19,7 +18,7 @@ import {
|
|||||||
} from "antd";
|
} from "antd";
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {differTime, itemRender} from "../../utils/utils";
|
import {differTime} from "../../utils/utils";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {PROTOCOL_COLORS} from "../../common/constants";
|
import {PROTOCOL_COLORS} from "../../common/constants";
|
||||||
import {DisconnectOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons";
|
import {DisconnectOutlined, ExclamationCircleOutlined, SyncOutlined, UndoOutlined} from "@ant-design/icons";
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {Button, Form, Input, Layout, PageHeader, Select, Switch, Tabs, Tooltip, Typography} from "antd";
|
import {Button, Form, Input, Layout, Select, Switch, Tabs, Tooltip, Typography} from "antd";
|
||||||
import {itemRender} from '../../utils/utils'
|
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {ExclamationCircleOutlined} from "@ant-design/icons";
|
import {ExclamationCircleOutlined} from "@ant-design/icons";
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {Button, Card, Form, Image, Input, Layout, Modal, PageHeader, Result, Space} from "antd";
|
import {Button, Card, Form, Image, Input, Layout, Modal, Result, Space} from "antd";
|
||||||
import {itemRender} from '../../utils/utils'
|
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
import {ExclamationCircleOutlined, ReloadOutlined} from "@ant-design/icons";
|
import {ExclamationCircleOutlined, ReloadOutlined} from "@ant-design/icons";
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {itemRender} from '../../utils/utils'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Badge,
|
Badge,
|
||||||
@ -12,7 +11,6 @@ import {
|
|||||||
Layout,
|
Layout,
|
||||||
Menu,
|
Menu,
|
||||||
Modal,
|
Modal,
|
||||||
PageHeader,
|
|
||||||
Row,
|
Row,
|
||||||
Space,
|
Space,
|
||||||
Table,
|
Table,
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import React, {Component} from 'react';
|
import React, {Component} from 'react';
|
||||||
import {itemRender} from '../../utils/utils'
|
|
||||||
|
|
||||||
import {Button, Col, Divider, Input, Layout, Modal, PageHeader, Row, Space, Table, Tooltip, Typography,} from "antd";
|
import {Button, Col, Divider, Input, Layout, Modal, Row, Space, Table, Tooltip, Typography,} from "antd";
|
||||||
import qs from "qs";
|
import qs from "qs";
|
||||||
import request from "../../common/request";
|
import request from "../../common/request";
|
||||||
import {message} from "antd/es";
|
import {message} from "antd/es";
|
||||||
|
Loading…
Reference in New Issue
Block a user