Ver código fonte

Merge pull request #430 from 9seconds/golang-idiomatic

More idiomatic Golang
tags/v2.2.7^2^2
Sergei Arkhipov 1 mês atrás
pai
commit
3793558c4c
Nenhuma conta vinculada ao e-mail do autor do commit
4 arquivos alterados com 22 adições e 26 exclusões
  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 Ver arquivo

@@ -52,10 +52,10 @@ type Config struct {
52 52
 		Blocklist    ListConfig `json:"blocklist"`
53 53
 		Allowlist    ListConfig `json:"allowlist"`
54 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 59
 		} `json:"doppelganger"`
60 60
 	} `json:"defense"`
61 61
 	Network struct {

+ 4
- 4
internal/config/parse.go Ver arquivo

@@ -47,10 +47,10 @@ type tomlConfig struct {
47 47
 			UpdateEach          string   `toml:"update-each" json:"updateEach,omitempty"`
48 48
 		} `toml:"allowlist" json:"allowlist,omitempty"`
49 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 54
 		} `toml:"doppelganger" json:"doppelganger,omitempty"`
55 55
 	} `toml:"defense" json:"defense,omitempty"`
56 56
 	Network struct {

+ 14
- 17
mtglib/conns.go Ver arquivo

@@ -3,6 +3,7 @@ package mtglib
3 3
 import (
4 4
 	"bytes"
5 5
 	"context"
6
+	"errors"
6 7
 	"fmt"
7 8
 	"io"
8 9
 	"net"
@@ -102,7 +103,7 @@ func newConnProxyProtocol(source, target essentials.Conn) *connProxyProtocol {
102 103
 // Both directions update the same timestamp so that activity in one direction
103 104
 // prevents the other (idle) direction from timing out.
104 105
 type idleTracker struct {
105
-	lastActive atomic.Int64 // unix nanos
106
+	lastActive atomic.Pointer[time.Time]
106 107
 	timeout    time.Duration
107 108
 }
108 109
 
@@ -114,13 +115,12 @@ func newIdleTracker(timeout time.Duration) *idleTracker {
114 115
 }
115 116
 
116 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 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 126
 type connIdleTimeout struct {
@@ -130,25 +130,22 @@ type connIdleTimeout struct {
130 130
 }
131 131
 
132 132
 func (c connIdleTimeout) Read(b []byte) (int, error) {
133
+	var netErr net.Error
134
+
133 135
 	for {
134 136
 		c.SetReadDeadline(time.Now().Add(c.tracker.timeout)) //nolint: errcheck
135 137
 
136 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 Ver arquivo

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

Carregando…
Cancelar
Salvar