1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-10-01 06:34:03 -04:00
Files
v2fly/app/observatory/command/command.go

73 lines
1.9 KiB
Go
Raw Normal View History

//go:build !confonly
2021-04-12 01:24:10 +01:00
// +build !confonly
2021-03-06 19:09:26 +00:00
package command
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
2021-06-19 11:21:35 +01:00
2021-03-06 19:09:26 +00:00
import (
"context"
2021-04-08 20:56:04 +01:00
2021-10-28 18:34:19 +08:00
"github.com/golang/protobuf/proto"
2021-04-08 20:56:04 +01:00
"google.golang.org/grpc"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/app/observatory"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/features"
"github.com/v2fly/v2ray-core/v5/features/extension"
2021-03-06 19:09:26 +00:00
)
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 11:21:35 +01:00
var result proto.Message
if request.Tag == "" {
observeResult, err := s.observatory.GetObservation(ctx)
if err != nil {
2021-06-19 11:22:29 +01:00
return nil, newError("cannot get observation").Base(err)
2021-06-19 11:21:35 +01:00
}
result = observeResult
} else {
if _, ok := s.observatory.(features.TaggedFeatures); !ok {
return nil, newError("observatory does not support tagged features")
}
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 11:21:35 +01:00
if err != nil {
2021-06-19 11:22:29 +01:00
return nil, newError("cannot get observation").Base(err)
2021-06-19 11:21:35 +01:00
}
result = observeResult
2021-03-06 19:09:26 +00:00
}
2021-06-19 11:21:35 +01: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
}))
}