mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:04:02 +03:00
Remove idle timeout from network
This commit is contained in:
+3
-4
@@ -40,7 +40,6 @@ func (b *base) ReadConfig(path, version string) error {
|
|||||||
|
|
||||||
func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
|
func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
|
||||||
tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
|
tcpTimeout := conf.Network.Timeout.TCP.Value(network.DefaultTimeout)
|
||||||
idleTimeout := conf.Network.Timeout.Idle.Value(network.DefaultIdleTimeout)
|
|
||||||
httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
|
httpTimeout := conf.Network.Timeout.HTTP.Value(network.DefaultHTTPTimeout)
|
||||||
dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
|
dohIP := conf.Network.DOHIP.Value(net.ParseIP(network.DefaultDOHHostname)).String()
|
||||||
bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
|
bufferSize := conf.TCPBuffer.Value(network.DefaultBufferSize)
|
||||||
@@ -61,14 +60,14 @@ func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network,
|
|||||||
|
|
||||||
switch len(proxyURLs) {
|
switch len(proxyURLs) {
|
||||||
case 0:
|
case 0:
|
||||||
return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout, idleTimeout)
|
return network.NewNetwork(baseDialer, userAgent, dohIP, httpTimeout)
|
||||||
case 1:
|
case 1:
|
||||||
socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
|
socksDialer, err := network.NewSocks5Dialer(baseDialer, proxyURLs[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
|
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout, idleTimeout)
|
return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
|
socksDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, proxyURLs)
|
||||||
@@ -76,5 +75,5 @@ func (b *base) makeNetwork(conf *config.Config, version string) (mtglib.Network,
|
|||||||
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
|
return nil, fmt.Errorf("cannot build socks5 dialer: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout, idleTimeout)
|
return network.NewNetwork(socksDialer, userAgent, dohIP, httpTimeout)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ func (suite *FireholTestSuite) TestRemoteFail() {
|
|||||||
|
|
||||||
func (suite *FireholTestSuite) TestMixed() {
|
func (suite *FireholTestSuite) TestMixed() {
|
||||||
dialer, _ := network.NewDefaultDialer(0, 0)
|
dialer, _ := network.NewDefaultDialer(0, 0)
|
||||||
ntw, _ := network.NewNetwork(dialer, "mtg", "1.1.1.1", 0, 0)
|
ntw, _ := network.NewNetwork(dialer, "mtg", "1.1.1.1", 0)
|
||||||
|
|
||||||
blocklist, err := ipblocklist.NewFirehol(logger.NewNoopLogger(),
|
blocklist, err := ipblocklist.NewFirehol(logger.NewNoopLogger(),
|
||||||
ntw, 2,
|
ntw, 2,
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -26,7 +25,6 @@ type Network interface {
|
|||||||
Dial(network, address string) (net.Conn, error)
|
Dial(network, address string) (net.Conn, error)
|
||||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||||
MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
|
MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
|
||||||
IdleTimeout() time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AntiReplayCache interface {
|
type AntiReplayCache interface {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultTimeout = 10 * time.Second
|
DefaultTimeout = 10 * time.Second
|
||||||
DefaultIdleTimeout = time.Minute
|
|
||||||
DefaultHTTPTimeout = 10 * time.Second
|
DefaultHTTPTimeout = 10 * time.Second
|
||||||
DefaultBufferSize = 4096
|
DefaultBufferSize = 4096
|
||||||
|
|
||||||
|
|||||||
+1
-14
@@ -27,7 +27,6 @@ func (n networkHTTPTransport) RoundTrip(req *http.Request) (*http.Response, erro
|
|||||||
type network struct {
|
type network struct {
|
||||||
dialer Dialer
|
dialer Dialer
|
||||||
dns doh.Resolver
|
dns doh.Resolver
|
||||||
idleTimeout time.Duration
|
|
||||||
httpTimeout time.Duration
|
httpTimeout time.Duration
|
||||||
userAgent string
|
userAgent string
|
||||||
}
|
}
|
||||||
@@ -71,10 +70,6 @@ func (n *network) MakeHTTPClient(dialFunc func(ctx context.Context,
|
|||||||
return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
|
return makeHTTPClient(n.userAgent, n.httpTimeout, dialFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *network) IdleTimeout() time.Duration {
|
|
||||||
return n.idleTimeout
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *network) dnsResolve(protocol, address string) ([]string, error) {
|
func (n *network) dnsResolve(protocol, address string) ([]string, error) {
|
||||||
if net.ParseIP(address) != nil {
|
if net.ParseIP(address) != nil {
|
||||||
return []string{address}, nil
|
return []string{address}, nil
|
||||||
@@ -131,7 +126,7 @@ func (n *network) dnsResolve(protocol, address string) ([]string, error) {
|
|||||||
|
|
||||||
func NewNetwork(dialer Dialer,
|
func NewNetwork(dialer Dialer,
|
||||||
userAgent, dohHostname string,
|
userAgent, dohHostname string,
|
||||||
httpTimeout, idleTimeout time.Duration) (mtglib.Network, error) {
|
httpTimeout time.Duration) (mtglib.Network, error) {
|
||||||
switch {
|
switch {
|
||||||
case httpTimeout < 0:
|
case httpTimeout < 0:
|
||||||
return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
|
return nil, fmt.Errorf("timeout should be positive number %s", httpTimeout)
|
||||||
@@ -139,20 +134,12 @@ func NewNetwork(dialer Dialer,
|
|||||||
httpTimeout = DefaultHTTPTimeout
|
httpTimeout = DefaultHTTPTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
|
||||||
case idleTimeout < 0:
|
|
||||||
return nil, fmt.Errorf("timeout should be positive number %s", idleTimeout)
|
|
||||||
case idleTimeout == 0:
|
|
||||||
idleTimeout = DefaultIdleTimeout
|
|
||||||
}
|
|
||||||
|
|
||||||
if net.ParseIP(dohHostname) == nil {
|
if net.ParseIP(dohHostname) == nil {
|
||||||
return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
|
return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &network{
|
return &network{
|
||||||
dialer: dialer,
|
dialer: dialer,
|
||||||
idleTimeout: idleTimeout,
|
|
||||||
httpTimeout: httpTimeout,
|
httpTimeout: httpTimeout,
|
||||||
userAgent: userAgent,
|
userAgent: userAgent,
|
||||||
dns: doh.Resolver{
|
dns: doh.Resolver{
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import (
|
|||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const statsdSleepTime = 3 * statsd.DefaultFlushInterval
|
||||||
|
|
||||||
type statsdFakeServer struct {
|
type statsdFakeServer struct {
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
buf *bytes.Buffer
|
buf *bytes.Buffer
|
||||||
@@ -100,7 +102,7 @@ func (suite *StatsdTestSuite) TestEventStartFinish() {
|
|||||||
RemoteIP: net.ParseIP("10.0.0.10"),
|
RemoteIP: net.ParseIP("10.0.0.10"),
|
||||||
})
|
})
|
||||||
|
|
||||||
time.Sleep(2 * statsd.DefaultFlushInterval)
|
time.Sleep(statsdSleepTime)
|
||||||
suite.Equal("mtg.active_connections:+1|g|#ip_type:ipv4", suite.statsdServer.String())
|
suite.Equal("mtg.active_connections:+1|g|#ip_type:ipv4", suite.statsdServer.String())
|
||||||
|
|
||||||
suite.statsd.EventFinish(mtglib.EventFinish{
|
suite.statsd.EventFinish(mtglib.EventFinish{
|
||||||
@@ -108,7 +110,7 @@ func (suite *StatsdTestSuite) TestEventStartFinish() {
|
|||||||
ConnID: "connID",
|
ConnID: "connID",
|
||||||
})
|
})
|
||||||
|
|
||||||
time.Sleep(2 * statsd.DefaultFlushInterval)
|
time.Sleep(statsdSleepTime)
|
||||||
suite.Contains(suite.statsdServer.String(), "mtg.session_duration")
|
suite.Contains(suite.statsdServer.String(), "mtg.session_duration")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +119,7 @@ func (suite *StatsdTestSuite) TestEventConcurrencyLimited() {
|
|||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
})
|
})
|
||||||
|
|
||||||
time.Sleep(2 * statsd.DefaultFlushInterval)
|
time.Sleep(statsdSleepTime)
|
||||||
suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
|
suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +129,7 @@ func (suite *StatsdTestSuite) TestEventIPBlocklisted() {
|
|||||||
RemoteIP: net.ParseIP("10.0.0.10"),
|
RemoteIP: net.ParseIP("10.0.0.10"),
|
||||||
})
|
})
|
||||||
|
|
||||||
time.Sleep(2 * statsd.DefaultFlushInterval)
|
time.Sleep(statsdSleepTime)
|
||||||
suite.Equal("mtg.ip_blocklisted:1|c|#ip_type:ipv4", suite.statsdServer.String())
|
suite.Equal("mtg.ip_blocklisted:1|c|#ip_type:ipv4", suite.statsdServer.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
@@ -29,7 +28,3 @@ func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
|
|||||||
network, address string) (net.Conn, error)) *http.Client {
|
network, address string) (net.Conn, error)) *http.Client {
|
||||||
return m.Called(dialFunc).Get(0).(*http.Client)
|
return m.Called(dialFunc).Get(0).(*http.Client)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MtglibNetworkMock) IdleTimeout() time.Duration {
|
|
||||||
return m.Called().Get(0).(time.Duration)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user