]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device, ratelimiter: replace uses of time.Now().Sub() with time.Since()
authorMatt Layher <mdlayher@gmail.com>
Mon, 3 Jun 2019 19:46:46 +0000 (15:46 -0400)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 3 Jun 2019 20:15:41 +0000 (22:15 +0200)
Simplification found by staticcheck:

$ staticcheck ./... | grep S1012
device/cookie.go:90:5: should use time.Since instead of time.Now().Sub (S1012)
device/cookie.go:127:5: should use time.Since instead of time.Now().Sub (S1012)
device/cookie.go:242:5: should use time.Since instead of time.Now().Sub (S1012)
device/noise-protocol.go:304:13: should use time.Since instead of time.Now().Sub (S1012)
device/receive.go:82:46: should use time.Since instead of time.Now().Sub (S1012)
device/send.go:132:5: should use time.Since instead of time.Now().Sub (S1012)
device/send.go:139:5: should use time.Since instead of time.Now().Sub (S1012)
device/send.go:235:59: should use time.Since instead of time.Now().Sub (S1012)
device/send.go:393:9: should use time.Since instead of time.Now().Sub (S1012)
ratelimiter/ratelimiter.go:79:10: should use time.Since instead of time.Now().Sub (S1012)
ratelimiter/ratelimiter.go:87:10: should use time.Since instead of time.Now().Sub (S1012)

Change applied using:

$ find . -type f -name "*.go" -exec sed -i "s/Now().Sub(/Since(/g" {} \;

Signed-off-by: Matt Layher <mdlayher@gmail.com>
device/cookie.go
device/noise-protocol.go
device/receive.go
device/send.go
ratelimiter/ratelimiter.go

index 3a88a0c018797d0b7588a4041039908b137ac7bc..f1341281c38427e33064e48cb6f19d08bb44701f 100644 (file)
@@ -87,7 +87,7 @@ func (st *CookieChecker) CheckMAC2(msg []byte, src []byte) bool {
        st.RLock()
        defer st.RUnlock()
 
-       if time.Now().Sub(st.mac2.secretSet) > CookieRefreshTime {
+       if time.Since(st.mac2.secretSet) > CookieRefreshTime {
                return false
        }
 
@@ -124,7 +124,7 @@ func (st *CookieChecker) CreateReply(
 
        // refresh cookie secret
 
-       if time.Now().Sub(st.mac2.secretSet) > CookieRefreshTime {
+       if time.Since(st.mac2.secretSet) > CookieRefreshTime {
                st.RUnlock()
                st.Lock()
                _, err := rand.Read(st.mac2.secret[:])
@@ -239,7 +239,7 @@ func (st *CookieGenerator) AddMacs(msg []byte) {
 
        // set mac2
 
-       if time.Now().Sub(st.mac2.cookieSet) > CookieRefreshTime {
+       if time.Since(st.mac2.cookieSet) > CookieRefreshTime {
                return
        }
 
index 1f70e810268436cc4820bc5c682153a7fb675bb5..dd75cc3b5571dcfc66838ad57eca8e6749992842 100644 (file)
@@ -301,7 +301,7 @@ func (device *Device) ConsumeMessageInitiation(msg *MessageInitiation) *Peer {
 
        var ok bool
        ok = timestamp.After(handshake.lastTimestamp)
-       ok = ok && time.Now().Sub(handshake.lastInitiationConsumption) > HandshakeInitationRate
+       ok = ok && time.Since(handshake.lastInitiationConsumption) > HandshakeInitationRate
        handshake.mutex.RUnlock()
        if !ok {
                return nil
index 22870f2de6c34c9b1d87c0c4dd53d0533fe69502..32d632b6286ad8400e4810a1062ff0bb58dac99e 100644 (file)
@@ -79,7 +79,7 @@ func (peer *Peer) keepKeyFreshReceiving() {
                return
        }
        keypair := peer.keypairs.Current()
-       if keypair != nil && keypair.isInitiator && time.Now().Sub(keypair.created) > (RejectAfterTime-KeepaliveTimeout-RekeyTimeout) {
+       if keypair != nil && keypair.isInitiator && time.Since(keypair.created) > (RejectAfterTime-KeepaliveTimeout-RekeyTimeout) {
                peer.timers.sentLastMinuteHandshake.Set(true)
                peer.SendHandshakeInitiation(false)
        }
index ae96aa20226cafb110fe068816bf058ffaea1fc1..21984275c7a0397fc1cca241c58d03e5caeef8d9 100644 (file)
@@ -129,14 +129,14 @@ func (peer *Peer) SendHandshakeInitiation(isRetry bool) error {
        }
 
        peer.handshake.mutex.RLock()
-       if time.Now().Sub(peer.handshake.lastSentHandshake) < RekeyTimeout {
+       if time.Since(peer.handshake.lastSentHandshake) < RekeyTimeout {
                peer.handshake.mutex.RUnlock()
                return nil
        }
        peer.handshake.mutex.RUnlock()
 
        peer.handshake.mutex.Lock()
-       if time.Now().Sub(peer.handshake.lastSentHandshake) < RekeyTimeout {
+       if time.Since(peer.handshake.lastSentHandshake) < RekeyTimeout {
                peer.handshake.mutex.Unlock()
                return nil
        }
@@ -232,7 +232,7 @@ func (peer *Peer) keepKeyFreshSending() {
                return
        }
        nonce := atomic.LoadUint64(&keypair.sendNonce)
-       if nonce > RekeyAfterMessages || (keypair.isInitiator && time.Now().Sub(keypair.created) > RekeyAfterTime) {
+       if nonce > RekeyAfterMessages || (keypair.isInitiator && time.Since(keypair.created) > RekeyAfterTime) {
                peer.SendHandshakeInitiation(false)
        }
 }
@@ -390,7 +390,7 @@ func (peer *Peer) RoutineNonce() {
 
                                keypair = peer.keypairs.Current()
                                if keypair != nil && keypair.sendNonce < RejectAfterMessages {
-                                       if time.Now().Sub(keypair.created) < RejectAfterTime {
+                                       if time.Since(keypair.created) < RejectAfterTime {
                                                break
                                        }
                                }
index 595e5a54d165575c02790b77c035764aaae8bb78..772c45aed6636765665b8ac99418e09aca4af6b2 100644 (file)
@@ -76,7 +76,7 @@ func (rate *Ratelimiter) Init() {
 
                                        for key, entry := range rate.tableIPv4 {
                                                entry.Lock()
-                                               if time.Now().Sub(entry.lastTime) > garbageCollectTime {
+                                               if time.Since(entry.lastTime) > garbageCollectTime {
                                                        delete(rate.tableIPv4, key)
                                                }
                                                entry.Unlock()
@@ -84,7 +84,7 @@ func (rate *Ratelimiter) Init() {
 
                                        for key, entry := range rate.tableIPv6 {
                                                entry.Lock()
-                                               if time.Now().Sub(entry.lastTime) > garbageCollectTime {
+                                               if time.Since(entry.lastTime) > garbageCollectTime {
                                                        delete(rate.tableIPv6, key)
                                                }
                                                entry.Unlock()