1f7c491048
- 实现文件系统日志(FilesystemLog)记录文件管理器操作 - 实现操作日志(OperationLog)记录用户操作行为 - 实现数据库SQL日志(DatabaseSQLLog)模型和API - 实现SSH会话命令记录(SessionCommand)含命令输出和风险等级 - 添加IP提取服务支持X-Real-IP和X-Forwarded-For - 添加日志自动清理功能 - 修复ProFormSwitch required验证问题 - 修复设置页面默认值问题 - 修复文件上传错误检测逻辑 - 修复资产树key前缀问题 - 添加VNC/RDP设置默认值 - 修复文件管理标题翻译
63 lines
2.3 KiB
Go
63 lines
2.3 KiB
Go
package env
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"next-terminal/server/config"
|
|
"next-terminal/server/model"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/logger"
|
|
)
|
|
|
|
func setupDB() *gorm.DB {
|
|
|
|
var logMode logger.Interface
|
|
if config.GlobalCfg.Debug {
|
|
logMode = logger.Default.LogMode(logger.Info)
|
|
} else {
|
|
logMode = logger.Default.LogMode(logger.Silent)
|
|
}
|
|
|
|
fmt.Printf("当前数据库模式为:%v\n", config.GlobalCfg.DB)
|
|
var err error
|
|
var db *gorm.DB
|
|
if config.GlobalCfg.DB == "mysql" {
|
|
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=60s",
|
|
config.GlobalCfg.Mysql.Username,
|
|
config.GlobalCfg.Mysql.Password,
|
|
config.GlobalCfg.Mysql.Hostname,
|
|
config.GlobalCfg.Mysql.Port,
|
|
config.GlobalCfg.Mysql.Database,
|
|
)
|
|
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
|
|
Logger: logMode,
|
|
})
|
|
} else {
|
|
dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc", config.GlobalCfg.Sqlite.File)
|
|
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{
|
|
Logger: logMode,
|
|
SkipDefaultTransaction: true,
|
|
})
|
|
}
|
|
|
|
if err != nil {
|
|
panic(fmt.Errorf("连接数据库异常: %v", err.Error()))
|
|
}
|
|
|
|
if err := db.AutoMigrate(&model.User{}, &model.Asset{}, &model.AssetAttribute{}, &model.Session{}, &model.Command{},
|
|
&model.Credential{}, &model.Property{}, &model.UserGroup{}, &model.UserGroupMember{},
|
|
&model.LoginLog{}, &model.Job{}, &model.JobLog{}, &model.AccessSecurity{}, &model.AccessGateway{},
|
|
&model.Storage{}, &model.Strategy{},
|
|
&model.AccessToken{}, &model.ShareSession{},
|
|
&model.Role{}, &model.RoleMenuRef{}, &model.UserRoleRef{},
|
|
&model.LoginPolicy{}, &model.LoginPolicyUserRef{}, &model.TimePeriod{},
|
|
&model.StorageLog{}, &model.Authorised{}, &model.Logo{}, &model.AssetGroup{},
|
|
&model.AgentGateway{}, &model.SshGateway{}, &model.GatewayGroup{}, &model.Website{}, &model.Certificate{}, &model.Snippet{}, &model.SessionAudit{}, &model.Department{}, &model.UserDepartmentRef{}, &model.DatabaseAsset{}, &model.CommandFilter{}, &model.CommandFilterRule{}, &model.AuthorisedAsset{}, &model.AuthorisedDatabaseAsset{}, &model.AuthorisedWebsite{}, &model.WebsiteGroup{}, &model.AccessLog{}, &model.FilesystemLog{}, &model.OperationLog{}, &model.DatabaseSQLLog{}, &model.SessionCommand{}); err != nil {
|
|
panic(fmt.Errorf("初始化数据库表结构异常: %v", err.Error()))
|
|
}
|
|
return db
|
|
}
|