feat: options to disable update check & fix client lazy mode

This commit is contained in:
Toby 2023-08-24 14:10:51 -07:00
parent 09355c4e21
commit 353aacfd62
4 changed files with 37 additions and 22 deletions

View File

@ -361,15 +361,21 @@ func runClient(cmd *cobra.Command, args []string) {
logger.Fatal("failed to load client config", zap.Error(err)) logger.Fatal("failed to load client config", zap.Error(err))
} }
c, err := client.NewReconnectableClient(hyConfig, connectLog, config.Lazy) c, err := client.NewReconnectableClient(hyConfig, func(c client.Client, count int) {
connectLog(count)
// On the client side, we start checking for updates after we successfully connect
// to the server, which, depending on whether lazy mode is enabled, may or may not
// be immediately after the client starts. We don't want the update check request
// to interfere with the lazy mode option.
if count == 1 && !disableUpdateCheck {
go runCheckUpdateClient(c)
}
}, config.Lazy)
if err != nil { if err != nil {
logger.Fatal("failed to initialize client", zap.Error(err)) logger.Fatal("failed to initialize client", zap.Error(err))
} }
defer c.Close() defer c.Close()
// TODO: add option to disable update checking
go runCheckUpdateClient(c) // TODO: fix lazy mode
uri := config.URI() uri := config.URI()
logger.Info("use this URI to share your server", zap.String("uri", uri)) logger.Info("use this URI to share your server", zap.String("uri", uri))
if showQR { if showQR {
@ -622,12 +628,7 @@ func (f *obfsConnFactory) New(addr net.Addr) (net.PacketConn, error) {
} }
func connectLog(count int) { func connectLog(count int) {
if count == 1 { logger.Info("connected to server", zap.Int("count", count))
logger.Info("connected to server")
} else {
// Not the first time, we have reconnected
logger.Info("reconnected to server", zap.Int("count", count))
}
} }
type socks5Logger struct{} type socks5Logger struct{}

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -22,6 +23,7 @@ const (
appLogLevelEnv = "HYSTERIA_LOG_LEVEL" appLogLevelEnv = "HYSTERIA_LOG_LEVEL"
appLogFormatEnv = "HYSTERIA_LOG_FORMAT" appLogFormatEnv = "HYSTERIA_LOG_FORMAT"
appDisableUpdateCheckEnv = "HYSTERIA_DISABLE_UPDATE_CHECK"
) )
var ( var (
@ -51,6 +53,7 @@ var (
cfgFile string cfgFile string
logLevel string logLevel string
logFormat string logFormat string
disableUpdateCheck bool
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@ -105,8 +108,9 @@ func init() {
func initFlags() { func initFlags() {
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file") rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", envOrDefault(appLogLevelEnv, "info"), "log level") rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", envOrDefaultString(appLogLevelEnv, "info"), "log level")
rootCmd.PersistentFlags().StringVarP(&logFormat, "log-format", "f", envOrDefault(appLogFormatEnv, "console"), "log format") rootCmd.PersistentFlags().StringVarP(&logFormat, "log-format", "f", envOrDefaultString(appLogFormatEnv, "console"), "log format")
rootCmd.PersistentFlags().BoolVar(&disableUpdateCheck, "disable-update-check", envOrDefaultBool(appDisableUpdateCheckEnv, false), "disable update check")
} }
func initConfig() { func initConfig() {
@ -149,9 +153,17 @@ func initLogger() {
} }
} }
func envOrDefault(key, def string) string { func envOrDefaultString(key, def string) string {
if v := os.Getenv(key); v != "" { if v := os.Getenv(key); v != "" {
return v return v
} }
return def return def
} }
func envOrDefaultBool(key string, def bool) bool {
if v := os.Getenv(key); v != "" {
b, _ := strconv.ParseBool(v)
return b
}
return def
}

View File

@ -606,7 +606,9 @@ func runServer(cmd *cobra.Command, args []string) {
} }
logger.Info("server up and running") logger.Info("server up and running")
if !disableUpdateCheck {
go runCheckUpdateServer() go runCheckUpdateServer()
}
if err := s.Serve(); err != nil { if err := s.Serve(); err != nil {
logger.Fatal("failed to serve", zap.Error(err)) logger.Fatal("failed to serve", zap.Error(err))

View File

@ -13,12 +13,12 @@ type reconnectableClientImpl struct {
config *Config config *Config
client Client client Client
count int count int
connectedFunc func(int) // called when successfully connected connectedFunc func(Client, int) // called when successfully connected
m sync.Mutex m sync.Mutex
closed bool // permanent close closed bool // permanent close
} }
func NewReconnectableClient(config *Config, connectedFunc func(int), lazy bool) (Client, error) { func NewReconnectableClient(config *Config, connectedFunc func(Client, int), lazy bool) (Client, error) {
// Make sure we capture any error in config and return it here, // Make sure we capture any error in config and return it here,
// so that the caller doesn't have to wait until the first call // so that the caller doesn't have to wait until the first call
// to TCP() or UDP() to get the error (when lazy is true). // to TCP() or UDP() to get the error (when lazy is true).
@ -48,7 +48,7 @@ func (rc *reconnectableClientImpl) reconnect() error {
} else { } else {
rc.count++ rc.count++
if rc.connectedFunc != nil { if rc.connectedFunc != nil {
rc.connectedFunc(rc.count) rc.connectedFunc(rc, rc.count)
} }
return nil return nil
} }