diff --git a/CHANGELOG.md b/CHANGELOG.md index 9834069..ad81786 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -160,4 +160,5 @@ # TODO -1. go版本升级到1.19.1 \ No newline at end of file +1. 修改握手时的签名算法 +2. go版本升级到1.19.1 \ No newline at end of file diff --git a/code/client/conn/conn.go b/code/client/conn/conn.go index 26b1f1a..a1c167c 100644 --- a/code/client/conn/conn.go +++ b/code/client/conn/conn.go @@ -103,7 +103,7 @@ func writeHandshake(conn *network.Conn, cfg *global.Configure) error { msg.To = "server" msg.Payload = &network.Msg_Hsp{ Hsp: &network.HandshakePayload{ - Enc: cfg.Enc[:], + Enc: cfg.Hasher.Hash(), }, } return conn.WriteMessage(&msg, 5*time.Second) diff --git a/code/client/global/conf.go b/code/client/global/conf.go index 06df673..a3b0890 100644 --- a/code/client/global/conf.go +++ b/code/client/global/conf.go @@ -1,12 +1,12 @@ package global import ( - "crypto/md5" "fmt" "os" "path/filepath" "time" + "github.com/lwch/natpass/code/hash" "github.com/lwch/natpass/code/utils" "github.com/lwch/runtime" "github.com/lwch/yaml" @@ -32,7 +32,7 @@ type Configure struct { Server string UseSSL bool SSLInsecure bool - Enc [md5.Size]byte + Hasher *hash.Hasher Links int LogDir string LogSize utils.Bytes @@ -103,7 +103,7 @@ func LoadConf(dir string) *Configure { Server: cfg.Server, UseSSL: cfg.SSL.Enabled, SSLInsecure: cfg.SSL.Insecure, - Enc: md5.Sum([]byte(cfg.Secret)), + Hasher: hash.New(cfg.Secret, 60), ReadTimeout: cfg.Link.ReadTimeout, WriteTimeout: cfg.Link.WriteTimeout, LogDir: cfg.Log.Dir, diff --git a/code/hash/hash.go b/code/hash/hash.go new file mode 100644 index 0000000..64136ee --- /dev/null +++ b/code/hash/hash.go @@ -0,0 +1,40 @@ +package hash + +import ( + "crypto/hmac" + "crypto/sha512" + "encoding/binary" + "hash" + "math" + "sync" + "time" +) + +type Hasher struct { + sync.Mutex + period uint + h hash.Hash +} + +func New(secret string, period uint) *Hasher { + if period == 0 { + period = 30 + } + return &Hasher{ + period: period, + h: hmac.New(sha512.New, []byte(secret)), + } +} + +func (h *Hasher) Hash() []byte { + now := time.Now() + i := math.Floor(float64(now.Unix()) / float64(h.period)) + var buf [8]byte + binary.BigEndian.PutUint64(buf[:], uint64(i)) + h.Lock() + defer h.Unlock() + h.h.Reset() + h.h.Write(buf[:]) + ret := h.h.Sum(nil) + return ret +} diff --git a/code/server/global/conf.go b/code/server/global/conf.go index df42ca7..28d91cf 100644 --- a/code/server/global/conf.go +++ b/code/server/global/conf.go @@ -1,11 +1,11 @@ package global import ( - "crypto/md5" "os" "path/filepath" "time" + "github.com/lwch/natpass/code/hash" "github.com/lwch/natpass/code/utils" "github.com/lwch/runtime" "github.com/lwch/yaml" @@ -14,7 +14,7 @@ import ( // Configure server configure type Configure struct { Listen uint16 - Enc [md5.Size]byte + Hasher *hash.Hasher TLSKey string TLSCrt string ReadTimeout time.Duration @@ -51,7 +51,7 @@ func LoadConf(dir string) *Configure { } return &Configure{ Listen: cfg.Listen, - Enc: md5.Sum([]byte(cfg.Secret)), + Hasher: hash.New(cfg.Secret, 60), TLSKey: cfg.TLS.Key, TLSCrt: cfg.TLS.Crt, ReadTimeout: cfg.Link.ReadTimeout, diff --git a/code/server/handler/handler.go b/code/server/handler/handler.go index dbf66e9..862ff32 100644 --- a/code/server/handler/handler.go +++ b/code/server/handler/handler.go @@ -91,7 +91,7 @@ func (h *Handler) readHandshake(c *network.Conn) (string, error) { if msg.GetXType() != network.Msg_handshake { return "", errNotHandshake } - n := bytes.Compare(msg.GetHsp().GetEnc(), h.cfg.Enc[:]) + n := bytes.Compare(msg.GetHsp().GetEnc(), h.cfg.Hasher.Hash()) if n != 0 { return "", errInvalidHandshake }