Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions app/router/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,28 @@ func (s *routingServer) AddRule(ctx context.Context, request *AddRuleRequest) (*
return nil, errors.New("unsupported router implementation")

}

func (s *routingServer) RemoveRule(ctx context.Context, request *RemoveRuleRequest) (*RemoveRuleResponse, error) {
if bo, ok := s.router.(routing.Router); ok {
return &RemoveRuleResponse{}, bo.RemoveRule(request.RuleTag)
}
return nil, errors.New("unsupported router implementation")
}

func (s *routingServer) ListRule(ctx context.Context, request *ListRuleRequest) (*ListRuleResponse, error) {
if bo, ok := s.router.(routing.Router); ok {
response := &ListRuleResponse{}
for _, v := range bo.ListRule() {
response.Rules = append(response.Rules, &ListRuleItem{
Tag: v.GetOutboundTag(),
RuleTag: v.GetRuleTag(),
})
}
return response, nil
}
return nil, errors.New("unsupported router implementation")
}

// NewRoutingServer creates a statistics service with statistics manager.
func NewRoutingServer(router routing.Router, routingStats stats.Channel) RoutingServiceServer {
return &routingServer{
Expand Down
587 changes: 315 additions & 272 deletions app/router/command/command.pb.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions app/router/command/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ message RemoveRuleRequest {

message RemoveRuleResponse {}

message ListRuleRequest {}

message ListRuleItem {
string tag = 1;
string ruleTag = 2;
}

message ListRuleResponse{
repeated ListRuleItem rules = 1;
}

service RoutingService {
rpc SubscribeRoutingStats(SubscribeRoutingStatsRequest)
returns (stream RoutingContext) {}
Expand All @@ -114,6 +125,8 @@ service RoutingService {

rpc AddRule(AddRuleRequest) returns (AddRuleResponse) {}
rpc RemoveRule(RemoveRuleRequest) returns (RemoveRuleResponse) {}

rpc ListRule(ListRuleRequest) returns (ListRuleResponse) {}
}

message Config {}
56 changes: 47 additions & 9 deletions app/router/command/command_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion app/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package router

import (
"context"
sync "sync"
"sync"

"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
Expand Down Expand Up @@ -181,6 +181,21 @@ func (r *Router) RemoveRule(tag string) error {
return errors.New("empty tag name!")

}

// ListRule implements routing.Router
func (r *Router) ListRule() []routing.Route {
r.mu.Lock()
defer r.mu.Unlock()
ruleList := make([]routing.Route, 0)
for _, rule := range r.rules {
ruleList = append(ruleList, &Route{
outboundTag: rule.Tag,
ruleTag: rule.RuleTag,
})
}
return ruleList
}

func (r *Router) pickRouteInternal(ctx routing.Context) (*Rule, routing.Context, error) {
// SkipDNSResolve is set from DNS module.
// the DOH remote server maybe a domain name,
Expand Down
6 changes: 6 additions & 0 deletions features/routing/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Router interface {
PickRoute(ctx Context) (Route, error)
AddRule(config *serial.TypedMessage, shouldAppend bool) error
RemoveRule(tag string) error
ListRule() []Route
}

// Route is the routing result of Router feature.
Expand Down Expand Up @@ -65,6 +66,11 @@ func (DefaultRouter) RemoveRule(tag string) error {
return common.ErrNoClue
}

// ListRule implements Router.
func (DefaultRouter) ListRule() []Route {
return nil
}

// Start implements common.Runnable.
func (DefaultRouter) Start() error {
return nil
Expand Down
1 change: 1 addition & 0 deletions main/commands/all/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var CmdAPI = &base.Command{
cmdInboundUserCount,
cmdAddRules,
cmdRemoveRules,
cmdListRules,
cmdSourceIpBlock,
cmdOnlineStats,
cmdOnlineStatsIpList,
Expand Down
43 changes: 43 additions & 0 deletions main/commands/all/api/rules_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
routerService "github.com/xtls/xray-core/app/router/command"
"github.com/xtls/xray-core/main/commands/base"
)

var cmdListRules = &base.Command{
CustomFlags: true,
UsageLine: "{{.Exec}} api lsrules [--server=127.0.0.1:8080]",
Short: "List routing rules",
Long: `
List routing rules in Xray.
Arguments:
-s, -server <server:port>
The API server address. Default 127.0.0.1:8080
-t, -timeout <seconds>
Timeout in seconds for calling API. Default 3
Example:
{{.Exec}} {{.LongName}} --server=127.0.0.1:8080
`,
Run: executeListRules,
}

func executeListRules(cmd *base.Command, args []string) {
setSharedFlags(cmd)
cmd.Flag.Parse(args)

conn, ctx, close := dialAPIServer()
defer close()

client := routerService.NewRoutingServiceClient(conn)
resp, err := client.ListRule(ctx, &routerService.ListRuleRequest{})
if err != nil {
base.Fatalf("failed to list rules: %s", err)
}
showJSONResponse(resp)
}