-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eecf0e8
commit 05217b6
Showing
18 changed files
with
110 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package server | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
"github.com/greenstatic/openspalib/request" | ||
"log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type ReplayDetect struct { | ||
HashedPackets map[string]bool | ||
mux sync.Mutex | ||
} | ||
|
||
// Discard packets that were generated more than 5 min ago - first line of | ||
// defense against replay defense | ||
func expiredPacket(packet request.Packet) error { | ||
var dur time.Duration = 5 * time.Minute | ||
if time.Now().Sub(packet.Payload.Timestamp) > dur { | ||
return errors.New("packet expired") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (rd *ReplayDetect) Setup() { | ||
rd.HashedPackets = make(map[string]bool) | ||
} | ||
|
||
// Checks if the packet has already been sent by taking the SHA-256 hash of | ||
// the packet and comparing it with all the received packets. | ||
func (rd *ReplayDetect) Check(packet []byte) error { | ||
|
||
if rd.HashedPackets == nil { | ||
log.Fatal("ReplayDetect struct was not setup using the Setup() function") | ||
} | ||
|
||
// Take hash of the packet | ||
sum := sha256.Sum256(packet) | ||
sumStr := string(sum[:]) | ||
|
||
rd.mux.Lock() | ||
defer rd.mux.Unlock() | ||
_, found := rd.HashedPackets[sumStr] | ||
if found { | ||
return errors.New("hash of packet found") | ||
} | ||
|
||
rd.HashedPackets[sumStr] = true | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters