command.go 1.7 KB

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