Просмотр исходного кода

More idiomatic Golang

tags/v2.2.7^2^2
9seconds 1 месяц назад
Родитель
Сommit
b6427ee321
4 измененных файлов: 22 добавлений и 26 удалений
  1. 4
    4
      internal/config/config.go
  2. 4
    4
      internal/config/parse.go
  3. 14
    17
      mtglib/conns.go
  4. 0
    1
      mtglib/proxy_opts.go

+ 4
- 4
internal/config/config.go Просмотреть файл

52
 		Blocklist    ListConfig `json:"blocklist"`
52
 		Blocklist    ListConfig `json:"blocklist"`
53
 		Allowlist    ListConfig `json:"allowlist"`
53
 		Allowlist    ListConfig `json:"allowlist"`
54
 		Doppelganger struct {
54
 		Doppelganger struct {
55
-			URLs            []TypeHttpsURL  `json:"urls"`
56
-			Repeats         TypeConcurrency `json:"repeats_per_raid"`
57
-			UpdateEach      TypeDuration    `json:"raid_each"`
58
-			DRS             TypeBool        `json:"drs"`
55
+			URLs       []TypeHttpsURL  `json:"urls"`
56
+			Repeats    TypeConcurrency `json:"repeats_per_raid"`
57
+			UpdateEach TypeDuration    `json:"raid_each"`
58
+			DRS        TypeBool        `json:"drs"`
59
 		} `json:"doppelganger"`
59
 		} `json:"doppelganger"`
60
 	} `json:"defense"`
60
 	} `json:"defense"`
61
 	Network struct {
61
 	Network struct {

+ 4
- 4
internal/config/parse.go Просмотреть файл

47
 			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
47
 			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
48
 		} `toml:"allowlist" json:"allowlist,omitempty"`
48
 		} `toml:"allowlist" json:"allowlist,omitempty"`
49
 		Doppelganger struct {
49
 		Doppelganger struct {
50
-			URLs            []string `toml:"urls" json:"urls,omitempty"`
51
-			Repeats         uint     `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
52
-			UpdateEach      string   `toml:"raid-each" json:"raid_each,omitempty"`
53
-			DRS             bool     `toml:"drs" json:"drs,omitempty"`
50
+			URLs       []string `toml:"urls" json:"urls,omitempty"`
51
+			Repeats    uint     `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
52
+			UpdateEach string   `toml:"raid-each" json:"raid_each,omitempty"`
53
+			DRS        bool     `toml:"drs" json:"drs,omitempty"`
54
 		} `toml:"doppelganger" json:"doppelganger,omitempty"`
54
 		} `toml:"doppelganger" json:"doppelganger,omitempty"`
55
 	} `toml:"defense" json:"defense,omitempty"`
55
 	} `toml:"defense" json:"defense,omitempty"`
56
 	Network struct {
56
 	Network struct {

+ 14
- 17
mtglib/conns.go Просмотреть файл

3
 import (
3
 import (
4
 	"bytes"
4
 	"bytes"
5
 	"context"
5
 	"context"
6
+	"errors"
6
 	"fmt"
7
 	"fmt"
7
 	"io"
8
 	"io"
8
 	"net"
9
 	"net"
102
 // Both directions update the same timestamp so that activity in one direction
103
 // Both directions update the same timestamp so that activity in one direction
103
 // prevents the other (idle) direction from timing out.
104
 // prevents the other (idle) direction from timing out.
104
 type idleTracker struct {
105
 type idleTracker struct {
105
-	lastActive atomic.Int64 // unix nanos
106
+	lastActive atomic.Pointer[time.Time]
106
 	timeout    time.Duration
107
 	timeout    time.Duration
107
 }
108
 }
108
 
109
 
114
 }
115
 }
115
 
116
 
116
 func (t *idleTracker) touch() {
117
 func (t *idleTracker) touch() {
117
-	t.lastActive.Store(time.Now().UnixNano())
118
+	stamp := time.Now()
119
+	t.lastActive.Store(&stamp)
118
 }
120
 }
119
 
121
 
120
 func (t *idleTracker) isIdle() bool {
122
 func (t *idleTracker) isIdle() bool {
121
-	last := time.Unix(0, t.lastActive.Load())
122
-
123
-	return time.Since(last) >= t.timeout
123
+	return time.Since(*t.lastActive.Load()) >= t.timeout
124
 }
124
 }
125
 
125
 
126
 type connIdleTimeout struct {
126
 type connIdleTimeout struct {
130
 }
130
 }
131
 
131
 
132
 func (c connIdleTimeout) Read(b []byte) (int, error) {
132
 func (c connIdleTimeout) Read(b []byte) (int, error) {
133
+	var netErr net.Error
134
+
133
 	for {
135
 	for {
134
 		c.SetReadDeadline(time.Now().Add(c.tracker.timeout)) //nolint: errcheck
136
 		c.SetReadDeadline(time.Now().Add(c.tracker.timeout)) //nolint: errcheck
135
 
137
 
136
 		n, err := c.Conn.Read(b)
138
 		n, err := c.Conn.Read(b)
137
-		if n > 0 {
138
-			c.tracker.touch()
139
-
140
-			return n, err //nolint: wrapcheck
141
-		}
142
-
143
-		if err != nil {
144
-			if netErr, ok := err.(net.Error); ok && netErr.Timeout() && !c.tracker.isIdle() { //nolint: errorlint
145
-				continue
146
-			}
147
 
139
 
148
-			return 0, err //nolint: wrapcheck
140
+		switch {
141
+		case err == nil:
142
+			c.tracker.touch()
143
+			return n, nil
144
+		case errors.As(err, &netErr) && netErr.Timeout() && !c.tracker.isIdle():
145
+			continue
149
 		}
146
 		}
150
 
147
 
151
-		return 0, nil
148
+		return n, err
152
 	}
149
 	}
153
 }
150
 }
154
 
151
 

+ 0
- 1
mtglib/proxy_opts.go Просмотреть файл

160
 
160
 
161
 	// DoppelGangerDRS defines if TLS Dynamic Record Sizing is active.
161
 	// DoppelGangerDRS defines if TLS Dynamic Record Sizing is active.
162
 	DoppelGangerDRS bool
162
 	DoppelGangerDRS bool
163
-
164
 }
163
 }
165
 
164
 
166
 func (p ProxyOpts) valid() error {
165
 func (p ProxyOpts) valid() error {

Загрузка…
Отмена
Сохранить