mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-02 00:11:56 +03:00
Add timeattack detector
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -46,6 +47,10 @@ type EventStream interface {
|
|||||||
Shutdown()
|
Shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TimeAttackDetector interface {
|
||||||
|
Valid(time.Time) error
|
||||||
|
}
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
Named(name string) Logger
|
Named(name string) Logger
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ type Proxy struct {
|
|||||||
|
|
||||||
secret Secret
|
secret Secret
|
||||||
network Network
|
network Network
|
||||||
|
timeAttackDetector TimeAttackDetector
|
||||||
antiReplayCache AntiReplayCache
|
antiReplayCache AntiReplayCache
|
||||||
ipBlocklist IPBlocklist
|
ipBlocklist IPBlocklist
|
||||||
eventStream EventStream
|
eventStream EventStream
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ type ProxyOpts struct {
|
|||||||
Secret Secret
|
Secret Secret
|
||||||
Network Network
|
Network Network
|
||||||
AntiReplayCache AntiReplayCache
|
AntiReplayCache AntiReplayCache
|
||||||
|
TimeAttackDetector TimeAttackDetector
|
||||||
IPBlocklist IPBlocklist
|
IPBlocklist IPBlocklist
|
||||||
EventStream EventStream
|
EventStream EventStream
|
||||||
Logger Logger
|
Logger Logger
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package timeattack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/v2/mtglib"
|
||||||
|
)
|
||||||
|
|
||||||
|
type detector struct {
|
||||||
|
time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d detector) Valid(then time.Time) error {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
diff := now.Sub(then)
|
||||||
|
if diff < 0 {
|
||||||
|
diff = -diff
|
||||||
|
}
|
||||||
|
|
||||||
|
if diff > d.Duration {
|
||||||
|
return fmt.Errorf("time is invalid. now=%d, then=%d, diff=%v",
|
||||||
|
now.Unix(),
|
||||||
|
then.Unix(),
|
||||||
|
diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDetector(duration time.Duration) mtglib.TimeAttackDetector {
|
||||||
|
return detector{
|
||||||
|
Duration: duration,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package timeattack_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/v2/timeattack"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DetectorTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *DetectorTestSuite) TestOp() {
|
||||||
|
d := timeattack.NewDetector(time.Second)
|
||||||
|
|
||||||
|
suite.NoError(d.Valid(time.Now()))
|
||||||
|
suite.NoError(d.Valid(time.Now().Add(100 * time.Millisecond)))
|
||||||
|
suite.NoError(d.Valid(time.Now().Add(-100 * time.Millisecond)))
|
||||||
|
suite.Error(d.Valid(time.Now().Add(time.Hour)))
|
||||||
|
suite.Error(d.Valid(time.Now().Add(-time.Hour)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDetector(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
suite.Run(t, &DetectorTestSuite{})
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package timeattack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/v2/mtglib"
|
||||||
|
)
|
||||||
|
|
||||||
|
type noop struct{}
|
||||||
|
|
||||||
|
func (n noop) Valid(_ time.Time) error { return nil }
|
||||||
|
|
||||||
|
func NewNoop() mtglib.TimeAttackDetector {
|
||||||
|
return noop{}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package timeattack_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/v2/timeattack"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NoopTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *NoopTestSuite) TestOp() {
|
||||||
|
d := timeattack.NewNoop()
|
||||||
|
|
||||||
|
suite.NoError(d.Valid(time.Now()))
|
||||||
|
suite.NoError(d.Valid(time.Now().Add(time.Hour)))
|
||||||
|
suite.NoError(d.Valid(time.Now().Add(-time.Hour)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNoop(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
suite.Run(t, &NoopTestSuite{})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user