From e27f01cb3477026a2bae9018e1197bbe68c8157b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=9D=E5=B0=98?= <237809796@qq.com> Date: Tue, 28 Nov 2023 17:04:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=EF=BC=9A=E6=9C=AA=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E6=9D=83=E9=99=90=E9=AA=8C=E8=AF=81=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.ini | 10 ++++---- auth.yml | 13 +++++++---- internal/auth/auth.go | 7 ++++++ internal/zbus/zbus-message-handler.go | 4 ++-- internal/zbus/ztimer.go | 6 +++++ pkg.bat | 33 ++++++++++++++++++++++----- 6 files changed, 55 insertions(+), 18 deletions(-) diff --git a/app.ini b/app.ini index a3fc0ed..5b1fc89 100644 --- a/app.ini +++ b/app.ini @@ -6,13 +6,13 @@ file=zhub.log [service] watch=0.0.0.0:711 addr=0.0.0.0:1216 -auth=1 +auth=0 [data] dir=./data [ztimer] -db.addr=127.0.0.1:3306 -db.user=root -db.password=123456 -db.database=zhub +# db.addr=127.0.0.1:3306 +# db.user=root +# db.password=123456 +# db.database=zhub diff --git a/auth.yml b/auth.yml index d3d60f3..ba1b53f 100644 --- a/auth.yml +++ b/auth.yml @@ -36,8 +36,10 @@ groups: description: Group 1 reads: - ^zcore:* # "zcore:" 开头的订阅 + - rpc-t writes: - ^zcore:* # "zcore:" 开头的发送 + - rpc-t - name: zcore description: Group 2 @@ -59,15 +61,16 @@ tokens: # 公开频道设置 channels: - - name: "-" - description: "无效占位符" - public: true - - name: "lock" description: "分布式锁通知频道" public: true - + - name: "trylock" + description: "分布式锁通知频道" + public: true - name: "app_local" description: "本地appname" public: true + - name: "DEV-LOCAL" + description: "本地appname" + public: true # --------------------------------------------- diff --git a/internal/auth/auth.go b/internal/auth/auth.go index a3e7542..a8b5896 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -8,6 +8,7 @@ import ( "strings" "sync" "time" + "zhub/internal/config" ) type User struct { @@ -63,6 +64,12 @@ type PermissionManager struct { func (p *PermissionManager) Init() error { p.lock.Lock() defer p.lock.Unlock() + + Conf := config.ReadConfig() + if !Conf.Service.Auth { + return nil + } + // Load YAML configuration from file data, err := os.ReadFile("./auth.yml") if err != nil { diff --git a/internal/zbus/zbus-message-handler.go b/internal/zbus/zbus-message-handler.go index 10457a5..af2e17c 100644 --- a/internal/zbus/zbus-message-handler.go +++ b/internal/zbus/zbus-message-handler.go @@ -79,7 +79,7 @@ func messageHandler(v Message) { // auth check switch cmd { case "publish", "broadcast", "delay", "rpc": - if !AuthManager.AuthCheck(c.user, rcmd[1], "w") { + if Conf.Service.Auth && !AuthManager.AuthCheck(c.user, rcmd[1], "w") { c.send("-Error: Insufficient permissions to send " + cmd + " [" + rcmd[1] + "] message.") log.Printf("[%d] -Auth: %s [%s]\n", c.sn, cmd, rcmd[1]) if cmd == "rpc" { @@ -174,7 +174,7 @@ func messageHandler(v Message) { // subscribe x y z for _, topic := range rcmd[1:] { // auth check - if !AuthManager.AuthCheck(c.user, rcmd[1], "r") { + if Conf.Service.Auth && !AuthManager.AuthCheck(c.user, rcmd[1], "r") { c.send("-Error: Insufficient permissions to " + cmd + " [" + rcmd[1] + "] message.") log.Printf("-Auth: %s [%s]\n", cmd, rcmd[1]) continue diff --git a/internal/zbus/ztimer.go b/internal/zbus/ztimer.go index 2050d92..f204612 100644 --- a/internal/zbus/ztimer.go +++ b/internal/zbus/ztimer.go @@ -156,6 +156,12 @@ func (s *ZBus) timer(rcmd []string, c *ZConn) { } func (s *ZBus) ReloadTimer() { + // 未配置 ztimer 数据库返回 + if Conf.Ztimer.Db.Addr == "" { + log.Println("No found ztimer config in app.ini") + return + } + db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", Conf.Ztimer.Db.User, Conf.Ztimer.Db.Password, diff --git a/pkg.bat b/pkg.bat index 6d491bb..274b05b 100644 --- a/pkg.bat +++ b/pkg.bat @@ -1,6 +1,27 @@ -SET GOOS=linux -SET GOARCH=amd64 -go build -o zhub.sh -ldflags "-s -w" -upx -9 zhub.sh - -rem scp zhub.sh dev:/opt/zhub + +@echo off + +rem 删除历史编译文件 +del zhub.sh zhub.exe zhub + +rem Linux +set GOOS=linux +set GOARCH=amd64 +go build -o zhub.sh -ldflags "-s -w" +upx -9 zhub.sh + +rem Windows +set GOOS=windows +set GOARCH=amd64 +go build -o zhub.exe -ldflags "-s -w" +upx -9 zhub.exe + +rem Mac +set GOOS=darwin +set GOARCH=amd64 +go build -o zhub -ldflags "-s -w" +upx -9 zhub + +move /Y zhub.sh ./tmp/zhub/ +move /Y zhub.exe ./tmp/zhub/ +move /Y zhub ./tmp/zhub/