mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:34:02 +03:00
Add new TCPBufferSize parameter to network
This commit is contained in:
@@ -27,6 +27,7 @@ type Network interface {
|
|||||||
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
|
IdleTimeout() time.Duration
|
||||||
|
TCPBufferSize() int
|
||||||
}
|
}
|
||||||
|
|
||||||
type AntiReplayCache interface {
|
type AntiReplayCache interface {
|
||||||
|
|||||||
@@ -60,6 +60,10 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
|
|||||||
return tcpConn, nil
|
return tcpConn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *defaultDialer) TCPBufferSize() int {
|
||||||
|
return d.bufferSize
|
||||||
|
}
|
||||||
|
|
||||||
func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
|
func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
|
||||||
switch {
|
switch {
|
||||||
case timeout < 0:
|
case timeout < 0:
|
||||||
|
|||||||
@@ -71,6 +71,10 @@ func (suite *DefaultDialerTestSuite) TestHTTPRequest() {
|
|||||||
suite.Equal(http.StatusOK, resp.StatusCode)
|
suite.Equal(http.StatusOK, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *DefaultDialerTestSuite) TestTCPBufferSize() {
|
||||||
|
suite.Equal(network.DefaultBufferSize, suite.d.TCPBufferSize())
|
||||||
|
}
|
||||||
|
|
||||||
func TestDefaultDialer(t *testing.T) {
|
func TestDefaultDialer(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
suite.Run(t, &DefaultDialerTestSuite{})
|
suite.Run(t, &DefaultDialerTestSuite{})
|
||||||
|
|||||||
@@ -29,4 +29,5 @@ var (
|
|||||||
type Dialer interface {
|
type Dialer 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)
|
||||||
|
TCPBufferSize() int
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,3 +22,7 @@ func (d *DialerMock) DialContext(ctx context.Context, network, address string) (
|
|||||||
|
|
||||||
return args.Get(0).(net.Conn), args.Error(1)
|
return args.Get(0).(net.Conn), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DialerMock) TCPBufferSize() int {
|
||||||
|
return d.Called().Int(0)
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ func (d *DialerMock) DialContext(ctx context.Context, network, address string) (
|
|||||||
return args.Get(0).(net.Conn), args.Error(1)
|
return args.Get(0).(net.Conn), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *DialerMock) TCPBufferSize() int {
|
||||||
|
return d.Called().Int(0)
|
||||||
|
}
|
||||||
|
|
||||||
type HTTPServerTestSuite struct {
|
type HTTPServerTestSuite struct {
|
||||||
httpServer *httptest.Server
|
httpServer *httptest.Server
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,12 +10,17 @@ import (
|
|||||||
|
|
||||||
type loadBalancedSocks5Dialer struct {
|
type loadBalancedSocks5Dialer struct {
|
||||||
dialers []Dialer
|
dialers []Dialer
|
||||||
|
bufferSize int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l loadBalancedSocks5Dialer) Dial(network, address string) (net.Conn, error) {
|
func (l loadBalancedSocks5Dialer) Dial(network, address string) (net.Conn, error) {
|
||||||
return l.DialContext(context.Background(), network, address)
|
return l.DialContext(context.Background(), network, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l loadBalancedSocks5Dialer) TCPBufferSize() int {
|
||||||
|
return l.bufferSize
|
||||||
|
}
|
||||||
|
|
||||||
func (l loadBalancedSocks5Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
func (l loadBalancedSocks5Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
length := len(l.dialers)
|
length := len(l.dialers)
|
||||||
start := rand.Intn(length)
|
start := rand.Intn(length)
|
||||||
@@ -46,5 +51,6 @@ func NewLoadBalancedSocks5Dialer(baseDialer Dialer, proxyURLs []*url.URL) (Diale
|
|||||||
|
|
||||||
return loadBalancedSocks5Dialer{
|
return loadBalancedSocks5Dialer{
|
||||||
dialers: dialers,
|
dialers: dialers,
|
||||||
|
bufferSize: baseDialer.TCPBufferSize(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ func (suite *LoadBalancedSocks5TestSuite) TestCannotDial() {
|
|||||||
baseDialer.On("DialContext", mock.Anything, "tcp", "127.0.0.2:1080").
|
baseDialer.On("DialContext", mock.Anything, "tcp", "127.0.0.2:1080").
|
||||||
Times(network.ProxyDialerOpenThreshold).
|
Times(network.ProxyDialerOpenThreshold).
|
||||||
Return(&net.TCPConn{}, io.EOF)
|
Return(&net.TCPConn{}, io.EOF)
|
||||||
|
baseDialer.On("TCPBufferSize").Return(network.DefaultBufferSize)
|
||||||
|
|
||||||
lbDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, []*url.URL{
|
lbDialer, err := network.NewLoadBalancedSocks5Dialer(baseDialer, []*url.URL{
|
||||||
{Scheme: "socks5", User: url.UserPassword("user", "password"), Host: "127.0.0.1:1080"},
|
{Scheme: "socks5", User: url.UserPassword("user", "password"), Host: "127.0.0.1:1080"},
|
||||||
|
|||||||
@@ -75,6 +75,10 @@ func (n *network) IdleTimeout() time.Duration {
|
|||||||
return n.idleTimeout
|
return n.idleTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *network) TCPBufferSize() int {
|
||||||
|
return n.dialer.TCPBufferSize()
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
+19
-1
@@ -2,16 +2,34 @@ package network
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"golang.org/x/net/proxy"
|
"golang.org/x/net/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type socks5Dialer struct {
|
||||||
|
proxy.ContextDialer
|
||||||
|
|
||||||
|
bufferSize int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s socks5Dialer) Dial(protocol, address string) (net.Conn, error) {
|
||||||
|
return s.ContextDialer.(proxy.Dialer).Dial(protocol, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s socks5Dialer) TCPBufferSize() int {
|
||||||
|
return s.bufferSize
|
||||||
|
}
|
||||||
|
|
||||||
func NewSocks5Dialer(baseDialer Dialer, proxyURL *url.URL) (Dialer, error) {
|
func NewSocks5Dialer(baseDialer Dialer, proxyURL *url.URL) (Dialer, error) {
|
||||||
rv, err := proxy.FromURL(proxyURL, baseDialer)
|
rv, err := proxy.FromURL(proxyURL, baseDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
|
return nil, fmt.Errorf("cannot initialize socks5 proxy dialer: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv.(Dialer), nil
|
return socks5Dialer{
|
||||||
|
ContextDialer: rv.(proxy.ContextDialer),
|
||||||
|
bufferSize: baseDialer.TCPBufferSize(),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,3 +33,7 @@ func (m *MtglibNetworkMock) MakeHTTPClient(dialFunc func(ctx context.Context,
|
|||||||
func (m *MtglibNetworkMock) IdleTimeout() time.Duration {
|
func (m *MtglibNetworkMock) IdleTimeout() time.Duration {
|
||||||
return m.Called().Get(0).(time.Duration)
|
return m.Called().Get(0).(time.Duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MtglibNetworkMock) TCPBufferSize() int {
|
||||||
|
return m.Called().Int(0)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user