diff --git a/cmd/run.go b/cmd/run.go index 16a8398e5..01c6d709a 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -25,6 +25,8 @@ import ( "github.com/daeuniverse/outbound/protocol/direct" "gopkg.in/natefinch/lumberjack.v2" + _ "net/http/pprof" + "github.com/daeuniverse/dae/cmd/internal" "github.com/daeuniverse/dae/common" "github.com/daeuniverse/dae/common/consts" @@ -124,6 +126,13 @@ func Run(log *logrus.Logger, conf *config.Config, externGeoDataDirs []string) (e return err } + var pprofServer *http.Server + if conf.Global.PprofPort != 0 { + pprofAddr := fmt.Sprintf("localhost:%d", conf.Global.PprofPort) + pprofServer = &http.Server{Addr: pprofAddr, Handler: nil} + go pprofServer.ListenAndServe() + } + // Serve tproxy TCP/UDP server util signals. var listener *control.Listener sigs := make(chan os.Signal, 1) @@ -277,6 +286,16 @@ loop: oldC.AbortConnections() } oldC.Close() + + if pprofServer != nil { + pprofServer.Shutdown(context.Background()) + pprofServer = nil + } + if newConf.Global.PprofPort != 0 { + pprofAddr := fmt.Sprintf("localhost:%d", conf.Global.PprofPort) + pprofServer = &http.Server{Addr: pprofAddr, Handler: nil} + go pprofServer.ListenAndServe() + } case syscall.SIGHUP: // Ignore. continue diff --git a/config/config.go b/config/config.go index e9cb944ca..4031e4ee9 100644 --- a/config/config.go +++ b/config/config.go @@ -41,6 +41,7 @@ type Global struct { SniffingTimeout time.Duration `mapstructure:"sniffing_timeout" default:"100ms"` TlsImplementation string `mapstructure:"tls_implementation" default:"tls"` UtlsImitate string `mapstructure:"utls_imitate" default:"chrome_auto"` + PprofPort uint16 `mapstructure:"pprof_port" default:"0"` } type Utls struct { diff --git a/example.dae b/example.dae index 6bf5746f1..3f581cd1a 100644 --- a/example.dae +++ b/example.dae @@ -9,6 +9,9 @@ global { # iptables tproxy rules. tproxy_port_protect: true + # Set non-zero value to enable pprof. + pprof_port: 0 + # If not zero, traffic sent from dae will be set SO_MARK. It is useful to avoid traffic loop with iptables tproxy # rules. so_mark_from_dae: 0