1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 02:46:33 -05:00
v2fly/app/observatory/command/command.go

70 lines
1.8 KiB
Go
Raw Normal View History

//go:build !confonly
2021-04-11 20:24:10 -04:00
// +build !confonly
2021-03-06 14:09:26 -05:00
package command
2021-06-19 06:21:35 -04:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
2021-03-06 14:09:26 -05:00
import (
"context"
2021-06-19 06:21:35 -04:00
"github.com/golang/protobuf/proto"
"github.com/v2fly/v2ray-core/v4/features"
2021-04-08 15:56:04 -04:00
"google.golang.org/grpc"
2021-03-06 14:09:26 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/app/observatory"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/features/extension"
)
type service struct {
UnimplementedObservatoryServiceServer
v *core.Instance
2021-03-06 16:08:27 -05:00
observatory extension.Observatory
2021-03-06 14:09:26 -05:00
}
func (s *service) GetOutboundStatus(ctx context.Context, request *GetOutboundStatusRequest) (*GetOutboundStatusResponse, error) {
2021-06-19 06:21:35 -04:00
var result proto.Message
if request.Tag == "" {
observeResult, err := s.observatory.GetObservation(ctx)
if err != nil {
2021-06-19 06:22:29 -04:00
return nil, newError("cannot get observation").Base(err)
2021-06-19 06:21:35 -04:00
}
result = observeResult
} else {
fet, err := s.observatory.(features.TaggedFeatures).GetFeaturesByTag(request.Tag)
if err != nil {
return nil, newError("cannot get tagged observatory").Base(err)
}
observeResult, err := fet.(extension.Observatory).GetObservation(ctx)
2021-06-19 06:21:35 -04:00
if err != nil {
2021-06-19 06:22:29 -04:00
return nil, newError("cannot get observation").Base(err)
2021-06-19 06:21:35 -04:00
}
result = observeResult
2021-03-06 14:09:26 -05:00
}
2021-06-19 06:21:35 -04:00
retdata := result.(*observatory.ObservationResult)
2021-03-06 14:09:26 -05:00
return &GetOutboundStatusResponse{
Status: retdata,
}, nil
}
func (s *service) Register(server *grpc.Server) {
RegisterObservatoryServiceServer(server, s)
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
s := core.MustFromContext(ctx)
2021-03-06 16:08:27 -05:00
sv := &service{v: s}
err := s.RequireFeatures(func(Observatory extension.Observatory) {
sv.observatory = Observatory
})
if err != nil {
return nil, err
}
return sv, nil
2021-03-06 14:09:26 -05:00
}))
}