新增:连接验权 auth 处理

git-svn-id: svn://47.119.165.148/zhub@156 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2022-06-23 15:31:10 +00:00
parent e9aa5cf615
commit 6a3d23ee8d
6 changed files with 30 additions and 9 deletions

View File

@@ -34,6 +34,7 @@ type Client struct {
chReceive chan []string // chan of receive message chReceive chan []string // chan of receive message
timerReceive chan []string // chan of timer timerReceive chan []string // chan of timer
lockFlag map[string]*Lock // chan of lock lockFlag map[string]*Lock // chan of lock
auth string
} }
type Lock struct { type Lock struct {
@@ -44,7 +45,7 @@ type Lock struct {
// duration int // lock duration // duration int // lock duration
} }
func Create(appname string, addr string, groupid string) (*Client, error) { func Create(appname, addr, groupid, auth string) (*Client, error) {
conn, err := net.Dial("tcp", addr) conn, err := net.Dial("tcp", addr)
if err != nil { if err != nil {
return &Client{}, err return &Client{}, err
@@ -65,8 +66,10 @@ func Create(appname string, addr string, groupid string) (*Client, error) {
chReceive: make(chan []string, 100), chReceive: make(chan []string, 100),
timerReceive: make(chan []string, 100), timerReceive: make(chan []string, 100),
lockFlag: make(map[string]*Lock), lockFlag: make(map[string]*Lock),
auth: auth,
} }
client.send("auth", auth)
client.send("groupid " + groupid) client.send("groupid " + groupid)
client.init() client.init()
return &client, err return &client, err
@@ -81,6 +84,7 @@ func (c *Client) reconn() (err error) {
continue continue
} else if err == nil { } else if err == nil {
c.conn = conn c.conn = conn
c.send("auth", c.auth)
c.send("groupid " + c.groupid) c.send("groupid " + c.groupid)
go c.receive() go c.receive()

2
go.mod
View File

@@ -1,6 +1,6 @@
module zhub module zhub
go 1.16 go 1.18
require ( require (
github.com/go-basic/uuid v1.0.0 // indirect github.com/go-basic/uuid v1.0.0 // indirect

View File

@@ -29,7 +29,7 @@ func main() {
} }
if len(os.Args) == 3 && strings.EqualFold(os.Args[1], "-r") { if len(os.Args) == 3 && strings.EqualFold(os.Args[1], "-r") {
if cli, err := cmd.Create("zhub-local", addr, "group-admin"); err != nil { if cli, err := cmd.Create("zhub-local", addr, "group-admin", "zchd@123456"); err != nil {
log.Println(err) log.Println(err)
} else { } else {
switch os.Args[2] { switch os.Args[2] {

View File

@@ -1,10 +1,6 @@
SET GOOS=linux SET GOOS=linux
SET GOARCH=amd64 SET GOARCH=amd64
go build -o zhub.sh -ldflags "-s -w" ./app.go go build -o zhub.sh -ldflags "-s -w" ./main.go
upx -9 zhub.sh upx -9 zhub.sh
scp zhub.sh pro:/opt/zhub rem scp zhub.sh dev:/opt/zhub
scp zhub.sh dev:/opt/zhub
scp zhub.sh qc:/opt/zhub
scp zhub.sh my:/opt/zhub
del zhub.sh

View File

@@ -32,6 +32,10 @@ func msgAccept(v Message) {
if LogDebug { if LogDebug {
log.Println("[", v.Conn.sn, "] rcmd: "+strings.Join(rcmd, " ")) log.Println("[", v.Conn.sn, "] rcmd: "+strings.Join(rcmd, " "))
} }
if !c.auth && !strings.EqualFold("auth", rcmd[0]) && strings.EqualFold(GetStr("service.auth", "0"), "1") {
c.send("-Auth: NOAUTH Authentication required:" + rcmd[0])
return
}
if len(rcmd) == 1 { if len(rcmd) == 1 {
switch strings.ToLower(rcmd[0]) { switch strings.ToLower(rcmd[0]) {
@@ -149,6 +153,21 @@ func msgAccept(v Message) {
return return
} }
zsub._unlock(Lock{key: rcmd[1], uuid: rcmd[2]}) zsub._unlock(Lock{key: rcmd[1], uuid: rcmd[2]})
case "auth":
if len(rcmd) != 2 || strings.IndexAny(rcmd[1], "@") == -1 {
c.send("-Error: invalid password!")
return
}
inx := strings.IndexAny(rcmd[1], "@") //user@pwd
if strings.EqualFold(GetStr("auth."+rcmd[1][:inx], ""), rcmd[1][inx+1:]) {
c.auth = true
c.send("+Auth: ok!")
} else {
c.send("-Auth: invalid password!")
}
return
default: default:
c.send("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]") c.send("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]")
return return

View File

@@ -87,6 +87,7 @@ type ZConn struct { //ZConn
substoped map[string]chan int // 关闭信号量 substoped map[string]chan int // 关闭信号量
ping int64 // 最后心跳时间 ping int64 // 最后心跳时间
pong int64 // 最后心跳回复时间 pong int64 // 最后心跳回复时间
auth bool // 是否已验证授权
} }
type Lock struct { type Lock struct {
@@ -483,6 +484,7 @@ func Info() map[string]interface{} {
m["groupid"] = c.groupid m["groupid"] = c.groupid
m["topics"] = c.topics m["topics"] = c.topics
m["timers"] = c.timers m["timers"] = c.timers
m["auth"] = c.auth
conns = append(conns, m) conns = append(conns, m)
} }