完善dockerfile构建镜像

This commit is contained in:
dushixiang
2020-12-24 23:21:51 +08:00
parent 348074670e
commit 72f7dd5dc6
36 changed files with 369 additions and 277 deletions

View File

@ -2,7 +2,7 @@ package api
import (
"github.com/labstack/echo/v4"
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/model"
"next-terminal/pkg/utils"
"time"
@ -29,7 +29,7 @@ func LoginEndpoint(c echo.Context) error {
token := utils.UUID()
config.Cache.Set(token, user, time.Minute*time.Duration(30))
global.Cache.Set(token, user, time.Minute*time.Duration(30))
model.UpdateUserById(&model.User{Online: true}, user.ID)
@ -38,7 +38,7 @@ func LoginEndpoint(c echo.Context) error {
func LogoutEndpoint(c echo.Context) error {
token := GetToken(c)
config.Cache.Delete(token)
global.Cache.Delete(token)
return Success(c, nil)
}

View File

@ -2,14 +2,14 @@ package api
import (
"github.com/labstack/echo/v4"
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"strings"
"time"
)
func Auth(next echo.HandlerFunc) echo.HandlerFunc {
urls := []string{"download", "login"}
urls := []string{"download", "recording", "login", "static", "favicon", "logo"}
return func(c echo.Context) error {
// 路由拦截 - 登录身份、资源权限判断等
@ -23,12 +23,12 @@ func Auth(next echo.HandlerFunc) echo.HandlerFunc {
}
token := GetToken(c)
user, found := config.Cache.Get(token)
user, found := global.Cache.Get(token)
if !found {
c.Logger().Error("您的登录信息已失效,请重新登录后再试。")
return Fail(c, 403, "您的登录信息已失效,请重新登录后再试。")
}
config.Cache.Set(token, user, time.Minute*time.Duration(30))
global.Cache.Set(token, user, time.Minute*time.Duration(30))
return next(c)
}
}

View File

@ -4,7 +4,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/model"
)
@ -12,7 +12,6 @@ const Token = "X-Auth-Token"
func SetupRoutes() *echo.Echo {
// Echo instance
e := echo.New()
e.File("/", "web/build/index.html")
@ -22,6 +21,15 @@ func SetupRoutes() *echo.Echo {
// Middleware
e.Use(middleware.Logger())
//fd, _ := os.OpenFile(
// "nt.log",
// os.O_RDWR|os.O_APPEND,
// 0666,
//)
//writer := io.MultiWriter(fd, os.Stdout)
//e.Logger.SetOutput(writer)
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
Skipper: middleware.DefaultSkipper,
@ -92,6 +100,7 @@ func SetupRoutes() *echo.Echo {
sessions.DELETE("/:id/rmdir", SessionRmDirEndpoint)
sessions.DELETE("/:id/rm", SessionRmEndpoint)
sessions.DELETE("/:id", SessionDeleteEndpoint)
sessions.GET("/:id/recording", SessionRecordingEndpoint)
}
e.GET("/properties", PropertyGetEndpoint)
@ -137,7 +146,7 @@ func GetToken(c echo.Context) string {
func GetCurrentAccount(c echo.Context) (model.User, bool) {
token := GetToken(c)
get, b := config.Cache.Get(token)
get, b := global.Cache.Get(token)
if b {
return get.(model.User), true
}

View File

@ -5,10 +5,11 @@ import (
"errors"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"io"
"io/ioutil"
"net/http"
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/model"
"next-terminal/pkg/utils"
"os"
@ -42,12 +43,14 @@ func SessionDeleteEndpoint(c echo.Context) error {
sessionIds := c.Param("id")
split := strings.Split(sessionIds, ",")
for i := range split {
model.DeleteSessionById(split[i])
drivePath, err := model.GetRecordingPath()
if err != nil {
continue
}
_ = os.Remove(path.Join(drivePath, split[i]))
if err := os.RemoveAll(path.Join(drivePath, split[i])); err != nil {
return err
}
model.DeleteSessionById(split[i])
}
return Success(c, nil)
@ -70,7 +73,7 @@ func SessionDiscontentEndpoint(c echo.Context) error {
split := strings.Split(sessionIds, ",")
for i := range split {
tun, ok := config.Store.Get(split[i])
tun, ok := global.Store.Get(split[i])
if ok {
CloseSession(split[i], tun)
}
@ -78,9 +81,9 @@ func SessionDiscontentEndpoint(c echo.Context) error {
return Success(c, nil)
}
func CloseSession(sessionId string, tun config.Tun) {
func CloseSession(sessionId string, tun global.Tun) {
_ = tun.Tun.Close()
config.Store.Del(sessionId)
global.Store.Del(sessionId)
session := model.Session{}
session.ID = sessionId
@ -172,7 +175,7 @@ func SessionUploadEndpoint(c echo.Context) error {
remoteFile := path.Join(remoteDir, filename)
if "ssh" == session.Protocol {
tun, ok := config.Store.Get(sessionId)
tun, ok := global.Store.Get(sessionId)
if !ok {
return errors.New("获取sftp客户端失败")
}
@ -226,7 +229,7 @@ func SessionDownloadEndpoint(c echo.Context) error {
remoteFile := c.QueryParam("file")
if "ssh" == session.Protocol {
tun, ok := config.Store.Get(sessionId)
tun, ok := global.Store.Get(sessionId)
if !ok {
return errors.New("获取sftp客户端失败")
}
@ -273,7 +276,7 @@ func SessionLsEndpoint(c echo.Context) error {
}
remoteDir := c.QueryParam("dir")
if "ssh" == session.Protocol {
tun, ok := config.Store.Get(sessionId)
tun, ok := global.Store.Get(sessionId)
if !ok {
return errors.New("获取sftp客户端失败")
}
@ -334,7 +337,7 @@ func SessionMkDirEndpoint(c echo.Context) error {
}
remoteDir := c.QueryParam("dir")
if "ssh" == session.Protocol {
tun, ok := config.Store.Get(sessionId)
tun, ok := global.Store.Get(sessionId)
if !ok {
return errors.New("获取sftp客户端失败")
}
@ -365,7 +368,7 @@ func SessionRmDirEndpoint(c echo.Context) error {
}
remoteDir := c.QueryParam("dir")
if "ssh" == session.Protocol {
tun, ok := config.Store.Get(sessionId)
tun, ok := global.Store.Get(sessionId)
if !ok {
return errors.New("获取sftp客户端失败")
}
@ -407,7 +410,7 @@ func SessionRmEndpoint(c echo.Context) error {
}
remoteFile := c.QueryParam("file")
if "ssh" == session.Protocol {
tun, ok := config.Store.Get(sessionId)
tun, ok := global.Store.Get(sessionId)
if !ok {
return errors.New("获取sftp客户端失败")
}
@ -428,3 +431,14 @@ func SessionRmEndpoint(c echo.Context) error {
}
return nil
}
func SessionRecordingEndpoint(c echo.Context) error {
sessionId := c.Param("id")
recordingPath, err := model.GetRecordingPath()
if err != nil {
return err
}
recording := path.Join(recordingPath, sessionId, "recording")
log.Printf("读取录屏文件:%s", recording)
return c.File(recording)
}

View File

@ -6,7 +6,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/pkg/sftp"
"log"
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/guacd"
"next-terminal/pkg/model"
"path"
@ -60,9 +60,9 @@ func TunEndpoint(c echo.Context) error {
}
if propertyMap[guacd.EnableRecording] == "true" {
configuration.SetParameter(guacd.CreateRecordingPath, path.Join(propertyMap[guacd.CreateRecordingPath], sessionId))
configuration.SetParameter(guacd.RecordingPath, path.Join(propertyMap[guacd.RecordingPath], sessionId))
} else {
configuration.SetParameter(guacd.CreateRecordingPath, "")
configuration.SetParameter(guacd.RecordingPath, "")
}
configuration.Protocol = session.Protocol
@ -110,15 +110,15 @@ func TunEndpoint(c echo.Context) error {
}
fmt.Printf("=====================================================\n")
fmt.Printf("connect to %v with config: %+v\n", addr, configuration)
fmt.Printf("connect to %v with global: %+v\n", addr, configuration)
fmt.Printf("=====================================================\n")
tun := config.Tun{
tun := global.Tun{
Tun: tunnel,
SftpClient: sftpClient,
}
config.Store.Set(sessionId, tun)
global.Store.Set(sessionId, tun)
if len(session.ConnectionId) == 0 {
session.ConnectionId = tunnel.UUID

View File

@ -1,14 +1,63 @@
package config
import (
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
)
var DB *gorm.DB
type Config struct {
Server *Server
Mysql *Mysql
}
var Cache *cache.Cache
type Mysql struct {
Hostname string
Port int
Username string
Password string
Database string
}
var NextTerminal *NextTerminalConfig
type Server struct {
Addr string
}
var Store *TunStore
func SetupConfig() *Config {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/nt/")
viper.AddConfigPath("$HOME/.nt")
viper.AddConfigPath(".")
pflag.String("mysql.hostname", "127.0.0.1", "mysql hostname")
pflag.Int("mysql.port", 3306, "mysql port")
pflag.String("mysql.username", "mysql", "mysql username")
pflag.String("mysql.password", "mysql", "mysql password")
pflag.String("mysql.database", "next_terminal", "mysql database")
pflag.String("server.addr", "0.0.0.0:8088", "server listen addr")
pflag.Parse()
_ = viper.BindPFlags(pflag.CommandLine)
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
var config = &Config{
Mysql: &Mysql{
Hostname: viper.GetString("mysql.hostname"),
Port: viper.GetInt("mysql.port"),
Username: viper.GetString("mysql.username"),
Password: viper.GetString("mysql.password"),
Database: viper.GetString("mysql.database"),
},
Server: &Server{
Addr: viper.GetString("server.addr"),
},
}
return config
}

View File

@ -1,31 +0,0 @@
package config
import (
"github.com/spf13/viper"
"log"
)
type NextTerminalConfig struct {
Dsn string
Addr string
}
func SetupConfig() *NextTerminalConfig {
viper.SetConfigName("next-terminal")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/next-terminal/")
viper.AddConfigPath("$HOME/.next-terminal")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
var config = &NextTerminalConfig{
Dsn: viper.GetString("next-terminal.dsn"),
Addr: viper.GetString("next-terminal.addr"),
}
return config
}

15
pkg/global/global.go Normal file
View File

@ -0,0 +1,15 @@
package global
import (
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
"next-terminal/pkg/config"
)
var DB *gorm.DB
var Cache *cache.Cache
var Config *config.Config
var Store *TunStore

View File

@ -1,4 +1,4 @@
package config
package global
import (
"github.com/pkg/sftp"

View File

@ -48,7 +48,7 @@ func RunDataFix() {
}
}
func InitProperties() {
func InitProperties() error {
propertyMap := model.FindAllPropertiesMap()
if len(propertyMap[guacd.Host]) == 0 {
@ -56,7 +56,9 @@ func InitProperties() {
Name: guacd.Host,
Value: "127.0.0.1",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.Port]) == 0 {
@ -64,7 +66,9 @@ func InitProperties() {
Name: guacd.Port,
Value: "4822",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableRecording]) == 0 {
@ -72,7 +76,9 @@ func InitProperties() {
Name: guacd.EnableRecording,
Value: "true",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.RecordingPath]) == 0 {
@ -81,7 +87,14 @@ func InitProperties() {
Name: guacd.RecordingPath,
Value: path + "/recording/",
}
_ = model.CreateNewProperty(&property)
if !utils.Exists(property.Value) {
if err := os.Mkdir(property.Value, os.ModePerm); err != nil {
return err
}
}
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.CreateRecordingPath]) == 0 {
@ -89,7 +102,9 @@ func InitProperties() {
Name: guacd.CreateRecordingPath,
Value: "true",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.DriveName]) == 0 {
@ -97,7 +112,9 @@ func InitProperties() {
Name: guacd.DriveName,
Value: "File-System",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.DrivePath]) == 0 {
@ -108,7 +125,14 @@ func InitProperties() {
Name: guacd.DrivePath,
Value: path + "/drive/",
}
_ = model.CreateNewProperty(&property)
if !utils.Exists(property.Value) {
if err := os.Mkdir(property.Value, os.ModePerm); err != nil {
return err
}
}
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.FontName]) == 0 {
@ -116,7 +140,9 @@ func InitProperties() {
Name: guacd.FontName,
Value: "menlo",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.FontSize]) == 0 {
@ -124,7 +150,9 @@ func InitProperties() {
Name: guacd.FontSize,
Value: "12",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.ColorScheme]) == 0 {
@ -132,7 +160,9 @@ func InitProperties() {
Name: guacd.ColorScheme,
Value: "gray-black",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableDrive]) == 0 {
@ -140,7 +170,9 @@ func InitProperties() {
Name: guacd.EnableDrive,
Value: "true",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableWallpaper]) == 0 {
@ -148,7 +180,9 @@ func InitProperties() {
Name: guacd.EnableWallpaper,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableTheming]) == 0 {
@ -156,7 +190,9 @@ func InitProperties() {
Name: guacd.EnableTheming,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableFontSmoothing]) == 0 {
@ -164,7 +200,9 @@ func InitProperties() {
Name: guacd.EnableFontSmoothing,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableFullWindowDrag]) == 0 {
@ -172,7 +210,9 @@ func InitProperties() {
Name: guacd.EnableFullWindowDrag,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableDesktopComposition]) == 0 {
@ -180,7 +220,9 @@ func InitProperties() {
Name: guacd.EnableDesktopComposition,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.EnableMenuAnimations]) == 0 {
@ -188,7 +230,9 @@ func InitProperties() {
Name: guacd.EnableMenuAnimations,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.DisableBitmapCaching]) == 0 {
@ -196,7 +240,9 @@ func InitProperties() {
Name: guacd.DisableBitmapCaching,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.DisableOffscreenCaching]) == 0 {
@ -204,7 +250,9 @@ func InitProperties() {
Name: guacd.DisableOffscreenCaching,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
if len(propertyMap[guacd.DisableGlyphCaching]) == 0 {
@ -212,6 +260,9 @@ func InitProperties() {
Name: guacd.DisableGlyphCaching,
Value: "false",
}
_ = model.CreateNewProperty(&property)
if err := model.CreateNewProperty(&property); err != nil {
return err
}
}
return nil
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/utils"
)
@ -27,12 +27,12 @@ func (r *Asset) TableName() string {
}
func FindAllAsset() (o []Asset, err error) {
err = config.DB.Find(&o).Error
err = global.DB.Find(&o).Error
return
}
func FindAssetByConditions(protocol string) (o []Asset, err error) {
db := config.DB
db := global.DB
if len(protocol) > 0 {
db = db.Where("protocol = ?", protocol)
@ -42,7 +42,7 @@ func FindAssetByConditions(protocol string) (o []Asset, err error) {
}
func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, total int64, err error) {
db := config.DB
db := global.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
}
@ -60,27 +60,27 @@ func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, t
}
func CreateNewAsset(o *Asset) (err error) {
if err = config.DB.Create(o).Error; err != nil {
if err = global.DB.Create(o).Error; err != nil {
return err
}
return nil
}
func FindAssetById(id string) (o Asset, err error) {
err = config.DB.Where("id = ?", id).First(&o).Error
err = global.DB.Where("id = ?", id).First(&o).Error
return
}
func UpdateAssetById(o *Asset, id string) {
o.ID = id
config.DB.Updates(o)
global.DB.Updates(o)
}
func DeleteAssetById(id string) {
config.DB.Where("id = ?", id).Delete(&Asset{})
global.DB.Where("id = ?", id).Delete(&Asset{})
}
func CountAsset() (total int64, err error) {
err = config.DB.Find(&Asset{}).Count(&total).Error
err = global.DB.Find(&Asset{}).Count(&total).Error
return
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/utils"
)
@ -18,7 +18,7 @@ func (r *Command) TableName() string {
func FindPageCommand(pageIndex, pageSize int, name, content string) (o []Command, total int64, err error) {
db := config.DB
db := global.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
}
@ -35,22 +35,22 @@ func FindPageCommand(pageIndex, pageSize int, name, content string) (o []Command
}
func CreateNewCommand(o *Command) (err error) {
if err = config.DB.Create(o).Error; err != nil {
if err = global.DB.Create(o).Error; err != nil {
return err
}
return nil
}
func FindCommandById(id string) (o Command, err error) {
err = config.DB.Where("id = ?", id).First(&o).Error
err = global.DB.Where("id = ?", id).First(&o).Error
return
}
func UpdateCommandById(o *Command, id string) {
o.ID = id
config.DB.Updates(o)
global.DB.Updates(o)
}
func DeleteCommandById(id string) {
config.DB.Where("id = ?", id).Delete(&Command{})
global.DB.Where("id = ?", id).Delete(&Command{})
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/utils"
)
@ -18,12 +18,12 @@ func (r *Credential) TableName() string {
}
func FindAllCredential() (o []Credential, err error) {
err = config.DB.Find(&o).Error
err = global.DB.Find(&o).Error
return
}
func FindPageCredential(pageIndex, pageSize int, name string) (o []Credential, total int64, err error) {
db := config.DB
db := global.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
}
@ -36,7 +36,7 @@ func FindPageCredential(pageIndex, pageSize int, name string) (o []Credential, t
}
func CreateNewCredential(o *Credential) (err error) {
if err = config.DB.Create(o).Error; err != nil {
if err = global.DB.Create(o).Error; err != nil {
return err
}
return nil
@ -44,20 +44,20 @@ func CreateNewCredential(o *Credential) (err error) {
func FindCredentialById(id string) (o Credential, err error) {
err = config.DB.Where("id = ?", id).First(&o).Error
err = global.DB.Where("id = ?", id).First(&o).Error
return
}
func UpdateCredentialById(o *Credential, id string) {
o.ID = id
config.DB.Updates(o)
global.DB.Updates(o)
}
func DeleteCredentialById(id string) {
config.DB.Where("id = ?", id).Delete(&Credential{})
global.DB.Where("id = ?", id).Delete(&Credential{})
}
func CountCredential() (total int64, err error) {
err = config.DB.Find(&Credential{}).Count(&total).Error
err = global.DB.Find(&Credential{}).Count(&total).Error
return
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
)
type Num struct {
@ -13,13 +13,13 @@ func (r *Num) TableName() string {
}
func FindAllTemp() (o []Num) {
if config.DB.Find(&o).Error != nil {
if global.DB.Find(&o).Error != nil {
return nil
}
return
}
func CreateNewTemp(o *Num) (err error) {
err = config.DB.Create(o).Error
err = global.DB.Create(o).Error
return
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/guacd"
)
@ -15,24 +15,24 @@ func (r *Property) TableName() string {
}
func FindAllProperties() (o []Property) {
if config.DB.Find(&o).Error != nil {
if global.DB.Find(&o).Error != nil {
return nil
}
return
}
func CreateNewProperty(o *Property) (err error) {
err = config.DB.Create(o).Error
err = global.DB.Create(o).Error
return
}
func UpdatePropertyByName(o *Property, name string) {
o.Name = name
config.DB.Updates(o)
global.DB.Updates(o)
}
func FindPropertyByName(name string) (o Property, err error) {
err = config.DB.Where("name = ?", name).First(&o).Error
err = global.DB.Where("name = ?", name).First(&o).Error
return
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/utils"
"time"
)
@ -57,7 +57,7 @@ type SessionVo struct {
func FindPageSession(pageIndex, pageSize int, status, userId, clientIp, assetId, protocol string) (results []SessionVo, total int64, err error) {
db := config.DB
db := global.DB
var params []interface{}
params = append(params, status)
@ -103,40 +103,40 @@ func FindPageSession(pageIndex, pageSize int, status, userId, clientIp, assetId,
}
func FindSessionByStatus(status string) (o []Session, err error) {
err = config.DB.Where("status = ?", status).Find(&o).Error
err = global.DB.Where("status = ?", status).Find(&o).Error
return
}
func CreateNewSession(o *Session) (err error) {
err = config.DB.Create(o).Error
err = global.DB.Create(o).Error
return
}
func FindSessionById(id string) (o Session, err error) {
err = config.DB.Where("id = ?", id).First(&o).Error
err = global.DB.Where("id = ?", id).First(&o).Error
return
}
func FindSessionByConnectionId(connectionId string) (o Session, err error) {
err = config.DB.Where("connection_id = ?", connectionId).First(&o).Error
err = global.DB.Where("connection_id = ?", connectionId).First(&o).Error
return
}
func UpdateSessionById(o *Session, id string) {
o.ID = id
config.DB.Updates(o)
global.DB.Updates(o)
}
func DeleteSessionById(id string) {
config.DB.Where("id = ?", id).Delete(&Session{})
global.DB.Where("id = ?", id).Delete(&Session{})
}
func DeleteSessionByStatus(status string) {
config.DB.Where("status = ?", status).Delete(&Session{})
global.DB.Where("status = ?", status).Delete(&Session{})
}
func CountOnlineSession() (total int64, err error) {
err = config.DB.Where("status = ?", Connected).Find(&Session{}).Count(&total).Error
err = global.DB.Where("status = ?", Connected).Find(&Session{}).Count(&total).Error
return
}
@ -155,7 +155,7 @@ func CountSessionByDay(day int) (results []D, err error) {
for i := range protocols {
var result []D
err = config.DB.Raw(sql, day, protocols[i], day).Scan(&result).Error
err = global.DB.Raw(sql, day, protocols[i], day).Scan(&result).Error
if err != nil {
return nil, err
}

View File

@ -1,7 +1,7 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/global"
"next-terminal/pkg/utils"
"reflect"
)
@ -25,7 +25,7 @@ func (r *User) IsEmpty() bool {
}
func FindAllUser() (o []User) {
if config.DB.Find(&o).Error != nil {
if global.DB.Find(&o).Error != nil {
return nil
}
return
@ -33,7 +33,7 @@ func FindAllUser() (o []User) {
func FindPageUser(pageIndex, pageSize int, username, nickname string) (o []User, total int64, err error) {
db := config.DB
db := global.DB
if len(username) > 0 {
db = db.Where("username like ?", "%"+username+"%")
}
@ -50,30 +50,30 @@ func FindPageUser(pageIndex, pageSize int, username, nickname string) (o []User,
}
func CreateNewUser(o *User) (err error) {
err = config.DB.Create(o).Error
err = global.DB.Create(o).Error
return
}
func FindUserById(id string) (o User, err error) {
err = config.DB.Where("id = ?", id).First(&o).Error
err = global.DB.Where("id = ?", id).First(&o).Error
return
}
func FindUserByUsername(username string) (o User, err error) {
err = config.DB.Where("username = ?", username).First(&o).Error
err = global.DB.Where("username = ?", username).First(&o).Error
return
}
func UpdateUserById(o *User, id string) {
o.ID = id
config.DB.Updates(o)
global.DB.Updates(o)
}
func DeleteUserById(id string) {
config.DB.Where("id = ?", id).Delete(&User{})
global.DB.Where("id = ?", id).Delete(&User{})
}
func CountUser() (total int64, err error) {
err = config.DB.Find(&User{}).Count(&total).Error
err = global.DB.Find(&User{}).Count(&total).Error
return
}

View File

@ -6,6 +6,7 @@ import (
"github.com/gofrs/uuid"
"golang.org/x/crypto/bcrypt"
"net"
"os"
"strconv"
"time"
)
@ -79,3 +80,29 @@ func Tcping(ip string, port int) bool {
defer conn.Close()
return true
}
// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
// 判断所给路径是否为文件夹
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// 判断所给路径是否为文件
func IsFile(path string) bool {
return !IsDir(path)
}