mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 10:54:02 +03:00
Deprecate bufferSize
This commit is contained in:
@@ -38,10 +38,9 @@ func makeNetwork(conf *config.Config, version string) (mtglib.Network, error) {
|
|||||||
tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
|
tcpTimeout := conf.Network.Timeout.TCP.Get(network.DefaultTimeout)
|
||||||
httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
|
httpTimeout := conf.Network.Timeout.HTTP.Get(network.DefaultHTTPTimeout)
|
||||||
dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
|
dohIP := conf.Network.DOHIP.Get(net.ParseIP(network.DefaultDOHHostname)).String()
|
||||||
bufferSize := conf.TCPBuffer.Get(network.DefaultBufferSize)
|
|
||||||
userAgent := "mtg/" + version
|
userAgent := "mtg/" + version
|
||||||
|
|
||||||
baseDialer, err := network.NewDefaultDialer(tcpTimeout, int(bufferSize))
|
baseDialer, err := network.NewDefaultDialer(tcpTimeout, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot build a default dialer: %w", err)
|
return nil, fmt.Errorf("cannot build a default dialer: %w", err)
|
||||||
}
|
}
|
||||||
@@ -181,7 +180,6 @@ func runProxy(conf *config.Config, version string) error {
|
|||||||
EventStream: eventStream,
|
EventStream: eventStream,
|
||||||
|
|
||||||
Secret: conf.Secret,
|
Secret: conf.Secret,
|
||||||
BufferSize: conf.TCPBuffer.Get(mtglib.DefaultBufferSize),
|
|
||||||
DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
|
DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
|
||||||
PreferIP: conf.PreferIP.Get(mtglib.DefaultPreferIP),
|
PreferIP: conf.PreferIP.Get(mtglib.DefaultPreferIP),
|
||||||
|
|
||||||
@@ -193,7 +191,7 @@ func runProxy(conf *config.Config, version string) error {
|
|||||||
return fmt.Errorf("cannot create a proxy: %w", err)
|
return fmt.Errorf("cannot create a proxy: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err := utils.NewListener(conf.BindTo.Get(""), int(opts.BufferSize))
|
listener, err := utils.NewListener(conf.BindTo.Get(""), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot start proxy: %w", err)
|
return fmt.Errorf("cannot start proxy: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,10 +38,6 @@ func (s *SimpleRun) Run(cli *CLI, version string) error { // nolint: cyclop
|
|||||||
return fmt.Errorf("incorrect concurrency: %w", err)
|
return fmt.Errorf("incorrect concurrency: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := conf.TCPBuffer.Set(s.TCPBuffer); err != nil {
|
|
||||||
return fmt.Errorf("incorrect tcp-buffer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := conf.PreferIP.Set(s.PreferIP); err != nil {
|
if err := conf.PreferIP.Set(s.PreferIP); err != nil {
|
||||||
return fmt.Errorf("incorrect prefer-ip: %w", err)
|
return fmt.Errorf("incorrect prefer-ip: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ type Config struct {
|
|||||||
AllowFallbackOnUnknownDC TypeBool `json:"allowFallbackOnUnknownDc"`
|
AllowFallbackOnUnknownDC TypeBool `json:"allowFallbackOnUnknownDc"`
|
||||||
Secret mtglib.Secret `json:"secret"`
|
Secret mtglib.Secret `json:"secret"`
|
||||||
BindTo TypeHostPort `json:"bindTo"`
|
BindTo TypeHostPort `json:"bindTo"`
|
||||||
TCPBuffer TypeBytes `json:"tcpBuffer"`
|
|
||||||
PreferIP TypePreferIP `json:"preferIp"`
|
PreferIP TypePreferIP `json:"preferIp"`
|
||||||
DomainFrontingPort TypePort `json:"domainFrontingPort"`
|
DomainFrontingPort TypePort `json:"domainFrontingPort"`
|
||||||
TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
|
TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ type tomlConfig struct {
|
|||||||
AllowFallbackOnUnknownDC bool `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
|
AllowFallbackOnUnknownDC bool `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
|
||||||
Secret string `toml:"secret" json:"secret"`
|
Secret string `toml:"secret" json:"secret"`
|
||||||
BindTo string `toml:"bind-to" json:"bindTo"`
|
BindTo string `toml:"bind-to" json:"bindTo"`
|
||||||
TCPBuffer string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
|
|
||||||
PreferIP string `toml:"prefer-ip" json:"preferIp,omitempty"`
|
PreferIP string `toml:"prefer-ip" json:"preferIp,omitempty"`
|
||||||
DomainFrontingPort uint `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
|
DomainFrontingPort uint `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
|
||||||
TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
|
TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import (
|
|||||||
|
|
||||||
type Listener struct {
|
type Listener struct {
|
||||||
net.Listener
|
net.Listener
|
||||||
|
|
||||||
bufferSize int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Listener) Accept() (net.Conn, error) {
|
func (l Listener) Accept() (net.Conn, error) {
|
||||||
@@ -19,7 +17,7 @@ func (l Listener) Accept() (net.Conn, error) {
|
|||||||
return nil, err // nolint: wrapcheck
|
return nil, err // nolint: wrapcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := network.SetClientSocketOptions(conn, l.bufferSize); err != nil {
|
if err := network.SetClientSocketOptions(conn, 0); err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
|
||||||
return nil, fmt.Errorf("cannot set TCP options: %w", err)
|
return nil, fmt.Errorf("cannot set TCP options: %w", err)
|
||||||
@@ -35,7 +33,6 @@ func NewListener(bindTo string, bufferSize int) (net.Listener, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Listener{
|
return Listener{
|
||||||
Listener: base,
|
Listener: base,
|
||||||
bufferSize: bufferSize,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,8 @@ type ProxyOpts struct {
|
|||||||
// buffers: to and from.
|
// buffers: to and from.
|
||||||
//
|
//
|
||||||
// This is an optional setting.
|
// This is an optional setting.
|
||||||
|
//
|
||||||
|
// Deprecated: this setting is no longer makes any effect.
|
||||||
BufferSize uint
|
BufferSize uint
|
||||||
|
|
||||||
// Concurrency is a size of the worker pool for connection management.
|
// Concurrency is a size of the worker pool for connection management.
|
||||||
|
|||||||
+5
-13
@@ -9,8 +9,6 @@ import (
|
|||||||
|
|
||||||
type defaultDialer struct {
|
type defaultDialer struct {
|
||||||
net.Dialer
|
net.Dialer
|
||||||
|
|
||||||
bufferSize int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
|
func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
|
||||||
@@ -30,7 +28,7 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we do not need to call to end user. End users call us.
|
// we do not need to call to end user. End users call us.
|
||||||
if err := SetServerSocketOptions(conn, d.bufferSize); err != nil {
|
if err := SetServerSocketOptions(conn, 0); err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
|
||||||
return nil, fmt.Errorf("cannot set socket options: %w", err)
|
return nil, fmt.Errorf("cannot set socket options: %w", err)
|
||||||
@@ -44,26 +42,20 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
|
|||||||
//
|
//
|
||||||
// The most default one you can imagine. But it has tunes TCP
|
// The most default one you can imagine. But it has tunes TCP
|
||||||
// connections and setups SO_REUSEPORT.
|
// connections and setups SO_REUSEPORT.
|
||||||
|
//
|
||||||
|
// bufferSize is deprecated and ignored. It is kept here for backward
|
||||||
|
// compatibility.
|
||||||
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:
|
||||||
return nil, fmt.Errorf("timeout %v should be positive number", timeout)
|
return nil, fmt.Errorf("timeout %v should be positive number", timeout)
|
||||||
case bufferSize < 0:
|
case timeout == 0:
|
||||||
return nil, fmt.Errorf("buffer size %d should be positive number", bufferSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
if timeout == 0 {
|
|
||||||
timeout = DefaultTimeout
|
timeout = DefaultTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
if bufferSize == 0 {
|
|
||||||
bufferSize = DefaultBufferSize
|
|
||||||
}
|
|
||||||
|
|
||||||
return &defaultDialer{
|
return &defaultDialer{
|
||||||
Dialer: net.Dialer{
|
Dialer: net.Dialer{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
},
|
},
|
||||||
bufferSize: bufferSize,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,10 +33,16 @@ const (
|
|||||||
// request.
|
// request.
|
||||||
DefaultHTTPTimeout = 10 * time.Second
|
DefaultHTTPTimeout = 10 * time.Second
|
||||||
|
|
||||||
|
// Deprecated:
|
||||||
|
//
|
||||||
// DefaultBufferSize defines a TCP buffer size. Both read and write, so
|
// DefaultBufferSize defines a TCP buffer size. Both read and write, so
|
||||||
// for real size, please multiply this number by 2.
|
// for real size, please multiply this number by 2.
|
||||||
DefaultBufferSize = 16 * 1024 // 16 kib
|
DefaultBufferSize = 16 * 1024 // 16 kib
|
||||||
|
|
||||||
|
// DefaultTCPKeepAlivePeriod defines a time period between 2
|
||||||
|
// consequitive probes.
|
||||||
|
DefaultTCPKeepAlivePeriod = 10 * time.Second
|
||||||
|
|
||||||
// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
|
// ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
|
||||||
// only.
|
// only.
|
||||||
//
|
//
|
||||||
|
|||||||
+11
-25
@@ -7,41 +7,27 @@ import (
|
|||||||
|
|
||||||
// SetClientSocketOptions tunes a TCP socket that represents a connection to
|
// SetClientSocketOptions tunes a TCP socket that represents a connection to
|
||||||
// end user (not Telegram service or fronting domain).
|
// end user (not Telegram service or fronting domain).
|
||||||
|
//
|
||||||
|
// bufferSize setting is deprecated and ignored.
|
||||||
func SetClientSocketOptions(conn net.Conn, bufferSize int) error {
|
func SetClientSocketOptions(conn net.Conn, bufferSize int) error {
|
||||||
tcpConn := conn.(*net.TCPConn) // nolint: forcetypeassert
|
return setCommonSocketOptions(conn.(*net.TCPConn))
|
||||||
|
|
||||||
if err := tcpConn.SetNoDelay(false); err != nil {
|
|
||||||
return fmt.Errorf("cannot disable TCP_NO_DELAY: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return setCommonSocketOptions(tcpConn, bufferSize)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetServerSocketOptions tunes a TCP socket that represents a connection to
|
// SetServerSocketOptions tunes a TCP socket that represents a connection to
|
||||||
// remote server like Telegram or fronting domain (but not end user).
|
// remote server like Telegram or fronting domain (but not end user).
|
||||||
func SetServerSocketOptions(conn net.Conn, bufferSize int) error {
|
func SetServerSocketOptions(conn net.Conn, bufferSize int) error {
|
||||||
tcpConn := conn.(*net.TCPConn) // nolint: forcetypeassert
|
return setCommonSocketOptions(conn.(*net.TCPConn))
|
||||||
|
|
||||||
if err := tcpConn.SetNoDelay(true); err != nil {
|
|
||||||
return fmt.Errorf("cannot enable TCP_NO_DELAY: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return setCommonSocketOptions(tcpConn, bufferSize)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
|
func setCommonSocketOptions(conn *net.TCPConn) error {
|
||||||
if err := conn.SetReadBuffer(bufferSize); err != nil {
|
if err := conn.SetKeepAlive(true); err != nil {
|
||||||
return fmt.Errorf("cannot set read buffer size: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := conn.SetWriteBuffer(bufferSize); err != nil {
|
|
||||||
return fmt.Errorf("cannot set write buffer size: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := conn.SetKeepAlive(false); err != nil {
|
|
||||||
return fmt.Errorf("cannot disable TCP keepalive probes: %w", err)
|
return fmt.Errorf("cannot disable TCP keepalive probes: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := conn.SetKeepAlivePeriod(DefaultTCPKeepAlivePeriod); err != nil {
|
||||||
|
return fmt.Errorf("cannot set time period of TCP keepalive probes: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := conn.SetLinger(tcpLingerTimeout); err != nil {
|
if err := conn.SetLinger(tcpLingerTimeout); err != nil {
|
||||||
return fmt.Errorf("cannot set TCP linger timeout: %w", err)
|
return fmt.Errorf("cannot set TCP linger timeout: %w", err)
|
||||||
}
|
}
|
||||||
@@ -51,7 +37,7 @@ func setCommonSocketOptions(conn *net.TCPConn, bufferSize int) error {
|
|||||||
return fmt.Errorf("cannot get underlying raw connection: %w", err)
|
return fmt.Errorf("cannot get underlying raw connection: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := setSocketReuseAddrPort(rawConn, bufferSize); err != nil {
|
if err := setSocketReuseAddrPort(rawConn); err != nil {
|
||||||
return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
|
return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
|
func setSocketReuseAddrPort(conn syscall.RawConn) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
conn.Control(func(fd uintptr) { // nolint: errcheck
|
conn.Control(func(fd uintptr) { // nolint: errcheck
|
||||||
|
|||||||
@@ -5,6 +5,6 @@ package network
|
|||||||
|
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
func setSocketReuseAddrPort(conn syscall.RawConn, bufferSize int) error {
|
func setSocketReuseAddrPort(conn syscall.RawConn) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user