update matcher to path matcher

This commit is contained in:
guochao 2025-04-01 11:12:34 +08:00
parent e14bcb205b
commit 85968bb5cf
2 changed files with 35 additions and 27 deletions

View File

@ -10,27 +10,27 @@ type UpstreamPriorityGroup struct {
Priority int `yaml:"priority"` Priority int `yaml:"priority"`
} }
type UpstreamMatch struct { type PathTransformation struct {
Match string `yaml:"match"` Match string `yaml:"match"`
Replace string `yaml:"replace"` Replace string `yaml:"replace"`
} }
type Upstream struct { type Upstream struct {
Server string `yaml:"server"` Server string `yaml:"server"`
Match UpstreamMatch `yaml:"match"` Path PathTransformation `yaml:"path"`
AllowedRedirect *string `yaml:"allowed-redirect"` AllowedRedirect *string `yaml:"allowed-redirect"`
PriorityGroups []UpstreamPriorityGroup `yaml:"priority-groups"` PriorityGroups []UpstreamPriorityGroup `yaml:"priority-groups"`
} }
func (upstream Upstream) GetPath(orig string) (string, bool, error) { func (upstream Upstream) GetPath(orig string) (string, bool, error) {
if upstream.Match.Match == "" || upstream.Match.Replace == "" { if upstream.Path.Match == "" || upstream.Path.Replace == "" {
return orig, true, nil return orig, true, nil
} }
matcher, err := regexp.Compile(upstream.Match.Match) matcher, err := regexp.Compile(upstream.Path.Match)
if err != nil { if err != nil {
return "", false, err return "", false, err
} }
return matcher.ReplaceAllString(orig, upstream.Match.Replace), matcher.MatchString(orig), nil return matcher.ReplaceAllString(orig, upstream.Path.Replace), matcher.MatchString(orig), nil
} }
type LocalStorage struct { type LocalStorage struct {

View File

@ -1,91 +1,99 @@
upstream: upstream:
- server: https://mirrors.aliyun.com - server: https://mirrors.aliyun.com
match: path:
match: /(debian|ubuntu|ubuntu-releases|alpine|archlinux|kali|manjaro|msys2|almalinux|rocky|centos|centos-stream|centos-vault|fedora|epel|gnu|pypi)/(.*) match: /(debian|ubuntu|ubuntu-ports|ubuntu-releases|alpine|archlinux|kali|manjaro|msys2|almalinux|rocky|centos|centos-stream|centos-vault|fedora|epel|gnu|pypi)/(.*)
replace: '/$1/$2' replace: '/$1/$2'
- server: https://mirrors.tencent.com - server: https://mirrors.tencent.com
match: path:
match: /(debian|ubuntu|ubuntu-releases|alpine|archlinux|kali|manjaro|msys2|almalinux|rocky|centos|centos-stream|centos-vault|fedora|epel|gnu)/(.*) match: /(debian|ubuntu|ubuntu-ports|ubuntu-releases|alpine|archlinux|kali|manjaro|msys2|almalinux|rocky|centos|centos-stream|centos-vault|fedora|epel|gnu)/(.*)
replace: '/$1/$2' replace: '/$1/$2'
- server: https://mirrors.ustc.edu.cn - server: https://mirrors.ustc.edu.cn
match: path:
match: /(debian|ubuntu|ubuntu-releases|alpine|archlinux|kali|manjaro|msys2|almalinux|rocky|centos|centos-stream|centos-vault|fedora|epel|elrepo|remi|rpmfusion|tailscale|gnu|rust-static|pypi)/(.*) match: /(debian|ubuntu|ubuntu-releases|alpine|archlinux|kali|manjaro|msys2|almalinux|rocky|centos|centos-stream|centos-vault|fedora|epel|elrepo|remi|rpmfusion|tailscale|gnu|rust-static|pypi)/(.*)
replace: '/$1/$2' replace: '/$1/$2'
- server: https://packages.microsoft.com/repos/code - server: https://packages.microsoft.com/repos/code
match: path:
match: /microsoft-code(.*) match: /microsoft-code(.*)
replace: '$1' replace: '$1'
- server: https://archive.ubuntu.com - server: https://archive.ubuntu.com
match: path:
match: /ubuntu/(.*) match: /ubuntu/(.*)
replace: /$1 replace: /$1
- server: https://ports.ubuntu.com
path:
match: /ubuntu-ports/(.*)
replace: /$1
# priority-groups: # priority-groups:
# - match: /dists/.* # - match: /dists/.*
# priority: 10 # priority: 10
- server: https://releases.ubuntu.com/ - server: https://releases.ubuntu.com/
match: path:
match: /ubuntu-releases/(.*) match: /ubuntu-releases/(.*)
replace: /$1 replace: /$1
- server: https://apt.releases.hashicorp.com - server: https://apt.releases.hashicorp.com
match: path:
match: /hashicorp-deb/(.*) match: /hashicorp-deb/(.*)
replace: /$1 replace: /$1
- server: https://rpm.releases.hashicorp.com - server: https://rpm.releases.hashicorp.com
match: path:
match: /hashicorp-rpm/(.*) match: /hashicorp-rpm/(.*)
replace: /$1 replace: /$1
- server: https://dl.google.com/linux/chrome - server: https://dl.google.com/linux/chrome
match: path:
match: /google-chrome/(.*) match: /google-chrome/(.*)
replace: /$1 replace: /$1
- server: https://pkgs.tailscale.com/stable - server: https://pkgs.tailscale.com/stable
match: path:
match: /tailscale/(.*) match: /tailscale/(.*)
replace: /$1 replace: /$1
- server: http://update.cs2c.com.cn:8080 - server: http://update.cs2c.com.cn:8080
match: path:
match: /kylinlinux/(.*) match: /kylinlinux/(.*)
replace: /$1 replace: /$1
- server: https://cdn.jsdelivr.net - server: https://cdn.jsdelivr.net
match: path:
match: /jsdelivr/(.*) match: /jsdelivr/(.*)
replace: /$1 replace: /$1
- server: https://dl-cdn.alpinelinux.org - server: https://dl-cdn.alpinelinux.org
match: path:
match: /alpine/(.*) match: /alpine/(.*)
replace: /$1 replace: /$1
- server: https://repo.almalinux.org - server: https://repo.almalinux.org
match: path:
match: /almalinux/(.*) match: /almalinux/(.*)
replace: /almalinux/$1 replace: /almalinux/$1
- server: https://dl.rockylinux.org/pub/rocky - server: https://dl.rockylinux.org/pub/rocky
match: path:
match: /rocky/(.*) match: /rocky/(.*)
replace: /$1 replace: /$1
- server: https://dl.fedoraproject.org/pub/epel - server: https://dl.fedoraproject.org/pub/epel
match: path:
match: /epel/(.*) match: /epel/(.*)
replace: /$1 replace: /$1
- server: https://deb.debian.org - server: https://deb.debian.org
match: path:
match: /(debian|debian-security)/(.*) match: /(debian|debian-security)/(.*)
replace: /$1/$2 replace: /$1/$2
# priority-groups: # priority-groups:
# - match: /dists/.* # - match: /dists/.*
# priority: 10 # priority: 10
- server: https://static.rust-lang.org - server: https://static.rust-lang.org
match: path:
match: /rust-static/(.*) match: /rust-static/(.*)
replace: /$1 replace: /$1
- server: https://storage.flutter-io.cn - server: https://storage.flutter-io.cn
match: path:
match: /flutter-storage/(.*) match: /flutter-storage/(.*)
replace: /$1 replace: /$1
- server: https://cdn-mirror.chaotic.cx/chaotic-aur - server: https://cdn-mirror.chaotic.cx/chaotic-aur
match: path:
match: /chaotic-aur/(.*) match: /chaotic-aur/(.*)
replace: /$1 replace: /$1
allowed-redirect: "^https?://cf-builds.garudalinux.org/.*/chaotic-aur/.*$" allowed-redirect: "^https?://cf-builds.garudalinux.org/.*/chaotic-aur/.*$"
- server: http://download.zfsonlinux.org
path:
match: /openzfs/(.*)
replace: /$1
misc: misc:
first-chunk-bytes: 31457280 ## 1024*1024*30, all upstreams with 200 response will wait for the first chunks to select a upstream by bandwidth, instead of by latency. defaults to 50M first-chunk-bytes: 31457280 ## 1024*1024*30, all upstreams with 200 response will wait for the first chunks to select a upstream by bandwidth, instead of by latency. defaults to 50M