Initial commit

This commit is contained in:
dushixiang
2020-12-20 21:19:11 +08:00
commit e7f2773c77
77 changed files with 27866 additions and 0 deletions

86
pkg/model/asset.go Normal file
View File

@ -0,0 +1,86 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/utils"
)
type Asset struct {
ID string `gorm:"primary_key " json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
Protocol string `json:"protocol"`
Port int `json:"port"`
AccountType string `json:"accountType"`
Username string `json:"username"`
Password string `json:"password"`
CredentialId string `json:"credentialId"`
PrivateKey string `json:"privateKey"`
Passphrase string `json:"passphrase"`
Description string `json:"description"`
Active bool `json:"active"`
Created utils.JsonTime `json:"created"`
}
func (r *Asset) TableName() string {
return "assets"
}
func FindAllAsset() (o []Asset, err error) {
err = config.DB.Find(&o).Error
return
}
func FindAssetByConditions(protocol string) (o []Asset, err error) {
db := config.DB
if len(protocol) > 0 {
db = db.Where("protocol = ?", protocol)
}
err = db.Find(&o).Error
return
}
func FindPageAsset(pageIndex, pageSize int, name, protocol string) (o []Asset, total int64, err error) {
db := config.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
}
if len(protocol) > 0 {
db = db.Where("protocol = ?", protocol)
}
err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error
if o == nil {
o = make([]Asset, 0)
}
return
}
func CreateNewAsset(o *Asset) (err error) {
if err = config.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
return
}
func UpdateAssetById(o *Asset, id string) {
o.ID = id
config.DB.Updates(o)
}
func DeleteAssetById(id string) {
config.DB.Where("id = ?", id).Delete(&Asset{})
}
func CountAsset() (total int64, err error) {
err = config.DB.Find(&Asset{}).Count(&total).Error
return
}

56
pkg/model/command.go Normal file
View File

@ -0,0 +1,56 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/utils"
)
type Command struct {
ID string `gorm:"primary_key" json:"id"`
Name string `json:"name"`
Content string `json:"content"`
Created utils.JsonTime `json:"created"`
}
func (r *Command) TableName() string {
return "commands"
}
func FindPageCommand(pageIndex, pageSize int, name, content string) (o []Command, total int64, err error) {
db := config.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
}
if len(content) > 0 {
db = db.Where("content like ?", "%"+content+"%")
}
err = db.Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error
if o == nil {
o = make([]Command, 0)
}
return
}
func CreateNewCommand(o *Command) (err error) {
if err = config.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
return
}
func UpdateCommandById(o *Command, id string) {
o.ID = id
config.DB.Updates(o)
}
func DeleteCommandById(id string) {
config.DB.Where("id = ?", id).Delete(&Command{})
}

63
pkg/model/credential.go Normal file
View File

@ -0,0 +1,63 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/utils"
)
type Credential struct {
ID string `gorm:"primary_key" json:"id"`
Name string `json:"name"`
Username string `json:"username"`
Password string `json:"password"`
Created utils.JsonTime `json:"created"`
}
func (r *Credential) TableName() string {
return "credentials"
}
func FindAllCredential() (o []Credential, err error) {
err = config.DB.Find(&o).Error
return
}
func FindPageCredential(pageIndex, pageSize int, name string) (o []Credential, total int64, err error) {
db := config.DB
if len(name) > 0 {
db = db.Where("name like ?", "%"+name+"%")
}
err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error
if o == nil {
o = make([]Credential, 0)
}
return
}
func CreateNewCredential(o *Credential) (err error) {
if err = config.DB.Create(o).Error; err != nil {
return err
}
return nil
}
func FindCredentialById(id string) (o Credential, err error) {
err = config.DB.Where("id = ?", id).First(&o).Error
return
}
func UpdateCredentialById(o *Credential, id string) {
o.ID = id
config.DB.Updates(o)
}
func DeleteCredentialById(id string) {
config.DB.Where("id = ?", id).Delete(&Credential{})
}
func CountCredential() (total int64, err error) {
err = config.DB.Find(&Credential{}).Count(&total).Error
return
}

78
pkg/model/property.go Normal file
View File

@ -0,0 +1,78 @@
package model
import (
"next-terminal/pkg/config"
"errors"
)
const (
GuacdHost = "host"
GuacdPort = "port"
GuacdFontName = "font-name"
GuacdFontSize = "font-size"
GuacdColorScheme = "color-scheme"
GuacdEnableSftp = "enable-sftp"
GuacdEnableDrive = "enable-drive"
GuacdDriveName = "drive-name"
GuacdDrivePath = "drive-path"
GuacdEnableWallpaper = "enable-wallpaper"
GuacdEnableTheming = "enable-theming"
GuacdEnableFontSmoothing = "enable-font-smoothing"
GuacdEnableFullWindowDrag = "enable-full-window-drag"
GuacdEnableDesktopComposition = "enable-desktop-composition"
GuacdEnableMenuAnimations = "enable-menu-animations"
GuacdDisableBitmapCaching = "disable-bitmap-caching"
GuacdDisableOffscreenCaching = "disable-offscreen-caching"
GuacdDisableGlyphCaching = "disable-glyph-caching"
)
type Property struct {
Name string `gorm:"primary_key" json:"name"`
Value string `json:"value"`
}
func (r *Property) TableName() string {
return "properties"
}
func FindAllProperties() (o []Property) {
if config.DB.Find(&o).Error != nil {
return nil
}
return
}
func CreateNewProperty(o *Property) (err error) {
err = config.DB.Create(o).Error
return
}
func UpdatePropertyByName(o *Property, name string) {
o.Name = name
config.DB.Updates(o)
}
func FindPropertyByName(name string) (o Property, err error) {
err = config.DB.Where("name = ?", name).First(&o).Error
return
}
func FindAllPropertiesMap() map[string]string {
properties := FindAllProperties()
propertyMap := make(map[string]string)
for i := range properties {
propertyMap[properties[i].Name] = properties[i].Value
}
return propertyMap
}
func GetDrivePath() (string, error) {
propertiesMap := FindAllPropertiesMap()
drivePath := propertiesMap[GuacdDrivePath]
if len(drivePath) == 0 {
return "", errors.New("获取RDP挂载目录失败")
}
return drivePath, nil
}

138
pkg/model/session.go Normal file
View File

@ -0,0 +1,138 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/utils"
)
const (
Connected = "connected"
Disconnected = "disconnected"
NoConnect = "no_connect"
)
type Session struct {
ID string `gorm:"primary_key" json:"id"`
Protocol string `json:"protocol"`
IP string `json:"ip"`
Port int `json:"port"`
ConnectionId string `json:"connectionId"`
AssetId string `json:"assetId"`
Username string `json:"username"`
Password string `json:"password"`
Creator string `json:"creator"`
ClientIP string `json:"clientIp"`
Width int `json:"width"`
Height int `json:"height"`
Status string `json:"status"`
ConnectedTime utils.JsonTime `json:"connectedTime"`
DisconnectedTime utils.JsonTime `json:"disconnectedTime"`
}
func (r *Session) TableName() string {
return "sessions"
}
type SessionVo struct {
ID string `json:"id"`
Protocol string `json:"protocol"`
IP string `json:"ip"`
Port int `json:"port"`
Username string `json:"username"`
ConnectionId string `json:"connectionId"`
AssetId string `json:"assetId"`
Creator string `json:"creator"`
ClientIP string `json:"clientIp"`
Width int `json:"width"`
Height int `json:"height"`
Status string `json:"status"`
ConnectedTime utils.JsonTime `json:"connectedTime"`
DisconnectedTime utils.JsonTime `json:"disconnectedTime"`
AssetName string `json:"assetName"`
CreatorName string `json:"creatorName"`
}
func FindPageSession(pageIndex, pageSize int, status, userId, clientIp, assetId, protocol string) (results []SessionVo, total int64, err error) {
db := config.DB
var params []interface{}
params = append(params, status)
itemSql := "SELECT s.id, s.protocol, s.connection_id, s.asset_id, s.creator, s.client_ip, s.width, s.height, s.ip, s.port, s.username, s.status, s.connected_time, s.disconnected_time, a.name AS asset_name, u.nickname AS creator_name FROM sessions s LEFT JOIN assets a ON s.asset_id = a.id LEFT JOIN users u ON s.creator = u.id WHERE s.STATUS = ? "
countSql := "select count(*) from sessions as s where s.status = ? "
if len(userId) > 0 {
itemSql += " and s.creator = ?"
countSql += " and s.creator = ?"
params = append(params, userId)
}
if len(clientIp) > 0 {
itemSql += " and s.client_ip like ?"
countSql += " and s.client_ip like ?"
params = append(params, "%"+clientIp+"%")
}
if len(assetId) > 0 {
itemSql += " and s.asset_id = ?"
countSql += " and s.asset_id = ?"
params = append(params, assetId)
}
if len(protocol) > 0 {
itemSql += " and s.protocol = ?"
countSql += " and s.protocol = ?"
params = append(params, protocol)
}
params = append(params, (pageIndex-1)*pageSize, pageSize)
itemSql += " order by s.connected_time desc LIMIT ?, ?"
db.Raw(countSql, params...).Scan(&total)
err = db.Raw(itemSql, params...).Scan(&results).Error
if results == nil {
results = make([]SessionVo, 0)
}
return
}
func FindSessionByStatus(status string) (o []Session, err error) {
err = config.DB.Where("status = ?", status).Find(&o).Error
return
}
func CreateNewSession(o *Session) (err error) {
err = config.DB.Create(o).Error
return
}
func FindSessionById(id string) (o Session, err error) {
err = config.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
return
}
func UpdateSessionById(o *Session, id string) {
o.ID = id
config.DB.Updates(o)
}
func DeleteSessionById(id string) {
config.DB.Where("id = ?", id).Delete(&Session{})
}
func DeleteSessionByStatus(status string) {
config.DB.Where("status = ?", status).Delete(&Session{})
}
func CountOnlineSession() (total int64, err error) {
err = config.DB.Where("status = ?", Connected).Find(&Session{}).Count(&total).Error
return
}

79
pkg/model/user.go Normal file
View File

@ -0,0 +1,79 @@
package model
import (
"next-terminal/pkg/config"
"next-terminal/pkg/utils"
"reflect"
)
type User struct {
ID string `gorm:"primary_key" json:"id"`
Username string `json:"username"`
Password string `json:"password"`
Nickname string `json:"nickname"`
Online bool `json:"online"`
Enabled bool `json:"enabled"`
Created utils.JsonTime `json:"created"`
}
func (r *User) TableName() string {
return "users"
}
func (r *User) IsEmpty() bool {
return reflect.DeepEqual(r, User{})
}
func FindAllUser() (o []User) {
if config.DB.Find(&o).Error != nil {
return nil
}
return
}
func FindPageUser(pageIndex, pageSize int, username, nickname string) (o []User, total int64, err error) {
db := config.DB
if len(username) > 0 {
db = db.Where("username like ?", "%"+username+"%")
}
if len(nickname) > 0 {
db = db.Where("nickname like ?", "%"+nickname+"%")
}
err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error
if o == nil {
o = make([]User, 0)
}
return
}
func CreateNewUser(o *User) (err error) {
err = config.DB.Create(o).Error
return
}
func FindUserById(id string) (o User, err error) {
err = config.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
return
}
func UpdateUserById(o *User, id string) {
o.ID = id
config.DB.Updates(o)
}
func DeleteUserById(id string) {
config.DB.Where("id = ?", id).Delete(&User{})
}
func CountUser() (total int64, err error) {
err = config.DB.Find(&User{}).Count(&total).Error
return
}