Skip to content

Commit

Permalink
chore: Optimized for Ubuntu acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Nov 20, 2022
1 parent 3956dfa commit 0f6c69f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 3 additions & 4 deletions internal/define/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ const (
UBUNTU_BENCHMAKR_URL = "dists/jammy/main/binary-amd64/Release"
)

var UBUNTU_HOST_PATTERN = regexp.MustCompile(
`https?://(\w{2}\.)?(security|archive).ubuntu.com/ubuntu/(.+)$`,
)
var UBUNTU_HOST_PATTERN = regexp.MustCompile(`/ubuntu/(.+)$`)

// http://mirrors.ubuntu.com/mirrors.txt 2022.11.19
// Sites that contain protocol headers, restrict access to resources using that protocol
Expand Down Expand Up @@ -53,6 +51,7 @@ var BUILDIN_UBUNTU_MIRRORS = GenerateBuildInList(UBUNTU_OFFICAL_MIRRORS, UBUNTU_
var UBUNTU_DEFAULT_CACHE_RULES = []Rule{
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`InRelease$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
Expand All @@ -61,5 +60,5 @@ var UBUNTU_DEFAULT_CACHE_RULES = []Rule{
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
// Add file file hash
{Pattern: regexp.MustCompile(`/by-hash/`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`\/by-hash\/`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
}
7 changes: 7 additions & 0 deletions internal/mirrors/mirrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mirrors

import (
"regexp"
"strings"

Define "github.com/soulteary/apt-proxy/internal/define"
)
Expand Down Expand Up @@ -65,9 +66,15 @@ func GetGeoMirrorUrlsByMode(mode int) (mirrors []string) {

func GetFullMirrorURL(mirror Define.UrlWithAlias) string {
if mirror.Http {
if strings.HasSuffix(mirror.URL, "http://") {
return mirror.URL
}
return "http://" + mirror.URL
}
if mirror.Https {
if strings.HasSuffix(mirror.URL, "https://") {
return mirror.URL
}
return "https://" + mirror.URL
}
return "https://" + mirror.URL
Expand Down
1 change: 0 additions & 1 deletion internal/server/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func (ap *AptProxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
fmt.Println("render internal urls error")
}
} else {

if Define.UBUNTU_HOST_PATTERN.MatchString(r.URL.Path) {
rule, match := Rewriter.MatchingRule(r.URL.Path, Define.UBUNTU_DEFAULT_CACHE_RULES)
if match {
Expand Down

0 comments on commit 0f6c69f

Please sign in to comment.