2022-01-22 22:10:45 +08:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-09 22:25:39 +08:00
|
|
|
"github.com/Dreamacro/clash/common/collections"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
RC "github.com/Dreamacro/clash/rule/common"
|
|
|
|
"github.com/Dreamacro/clash/rule/provider"
|
2022-04-16 00:21:08 +08:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-01-22 22:10:45 +08:00
|
|
|
)
|
|
|
|
|
2022-04-16 00:21:08 +08:00
|
|
|
func parseRuleByPayload(payload string) ([]C.Rule, error) {
|
2022-01-22 22:10:45 +08:00
|
|
|
regex, err := regexp.Compile("\\(.*\\)")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if regex.MatchString(payload) {
|
2022-01-26 14:02:10 +08:00
|
|
|
subAllRanges, err := format(payload)
|
2022-01-22 22:10:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 14:02:10 +08:00
|
|
|
rules := make([]C.Rule, 0, len(subAllRanges))
|
|
|
|
|
2022-04-16 00:21:08 +08:00
|
|
|
subRanges := findSubRuleRange(payload, subAllRanges)
|
2022-01-26 14:02:10 +08:00
|
|
|
for _, subRange := range subRanges {
|
|
|
|
subPayload := payload[subRange.start+1 : subRange.end]
|
2022-01-22 22:10:45 +08:00
|
|
|
|
|
|
|
rule, err := payloadToRule(subPayload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rules = append(rules, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("payload format error")
|
|
|
|
}
|
|
|
|
|
|
|
|
func containRange(r Range, preStart, preEnd int) bool {
|
|
|
|
return preStart < r.start && preEnd > r.end
|
|
|
|
}
|
|
|
|
|
|
|
|
func payloadToRule(subPayload string) (C.Rule, error) {
|
|
|
|
splitStr := strings.SplitN(subPayload, ",", 2)
|
2022-04-09 22:25:39 +08:00
|
|
|
if len(splitStr) < 2 {
|
2022-04-16 00:21:08 +08:00
|
|
|
return nil, fmt.Errorf("[%s] format is error", subPayload)
|
2022-04-09 22:25:39 +08:00
|
|
|
}
|
|
|
|
|
2022-01-22 22:10:45 +08:00
|
|
|
tp := splitStr[0]
|
|
|
|
payload := splitStr[1]
|
|
|
|
if tp == "NOT" || tp == "OR" || tp == "AND" {
|
2022-01-26 14:02:10 +08:00
|
|
|
return parseRule(tp, payload, nil)
|
2022-01-22 22:10:45 +08:00
|
|
|
}
|
2022-02-06 04:30:54 +08:00
|
|
|
if tp == "GEOSITE" {
|
2022-02-06 04:41:34 +08:00
|
|
|
if err := initGeoSite(); err != nil {
|
2022-03-18 01:25:59 +08:00
|
|
|
log.Errorln("can't initial GeoSite: %s", err)
|
2022-02-06 04:30:54 +08:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 22:10:45 +08:00
|
|
|
|
|
|
|
param := strings.Split(payload, ",")
|
2022-01-26 14:02:10 +08:00
|
|
|
return parseRule(tp, param[0], param[1:])
|
2022-01-22 22:10:45 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 14:02:10 +08:00
|
|
|
func parseRule(tp, payload string, params []string) (C.Rule, error) {
|
2022-01-22 22:10:45 +08:00
|
|
|
var (
|
|
|
|
parseErr error
|
|
|
|
parsed C.Rule
|
|
|
|
)
|
|
|
|
|
|
|
|
switch tp {
|
|
|
|
case "DOMAIN":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed = RC.NewDomain(payload, "")
|
2022-01-22 22:10:45 +08:00
|
|
|
case "DOMAIN-SUFFIX":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed = RC.NewDomainSuffix(payload, "")
|
2022-01-22 22:10:45 +08:00
|
|
|
case "DOMAIN-KEYWORD":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed = RC.NewDomainKeyword(payload, "")
|
2022-01-22 22:10:45 +08:00
|
|
|
case "GEOSITE":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewGEOSITE(payload, "")
|
2022-01-22 22:10:45 +08:00
|
|
|
case "GEOIP":
|
|
|
|
noResolve := RC.HasNoResolve(params)
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewGEOIP(payload, "", noResolve)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
|
|
|
noResolve := RC.HasNoResolve(params)
|
2022-04-16 00:21:08 +08:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, "", RC.WithIPCIDRNoResolve(noResolve))
|
2022-01-22 22:10:45 +08:00
|
|
|
case "SRC-IP-CIDR":
|
2022-04-16 00:21:08 +08:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, "", RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
|
2022-01-22 22:10:45 +08:00
|
|
|
case "SRC-PORT":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewPort(payload, "", true)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "DST-PORT":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewPort(payload, "", false)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "PROCESS-NAME":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, "", true)
|
2022-03-16 17:54:44 +08:00
|
|
|
case "PROCESS-PATH":
|
2022-03-17 23:24:07 +08:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, "", false)
|
2022-01-22 22:10:45 +08:00
|
|
|
case "RULE-SET":
|
2022-05-18 18:43:44 +08:00
|
|
|
noResolve := RC.HasNoResolve(params)
|
|
|
|
parsed, parseErr = provider.NewRuleSet(payload, "", noResolve)
|
2022-05-20 23:17:16 +08:00
|
|
|
case "IN-TYPE":
|
|
|
|
parsed, parseErr = RC.NewInType(payload, "")
|
2022-01-22 22:10:45 +08:00
|
|
|
case "NOT":
|
|
|
|
parsed, parseErr = NewNOT(payload, "")
|
|
|
|
case "AND":
|
|
|
|
parsed, parseErr = NewAND(payload, "")
|
|
|
|
case "OR":
|
|
|
|
parsed, parseErr = NewOR(payload, "")
|
|
|
|
case "NETWORK":
|
|
|
|
parsed, parseErr = RC.NewNetworkType(payload, "")
|
|
|
|
default:
|
2022-04-16 00:21:08 +08:00
|
|
|
parsed, parseErr = nil, fmt.Errorf("unsupported rule type %s", tp)
|
2022-01-22 22:10:45 +08:00
|
|
|
}
|
|
|
|
|
2022-03-19 22:27:22 +08:00
|
|
|
if parseErr != nil {
|
|
|
|
return nil, parseErr
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleExtra := &C.RuleExtra{
|
|
|
|
Network: RC.FindNetwork(params),
|
|
|
|
SourceIPs: RC.FindSourceIPs(params),
|
|
|
|
ProcessNames: RC.FindProcessName(params),
|
|
|
|
}
|
|
|
|
|
|
|
|
parsed.SetRuleExtra(ruleExtra)
|
|
|
|
|
|
|
|
return parsed, nil
|
2022-01-22 22:10:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Range struct {
|
|
|
|
start int
|
|
|
|
end int
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
|
|
|
func format(payload string) ([]Range, error) {
|
|
|
|
stack := collections.NewStack()
|
|
|
|
num := 0
|
|
|
|
subRanges := make([]Range, 0)
|
|
|
|
for i, c := range payload {
|
|
|
|
if c == '(' {
|
|
|
|
sr := Range{
|
|
|
|
start: i,
|
|
|
|
index: num,
|
|
|
|
}
|
|
|
|
|
|
|
|
num++
|
|
|
|
stack.Push(sr)
|
|
|
|
} else if c == ')' {
|
2022-04-16 00:21:08 +08:00
|
|
|
if stack.Len() == 0 {
|
|
|
|
return nil, fmt.Errorf("missing '('")
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:10:45 +08:00
|
|
|
sr := stack.Pop().(Range)
|
|
|
|
sr.end = i
|
|
|
|
subRanges = append(subRanges, sr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack.Len() != 0 {
|
|
|
|
return nil, fmt.Errorf("format error is missing )")
|
|
|
|
}
|
|
|
|
|
|
|
|
sortResult := make([]Range, len(subRanges))
|
|
|
|
for _, sr := range subRanges {
|
|
|
|
sortResult[sr.index] = sr
|
|
|
|
}
|
|
|
|
|
|
|
|
return sortResult, nil
|
|
|
|
}
|
2022-01-26 14:02:10 +08:00
|
|
|
|
2022-04-16 00:21:08 +08:00
|
|
|
func findSubRuleRange(payload string, ruleRanges []Range) []Range {
|
2022-01-26 14:02:10 +08:00
|
|
|
payloadLen := len(payload)
|
|
|
|
subRuleRange := make([]Range, 0)
|
|
|
|
for _, rr := range ruleRanges {
|
2022-04-16 00:21:08 +08:00
|
|
|
if rr.start == 0 && rr.end == payloadLen-1 {
|
2022-01-26 14:02:10 +08:00
|
|
|
// 最大范围跳过
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
containInSub := false
|
|
|
|
for _, r := range subRuleRange {
|
|
|
|
if containRange(rr, r.start, r.end) {
|
|
|
|
// The subRuleRange contains a range of rr, which is the next level node of the tree
|
|
|
|
containInSub = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !containInSub {
|
|
|
|
subRuleRange = append(subRuleRange, rr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return subRuleRange
|
|
|
|
}
|
2022-02-06 04:41:34 +08:00
|
|
|
|
|
|
|
func downloadGeoSite(path string) (err error) {
|
|
|
|
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGeoSite() error {
|
|
|
|
if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) {
|
|
|
|
log.Infoln("Need GeoSite but can't find GeoSite.dat, start download")
|
|
|
|
if err := downloadGeoSite(C.Path.GeoSite()); err != nil {
|
|
|
|
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
|
|
|
}
|
|
|
|
log.Infoln("Download GeoSite.dat finish")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|