diff --git a/mtglib/init.go b/mtglib/init.go index 34e9e4e..9f3ba08 100644 --- a/mtglib/init.go +++ b/mtglib/init.go @@ -129,7 +129,15 @@ type EventStream interface { Send(context.Context, Event) } +// TimeAttackDetector is an abstraction that checks a time, taken from +// the faketls client hello message. This timestamp is encoded into +// client-generated random bytes and can be extracted after some client +// hello verification. +// +// This is mostly to prevent replay attacks. type TimeAttackDetector interface { + // Valid returns an error if timestamp is invalid or should not be + // accepted. Valid(time.Time) error } diff --git a/timeattack/detector.go b/timeattack/detector.go index ba70d3b..4ed1179 100644 --- a/timeattack/detector.go +++ b/timeattack/detector.go @@ -29,6 +29,9 @@ func (d detector) Valid(then time.Time) error { return nil } +// NewDetector returns a new TimeAttackDetector which validates that +// timestamp belongs to intervar [X-duration, X+duration], so a small +// timeshift is acceptable. func NewDetector(duration time.Duration) mtglib.TimeAttackDetector { return detector{ Duration: duration, diff --git a/timeattack/init.go b/timeattack/init.go index 5bf3ab4..1c66919 100644 --- a/timeattack/init.go +++ b/timeattack/init.go @@ -1,7 +1,10 @@ +// TimeAttack has implementation of mtglib.TimeAttackDetector> package timeattack import "time" -const ( - DefaultDuration = 5 * time.Second -) +// DefaultDuration is a default duration when timestamps are acceptable. +// +// It means that all timestamps which are X-DefaultDuration <= X <= +// X+DefaultDuration are fine. +const DefaultDuration = 5 * time.Second diff --git a/timeattack/noop.go b/timeattack/noop.go index 43537c5..e3827b8 100644 --- a/timeattack/noop.go +++ b/timeattack/noop.go @@ -10,6 +10,7 @@ type noop struct{} func (n noop) Valid(_ time.Time) error { return nil } +// NewNoop returns TimeAttackDetector which accepts all timestamps. func NewNoop() mtglib.TimeAttackDetector { return noop{} }