Explorar el Código

Merge pull request #416 from dolonet/fix/domain-fronting-idle-timeout

fix: apply idle timeout to domain fronting relay
tags/v2.2.5^2^2
Sergei Arkhipov hace 1 mes
padre
commit
735466b90d
No account linked to committer's email address
Se han modificado 4 ficheros con 34 adiciones y 2 borrados
  1. 2
    0
      internal/cli/run_proxy.go
  2. 19
    0
      mtglib/conns.go
  3. 5
    2
      mtglib/proxy.go
  4. 8
    0
      mtglib/proxy_opts.go

+ 2
- 0
internal/cli/run_proxy.go Ver fichero

@@ -5,6 +5,7 @@ import (
5 5
 	"fmt"
6 6
 	"net"
7 7
 	"os"
8
+	"time"
8 9
 
9 10
 	"github.com/9seconds/mtg/v2/antireplay"
10 11
 	"github.com/9seconds/mtg/v2/events"
@@ -262,6 +263,7 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen
262 263
 
263 264
 		AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),
264 265
 		TolerateTimeSkewness:     conf.TolerateTimeSkewness.Value,
266
+		IdleTimeout:              conf.Network.Timeout.Idle.Get(time.Minute),
265 267
 
266 268
 		DoppelGangerURLs:    doppelGangerURLs,
267 269
 		DoppelGangerPerRaid: conf.Defense.Doppelganger.Repeats.Get(mtglib.DoppelGangerPerRaid),

+ 19
- 0
mtglib/conns.go Ver fichero

@@ -6,6 +6,7 @@ import (
6 6
 	"fmt"
7 7
 	"io"
8 8
 	"net"
9
+	"time"
9 10
 
10 11
 	"github.com/9seconds/mtg/v2/essentials"
11 12
 	"github.com/pires/go-proxyproto"
@@ -95,3 +96,21 @@ func newConnProxyProtocol(source, target essentials.Conn) *connProxyProtocol {
95 96
 		sourceAddr: source.RemoteAddr(),
96 97
 	}
97 98
 }
99
+
100
+type connIdleTimeout struct {
101
+	essentials.Conn
102
+
103
+	timeout time.Duration
104
+}
105
+
106
+func (c connIdleTimeout) Read(b []byte) (int, error) {
107
+	c.SetReadDeadline(time.Now().Add(c.timeout)) //nolint: errcheck
108
+
109
+	return c.Conn.Read(b) //nolint: wrapcheck
110
+}
111
+
112
+func (c connIdleTimeout) Write(b []byte) (int, error) {
113
+	c.SetWriteDeadline(time.Now().Add(c.timeout)) //nolint: errcheck
114
+
115
+	return c.Conn.Write(b) //nolint: wrapcheck
116
+}

+ 5
- 2
mtglib/proxy.go Ver fichero

@@ -27,6 +27,7 @@ type Proxy struct {
27 27
 
28 28
 	allowFallbackOnUnknownDC    bool
29 29
 	tolerateTimeSkewness        time.Duration
30
+	idleTimeout                 time.Duration
30 31
 	domainFrontingPort          int
31 32
 	domainFrontingIP            string
32 33
 	domainFrontingProxyProtocol bool
@@ -151,6 +152,7 @@ func (p *Proxy) Serve(listener net.Listener) error {
151 152
 		case errors.Is(err, ants.ErrPoolClosed):
152 153
 			return nil
153 154
 		case errors.Is(err, ants.ErrPoolOverload):
155
+			conn.Close() //nolint: errcheck
154 156
 			logger.Info("connection was concurrency limited")
155 157
 			p.eventStream.Send(p.ctx, NewEventConcurrencyLimited())
156 158
 		}
@@ -306,8 +308,8 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
306 308
 	relay.Relay(
307 309
 		ctx,
308 310
 		ctx.logger.Named("domain-fronting"),
309
-		frontConn,
310
-		conn,
311
+		connIdleTimeout{Conn: frontConn, timeout: p.idleTimeout},
312
+		connIdleTimeout{Conn: conn, timeout: p.idleTimeout},
311 313
 	)
312 314
 }
313 315
 
@@ -339,6 +341,7 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) {
339 341
 		domainFrontingPort:       opts.getDomainFrontingPort(),
340 342
 		domainFrontingIP:         opts.DomainFrontingIP,
341 343
 		tolerateTimeSkewness:     opts.getTolerateTimeSkewness(),
344
+		idleTimeout:              opts.getIdleTimeout(),
342 345
 		allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
343 346
 		telegram:                 tg,
344 347
 		doppelGanger: doppel.NewGanger(

+ 8
- 0
mtglib/proxy_opts.go Ver fichero

@@ -216,6 +216,14 @@ func (p ProxyOpts) getPreferIP() string {
216 216
 	return p.PreferIP
217 217
 }
218 218
 
219
+func (p ProxyOpts) getIdleTimeout() time.Duration {
220
+	if p.IdleTimeout == 0 {
221
+		return time.Minute
222
+	}
223
+
224
+	return p.IdleTimeout
225
+}
226
+
219 227
 func (p ProxyOpts) getLogger(name string) Logger {
220 228
 	return p.Logger.Named(name)
221 229
 }

Loading…
Cancelar
Guardar