mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-27 19:53:50 +08:00
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package simple
|
||||
|
||||
import (
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/os/grpool"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/utility/encrypt"
|
||||
@@ -40,19 +40,21 @@ func CheckPassword(input, salt, hash string) (err error) {
|
||||
|
||||
// SafeGo 安全的调用协程,遇到错误时输出错误日志而不是抛出panic
|
||||
func SafeGo(ctx context.Context, f func(ctx context.Context), level ...interface{}) {
|
||||
go func() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
var newLevel = glog.LEVEL_ERRO
|
||||
if len(level) > 0 {
|
||||
newLevel = gconv.Int(level[0])
|
||||
}
|
||||
Logf(newLevel, ctx, "SafeGo failed err:%+v", err)
|
||||
}
|
||||
}()
|
||||
var newLevel = glog.LEVEL_ERRO
|
||||
if len(level) > 0 {
|
||||
newLevel = gconv.Int(level[0])
|
||||
}
|
||||
|
||||
err := grpool.AddWithRecover(ctx, func(ctx context.Context) {
|
||||
f(ctx)
|
||||
}()
|
||||
}, func(ctx context.Context, err error) {
|
||||
Logf(newLevel, ctx, "SafeGo exec failed:%+v", err)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
Logf(newLevel, ctx, "SafeGo AddWithRecover err:%+v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Logf(level int, ctx context.Context, format string, v ...interface{}) {
|
||||
|
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package useragent
|
||||
|
||||
import (
|
||||
|
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package validate
|
||||
|
||||
import (
|
||||
@@ -14,6 +13,7 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -33,14 +33,8 @@ func IsHTTPS(ctx context.Context) bool {
|
||||
g.Log().Info(ctx, "IsHTTPS ctx not request")
|
||||
return false
|
||||
}
|
||||
var (
|
||||
proto = r.Header.Get("X-Forwarded-Proto")
|
||||
)
|
||||
|
||||
if r.TLS != nil || gstr.Equal(proto, "https") {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return r.TLS != nil || gstr.Equal(r.Header.Get("X-Forwarded-Proto"), "https")
|
||||
}
|
||||
|
||||
// IsIp 是否为ipv4
|
||||
@@ -149,3 +143,59 @@ func IsSameMinute(t1, t2 int64) bool {
|
||||
d2 := time.Unix(t2, 0).Format("2006-01-02 15:04")
|
||||
return d1 == d2
|
||||
}
|
||||
|
||||
// IsMobileVisit 是否为移动端访问
|
||||
func IsMobileVisit(userAgent string) bool {
|
||||
if len(userAgent) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
isMobile := false
|
||||
mobileKeywords := []string{"Mobile", "Android", "Silk/", "Kindle", "BlackBerry", "Opera Mini", "Opera Mobi"}
|
||||
for i := 0; i < len(mobileKeywords); i++ {
|
||||
if strings.Contains(userAgent, mobileKeywords[i]) {
|
||||
isMobile = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return isMobile
|
||||
}
|
||||
|
||||
// IsWxBrowserVisit 是否为微信访问
|
||||
func IsWxBrowserVisit(userAgent string) bool {
|
||||
if len(userAgent) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
is := false
|
||||
userAgent = strings.ToLower(userAgent)
|
||||
mobileKeywords := []string{"MicroMessenger"}
|
||||
for i := 0; i < len(mobileKeywords); i++ {
|
||||
if strings.Contains(userAgent, strings.ToLower(mobileKeywords[i])) {
|
||||
is = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return is
|
||||
}
|
||||
|
||||
// IsWxMiniProgramVisit 是否为微信小程序访问
|
||||
func IsWxMiniProgramVisit(userAgent string) bool {
|
||||
if len(userAgent) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
is := false
|
||||
userAgent = strings.ToLower(userAgent)
|
||||
mobileKeywords := []string{"miniProgram"}
|
||||
for i := 0; i < len(mobileKeywords); i++ {
|
||||
if strings.Contains(userAgent, strings.ToLower(mobileKeywords[i])) {
|
||||
is = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return is
|
||||
}
|
||||
|
Reference in New Issue
Block a user