diff --git a/service/conf/environmentConfig.go b/service/conf/environmentConfig.go index 0679c5fa..1534040c 100644 --- a/service/conf/environmentConfig.go +++ b/service/conf/environmentConfig.go @@ -19,6 +19,7 @@ type Params struct { Config string `id:"config" short:"c" desc:"v2rayA configuration directory"` V2rayConfigDirectory string `id:"v2ray-confdir" desc:"Additional v2ray config directory, files in it will be combined with config generated by v2rayA"` V2rayAssetsDirectory string `id:"v2ray-assetsdir" desc:"v2ray-core assets directory for searching and downloading files like geoip.dat. This will override environment V2RAY_LOCATION_ASSET and XRAY_LOCATION_ASSET."` + CoreStartupTimeout int64 `id:"core-startup-timeout" default:"15" desc:"Timeout duration in seconds for starting v2ray or xray core. On devices with lower performance, consider increasing this value."` TransparentHook string `id:"transparent-hook" desc:"the executable file to run in the transparent proxy life-cycle. v2rayA will pass in the --transparent-type (tproxy, redirect) and --stage (pre-start, post-start, pre-stop, post-stop) arguments."` CoreHook string `id:"core-hook" desc:"the executable file to run in the v2ray-core life-cycle. v2rayA will pass in the --stage (pre-start, post-start, pre-stop, post-stop) argument."` PluginManager string `id:"plugin-manager" desc:"the executable file to run in the v2ray-core life-cycle. v2rayA will pass in the --stage (pre-start, post-start, pre-stop, post-stop) argument."` @@ -104,6 +105,10 @@ func initFunc() { log.Fatal("failed to set V2rayAssetsDirectory: %v", err) } } + + if params.CoreStartupTimeout <= 0 { + log.Fatal("invalid CoreStartupTimeout: %d seconds. It must be a positive integer greater than 0.", params.CoreStartupTimeout) + } } var once sync.Once diff --git a/service/core/v2ray/process.go b/service/core/v2ray/process.go index 2406d96a..b3aea3e7 100644 --- a/service/core/v2ray/process.go +++ b/service/core/v2ray/process.go @@ -18,6 +18,7 @@ import ( "strconv" "strings" "sync" + "syscall" "time" ) @@ -138,6 +139,7 @@ func NewProcess(tmpl *Template, } log.Trace("portList for connectivity test: %+v", portList) startTime := time.Now() + startTimeOut := time.Duration(conf.GetEnvironmentConfig().CoreStartupTimeout) * time.Second for i := 0; i < len(portList); { conn, err := net.Dial("tcp", net.JoinHostPort("127.0.0.1", portList[i])) if err == nil { @@ -151,7 +153,9 @@ func NewProcess(tmpl *Template, } return nil, fmt.Errorf("unexpected exiting: check the log for more information") } - if time.Since(startTime) > 30*time.Second { + if time.Since(startTime) > startTimeOut { + log.Info("Attempting to terminate timed-out process with SIGTERM") + _ = proc.Signal(syscall.SIGTERM) return nil, fmt.Errorf("timeout: check the log for more information") } time.Sleep(100 * time.Millisecond)