mirror of
https://github.com/cmz0228/hysteria-dev.git
synced 2025-06-15 16:59:52 +00:00
Merge pull request #421 from HyNetwork/wip-disable-auto-reconnect
Allow users disable the auto reconnect behavior and related timeout
This commit is contained in:
commit
f9f668644b
@ -75,10 +75,15 @@ func client(config *clientConfig) {
|
||||
MaxStreamReceiveWindow: config.ReceiveWindowConn,
|
||||
InitialConnectionReceiveWindow: config.ReceiveWindow,
|
||||
MaxConnectionReceiveWindow: config.ReceiveWindow,
|
||||
KeepAlivePeriod: KeepAlivePeriod,
|
||||
HandshakeIdleTimeout: time.Duration(config.Connectivity.HandshakeIdleTimeout) * time.Second,
|
||||
MaxIdleTimeout: time.Duration(config.Connectivity.MaxIdleTimeout) * time.Second,
|
||||
DisablePathMTUDiscovery: config.DisableMTUDiscovery,
|
||||
EnableDatagrams: true,
|
||||
}
|
||||
quicConfig.KeepAlivePeriod = quicConfig.MaxIdleTimeout / 2
|
||||
if quicConfig.KeepAlivePeriod == 0 {
|
||||
quicConfig.KeepAlivePeriod = DefaultKeepAlivePeriod
|
||||
}
|
||||
if config.ReceiveWindowConn == 0 {
|
||||
quicConfig.InitialStreamReceiveWindow = DefaultStreamReceiveWindow
|
||||
quicConfig.MaxStreamReceiveWindow = DefaultStreamReceiveWindow
|
||||
@ -141,7 +146,19 @@ func client(config *clientConfig) {
|
||||
transport.DefaultClientTransport, up, down,
|
||||
func(refBPS uint64) congestion.CongestionControl {
|
||||
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
||||
}, obfuscator)
|
||||
}, obfuscator, func(err error) {
|
||||
if config.Connectivity.DisableAutoReconnect {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"addr": config.Server,
|
||||
"error": err,
|
||||
}).Fatal("Connection to server lost, exiting...")
|
||||
} else {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"addr": config.Server,
|
||||
"error": err,
|
||||
}).Info("Connection to server lost, reconnecting...")
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
logrus.WithField("error", err).Error("Failed to initialize client")
|
||||
if try <= config.Retry || config.Retry < 0 {
|
||||
|
@ -23,7 +23,7 @@ const (
|
||||
|
||||
DefaultMMDBFilename = "GeoLite2-Country.mmdb"
|
||||
|
||||
KeepAlivePeriod = 10 * time.Second
|
||||
DefaultKeepAlivePeriod = 10 * time.Second
|
||||
)
|
||||
|
||||
var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`)
|
||||
@ -131,7 +131,7 @@ func (r *Relay) Check() error {
|
||||
if len(r.Remote) == 0 {
|
||||
return errors.New("no relay remote address")
|
||||
}
|
||||
if r.Timeout != 0 && r.Timeout <= 4 {
|
||||
if r.Timeout != 0 && r.Timeout < 4 {
|
||||
return errors.New("invalid relay timeout")
|
||||
}
|
||||
return nil
|
||||
@ -147,6 +147,11 @@ type clientConfig struct {
|
||||
Retry int `json:"retry"`
|
||||
RetryInterval int `json:"retry_interval"`
|
||||
// Optional below
|
||||
Connectivity struct {
|
||||
DisableAutoReconnect bool `json:"disable_auto_reconnect"`
|
||||
HandshakeIdleTimeout int `json:"handshake_idle_timeout"`
|
||||
MaxIdleTimeout int `json:"max_idle_timeout"`
|
||||
} `json:"connectivity"`
|
||||
SOCKS5 struct {
|
||||
Listen string `json:"listen"`
|
||||
Timeout int `json:"timeout"`
|
||||
@ -231,10 +236,16 @@ func (c *clientConfig) Check() error {
|
||||
len(c.TCPRedirect.Listen) == 0 {
|
||||
return errors.New("please enable at least one mode")
|
||||
}
|
||||
if c.SOCKS5.Timeout != 0 && c.SOCKS5.Timeout <= 4 {
|
||||
if c.Connectivity.HandshakeIdleTimeout != 0 && c.Connectivity.HandshakeIdleTimeout < 2 {
|
||||
return errors.New("invalid handshake idle timeout")
|
||||
}
|
||||
if c.Connectivity.MaxIdleTimeout != 0 && c.Connectivity.MaxIdleTimeout < 4 {
|
||||
return errors.New("invalid max idle timeout")
|
||||
}
|
||||
if c.SOCKS5.Timeout != 0 && c.SOCKS5.Timeout < 4 {
|
||||
return errors.New("invalid SOCKS5 timeout")
|
||||
}
|
||||
if c.HTTP.Timeout != 0 && c.HTTP.Timeout <= 4 {
|
||||
if c.HTTP.Timeout != 0 && c.HTTP.Timeout < 4 {
|
||||
return errors.New("invalid HTTP timeout")
|
||||
}
|
||||
if c.TUN.Timeout != 0 && c.TUN.Timeout < 4 {
|
||||
@ -246,10 +257,10 @@ func (c *clientConfig) Check() error {
|
||||
if len(c.UDPRelay.Listen) > 0 && len(c.UDPRelay.Remote) == 0 {
|
||||
return errors.New("no UDP relay remote address")
|
||||
}
|
||||
if c.TCPRelay.Timeout != 0 && c.TCPRelay.Timeout <= 4 {
|
||||
if c.TCPRelay.Timeout != 0 && c.TCPRelay.Timeout < 4 {
|
||||
return errors.New("invalid TCP relay timeout")
|
||||
}
|
||||
if c.UDPRelay.Timeout != 0 && c.UDPRelay.Timeout <= 4 {
|
||||
if c.UDPRelay.Timeout != 0 && c.UDPRelay.Timeout < 4 {
|
||||
return errors.New("invalid UDP relay timeout")
|
||||
}
|
||||
for _, r := range c.TCPRelays {
|
||||
@ -262,13 +273,13 @@ func (c *clientConfig) Check() error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if c.TCPTProxy.Timeout != 0 && c.TCPTProxy.Timeout <= 4 {
|
||||
if c.TCPTProxy.Timeout != 0 && c.TCPTProxy.Timeout < 4 {
|
||||
return errors.New("invalid TCP TProxy timeout")
|
||||
}
|
||||
if c.UDPTProxy.Timeout != 0 && c.UDPTProxy.Timeout <= 4 {
|
||||
if c.UDPTProxy.Timeout != 0 && c.UDPTProxy.Timeout < 4 {
|
||||
return errors.New("invalid UDP TProxy timeout")
|
||||
}
|
||||
if c.TCPRedirect.Timeout != 0 && c.TCPRedirect.Timeout <= 4 {
|
||||
if c.TCPRedirect.Timeout != 0 && c.TCPRedirect.Timeout < 4 {
|
||||
return errors.New("invalid TCP Redirect timeout")
|
||||
}
|
||||
if len(c.Server) == 0 {
|
||||
|
@ -77,7 +77,7 @@ func server(config *serverConfig) {
|
||||
InitialConnectionReceiveWindow: config.ReceiveWindowClient,
|
||||
MaxConnectionReceiveWindow: config.ReceiveWindowClient,
|
||||
MaxIncomingStreams: int64(config.MaxConnClient),
|
||||
KeepAlivePeriod: KeepAlivePeriod,
|
||||
KeepAlivePeriod: DefaultKeepAlivePeriod,
|
||||
DisablePathMTUDiscovery: config.DisableMTUDiscovery,
|
||||
EnableDatagrams: true,
|
||||
}
|
||||
|
@ -44,11 +44,13 @@ type Client struct {
|
||||
udpSessionMutex sync.RWMutex
|
||||
udpSessionMap map[uint32]chan *udpMessage
|
||||
udpDefragger defragger
|
||||
|
||||
quicReconnectFunc func(err error)
|
||||
}
|
||||
|
||||
func NewClient(serverAddr string, protocol string, auth []byte, tlsConfig *tls.Config, quicConfig *quic.Config,
|
||||
transport *transport.ClientTransport, sendBPS uint64, recvBPS uint64, congestionFactory CongestionFactory,
|
||||
obfuscator obfs.Obfuscator,
|
||||
obfuscator obfs.Obfuscator, quicReconnectFunc func(err error),
|
||||
) (*Client, error) {
|
||||
quicConfig.DisablePathMTUDiscovery = quicConfig.DisablePathMTUDiscovery || pmtud_fix.DisablePathMTUDiscovery
|
||||
c := &Client{
|
||||
@ -62,6 +64,7 @@ func NewClient(serverAddr string, protocol string, auth []byte, tlsConfig *tls.C
|
||||
obfuscator: obfuscator,
|
||||
tlsConfig: tlsConfig,
|
||||
quicConfig: quicConfig,
|
||||
quicReconnectFunc: quicReconnectFunc,
|
||||
}
|
||||
if err := c.connectToServer(); err != nil {
|
||||
return nil, err
|
||||
@ -173,6 +176,7 @@ func (c *Client) openStreamWithReconnect() (quic.Connection, quic.Stream, error)
|
||||
// Temporary error, just return
|
||||
return nil, nil, err
|
||||
}
|
||||
c.quicReconnectFunc(err)
|
||||
// Permanent error, need to reconnect
|
||||
if err := c.connectToServer(); err != nil {
|
||||
// Still error, oops
|
||||
|
Loading…
x
Reference in New Issue
Block a user