mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:04:02 +03:00
Merge pull request #430 from 9seconds/golang-idiomatic
More idiomatic Golang
This commit is contained in:
@@ -52,10 +52,10 @@ type Config struct {
|
|||||||
Blocklist ListConfig `json:"blocklist"`
|
Blocklist ListConfig `json:"blocklist"`
|
||||||
Allowlist ListConfig `json:"allowlist"`
|
Allowlist ListConfig `json:"allowlist"`
|
||||||
Doppelganger struct {
|
Doppelganger struct {
|
||||||
URLs []TypeHttpsURL `json:"urls"`
|
URLs []TypeHttpsURL `json:"urls"`
|
||||||
Repeats TypeConcurrency `json:"repeats_per_raid"`
|
Repeats TypeConcurrency `json:"repeats_per_raid"`
|
||||||
UpdateEach TypeDuration `json:"raid_each"`
|
UpdateEach TypeDuration `json:"raid_each"`
|
||||||
DRS TypeBool `json:"drs"`
|
DRS TypeBool `json:"drs"`
|
||||||
} `json:"doppelganger"`
|
} `json:"doppelganger"`
|
||||||
} `json:"defense"`
|
} `json:"defense"`
|
||||||
Network struct {
|
Network struct {
|
||||||
|
|||||||
@@ -47,10 +47,10 @@ type tomlConfig struct {
|
|||||||
UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
|
UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
|
||||||
} `toml:"allowlist" json:"allowlist,omitempty"`
|
} `toml:"allowlist" json:"allowlist,omitempty"`
|
||||||
Doppelganger struct {
|
Doppelganger struct {
|
||||||
URLs []string `toml:"urls" json:"urls,omitempty"`
|
URLs []string `toml:"urls" json:"urls,omitempty"`
|
||||||
Repeats uint `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
|
Repeats uint `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
|
||||||
UpdateEach string `toml:"raid-each" json:"raid_each,omitempty"`
|
UpdateEach string `toml:"raid-each" json:"raid_each,omitempty"`
|
||||||
DRS bool `toml:"drs" json:"drs,omitempty"`
|
DRS bool `toml:"drs" json:"drs,omitempty"`
|
||||||
} `toml:"doppelganger" json:"doppelganger,omitempty"`
|
} `toml:"doppelganger" json:"doppelganger,omitempty"`
|
||||||
} `toml:"defense" json:"defense,omitempty"`
|
} `toml:"defense" json:"defense,omitempty"`
|
||||||
Network struct {
|
Network struct {
|
||||||
|
|||||||
+14
-17
@@ -3,6 +3,7 @@ package mtglib
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
@@ -102,7 +103,7 @@ func newConnProxyProtocol(source, target essentials.Conn) *connProxyProtocol {
|
|||||||
// Both directions update the same timestamp so that activity in one direction
|
// Both directions update the same timestamp so that activity in one direction
|
||||||
// prevents the other (idle) direction from timing out.
|
// prevents the other (idle) direction from timing out.
|
||||||
type idleTracker struct {
|
type idleTracker struct {
|
||||||
lastActive atomic.Int64 // unix nanos
|
lastActive atomic.Pointer[time.Time]
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,13 +115,12 @@ func newIdleTracker(timeout time.Duration) *idleTracker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *idleTracker) touch() {
|
func (t *idleTracker) touch() {
|
||||||
t.lastActive.Store(time.Now().UnixNano())
|
stamp := time.Now()
|
||||||
|
t.lastActive.Store(&stamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *idleTracker) isIdle() bool {
|
func (t *idleTracker) isIdle() bool {
|
||||||
last := time.Unix(0, t.lastActive.Load())
|
return time.Since(*t.lastActive.Load()) >= t.timeout
|
||||||
|
|
||||||
return time.Since(last) >= t.timeout
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type connIdleTimeout struct {
|
type connIdleTimeout struct {
|
||||||
@@ -130,25 +130,22 @@ type connIdleTimeout struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c connIdleTimeout) Read(b []byte) (int, error) {
|
func (c connIdleTimeout) Read(b []byte) (int, error) {
|
||||||
|
var netErr net.Error
|
||||||
|
|
||||||
for {
|
for {
|
||||||
c.SetReadDeadline(time.Now().Add(c.tracker.timeout)) //nolint: errcheck
|
c.SetReadDeadline(time.Now().Add(c.tracker.timeout)) //nolint: errcheck
|
||||||
|
|
||||||
n, err := c.Conn.Read(b)
|
n, err := c.Conn.Read(b)
|
||||||
if n > 0 {
|
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
c.tracker.touch()
|
c.tracker.touch()
|
||||||
|
return n, nil
|
||||||
return n, err //nolint: wrapcheck
|
case errors.As(err, &netErr) && netErr.Timeout() && !c.tracker.isIdle():
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
return n, err
|
||||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() && !c.tracker.isIdle() { //nolint: errorlint
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, err //nolint: wrapcheck
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -160,7 +160,6 @@ type ProxyOpts struct {
|
|||||||
|
|
||||||
// DoppelGangerDRS defines if TLS Dynamic Record Sizing is active.
|
// DoppelGangerDRS defines if TLS Dynamic Record Sizing is active.
|
||||||
DoppelGangerDRS bool
|
DoppelGangerDRS bool
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p ProxyOpts) valid() error {
|
func (p ProxyOpts) valid() error {
|
||||||
|
|||||||
Reference in New Issue
Block a user