Skip to content

Commit 2c27be1

Browse files
committed
发布v2.4.4版本,本次为优化版本。更新内容请查看:https://github.com/bufanyun/hotgo/blob/v2.0/docs/guide-zh-CN/start-update-log.md
1 parent 1acc6d1 commit 2c27be1

File tree

16 files changed

+301
-274
lines changed

16 files changed

+301
-274
lines changed

docs/guide-zh-CN/start-update-log.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
1212
> 如果升级(覆盖)代码后打开会出现 sql 报错, 请检查更新的数据库格式或自行调整
1313
14+
### v2.4.4
15+
updated 2023.03.16
16+
- 优化:优化代码生成多库生成时的菜单sql默认指向默认数据库分组
17+
- 优化:优化TCP服务认证机制
18+
1419
### v2.4.2
1520
updated 2023.03.11
1621
- 修复:修复字典管理列表无法添加/编辑问题

docs/guide-zh-CN/sys-exploit.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
- 字段名小写,多关键字使用下划线分割(关键字尽量全称)
132132
- 禁止使用保留字并且尽量少用含有关键词来命名
133133
- 临时表必须以tmp_开头、以日期结尾,备份表必须以bak_开头、以日期结尾
134+
- 同数据库表名设置统一前缀,默认是`hg_`
134135
- 插件模块表名建议以`hg_addon_`开头,如:`hg_addon_hgexample_table`,含义:`插件_案例_表格`。在生成代码时可自动识别实体命名为:`Table`
135136
136137
#### 基础规范

server/internal/consts/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ package consts
77

88
// VersionApp HotGo版本
99
const (
10-
VersionApp = "2.4.2"
10+
VersionApp = "2.4.4"
1111
)

server/internal/library/hggen/views/curd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (l *gCurd) initInput(ctx context.Context, in *CurdPreviewInput) (err error)
116116
return err
117117
}
118118

119-
initStep(ctx, in)
119+
initStep(in)
120120
in.options.dictMap = make(g.Map)
121121

122122
if len(in.Config.Application.Crud.Templates)-1 < in.In.GenTemplate {
@@ -138,7 +138,7 @@ func (l *gCurd) initInput(ctx context.Context, in *CurdPreviewInput) (err error)
138138
return
139139
}
140140

141-
func initStep(ctx context.Context, in *CurdPreviewInput) {
141+
func initStep(in *CurdPreviewInput) {
142142
in.options.Step = new(CurdStep)
143143
in.options.Step.HasMaxSort = HasMaxSort(in.masterFields)
144144
in.options.Step.HasAdd = gstr.InArray(in.options.HeadOps, "add")
@@ -642,7 +642,7 @@ func (l *gCurd) generateWebViewContent(ctx context.Context, in *CurdPreviewInput
642642
func (l *gCurd) generateSqlContent(ctx context.Context, in *CurdPreviewInput) (err error) {
643643
var (
644644
name = "source.sql"
645-
config = g.DB(in.In.DbName).GetConfig()
645+
config = g.DB("default").GetConfig()
646646
tplData = g.Map{
647647
"dbName": config.Name,
648648
"menuTable": config.Prefix + "admin_menu",
@@ -652,7 +652,7 @@ func (l *gCurd) generateSqlContent(ctx context.Context, in *CurdPreviewInput) (e
652652
)
653653

654654
if in.options.Menu.Pid > 0 {
655-
tplData["mainComponent"] = "ParentLayout" //gstr.LcFirst(in.In.VarName)
655+
tplData["mainComponent"] = "ParentLayout"
656656
}
657657

658658
genFile.Path = file.MergeAbs(in.Config.Application.Crud.Templates[in.In.GenTemplate].SqlPath, convert.CamelCaseToUnderline(in.In.VarName)+"_menu.sql")

server/internal/library/location/location.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ type WhoisRegionData struct {
5454

5555
// WhoisLocation 通过Whois接口查询IP归属地
5656
func WhoisLocation(ctx context.Context, ip string) (*IpLocationData, error) {
57-
if !validate.IsIp(ip) {
58-
return nil, fmt.Errorf("invalid input ip:%v", ip)
59-
}
60-
6157
response, err := g.Client().Timeout(10*time.Second).Get(ctx, whoisApi+ip)
6258
if err != nil {
6359
return nil, err
@@ -90,10 +86,6 @@ func WhoisLocation(ctx context.Context, ip string) (*IpLocationData, error) {
9086

9187
// Cz88Find 通过Cz88的IP库查询IP归属地
9288
func Cz88Find(ctx context.Context, ip string) (*IpLocationData, error) {
93-
if !validate.IsIp(ip) {
94-
return nil, fmt.Errorf("invalid input ip:%v", ip)
95-
}
96-
9789
loc, err := iploc.OpenWithoutIndexes("./resource/ip/qqwry-utf8.dat")
9890
if err != nil {
9991
return nil, fmt.Errorf("%v for help, please go to: https://github.com/kayon/iploc", err.Error())
@@ -129,6 +121,13 @@ func IsJurisByIpTitle(title string) bool {
129121

130122
// GetLocation 获取IP归属地信息
131123
func GetLocation(ctx context.Context, ip string) (*IpLocationData, error) {
124+
if !validate.IsIp(ip) {
125+
return nil, fmt.Errorf("invalid input ip:%v", ip)
126+
}
127+
128+
if validate.IsLocalIPAddr(ip) {
129+
return nil, fmt.Errorf("must be a public ip:%v", ip)
130+
}
132131
method := g.Cfg().MustGet(ctx, "hotgo.ipMethod", "cz88")
133132
if method.String() == "whois" {
134133
return WhoisLocation(ctx, ip)

server/internal/library/network/tcp/model.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package tcp
22

3+
import "github.com/gogf/gf/v2/os/gtime"
4+
35
// 定时任务
46
const (
57
cronHeartbeatVerify = "tcpHeartbeatVerify"
68
cronHeartbeat = "tcpHeartbeat"
9+
cronAuthVerify = "tcpAuthVerify"
710
)
811

912
// 认证分组
@@ -15,10 +18,11 @@ const (
1518

1619
// AuthMeta 认证元数据
1720
type AuthMeta struct {
18-
Group string `json:"group"`
19-
Name string `json:"name"`
20-
AppId string `json:"appId"`
21-
SecretKey string `json:"secretKey"`
21+
Group string `json:"group"`
22+
Name string `json:"name"`
23+
AppId string `json:"appId"`
24+
SecretKey string `json:"secretKey"`
25+
EndAt *gtime.Time `json:"-"`
2226
}
2327

2428
// CallbackEvent 回调事件

server/internal/library/network/tcp/server_cron.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,24 @@ func (server *Server) startCron() {
2727
for _, client := range server.clients {
2828
if client.heartbeat < gtime.Timestamp()-300 {
2929
client.Conn.Close()
30-
server.Logger.Debugf(server.Ctx, "client heartbeat timeout, about to reconnect.. auth:%+v", client.Auth)
30+
server.Logger.Debugf(server.Ctx, "client heartbeat timeout, close conn. auth:%+v", client.Auth)
3131
}
3232
}
3333
}, server.getCronKey(cronHeartbeatVerify))
3434
}
35+
36+
// 认证检查
37+
if gcron.Search(server.getCronKey(cronAuthVerify)) == nil {
38+
gcron.AddSingleton(server.Ctx, "@every 300s", func(ctx context.Context) {
39+
if server.clients == nil {
40+
return
41+
}
42+
for _, client := range server.clients {
43+
if client.Auth.EndAt.Before(gtime.Now()) {
44+
client.Conn.Close()
45+
server.Logger.Debugf(server.Ctx, "client auth expired, close conn. auth:%+v", client.Auth)
46+
}
47+
}
48+
}, server.getCronKey(cronAuthVerify))
49+
}
3550
}

server/internal/library/network/tcp/server_handle.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (server *Server) onServerLogin(args ...interface{}) {
107107
Name: in.Name,
108108
AppId: in.AppId,
109109
SecretKey: models.SecretKey,
110+
EndAt: models.EndAt,
110111
},
111112
heartbeat: gtime.Timestamp(),
112113
}

server/internal/logic/sys/gen_codes.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,31 @@ func (s *sSysGenCodes) TableSelect(ctx context.Context, in sysin.GenCodesTableSe
240240
continue
241241
}
242242

243+
newValue := v.Value
244+
if config.Prefix != "" {
245+
newValue = gstr.SubStrFromEx(v.Value, config.Prefix)
246+
}
247+
if newValue == "" {
248+
err = gerror.Newf("表名[%v]前缀必须和配置中的前缀设置[%v] 保持一致", v.Value, config.Prefix)
249+
return
250+
}
251+
243252
// 如果是插件模块,则移除掉插件表前缀
244-
defVarName := gstr.SubStrFromEx(v.Value, config.Prefix)
245-
bt, err := gregex.Replace(patternStr, []byte(repStr), []byte(defVarName))
253+
bt, err := gregex.Replace(patternStr, []byte(repStr), []byte(newValue))
246254
if err != nil {
255+
err = gerror.Newf("表名[%v] gregex.Replace err:%v", v.Value, err.Error())
247256
break
248257
}
249-
defVarName = gstr.CaseCamel(string(bt))
250258

251259
row := new(sysin.GenCodesTableSelectModel)
252260
row = v
253261
row.DefTableComment = v.Label
254-
row.DaoName = gstr.CaseCamel(gstr.SubStrFromEx(v.Value, config.Prefix))
255-
row.DefVarName = defVarName
256-
row.DefAlias = gstr.CaseCamelLower(gstr.SubStrFromEx(v.Value, config.Prefix))
262+
row.DaoName = gstr.CaseCamel(newValue)
263+
row.DefVarName = gstr.CaseCamel(string(bt))
264+
row.DefAlias = gstr.CaseCamelLower(newValue)
257265
row.Name = fmt.Sprintf("%s (%s)", v.Value, v.Label)
258266
row.Label = row.Name
267+
259268
res = append(res, row)
260269
}
261270
return res, nil

server/internal/logic/sys/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (s *sSysLog) AnalysisLog(ctx context.Context) entity.SysLog {
198198

199199
ipData, err := location.GetLocation(ctx, clientIp)
200200
if err != nil {
201-
g.Log().Errorf(ctx, "location.GetLocation err:%+v", err)
201+
g.Log().Infof(ctx, "location.GetLocation clientIp:%v, err:%+v", clientIp, err)
202202
}
203203
if ipData == nil {
204204
ipData = new(location.IpLocationData)

server/internal/logic/tcpclient/auth.go

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package tcpclient
22

33
import (
44
"context"
5-
"github.com/gogf/gf/v2/errors/gcode"
65
"github.com/gogf/gf/v2/frame/g"
7-
"github.com/gogf/gf/v2/os/gcron"
8-
"github.com/gogf/gf/v2/util/gconv"
96
"hotgo/internal/library/network/tcp"
10-
"hotgo/internal/model/input/msgin"
117
"hotgo/internal/service"
128
"hotgo/utility/simple"
139
)
@@ -37,8 +33,8 @@ func (s *sTCPAuth) Start(ctx context.Context) {
3733
AppId: "mengshuai",
3834
SecretKey: "123456",
3935
},
40-
LoginEvent: s.loginEvent,
41-
CloseEvent: s.closeEvent,
36+
LoginEvent: s.onLoginEvent,
37+
CloseEvent: s.onCloseEvent,
4238
})
4339
if err != nil {
4440
g.Log().Infof(ctx, "TCPAuth NewClient fail:%+v", err)
@@ -48,7 +44,7 @@ func (s *sTCPAuth) Start(ctx context.Context) {
4844
s.client = client
4945

5046
err = s.client.RegisterRouter(map[string]tcp.RouterHandler{
51-
"ResponseAuthSummary": s.onResponseAuthSummary, // 获取授权信息
47+
// ...
5248
})
5349

5450
if err != nil {
@@ -63,48 +59,25 @@ func (s *sTCPAuth) Start(ctx context.Context) {
6359
})
6460
}
6561

66-
// Stop 关闭服务
62+
// Stop 停止服务
6763
func (s *sTCPAuth) Stop(ctx context.Context) {
6864
if s.client != nil {
6965
s.client.Stop()
7066
g.Log().Debug(ctx, "TCPAuth stop..")
7167
}
7268
}
7369

74-
func (s *sTCPAuth) loginEvent() {
75-
// 登录成功后立即请求一次授权信息
76-
s.client.Write(&msgin.AuthSummary{})
77-
78-
// 定时检查授权
79-
gcron.Add(s.client.Ctx, "@every 1200s", func(ctx context.Context) {
80-
if !s.client.IsLogin {
81-
g.Log().Infof(ctx, "TCPAuthVerify client is not logged in, skipped")
82-
return
83-
}
84-
s.client.Write(&msgin.AuthSummary{})
85-
}, "TCPAuthVerify")
70+
// IsLogin 是否已登录认证
71+
func (s *sTCPAuth) IsLogin() bool {
72+
return s.client.IsLogin
8673
}
8774

88-
func (s *sTCPAuth) closeEvent() {
89-
// 关闭连接后,删除定时检查授权
90-
gcron.Remove("TCPAuthVerify")
75+
// onLoginEvent 登录认证成功事件
76+
func (s *sTCPAuth) onLoginEvent() {
77+
// ...
9178
}
9279

93-
func (s *sTCPAuth) onResponseAuthSummary(args ...interface{}) {
94-
var in *msgin.ResponseAuthSummary
95-
if err := gconv.Scan(args[0], &in); err != nil {
96-
s.client.Logger.Infof(s.client.Ctx, "ResponseAuthSummary message Scan failed:%+v, args:%+v", err, args[0])
97-
return
98-
}
99-
s.client.Logger.Infof(s.client.Ctx, "onResponseAuthSummary in:%+v", *in)
100-
101-
// 授权异常
102-
if in.Code != gcode.CodeOK.Code() {
103-
s.client.Logger.Infof(s.client.Ctx, "onResponseAuthSummary authorization verification failed:%+v", in.Message)
104-
s.client.Destroy()
105-
return
106-
}
107-
108-
// 授权通过
109-
// 后续可以做一些操作...
80+
// onCloseEvent 连接关闭回调事件
81+
func (s *sTCPAuth) onCloseEvent() {
82+
// ...
11083
}

server/internal/service/admin.go

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)