github.com/v2fly/v2ray-core/v4@v4.45.2/app/observatory/command/command.go (about)

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