mihomo/rewrite/rewrite.go

106 lines
2.0 KiB
Go
Raw Permalink Normal View History

2022-04-10 03:59:27 +08:00
package rewrites
import (
2023-09-03 22:00:37 +08:00
regexp "github.com/dlclark/regexp2"
2022-04-10 03:59:27 +08:00
"strconv"
"strings"
C "github.com/Dreamacro/clash/constant"
"github.com/gofrs/uuid"
)
2023-09-03 23:42:27 +08:00
type RawMitmRule struct {
Url string `yaml:"url" json:"url"`
Action C.RewriteType `yaml:"action" json:"action"`
Old *string `yaml:"old" json:"old"`
New string `yaml:"new" json:"new"`
}
2022-04-10 03:59:27 +08:00
type RewriteRule struct {
id string
urlRegx *regexp.Regexp
ruleType C.RewriteType
ruleRegx *regexp.Regexp
rulePayload string
}
func (r *RewriteRule) ID() string {
return r.id
}
func (r *RewriteRule) URLRegx() *regexp.Regexp {
return r.urlRegx
}
func (r *RewriteRule) RuleType() C.RewriteType {
return r.ruleType
}
func (r *RewriteRule) RuleRegx() *regexp.Regexp {
return r.ruleRegx
}
func (r *RewriteRule) RulePayload() string {
return r.rulePayload
}
func (r *RewriteRule) ReplaceURLPayload(matchSub []string) string {
url := r.rulePayload
l := len(matchSub)
if l < 2 {
return url
}
for i := 1; i < l; i++ {
url = strings.ReplaceAll(url, "$"+strconv.Itoa(i), matchSub[i])
}
return url
}
func (r *RewriteRule) ReplaceSubPayload(oldData string) string {
payload := r.rulePayload
2023-09-04 13:31:18 +08:00
2022-04-10 03:59:27 +08:00
if r.ruleRegx == nil {
return oldData
}
2023-09-03 22:00:37 +08:00
sub, err := r.ruleRegx.FindStringMatch(oldData)
2022-04-10 03:59:27 +08:00
2023-09-04 13:31:18 +08:00
for err == nil && sub != nil {
var (
groups []string
sPayload = payload
)
for _, fg := range sub.Groups() {
groups = append(groups, fg.String())
}
2023-09-03 22:00:37 +08:00
2023-09-04 13:31:18 +08:00
l := len(groups)
2023-09-03 22:00:37 +08:00
2023-09-04 13:31:18 +08:00
for i := 1; i < l; i++ {
sPayload = strings.Replace(payload, "$"+strconv.Itoa(i), groups[i], 1)
}
oldData = strings.Replace(oldData, groups[0], sPayload, 1)
sub, err = r.ruleRegx.FindNextMatch(sub)
2022-04-10 03:59:27 +08:00
}
2023-09-04 13:31:18 +08:00
return oldData
2022-04-10 03:59:27 +08:00
}
func NewRewriteRule(urlRegx *regexp.Regexp, ruleType C.RewriteType, ruleRegx *regexp.Regexp, rulePayload string) *RewriteRule {
id, _ := uuid.NewV4()
return &RewriteRule{
id: id.String(),
urlRegx: urlRegx,
ruleType: ruleType,
ruleRegx: ruleRegx,
rulePayload: rulePayload,
}
}
var _ C.Rewrite = (*RewriteRule)(nil)