command.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "time"
  8. "google.golang.org/grpc"
  9. core "github.com/v2fly/v2ray-core/v4"
  10. "github.com/v2fly/v2ray-core/v4/common"
  11. "github.com/v2fly/v2ray-core/v4/features/routing"
  12. "github.com/v2fly/v2ray-core/v4/features/stats"
  13. )
  14. // routingServer is an implementation of RoutingService.
  15. type routingServer struct {
  16. router routing.Router
  17. routingStats stats.Channel
  18. }
  19. // NewRoutingServer creates a statistics service with statistics manager.
  20. func NewRoutingServer(router routing.Router, routingStats stats.Channel) RoutingServiceServer {
  21. return &routingServer{
  22. router: router,
  23. routingStats: routingStats,
  24. }
  25. }
  26. func (s *routingServer) TestRoute(ctx context.Context, request *TestRouteRequest) (*RoutingContext, error) {
  27. if request.RoutingContext == nil {
  28. return nil, newError("Invalid routing request.")
  29. }
  30. route, err := s.router.PickRoute(AsRoutingContext(request.RoutingContext))
  31. if err != nil {
  32. return nil, err
  33. }
  34. if request.PublishResult && s.routingStats != nil {
  35. ctx, _ := context.WithTimeout(context.Background(), 4*time.Second) // nolint: govet
  36. s.routingStats.Publish(ctx, route)
  37. }
  38. return AsProtobufMessage(request.FieldSelectors)(route), nil
  39. }
  40. func (s *routingServer) SubscribeRoutingStats(request *SubscribeRoutingStatsRequest, stream RoutingService_SubscribeRoutingStatsServer) error {
  41. if s.routingStats == nil {
  42. return newError("Routing statistics not enabled.")
  43. }
  44. genMessage := AsProtobufMessage(request.FieldSelectors)
  45. subscriber, err := stats.SubscribeRunnableChannel(s.routingStats)
  46. if err != nil {
  47. return err
  48. }
  49. defer stats.UnsubscribeClosableChannel(s.routingStats, subscriber)
  50. for {
  51. select {
  52. case value, ok := <-subscriber:
  53. if !ok {
  54. return newError("Upstream closed the subscriber channel.")
  55. }
  56. route, ok := value.(routing.Route)
  57. if !ok {
  58. return newError("Upstream sent malformed statistics.")
  59. }
  60. err := stream.Send(genMessage(route))
  61. if err != nil {
  62. return err
  63. }
  64. case <-stream.Context().Done():
  65. return stream.Context().Err()
  66. }
  67. }
  68. }
  69. func (s *routingServer) mustEmbedUnimplementedRoutingServiceServer() {}
  70. type service struct {
  71. v *core.Instance
  72. }
  73. func (s *service) Register(server *grpc.Server) {
  74. common.Must(s.v.RequireFeatures(func(router routing.Router, stats stats.Manager) {
  75. RegisterRoutingServiceServer(server, NewRoutingServer(router, nil))
  76. }))
  77. }
  78. func init() {
  79. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
  80. s := core.MustFromContext(ctx)
  81. return &service{v: s}, nil
  82. }))
  83. }