From 669bd3317a8dfde88004d43fcf7d19cd6b6a6745 Mon Sep 17 00:00:00 2001 From: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Date: Fri, 22 Jan 2021 17:44:42 +0800 Subject: [PATCH] Support `regexp` rule type Compatible with V2Ray rules --- docs/content/advance/router.md | 2 +- tunnel/router/client.go | 13 +++++++++++++ tunnel/router/router_test.go | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/content/advance/router.md b/docs/content/advance/router.md index ca7a81f..c7cd5ac 100644 --- a/docs/content/advance/router.md +++ b/docs/content/advance/router.md @@ -69,7 +69,7 @@ Trojan-Go内建的路由模块可以帮助你实现国内直连,即客户端 - "full:",完全域名匹配 -- "regex:",正则表达式匹配 +- "regexp:",正则表达式匹配 - "cidr:",CIDR匹配 diff --git a/tunnel/router/client.go b/tunnel/router/client.go index 0811961..48a40fa 100644 --- a/tunnel/router/client.go +++ b/tunnel/router/client.go @@ -418,6 +418,19 @@ func NewClient(ctx context.Context, underlay tunnel.Client) (*Client, error) { }) } + // Just for compatibility with V2Ray rule type `regexp` + regexpInfo := loadCode(cfg, "regexp:") + for _, info := range regexpInfo { + if _, err := regexp.Compile(info.code); err != nil { + return nil, common.NewError("invalid regular expression: " + info.code).Base(err) + } + client.domains[info.strategy] = append(client.domains[info.strategy], &v2router.Domain{ + Type: v2router.Domain_Regex, + Value: info.code, + Attribute: nil, + }) + } + fullInfo := loadCode(cfg, "full:") for _, info := range fullInfo { client.domains[info.strategy] = append(client.domains[info.strategy], &v2router.Domain{ diff --git a/tunnel/router/router_test.go b/tunnel/router/router_test.go index 41c56bd..00e16f6 100644 --- a/tunnel/router/router_test.go +++ b/tunnel/router/router_test.go @@ -77,11 +77,11 @@ router: - "full:localhost" - "domain:bypass.com" block: - - "regex:blockreg(.*)" + - "regexp:blockreg(.*)" - "full:blockfull" - "domain:block.com" proxy: - - "regex:proxyreg(.*)" + - "regexp:proxyreg(.*)" - "full:proxyfull" - "domain:proxy.com" - "cidr:192.168.1.1/16"