This commit is contained in:
孟帅
2023-04-05 12:29:33 +08:00
parent c709073ed7
commit 075a12ce7e
27 changed files with 703 additions and 168 deletions

View File

@@ -3,11 +3,12 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package consts
const (
UploadDriveLocal = "local" // 本地驱动
UploadDriveOss = "oss" // 阿里云oss
UploadDriveUCloud = "ucloud" // ucloud对象存储
UploadDriveCos = "cos" // 腾讯云cos
UploadDriveOss = "oss" // 阿里云oss
UploadDriveQiNiu = "qiniu" // 七牛云对象存储
)

View File

@@ -7,5 +7,5 @@ package consts
// VersionApp HotGo版本
const (
VersionApp = "2.4.4"
VersionApp = "2.4.9"
)

View File

@@ -10,11 +10,11 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/process"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/process"
"hotgo/internal/consts"
"hotgo/internal/model"
"hotgo/internal/service"
@@ -69,7 +69,7 @@ func (c *cMonitor) RunInfo(client *websocket.Client, req *websocket.WRequest) {
// GO运行信息
"goName": "Golang",
"version": runtime.Version(),
"startTime": meta.STartTime,
"startTime": gtime.New(meta.STartTime),
"runTime": gtime.Now().Timestamp() - meta.STartTime,
"rootPath": runtime.GOROOT(),
"pwd": pwd,
@@ -91,7 +91,6 @@ func (c *cMonitor) RunInfo(client *websocket.Client, req *websocket.WRequest) {
// Trends 实时数据
func (c *cMonitor) Trends(client *websocket.Client, req *websocket.WRequest) {
type NetC struct {
Time *gtime.Time `json:"time"`
BytesSent string `json:"bytesSent"` // number of bytes sent
@@ -101,19 +100,38 @@ func (c *cMonitor) Trends(client *websocket.Client, req *websocket.WRequest) {
}
var (
mCpu, _ = cpu.Info()
mCpuUsed float64
mMem, _ = mem.VirtualMemory()
mMemUsed float64
mDisk, _ = disk.Usage("/")
mProcess, _ = process.Pids()
mLoadAvg = new(model.LoadAvgStats)
data = g.Map{}
monitorHeads []MonitorHead
nets []NetC
meta = service.AdminMonitor().GetMeta(client.Context())
mCpu, cpuErr = cpu.Info()
mCpuUsed float64
mMem, memErr = mem.VirtualMemory()
mMemUsed float64
mDisk, diskErr = disk.Usage("/")
mProcess, ProcessErr = process.Pids()
mLoadAvg = new(model.LoadAvgStats)
data = g.Map{}
monitorHeads []MonitorHead
nets []NetC
meta = service.AdminMonitor().GetMeta(client.Context())
)
if cpuErr != nil {
g.Log().Infof(client.Context(), "read CPU info fail:%+v", cpuErr)
mCpu = []cpu.InfoStat{{VendorID: "", ModelName: ""}}
}
if memErr != nil {
g.Log().Infof(client.Context(), "read mem info fail:%+v", memErr)
mMem = new(mem.VirtualMemoryStat)
}
if diskErr != nil {
g.Log().Infof(client.Context(), "read disk info fail:%+v", diskErr)
mDisk = new(disk.UsageStat)
}
if ProcessErr != nil {
g.Log().Infof(client.Context(), "read process.Pids fail:%+v", ProcessErr)
}
// cpu使用率
cu, err := cpu.Percent(time.Second, false)
if err == nil {

View File

@@ -85,18 +85,23 @@ func (server *Server) onServerLogin(args ...interface{}) {
}
}
// 检查是否存在多地登录,如果连接超出上限,直接将所有已连接断开,然后在吧新的连接加入进来
// 检查是否存在多地登录如果连接超出上限,直接将所有已连接断开
clients := server.getAppIdClients(models.Appid)
online := len(clients) + 1
if online > models.OnlineLimit {
online = 1
online = 0
res2 := new(msgin.ResponseServerLogin)
res2.Code = 8
res2.Message = "授权登录端超出上限请联系管理员"
res2.Message = "授权登录端超出上限已进行记录。请立即终止操作。如有疑问请联系管理员"
for _, client := range clients {
server.Write(client.Conn, res2)
client.Conn.Close()
}
// 当前连接也踢掉
server.Write(conn, res2)
conn.Close()
return
}
server.mutexConns.Lock()

View File

@@ -5,8 +5,8 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcron"
"github.com/gogf/gf/v2/os/gtime"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/v3/load"
"github.com/shirou/gopsutil/v3/net"
"hotgo/internal/library/location"
"hotgo/internal/model"
"hotgo/internal/service"

View File

@@ -8,6 +8,7 @@ package common
import (
"context"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
@@ -15,6 +16,9 @@ import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/grand"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
"github.com/tencentyun/cos-go-sdk-v5"
ufile "github.com/ufilesdk-dev/ufile-gosdk"
"hotgo/internal/consts"
"hotgo/internal/dao"
@@ -24,8 +28,10 @@ import (
"hotgo/utility/encrypt"
f "hotgo/utility/file"
"hotgo/utility/format"
"hotgo/utility/url"
utilityurl "hotgo/utility/url"
"hotgo/utility/validate"
"net/http"
"net/url"
"strconv"
"strings"
"time"
@@ -68,6 +74,12 @@ func (s *sCommonUpload) UploadFile(ctx context.Context, file *ghttp.UploadFile)
return s.UploadLocal(ctx, conf, file, meta)
case consts.UploadDriveUCloud:
return s.UploadUCloud(ctx, conf, file, meta)
case consts.UploadDriveCos:
return s.UploadCOS(ctx, conf, file, meta)
case consts.UploadDriveOss:
return s.UploadOSS(ctx, conf, file, meta)
case consts.UploadDriveQiNiu:
return s.UploadQiNiu(ctx, conf, file, meta)
default:
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
}
@@ -103,6 +115,12 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
return s.UploadLocal(ctx, conf, file, meta)
case consts.UploadDriveUCloud:
return s.UploadUCloud(ctx, conf, file, meta)
case consts.UploadDriveCos:
return s.UploadCOS(ctx, conf, file, meta)
case consts.UploadDriveOss:
return s.UploadOSS(ctx, conf, file, meta)
case consts.UploadDriveQiNiu:
return s.UploadQiNiu(ctx, conf, file, meta)
default:
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
}
@@ -110,13 +128,7 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
// UploadLocal 上传本地
func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
result, err = dao.SysAttachment.GetMd5File(ctx, meta.Md5)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
}
if result != nil {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
@@ -159,13 +171,7 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfi
// UploadUCloud 上传UCloud对象存储
func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
result, err = dao.SysAttachment.GetMd5File(ctx, meta.Md5)
if err != nil {
err = gerror.Wrap(err, consts.ErrorORM)
return
}
if result != nil {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
@@ -218,6 +224,171 @@ func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConf
return
}
// UploadCOS 上传腾讯云对象存储
func (s *sCommonUpload) UploadCOS(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
if conf.CosPath == "" {
err = gerror.New("COS存储驱动必须配置存储路径!")
return
}
nowDate := time.Now().Format("2006-01-02")
fileName := gfile.Basename(file.Filename)
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
fileName = fileName + gfile.Ext(file.Filename)
fullPath := conf.CosPath + nowDate + "/" + fileName
// 流式上传本地小文件
f2, err := file.Open()
defer func() {
_ = f2.Close()
}()
if err != nil {
return nil, err
}
u, _ := url.Parse(conf.CosBucketURL)
b := &cos.BaseURL{BucketURL: u}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: conf.CosSecretId,
SecretKey: conf.CosSecretKey,
},
})
_, err = c.Object.Put(ctx, fullPath, f2, nil)
if err != nil {
return nil, err
}
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveCos)
if err != nil {
return nil, err
}
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
result = &sysin.AttachmentListModel{
SysAttachment: *attachment,
SizeFormat: format.FileSize(attachment.Size),
}
return
}
// UploadOSS 上传阿里云云对象存储
func (s *sCommonUpload) UploadOSS(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
if conf.OssPath == "" {
err = gerror.New("OSS存储驱动必须配置存储路径!")
return
}
nowDate := time.Now().Format("2006-01-02")
fileName := gfile.Basename(file.Filename)
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
fileName = fileName + gfile.Ext(file.Filename)
fullPath := conf.OssPath + nowDate + "/" + fileName
// 流式上传本地小文件
f2, err := file.Open()
defer func() {
_ = f2.Close()
}()
if err != nil {
return nil, err
}
client, err := oss.New(conf.OssEndpoint, conf.OssSecretId, conf.OssSecretKey)
if err != nil {
return nil, err
}
bucket, err := client.Bucket(conf.OssBucket)
if err != nil {
return nil, err
}
if err = bucket.PutObject(fullPath, f2); err != nil {
return nil, err
}
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveOss)
if err != nil {
return nil, err
}
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
result = &sysin.AttachmentListModel{
SysAttachment: *attachment,
SizeFormat: format.FileSize(attachment.Size),
}
return
}
// UploadQiNiu 上传七牛云对象存储
func (s *sCommonUpload) UploadQiNiu(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
return
}
if conf.QiNiuPath == "" {
err = gerror.New("七牛云存储驱动必须配置存储路径!")
return
}
nowDate := time.Now().Format("2006-01-02")
fileName := gfile.Basename(file.Filename)
fileName = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
fileName = fileName + gfile.Ext(file.Filename)
fullPath := conf.QiNiuPath + nowDate + "/" + fileName
// 流式上传本地小文件
f2, err := file.Open()
defer func() {
_ = f2.Close()
}()
if err != nil {
return nil, err
}
putPolicy := storage.PutPolicy{
Scope: conf.QiNiuBucket,
}
token := putPolicy.UploadToken(qbox.NewMac(conf.QiNiuAccessKey, conf.QiNiuSecretKey))
cfg := storage.Config{}
// 是否使用https域名
cfg.UseHTTPS = true
// 上传是否使用CDN上传加速
cfg.UseCdnDomains = false
// 空间对应的机房
cfg.Region, err = storage.GetRegion(conf.QiNiuAccessKey, conf.QiNiuBucket)
if err != nil {
return
}
if err = storage.NewFormUploader(&cfg).Put(ctx, &storage.PutRet{}, token, fullPath, f2, file.Size, &storage.PutExtra{}); err != nil {
return
}
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveQiNiu)
if err != nil {
return nil, err
}
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
result = &sysin.AttachmentListModel{
SysAttachment: *attachment,
SizeFormat: format.FileSize(attachment.Size),
}
return
}
// LastUrl 根据驱动获取最终文件访问地址
func (s *sCommonUpload) LastUrl(ctx context.Context, conf *model.UploadConfig, fullPath, drive string) string {
if validate.IsURL(fullPath) {
@@ -226,14 +397,30 @@ func (s *sCommonUpload) LastUrl(ctx context.Context, conf *model.UploadConfig, f
switch drive {
case consts.UploadDriveLocal:
return url.GetAddr(ctx) + "/" + fullPath
return utilityurl.GetAddr(ctx) + "/" + fullPath
case consts.UploadDriveUCloud:
return conf.UCloudEndpoint + "/" + fullPath
case consts.UploadDriveCos:
return conf.CosBucketURL + "/" + fullPath
case consts.UploadDriveOss:
return conf.OssBucketURL + "/" + fullPath
case consts.UploadDriveQiNiu:
return conf.QiNiuDomain + "/" + fullPath
default:
return fullPath
}
}
// HasFile 文件是否存在
func (s *sCommonUpload) HasFile(ctx context.Context, md5 string) (bool, error) {
result, err := dao.SysAttachment.GetMd5File(ctx, md5)
if err != nil {
return false, err
}
return result != nil, nil
}
// fileMeta 上传文件元数据
func (s *sCommonUpload) fileMeta(file *ghttp.UploadFile) (meta *sysin.UploadFileMeta, err error) {
meta = new(sysin.UploadFileMeta)

View File

@@ -18,10 +18,7 @@ import (
// ApiAuth API鉴权中间件
func (s *sMiddleware) ApiAuth(r *ghttp.Request) {
var (
ctx = r.Context()
)
var ctx = r.Context()
// 替换掉模块前缀
routerPrefix := g.Cfg().MustGet(ctx, "router.api.prefix", "/api")

View File

@@ -97,9 +97,7 @@ func (s *sMiddleware) DemoLimit(r *ghttp.Request) {
// Addon 插件中间件
func (s *sMiddleware) Addon(r *ghttp.Request) {
var (
ctx = r.Context()
)
var ctx = r.Context()
if contexts.Get(ctx).Module == "" {
g.Log().Warning(ctx, "application module is not initialized.")

View File

@@ -16,7 +16,7 @@ func (s *sMiddleware) Blacklist(r *ghttp.Request) {
response.JsonExit(r, gcode.CodeServerBusy.Code(), "请求异常,已被封禁,如有疑问请联系管理员!")
}
} else {
g.Log().Warningf(r.Context(), "blacklists uninitialized")
g.Log().Infof(r.Context(), "blacklists uninitialized")
}
r.Middleware.Next()

View File

@@ -18,10 +18,7 @@ import (
// WebSocketToken 检查ws连接token
func (s *sMiddleware) WebSocketToken(r *ghttp.Request) {
var (
ctx = r.Context()
)
var ctx = r.Context()
// 替换掉模块前缀
routerPrefix := g.Cfg().MustGet(ctx, "router.ws.prefix", "/socket")

View File

@@ -255,7 +255,7 @@ func (s *sSysLog) Delete(ctx context.Context, in sysin.LogDeleteInp) (err error)
// List 列表
func (s *sSysLog) List(ctx context.Context, in sysin.LogListInp) (list []*sysin.LogListModel, totalCount int, err error) {
mod := dao.SysLog.Ctx(ctx)
mod := dao.SysLog.Ctx(ctx).FieldsEx("get_data", "header_data", "post_data")
// 访问路径
if in.Url != "" {

View File

@@ -69,6 +69,9 @@ func (s *sTCPAuth) Stop(ctx context.Context) {
// IsLogin 是否已登录认证
func (s *sTCPAuth) IsLogin() bool {
if s.client == nil {
return false
}
return s.client.IsLogin
}

View File

@@ -46,8 +46,8 @@ type CashConfig struct {
// UploadConfig 上传配置
type UploadConfig struct {
Drive string `json:"uploadDrive"`
// 基本配置
// 通用配置
Drive string `json:"uploadDrive"`
FileSize int64 `json:"uploadFileSize"`
FileType string `json:"uploadFileType"`
ImageSize int64 `json:"uploadImageSize"`
@@ -62,6 +62,24 @@ type UploadConfig struct {
UCloudPath string `json:"uploadUCloudPath"`
UCloudPrivateKey string `json:"uploadUCloudPrivateKey"`
UCloudPublicKey string `json:"uploadUCloudPublicKey"`
// 腾讯云cos配置
CosSecretId string `json:"uploadCosSecretId"`
CosSecretKey string `json:"uploadCosSecretKey"`
CosBucketURL string `json:"uploadCosBucketURL"`
CosPath string `json:"uploadCosPath"`
// 阿里云oss配置
OssSecretId string `json:"uploadOssSecretId"`
OssSecretKey string `json:"uploadOssSecretKey"`
OssEndpoint string `json:"uploadOssEndpoint"`
OssBucketURL string `json:"uploadOssBucketURL"`
OssPath string `json:"uploadOssPath"`
OssBucket string `json:"uploadOssBucket"`
// 七牛云对象存储配置
QiNiuAccessKey string `json:"uploadQiNiuAccessKey"`
QiNiuSecretKey string `json:"uploadQiNiuSecretKey"`
QiNiuDomain string `json:"uploadQiNiuDomain"`
QiNiuPath string `json:"uploadQiNiuPath"`
QiNiuBucket string `json:"uploadQiNiuBucket"`
}
// GeoConfig 地理配置