diff --git a/main/commands/run.go b/main/commands/run.go index 066bfae1845..2fbb7a2bce0 100644 --- a/main/commands/run.go +++ b/main/commands/run.go @@ -14,6 +14,7 @@ import ( "github.com/v2fly/v2ray-core/v5/common/cmdarg" "github.com/v2fly/v2ray-core/v5/common/platform" "github.com/v2fly/v2ray-core/v5/main/commands/base" + "github.com/v2fly/v2ray-core/v5/main/plugins" ) // CmdRun runs V2Ray with config @@ -75,6 +76,12 @@ func setConfigFlags(cmd *base.Command) { func executeRun(cmd *base.Command, args []string) { setConfigFlags(cmd) + var pluginFuncs []func() error + for _, plugin := range plugins.Plugins { + if f := plugin(cmd); f != nil { + pluginFuncs = append(pluginFuncs, f) + } + } cmd.Flag.Parse(args) printVersion() configFiles = getConfigFilePath() @@ -83,6 +90,14 @@ func executeRun(cmd *base.Command, args []string) { base.Fatalf("Failed to start: %s", err) } + for _, f := range pluginFuncs { + go func(f func() error) { + if err := f(); err != nil { + log.Print(err) + } + }(f) + } + if err := server.Start(); err != nil { base.Fatalf("Failed to start: %s", err) } diff --git a/main/distro/all/all.go b/main/distro/all/all.go index 200f1228304..52b308523a8 100644 --- a/main/distro/all/all.go +++ b/main/distro/all/all.go @@ -33,7 +33,6 @@ import ( // Developer preview features _ "github.com/v2fly/v2ray-core/v5/app/instman" _ "github.com/v2fly/v2ray-core/v5/app/observatory" - _ "github.com/v2fly/v2ray-core/v5/app/restfulapi" _ "github.com/v2fly/v2ray-core/v5/app/tun" // Inbound and outbound proxies. diff --git a/main/plugins/plugin.go b/main/plugins/plugin.go new file mode 100644 index 00000000000..58786b00ab6 --- /dev/null +++ b/main/plugins/plugin.go @@ -0,0 +1,11 @@ +package plugins + +import "github.com/v2fly/v2ray-core/v5/main/commands/base" + +var Plugins []Plugin + +type Plugin func(*base.Command) func() error + +func RegisterPlugin(plugin Plugin) { + Plugins = append(Plugins, plugin) +} diff --git a/main/plugins/plugin_pprof/plugin_pprof.go b/main/plugins/plugin_pprof/plugin_pprof.go new file mode 100644 index 00000000000..5da1a7b75c7 --- /dev/null +++ b/main/plugins/plugin_pprof/plugin_pprof.go @@ -0,0 +1,29 @@ +package plugin_pprof + +import ( + "github.com/v2fly/v2ray-core/v5/main/plugins" + "net/http" + "net/http/pprof" + + "github.com/v2fly/v2ray-core/v5/main/commands/base" +) + +var pprofPlugin plugins.Plugin = func(cmd *base.Command) func() error { + addr := cmd.Flag.String("pprof", "", "") + return func() error { + if *addr != "" { + h := http.NewServeMux() + h.HandleFunc("/debug/pprof/", pprof.Index) + h.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) + h.HandleFunc("/debug/pprof/profile", pprof.Profile) + h.HandleFunc("/debug/pprof/symbol", pprof.Symbol) + h.HandleFunc("/debug/pprof/trace", pprof.Trace) + return (&http.Server{Addr: *addr, Handler: h}).ListenAndServe() + } + return nil + } +} + +func init() { + plugins.RegisterPlugin(pprofPlugin) +}