mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 10:09:54 +08:00
This commit is contained in:
@@ -10,5 +10,7 @@ import (
|
||||
_ "hotgo/internal/logic/hook"
|
||||
_ "hotgo/internal/logic/middleware"
|
||||
_ "hotgo/internal/logic/sys"
|
||||
_ "hotgo/internal/logic/tcpclient"
|
||||
_ "hotgo/internal/logic/tcpserver"
|
||||
_ "hotgo/internal/logic/view"
|
||||
)
|
||||
|
@@ -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 sys
|
||||
|
||||
import (
|
||||
@@ -11,13 +10,12 @@ import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/global"
|
||||
"hotgo/internal/model/input/sysin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/validate"
|
||||
)
|
||||
|
||||
@@ -176,91 +174,12 @@ func (s *sSysBlacklist) Load(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
matchStrategy := func(originIp string) {
|
||||
// 多个IP
|
||||
if gstr.Contains(originIp, ",") {
|
||||
ips := gstr.Explode(",", originIp)
|
||||
if len(ips) > 0 {
|
||||
for _, ip := range ips {
|
||||
if !validate.IsIp(ip) {
|
||||
continue
|
||||
}
|
||||
global.Blacklists[ip] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// IP段
|
||||
if gstr.Contains(originIp, "/24") {
|
||||
segment := gstr.Replace(originIp, "/24", "")
|
||||
if !validate.IsIp(segment) {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
start = gstr.Explode(".", segment)
|
||||
prefix = gstr.Implode(".", start[:len(start)-1]) + "."
|
||||
index = gconv.Int(start[len(start)-1])
|
||||
)
|
||||
|
||||
if index < 1 {
|
||||
index = 1
|
||||
}
|
||||
|
||||
for i := index; i <= 254; i++ {
|
||||
global.Blacklists[prefix+gconv.String(i)] = struct{}{}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// IP范围
|
||||
if gstr.Contains(originIp, "-") {
|
||||
originIps := gstr.Explode("-", originIp)
|
||||
if len(originIps) != 2 {
|
||||
return
|
||||
}
|
||||
|
||||
if !validate.IsIp(originIps[0]) || !validate.IsIp(originIps[1]) {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
start = gstr.Explode(".", originIps[0])
|
||||
prefix = gstr.Implode(".", start[:len(start)-1]) + "."
|
||||
startIndex = gconv.Int(gstr.SubStrFromREx(originIps[0], "."))
|
||||
endIndex = gconv.Int(gstr.SubStrFromREx(originIps[1], "."))
|
||||
)
|
||||
|
||||
if startIndex >= endIndex {
|
||||
global.Blacklists[originIps[0]] = struct{}{}
|
||||
return
|
||||
}
|
||||
|
||||
if startIndex < 1 {
|
||||
startIndex = 1
|
||||
}
|
||||
|
||||
if endIndex > 254 {
|
||||
endIndex = 254
|
||||
}
|
||||
|
||||
for i := startIndex; i <= endIndex; i++ {
|
||||
global.Blacklists[prefix+gconv.String(i)] = struct{}{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 指定IP
|
||||
if validate.IsIp(originIp) {
|
||||
global.Blacklists[originIp] = struct{}{}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range array {
|
||||
matchStrategy(v.String())
|
||||
list := convert.IpFilterStrategy(v.String())
|
||||
if len(list) > 0 {
|
||||
for k, _ := range list {
|
||||
global.Blacklists[k] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
110
server/internal/logic/tcpclient/auth.go
Normal file
110
server/internal/logic/tcpclient/auth.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package tcpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcron"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
"hotgo/internal/model/input/msgin"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
// tcp授权
|
||||
type sTCPAuth struct {
|
||||
client *tcp.Client
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterTCPAuth(newTCPAuth())
|
||||
}
|
||||
|
||||
func newTCPAuth() *sTCPAuth {
|
||||
return &sTCPAuth{}
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
func (s *sTCPAuth) Start(ctx context.Context) {
|
||||
g.Log().Debug(ctx, "TCPAuth start..")
|
||||
simple.SafeGo(ctx, func(ctx context.Context) {
|
||||
client, err := tcp.NewClient(&tcp.ClientConfig{
|
||||
Addr: "127.0.0.1:8099",
|
||||
Auth: &tcp.AuthMeta{
|
||||
Group: "auth",
|
||||
Name: "auth1",
|
||||
AppId: "mengshuai",
|
||||
SecretKey: "123456",
|
||||
},
|
||||
LoginEvent: s.loginEvent,
|
||||
CloseEvent: s.closeEvent,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Infof(ctx, "TCPAuth NewClient fail:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
s.client = client
|
||||
|
||||
err = s.client.RegisterRouter(map[string]tcp.RouterHandler{
|
||||
"ResponseAuthSummary": s.onResponseAuthSummary, // 获取授权信息
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
g.Log().Infof(ctx, "TCPAuth RegisterRouter fail:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = s.client.Start(); err != nil {
|
||||
g.Log().Infof(ctx, "TCPAuth Start fail:%+v", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Stop 关闭服务
|
||||
func (s *sTCPAuth) Stop(ctx context.Context) {
|
||||
if s.client != nil {
|
||||
s.client.Stop()
|
||||
g.Log().Debug(ctx, "TCPAuth stop..")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sTCPAuth) loginEvent() {
|
||||
// 登录成功后立即请求一次授权信息
|
||||
s.client.Write(&msgin.AuthSummary{})
|
||||
|
||||
// 定时检查授权
|
||||
gcron.Add(s.client.Ctx, "@every 1200s", func(ctx context.Context) {
|
||||
if !s.client.IsLogin {
|
||||
g.Log().Infof(ctx, "TCPAuthVerify client is not logged in, skipped")
|
||||
return
|
||||
}
|
||||
s.client.Write(&msgin.AuthSummary{})
|
||||
}, "TCPAuthVerify")
|
||||
}
|
||||
|
||||
func (s *sTCPAuth) closeEvent() {
|
||||
// 关闭连接后,删除定时检查授权
|
||||
gcron.Remove("TCPAuthVerify")
|
||||
}
|
||||
|
||||
func (s *sTCPAuth) onResponseAuthSummary(args ...interface{}) {
|
||||
var in *msgin.ResponseAuthSummary
|
||||
if err := gconv.Scan(args[0], &in); err != nil {
|
||||
s.client.Logger.Infof(s.client.Ctx, "ResponseAuthSummary message Scan failed:%+v, args:%+v", err, args[0])
|
||||
return
|
||||
}
|
||||
s.client.Logger.Infof(s.client.Ctx, "onResponseAuthSummary in:%+v", *in)
|
||||
|
||||
// 授权异常
|
||||
if in.Code != gcode.CodeOK.Code() {
|
||||
s.client.Logger.Infof(s.client.Ctx, "onResponseAuthSummary authorization verification failed:%+v", in.Message)
|
||||
s.client.Destroy()
|
||||
return
|
||||
}
|
||||
|
||||
// 授权通过
|
||||
// 后续可以做一些操作...
|
||||
}
|
73
server/internal/logic/tcpserver/auth_handle.go
Normal file
73
server/internal/logic/tcpserver/auth_handle.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package tcpserver
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/dao"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
"hotgo/internal/model/entity"
|
||||
"hotgo/internal/model/input/msgin"
|
||||
)
|
||||
|
||||
// onAuthSummary 获取授权信息
|
||||
func (s *sTCPServer) onAuthSummary(args ...interface{}) {
|
||||
var (
|
||||
in *msgin.AuthSummary
|
||||
client = args[1].(*tcp.ClientConn)
|
||||
res = new(msgin.ResponseAuthSummary)
|
||||
models *entity.SysServeLicense
|
||||
)
|
||||
|
||||
if err := gconv.Scan(args, &in); err != nil {
|
||||
s.serv.Logger.Infof(s.serv.Ctx, "onAuthSummary message Scan failed:%+v, args:%+v", err, args)
|
||||
return
|
||||
}
|
||||
|
||||
if client.Auth == nil {
|
||||
res.Code = 1
|
||||
res.Message = "登录信息获取失败,请重新登录"
|
||||
s.serv.Write(client.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if err := dao.SysServeLicense.Ctx(s.serv.Ctx).Where("appid = ?", client.Auth.AppId).Scan(&models); err != nil {
|
||||
res.Code = 2
|
||||
res.Message = err.Error()
|
||||
s.serv.Write(client.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models == nil {
|
||||
res.Code = 3
|
||||
res.Message = "授权信息不存在"
|
||||
s.serv.Write(client.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models.Status != consts.StatusEnabled {
|
||||
res.Code = 4
|
||||
res.Message = "授权已禁用,请联系管理员"
|
||||
s.serv.Write(client.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models.Group != client.Auth.Group {
|
||||
res.Code = 5
|
||||
res.Message = "你登录的授权分组未得到授权,请联系管理员"
|
||||
s.serv.Write(client.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
if models.EndAt.Before(gtime.Now()) {
|
||||
res.Code = 6
|
||||
res.Message = "授权已过期,请联系管理员"
|
||||
s.serv.Write(client.Conn, res)
|
||||
return
|
||||
}
|
||||
|
||||
res.Data = new(msgin.AuthSummaryData)
|
||||
res.Data.EndAt = models.EndAt
|
||||
res.Data.Online = models.Online
|
||||
s.serv.Write(client.Conn, res)
|
||||
}
|
68
server/internal/logic/tcpserver/init.go
Normal file
68
server/internal/logic/tcpserver/init.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package tcpserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"hotgo/internal/library/network/tcp"
|
||||
"hotgo/internal/service"
|
||||
"hotgo/utility/simple"
|
||||
)
|
||||
|
||||
type sTCPServer struct {
|
||||
serv *tcp.Server
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterTCPServer(newTCPServer())
|
||||
}
|
||||
|
||||
func newTCPServer() *sTCPServer {
|
||||
return &sTCPServer{}
|
||||
}
|
||||
|
||||
// Start 启动服务
|
||||
func (s *sTCPServer) Start(ctx context.Context) {
|
||||
simple.SafeGo(ctx, func(ctx context.Context) {
|
||||
g.Log().Debug(ctx, "TCPServer start..")
|
||||
|
||||
server, err := tcp.NewServer(&tcp.ServerConfig{
|
||||
Name: "hotgo",
|
||||
Addr: g.Cfg().MustGet(ctx, "tcpServe.address").String(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
g.Log().Warningf(ctx, "TCPServer start fail:%+v", err)
|
||||
return
|
||||
}
|
||||
|
||||
s.serv = server
|
||||
|
||||
// 消息队列路由
|
||||
s.serv.RegisterQueueRouter(map[string]tcp.RouterHandler{
|
||||
// ...
|
||||
})
|
||||
|
||||
// 定时任务路由
|
||||
s.serv.RegisterCronRouter(map[string]tcp.RouterHandler{
|
||||
// ...
|
||||
})
|
||||
|
||||
// 授权服务路由
|
||||
s.serv.RegisterAuthRouter(map[string]tcp.RouterHandler{
|
||||
"AuthSummary": s.onAuthSummary, // 获取授权信息
|
||||
})
|
||||
|
||||
// 服务监听
|
||||
if err := s.serv.Listen(); err != nil {
|
||||
g.Log().Warningf(ctx, "TCPServer Listen err:%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Stop 关闭服务
|
||||
func (s *sTCPServer) Stop(ctx context.Context) {
|
||||
if s.serv != nil {
|
||||
s.serv.Close()
|
||||
g.Log().Debug(ctx, "TCPServer stop..")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user