diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 371313e8..c3f17c15 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -28,6 +28,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 env: @@ -63,6 +64,7 @@ builds: - with_openvpn - with_trusttunnel - with_sudoku + - with_profiler - badlinkname - tfogo_checklinkname0 targets: @@ -123,6 +125,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 - with_naive_outbound @@ -155,6 +158,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 - with_naive_outbound @@ -187,6 +191,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 - with_naive_outbound @@ -219,6 +224,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 - with_naive_outbound @@ -251,6 +257,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 - with_naive_outbound @@ -299,6 +306,7 @@ builds: - with_sudoku - with_manager - with_admin_panel + - with_profiler - badlinkname - tfogo_checklinkname0 - with_naive_outbound @@ -353,6 +361,7 @@ builds: - with_openvpn - with_trusttunnel - with_sudoku + - with_profiler - badlinkname - tfogo_checklinkname0 targets: @@ -424,6 +433,7 @@ builds: - with_openvpn - with_trusttunnel - with_sudoku + - with_profiler - badlinkname - tfogo_checklinkname0 hooks: diff --git a/option/profiler.go b/option/profiler.go index b088ec1e..3c6eb78f 100644 --- a/option/profiler.go +++ b/option/profiler.go @@ -1,9 +1,5 @@ package option -import "github.com/sagernet/sing/common/json/badoption" - type ProfilerServiceOptions struct { - Listen string `json:"listen,omitempty"` - ReadTimeout badoption.Duration `json:"read_timeout,omitempty"` - WriteTimeout badoption.Duration `json:"write_timeout,omitempty"` + ListenOptions } diff --git a/service/profiler/service.go b/service/profiler/service.go index 83ea0d58..693fb934 100644 --- a/service/profiler/service.go +++ b/service/profiler/service.go @@ -6,14 +6,15 @@ import ( "net/http/pprof" runtimePprof "runtime/pprof" "strings" - "time" "github.com/sagernet/sing-box/adapter" boxService "github.com/sagernet/sing-box/adapter/service" + "github.com/sagernet/sing-box/common/listener" C "github.com/sagernet/sing-box/constant" "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" E "github.com/sagernet/sing/common/exceptions" + N "github.com/sagernet/sing/common/network" "github.com/go-chi/chi/v5" ) @@ -24,23 +25,21 @@ func RegisterService(registry *boxService.Registry) { type Service struct { boxService.Adapter - logger log.ContextLogger - listen string - readTimeout time.Duration - writeTimeout time.Duration - server *http.Server + logger log.ContextLogger + listener *listener.Listener + server *http.Server } func NewService(ctx context.Context, logger log.ContextLogger, tag string, options option.ProfilerServiceOptions) (adapter.Service, error) { - if options.Listen == "" { - return nil, E.New("missing listen") - } return &Service{ - Adapter: boxService.NewAdapter(C.TypeProfiler, tag), - logger: logger, - listen: options.Listen, - readTimeout: time.Duration(options.ReadTimeout), - writeTimeout: time.Duration(options.WriteTimeout), + Adapter: boxService.NewAdapter(C.TypeProfiler, tag), + logger: logger, + listener: listener.New(listener.Options{ + Context: ctx, + Logger: logger, + Network: []string{N.NetworkTCP}, + Listen: options.ListenOptions, + }), }, nil } @@ -69,14 +68,15 @@ func (s *Service) Start(stage adapter.StartStage) error { }) s.server = &http.Server{ - Addr: s.listen, - Handler: r, - ReadTimeout: s.readTimeout, - WriteTimeout: s.writeTimeout, + Handler: r, } - s.logger.Info("profiler listening at ", s.listen) + tcpListener, err := s.listener.ListenTCP() + if err != nil { + return err + } + s.logger.Info("profiler listening at ", tcpListener.Addr()) go func() { - err := s.server.ListenAndServe() + err := s.server.Serve(tcpListener) if err != nil && !E.IsClosed(err) { s.logger.Error(E.Cause(err, "serve profiler")) } @@ -89,5 +89,5 @@ func (s *Service) Close() error { _ = s.server.Close() s.server = nil } - return nil + return s.listener.Close() }