mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 17:38:41 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
95
app/router/command/command.go
Normal file
95
app/router/command/command.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
// +build !confonly
|
||||
|
||||
package command
|
||||
|
||||
//go:generate go run github.com/xtls/xray-core/v1/common/errors/errorgen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
"github.com/xtls/xray-core/v1/core"
|
||||
"github.com/xtls/xray-core/v1/features/routing"
|
||||
"github.com/xtls/xray-core/v1/features/stats"
|
||||
)
|
||||
|
||||
// routingServer is an implementation of RoutingService.
|
||||
type routingServer struct {
|
||||
router routing.Router
|
||||
routingStats stats.Channel
|
||||
}
|
||||
|
||||
// NewRoutingServer creates a statistics service with statistics manager.
|
||||
func NewRoutingServer(router routing.Router, routingStats stats.Channel) RoutingServiceServer {
|
||||
return &routingServer{
|
||||
router: router,
|
||||
routingStats: routingStats,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *routingServer) TestRoute(ctx context.Context, request *TestRouteRequest) (*RoutingContext, error) {
|
||||
if request.RoutingContext == nil {
|
||||
return nil, newError("Invalid routing request.")
|
||||
}
|
||||
route, err := s.router.PickRoute(AsRoutingContext(request.RoutingContext))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if request.PublishResult && s.routingStats != nil {
|
||||
ctx, _ := context.WithTimeout(context.Background(), 4*time.Second)
|
||||
s.routingStats.Publish(ctx, route)
|
||||
}
|
||||
return AsProtobufMessage(request.FieldSelectors)(route), nil
|
||||
}
|
||||
|
||||
func (s *routingServer) SubscribeRoutingStats(request *SubscribeRoutingStatsRequest, stream RoutingService_SubscribeRoutingStatsServer) error {
|
||||
if s.routingStats == nil {
|
||||
return newError("Routing statistics not enabled.")
|
||||
}
|
||||
genMessage := AsProtobufMessage(request.FieldSelectors)
|
||||
subscriber, err := stats.SubscribeRunnableChannel(s.routingStats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer stats.UnsubscribeClosableChannel(s.routingStats, subscriber)
|
||||
for {
|
||||
select {
|
||||
case value, ok := <-subscriber:
|
||||
if !ok {
|
||||
return newError("Upstream closed the subscriber channel.")
|
||||
}
|
||||
route, ok := value.(routing.Route)
|
||||
if !ok {
|
||||
return newError("Upstream sent malformed statistics.")
|
||||
}
|
||||
err := stream.Send(genMessage(route))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case <-stream.Context().Done():
|
||||
return stream.Context().Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *routingServer) mustEmbedUnimplementedRoutingServiceServer() {}
|
||||
|
||||
type service struct {
|
||||
v *core.Instance
|
||||
}
|
||||
|
||||
func (s *service) Register(server *grpc.Server) {
|
||||
common.Must(s.v.RequireFeatures(func(router routing.Router, stats stats.Manager) {
|
||||
RegisterRoutingServiceServer(server, NewRoutingServer(router, nil))
|
||||
}))
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
||||
s := core.MustFromContext(ctx)
|
||||
return &service{v: s}, nil
|
||||
}))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue