Pārlūkot izejas kodu

Add timeattack detector

tags/v2.0.0-rc1
9seconds 5 gadus atpakaļ
vecāks
revīzija
f3112d4ba6

+ 5
- 0
mtglib/init.go Parādīt failu

@@ -5,6 +5,7 @@ import (
5 5
 	"errors"
6 6
 	"net"
7 7
 	"net/http"
8
+	"time"
8 9
 )
9 10
 
10 11
 var (
@@ -46,6 +47,10 @@ type EventStream interface {
46 47
 	Shutdown()
47 48
 }
48 49
 
50
+type TimeAttackDetector interface {
51
+	Valid(time.Time) error
52
+}
53
+
49 54
 type Logger interface {
50 55
 	Named(name string) Logger
51 56
 

+ 7
- 6
mtglib/proxy.go Parādīt failu

@@ -17,12 +17,13 @@ type Proxy struct {
17 17
 	streamWaitGroup sync.WaitGroup
18 18
 	workerPool      *ants.PoolWithFunc
19 19
 
20
-	secret          Secret
21
-	network         Network
22
-	antiReplayCache AntiReplayCache
23
-	ipBlocklist     IPBlocklist
24
-	eventStream     EventStream
25
-	logger          Logger
20
+	secret             Secret
21
+	network            Network
22
+	timeAttackDetector TimeAttackDetector
23
+	antiReplayCache    AntiReplayCache
24
+	ipBlocklist        IPBlocklist
25
+	eventStream        EventStream
26
+	logger             Logger
26 27
 }
27 28
 
28 29
 func (p *Proxy) ServeConn(conn net.Conn) {

+ 7
- 6
mtglib/proxy_opts.go Parādīt failu

@@ -3,12 +3,13 @@ package mtglib
3 3
 import "time"
4 4
 
5 5
 type ProxyOpts struct {
6
-	Secret          Secret
7
-	Network         Network
8
-	AntiReplayCache AntiReplayCache
9
-	IPBlocklist     IPBlocklist
10
-	EventStream     EventStream
11
-	Logger          Logger
6
+	Secret             Secret
7
+	Network            Network
8
+	AntiReplayCache    AntiReplayCache
9
+	TimeAttackDetector TimeAttackDetector
10
+	IPBlocklist        IPBlocklist
11
+	EventStream        EventStream
12
+	Logger             Logger
12 13
 
13 14
 	BufferSize  uint
14 15
 	Concurrency uint

+ 36
- 0
timeattack/detector.go Parādīt failu

@@ -0,0 +1,36 @@
1
+package timeattack
2
+
3
+import (
4
+	"fmt"
5
+	"time"
6
+
7
+	"github.com/9seconds/mtg/v2/mtglib"
8
+)
9
+
10
+type detector struct {
11
+	time.Duration
12
+}
13
+
14
+func (d detector) Valid(then time.Time) error {
15
+	now := time.Now()
16
+
17
+	diff := now.Sub(then)
18
+	if diff < 0 {
19
+		diff = -diff
20
+	}
21
+
22
+	if diff > d.Duration {
23
+		return fmt.Errorf("time is invalid. now=%d, then=%d, diff=%v",
24
+			now.Unix(),
25
+			then.Unix(),
26
+			diff)
27
+	}
28
+
29
+	return nil
30
+}
31
+
32
+func NewDetector(duration time.Duration) mtglib.TimeAttackDetector {
33
+	return detector{
34
+		Duration: duration,
35
+	}
36
+}

+ 28
- 0
timeattack/detector_test.go Parādīt failu

@@ -0,0 +1,28 @@
1
+package timeattack_test
2
+
3
+import (
4
+	"testing"
5
+	"time"
6
+
7
+	"github.com/9seconds/mtg/v2/timeattack"
8
+	"github.com/stretchr/testify/suite"
9
+)
10
+
11
+type DetectorTestSuite struct {
12
+	suite.Suite
13
+}
14
+
15
+func (suite *DetectorTestSuite) TestOp() {
16
+	d := timeattack.NewDetector(time.Second)
17
+
18
+	suite.NoError(d.Valid(time.Now()))
19
+	suite.NoError(d.Valid(time.Now().Add(100 * time.Millisecond)))
20
+	suite.NoError(d.Valid(time.Now().Add(-100 * time.Millisecond)))
21
+	suite.Error(d.Valid(time.Now().Add(time.Hour)))
22
+	suite.Error(d.Valid(time.Now().Add(-time.Hour)))
23
+}
24
+
25
+func TestDetector(t *testing.T) {
26
+	t.Parallel()
27
+	suite.Run(t, &DetectorTestSuite{})
28
+}

+ 15
- 0
timeattack/noop.go Parādīt failu

@@ -0,0 +1,15 @@
1
+package timeattack
2
+
3
+import (
4
+	"time"
5
+
6
+	"github.com/9seconds/mtg/v2/mtglib"
7
+)
8
+
9
+type noop struct{}
10
+
11
+func (n noop) Valid(_ time.Time) error { return nil }
12
+
13
+func NewNoop() mtglib.TimeAttackDetector {
14
+	return noop{}
15
+}

+ 26
- 0
timeattack/noop_test.go Parādīt failu

@@ -0,0 +1,26 @@
1
+package timeattack_test
2
+
3
+import (
4
+	"testing"
5
+	"time"
6
+
7
+	"github.com/9seconds/mtg/v2/timeattack"
8
+	"github.com/stretchr/testify/suite"
9
+)
10
+
11
+type NoopTestSuite struct {
12
+	suite.Suite
13
+}
14
+
15
+func (suite *NoopTestSuite) TestOp() {
16
+	d := timeattack.NewNoop()
17
+
18
+	suite.NoError(d.Valid(time.Now()))
19
+	suite.NoError(d.Valid(time.Now().Add(time.Hour)))
20
+	suite.NoError(d.Valid(time.Now().Add(-time.Hour)))
21
+}
22
+
23
+func TestNoop(t *testing.T) {
24
+	t.Parallel()
25
+	suite.Run(t, &NoopTestSuite{})
26
+}

Notiek ielāde…
Atcelt
Saglabāt