mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 18:44:02 +03:00
Get rid of buffersize everywhere
This commit is contained in:
@@ -61,6 +61,8 @@ const (
|
|||||||
DefaultConcurrency = 4096
|
DefaultConcurrency = 4096
|
||||||
|
|
||||||
// DefaultBufferSize is a default size of a copy buffer.
|
// DefaultBufferSize is a default size of a copy buffer.
|
||||||
|
//
|
||||||
|
// Deprecated: this setting no longer makes any effect.
|
||||||
DefaultBufferSize = 16 * 1024 // 16 kib
|
DefaultBufferSize = 16 * 1024 // 16 kib
|
||||||
|
|
||||||
// DefaultDomainFrontingPort is a default port (HTTPS) to connect to in
|
// DefaultDomainFrontingPort is a default port (HTTPS) to connect to in
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package relay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type conn struct {
|
|
||||||
net.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c conn) Read(p []byte) (int, error) {
|
|
||||||
if err := c.SetReadDeadline(time.Now().Add(getTimeout())); err != nil {
|
|
||||||
return 0, fmt.Errorf("cannot set read deadline: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Conn.Read(p) // nolint: wrapcheck
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,7 @@
|
|||||||
package relay
|
package relay
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ConnectionTimeToLiveMin = 2 * time.Minute
|
bufferSize = 32 * 1024
|
||||||
ConnectionTimeToLiveMax = 10 * time.Minute
|
|
||||||
TimeoutMin = 20 * time.Second
|
|
||||||
TimeoutMax = time.Minute
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
|
|||||||
@@ -9,21 +9,16 @@ type eastWest struct {
|
|||||||
|
|
||||||
var eastWestPool = sync.Pool{
|
var eastWestPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return &eastWest{}
|
return &eastWest{
|
||||||
|
east: make([]byte, bufferSize),
|
||||||
|
west: make([]byte, bufferSize),
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func acquireEastWest(bufferSize int) *eastWest {
|
func acquireEastWest() *eastWest {
|
||||||
wanted := eastWestPool.Get().(*eastWest) // nolint: forcetypeassert
|
wanted := eastWestPool.Get().(*eastWest) // nolint: forcetypeassert
|
||||||
|
|
||||||
if len(wanted.east) != bufferSize {
|
|
||||||
wanted.east = make([]byte, bufferSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(wanted.west) != bufferSize {
|
|
||||||
wanted.west = make([]byte, bufferSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
return wanted
|
return wanted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,16 +3,14 @@ package relay
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Relay(ctx context.Context, log Logger, bufferSize int,
|
func Relay(ctx context.Context, log Logger, telegramConn, clientConn io.ReadWriteCloser) {
|
||||||
telegramConn net.Conn, clientConn io.ReadWriteCloser) {
|
|
||||||
defer telegramConn.Close()
|
defer telegramConn.Close()
|
||||||
defer clientConn.Close()
|
defer clientConn.Close()
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, getConnectionTimeToLive())
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -21,13 +19,9 @@ func Relay(ctx context.Context, log Logger, bufferSize int,
|
|||||||
clientConn.Close()
|
clientConn.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
buffers := acquireEastWest(bufferSize)
|
buffers := acquireEastWest()
|
||||||
defer releaseEastWest(buffers)
|
defer releaseEastWest(buffers)
|
||||||
|
|
||||||
telegramConn = conn{
|
|
||||||
Conn: telegramConn,
|
|
||||||
}
|
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
wg.Add(2) // nolint: gomnd
|
wg.Add(2) // nolint: gomnd
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ func (suite *RelayTestSuite) TearDownTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (suite *RelayTestSuite) TestExit() {
|
func (suite *RelayTestSuite) TestExit() {
|
||||||
suite.telegramConnMock.On("SetReadDeadline", mock.Anything).Return(nil)
|
|
||||||
suite.telegramConnMock.On("Close").Return(nil)
|
suite.telegramConnMock.On("Close").Return(nil)
|
||||||
suite.telegramConnMock.On("Read", mock.Anything).Return(10, io.EOF).Once()
|
suite.telegramConnMock.On("Read", mock.Anything).Return(10, io.EOF).Once()
|
||||||
suite.telegramConnMock.On("Write", mock.Anything).Return(10, io.EOF).Maybe()
|
suite.telegramConnMock.On("Write", mock.Anything).Return(10, io.EOF).Maybe()
|
||||||
@@ -46,8 +45,7 @@ func (suite *RelayTestSuite) TestExit() {
|
|||||||
suite.clientConnMock.On("Write", mock.Anything).Return(10, io.EOF).Maybe()
|
suite.clientConnMock.On("Write", mock.Anything).Return(10, io.EOF).Maybe()
|
||||||
suite.clientConnMock.On("Close").Return(nil)
|
suite.clientConnMock.On("Close").Return(nil)
|
||||||
|
|
||||||
relay.Relay(suite.ctx, suite.loggerMock, 1024,
|
relay.Relay(suite.ctx, suite.loggerMock, suite.telegramConnMock, suite.clientConnMock)
|
||||||
suite.telegramConnMock, suite.clientConnMock)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRelay(t *testing.T) {
|
func TestRelay(t *testing.T) {
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package relay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func getConnectionTimeToLive() time.Duration {
|
|
||||||
return getTime(ConnectionTimeToLiveMin, ConnectionTimeToLiveMax)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTimeout() time.Duration {
|
|
||||||
return getTime(TimeoutMin, TimeoutMax)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTime(minDuration, maxDuration time.Duration) time.Duration {
|
|
||||||
minDurationInSeconds := int(minDuration.Seconds())
|
|
||||||
maxDurationInSeconds := int(maxDuration.Seconds())
|
|
||||||
number := minDurationInSeconds + rand.Intn(maxDurationInSeconds-minDurationInSeconds)
|
|
||||||
|
|
||||||
return time.Duration(number) * time.Second
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
package relay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/suite"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TimeoutsTestSuite struct {
|
|
||||||
suite.Suite
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *TimeoutsTestSuite) TestGetConnectionTimeToLive() {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
value := getConnectionTimeToLive()
|
|
||||||
message := fmt.Sprintf("generated value is %v", value)
|
|
||||||
|
|
||||||
suite.GreaterOrEqual(value, ConnectionTimeToLiveMin, message)
|
|
||||||
suite.LessOrEqual(value, ConnectionTimeToLiveMax, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *TimeoutsTestSuite) TestGetTimeout() {
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
value := getTimeout()
|
|
||||||
message := fmt.Sprintf("generated value is %v", value)
|
|
||||||
|
|
||||||
suite.GreaterOrEqual(value, TimeoutMin, message)
|
|
||||||
suite.LessOrEqual(value, TimeoutMax, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTimeouts(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
suite.Run(t, &TimeoutsTestSuite{})
|
|
||||||
}
|
|
||||||
@@ -25,7 +25,6 @@ type Proxy struct {
|
|||||||
|
|
||||||
allowFallbackOnUnknownDC bool
|
allowFallbackOnUnknownDC bool
|
||||||
tolerateTimeSkewness time.Duration
|
tolerateTimeSkewness time.Duration
|
||||||
bufferSize int
|
|
||||||
domainFrontingPort int
|
domainFrontingPort int
|
||||||
workerPool *ants.PoolWithFunc
|
workerPool *ants.PoolWithFunc
|
||||||
telegram *telegram.Telegram
|
telegram *telegram.Telegram
|
||||||
@@ -84,7 +83,6 @@ func (p *Proxy) ServeConn(conn net.Conn) {
|
|||||||
relay.Relay(
|
relay.Relay(
|
||||||
ctx,
|
ctx,
|
||||||
ctx.logger.Named("relay"),
|
ctx.logger.Named("relay"),
|
||||||
p.bufferSize,
|
|
||||||
ctx.telegramConn,
|
ctx.telegramConn,
|
||||||
ctx.clientConn,
|
ctx.clientConn,
|
||||||
)
|
)
|
||||||
@@ -267,7 +265,6 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
|
|||||||
relay.Relay(
|
relay.Relay(
|
||||||
ctx,
|
ctx,
|
||||||
ctx.logger.Named("domain-fronting"),
|
ctx.logger.Named("domain-fronting"),
|
||||||
p.bufferSize,
|
|
||||||
frontConn,
|
frontConn,
|
||||||
conn,
|
conn,
|
||||||
)
|
)
|
||||||
@@ -296,7 +293,6 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) {
|
|||||||
logger: opts.getLogger("proxy"),
|
logger: opts.getLogger("proxy"),
|
||||||
domainFrontingPort: opts.getDomainFrontingPort(),
|
domainFrontingPort: opts.getDomainFrontingPort(),
|
||||||
tolerateTimeSkewness: opts.getTolerateTimeSkewness(),
|
tolerateTimeSkewness: opts.getTolerateTimeSkewness(),
|
||||||
bufferSize: opts.getBufferSize(),
|
|
||||||
allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
|
allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
|
||||||
telegram: tg,
|
telegram: tg,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,14 +131,6 @@ func (p ProxyOpts) valid() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p ProxyOpts) getBufferSize() int {
|
|
||||||
if p.BufferSize < 1 {
|
|
||||||
return DefaultBufferSize
|
|
||||||
}
|
|
||||||
|
|
||||||
return int(p.BufferSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p ProxyOpts) getConcurrency() int {
|
func (p ProxyOpts) getConcurrency() int {
|
||||||
if p.Concurrency == 0 {
|
if p.Concurrency == 0 {
|
||||||
return DefaultConcurrency
|
return DefaultConcurrency
|
||||||
|
|||||||
@@ -30,11 +30,6 @@ func (suite *DefaultDialerTestSuite) TestNegativeTimeout() {
|
|||||||
suite.Error(err)
|
suite.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *DefaultDialerTestSuite) TestNegativeBufferSize() {
|
|
||||||
_, err := network.NewDefaultDialer(0, -1)
|
|
||||||
suite.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
|
func (suite *DefaultDialerTestSuite) TestUnsupportedProtocol() {
|
||||||
_, err := suite.d.DialContext(context.Background(),
|
_, err := suite.d.DialContext(context.Background(),
|
||||||
"udp",
|
"udp",
|
||||||
|
|||||||
Reference in New Issue
Block a user