1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-01 11:35:23 +00:00
v2fly/app/observatory/command/command.go

66 lines
1.7 KiB
Go
Raw Normal View History

//go:build !confonly
2021-04-12 00:24:10 +00:00
// +build !confonly
2021-03-06 19:09:26 +00:00
package command
2021-06-19 10:21:35 +00:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
2021-03-06 19:09:26 +00:00
import (
"context"
2021-06-19 10:21:35 +00:00
"github.com/golang/protobuf/proto"
"github.com/v2fly/v2ray-core/v4/features"
2021-04-08 19:56:04 +00:00
"google.golang.org/grpc"
2021-03-06 19:09:26 +00: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 21:08:27 +00:00
observatory extension.Observatory
2021-03-06 19:09:26 +00:00
}
func (s *service) GetOutboundStatus(ctx context.Context, request *GetOutboundStatusRequest) (*GetOutboundStatusResponse, error) {
2021-06-19 10:21:35 +00:00
var result proto.Message
if request.Tag == "" {
observeResult, err := s.observatory.GetObservation(ctx)
if err != nil {
2021-06-19 10:22:29 +00:00
return nil, newError("cannot get observation").Base(err)
2021-06-19 10:21:35 +00:00
}
result = observeResult
} else {
2021-06-19 10:22:29 +00:00
observeResult, err := common.Must2(s.observatory.(features.TaggedFeatures).GetFeaturesByTag(request.Tag)).(extension.Observatory).GetObservation(ctx)
2021-06-19 10:21:35 +00:00
if err != nil {
2021-06-19 10:22:29 +00:00
return nil, newError("cannot get observation").Base(err)
2021-06-19 10:21:35 +00:00
}
result = observeResult
2021-03-06 19:09:26 +00:00
}
2021-06-19 10:21:35 +00:00
retdata := result.(*observatory.ObservationResult)
2021-03-06 19:09:26 +00: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 21:08:27 +00: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 19:09:26 +00:00
}))
}