command.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //go:build !confonly
  2. // +build !confonly
  3. package command
  4. import (
  5. "context"
  6. "google.golang.org/grpc"
  7. core "github.com/v2fly/v2ray-core/v4"
  8. "github.com/v2fly/v2ray-core/v4/app/observatory"
  9. "github.com/v2fly/v2ray-core/v4/common"
  10. "github.com/v2fly/v2ray-core/v4/features/extension"
  11. )
  12. type service struct {
  13. UnimplementedObservatoryServiceServer
  14. v *core.Instance
  15. observatory extension.Observatory
  16. }
  17. func (s *service) GetOutboundStatus(ctx context.Context, request *GetOutboundStatusRequest) (*GetOutboundStatusResponse, error) {
  18. resp, err := s.observatory.GetObservation(ctx)
  19. if err != nil {
  20. return nil, err
  21. }
  22. retdata := resp.(*observatory.ObservationResult)
  23. return &GetOutboundStatusResponse{
  24. Status: retdata,
  25. }, nil
  26. }
  27. func (s *service) Register(server *grpc.Server) {
  28. RegisterObservatoryServiceServer(server, s)
  29. }
  30. func init() {
  31. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
  32. s := core.MustFromContext(ctx)
  33. sv := &service{v: s}
  34. err := s.RequireFeatures(func(Observatory extension.Observatory) {
  35. sv.observatory = Observatory
  36. })
  37. if err != nil {
  38. return nil, err
  39. }
  40. return sv, nil
  41. }))
  42. }