From 36de957bf3a22a4785e8ce3d111b9a20de0d36f0 Mon Sep 17 00:00:00 2001 From: benjafire Date: Wed, 28 Mar 2018 17:50:05 +0800 Subject: [PATCH] add replaciton master auth support (#336) --- config/config.go | 1 + server/replication.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index f8aa639..6d021d5 100644 --- a/config/config.go +++ b/config/config.go @@ -83,6 +83,7 @@ type ReplicationConfig struct { SyncLog int `toml:"sync_log"` Compression bool `toml:"compression"` UseMmap bool `toml:"use_mmap"` + MasterPassword string `toml:"master_password"` } type SnapshotConfig struct { diff --git a/server/replication.go b/server/replication.go index cfc4f26..911488c 100644 --- a/server/replication.go +++ b/server/replication.go @@ -110,12 +110,35 @@ func (m *master) checkConn() error { var err error if m.conn == nil { m.conn, err = goredis.Connect(m.addr) - } else { - if _, err = m.conn.Do("PING"); err != nil { - m.conn.Close() - m.conn = nil + + if err != nil { + return err } } + + // already connected and has master password + if len(m.app.cfg.Replication.MasterPassword) != 0 { + var res string + + res, err = goredis.String(m.conn.Do("auth", m.app.cfg.Replication.MasterPassword)) + + if err != nil || strings.ToUpper(res) != "OK" { + m.conn.Close() + m.conn = nil + + if err == nil { + err = fmt.Errorf("master auth fail , res=%s , password=%s", res, m.app.cfg.Replication.MasterPassword) + } + + return err + } + } + + if _, err = m.conn.Do("PING"); err != nil { + m.conn.Close() + m.conn = nil + } + return err }