Skip to content

Commit

Permalink
feat: ubuntu ports (ARMv64)
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Dec 1, 2024
1 parent fb928f4 commit 29e1e53
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 94 deletions.
22 changes: 11 additions & 11 deletions define/ubuntu-ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ var UBUNTU_PORTS_CUSTOM_MIRRORS = []string{
var BUILDIN_UBUNTU_PORTS_MIRRORS = GenerateBuildInList(UBUNTU_PORTS_OFFICIAL_MIRRORS, UBUNTU_PORTS_CUSTOM_MIRRORS)

var UBUNTU_PORTS_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},
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
{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},
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`InRelease$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU_PORTS},
// 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_PORTS},
}
2 changes: 1 addition & 1 deletion define/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

const (
UBUNTU_GEO_MIRROR_API = "http://mirrors.ubuntu.com/mirrors.txt"
UBUNTU_BENCHMAKR_URL = "dists/jammy/main/binary-amd64/Release"
UBUNTU_BENCHMAKR_URL = "dists/noble/main/binary-amd64/Release"
)

var UBUNTU_HOST_PATTERN = regexp.MustCompile(`/ubuntu/(.+)$`)
Expand Down
50 changes: 0 additions & 50 deletions internal/benchmarks/benchmark_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/mirrors/mirrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func GetPredefinedConfiguration(proxyMode int) (string, *regexp.Regexp) {
case Define.TYPE_LINUX_DISTROS_UBUNTU:
return Define.UBUNTU_BENCHMAKR_URL, Define.UBUNTU_HOST_PATTERN
case Define.TYPE_LINUX_DISTROS_UBUNTU_PORTS:
return Define.UBUNTU_BENCHMAKR_URL, Define.UBUNTU_PORTS_HOST_PATTERN
return Define.UBUNTU_PORTS_BENCHMAKR_URL, Define.UBUNTU_PORTS_HOST_PATTERN
case Define.TYPE_LINUX_DISTROS_DEBIAN:
return Define.DEBIAN_BENCHMAKR_URL, Define.DEBIAN_HOST_PATTERN
case Define.TYPE_LINUX_DISTROS_CENTOS:
Expand Down
54 changes: 27 additions & 27 deletions internal/rewriter/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ type URLRewriter struct {

// URLRewriters manages rewriters for different distributions
type URLRewriters struct {
ubuntu *URLRewriter
ubuntuPorts *URLRewriter
debian *URLRewriter
centos *URLRewriter
alpine *URLRewriter
mu sync.RWMutex
Ubuntu *URLRewriter
UbuntuPorts *URLRewriter
Debian *URLRewriter
Centos *URLRewriter
Alpine *URLRewriter
Mu sync.RWMutex
}

// getRewriterConfig returns configuration for a specific distribution
Expand Down Expand Up @@ -59,20 +59,20 @@ func createRewriter(mode int) *URLRewriter {
mirror := getMirror()

if mirror != nil {
log.Printf("using specified %s mirror %s", name, mirror)
log.Printf("using specified [%s] mirror [%s]", name, mirror)
rewriter.mirror = mirror
return rewriter
}

mirrorURLs := mirrors.GetGeoMirrorUrlsByMode(mode)
fastest, err := benchmarks.GetTheFastestMirror(mirrorURLs, benchmarkURL)
if err != nil {
log.Printf("Error finding fastest %s mirror: %v", name, err)
log.Printf("Error finding fastest [%s] mirror: %v", name, err)
return rewriter
}

if mirror, err := url.Parse(fastest); err == nil {
log.Printf("using fastest %s mirror %s", name, fastest)
log.Printf("using fastest [%s] mirror [%s]", name, fastest)
rewriter.mirror = mirror
}

Expand All @@ -85,21 +85,21 @@ func CreateNewRewriters(mode int) *URLRewriters {

switch mode {
case define.TYPE_LINUX_DISTROS_UBUNTU:
rewriters.ubuntu = createRewriter(mode)
rewriters.Ubuntu = createRewriter(mode)
case define.TYPE_LINUX_DISTROS_UBUNTU_PORTS:
rewriters.ubuntuPorts = createRewriter(mode)
rewriters.UbuntuPorts = createRewriter(mode)
case define.TYPE_LINUX_DISTROS_DEBIAN:
rewriters.debian = createRewriter(mode)
rewriters.Debian = createRewriter(mode)
case define.TYPE_LINUX_DISTROS_CENTOS:
rewriters.centos = createRewriter(mode)
rewriters.Centos = createRewriter(mode)
case define.TYPE_LINUX_DISTROS_ALPINE:
rewriters.alpine = createRewriter(mode)
rewriters.Alpine = createRewriter(mode)
default:
rewriters.ubuntu = createRewriter(define.TYPE_LINUX_DISTROS_UBUNTU)
rewriters.ubuntuPorts = createRewriter(define.TYPE_LINUX_DISTROS_UBUNTU_PORTS)
rewriters.debian = createRewriter(define.TYPE_LINUX_DISTROS_DEBIAN)
rewriters.centos = createRewriter(define.TYPE_LINUX_DISTROS_CENTOS)
rewriters.alpine = createRewriter(define.TYPE_LINUX_DISTROS_ALPINE)
rewriters.Ubuntu = createRewriter(define.TYPE_LINUX_DISTROS_UBUNTU)
rewriters.UbuntuPorts = createRewriter(define.TYPE_LINUX_DISTROS_UBUNTU_PORTS)
rewriters.Debian = createRewriter(define.TYPE_LINUX_DISTROS_DEBIAN)
rewriters.Centos = createRewriter(define.TYPE_LINUX_DISTROS_CENTOS)
rewriters.Alpine = createRewriter(define.TYPE_LINUX_DISTROS_ALPINE)
}

return rewriters
Expand Down Expand Up @@ -131,21 +131,21 @@ func GetRewriteRulesByMode(mode int) []define.Rule {

// RewriteRequestByMode rewrites the request URL based on the mode
func RewriteRequestByMode(r *http.Request, rewriters *URLRewriters, mode int) {
rewriters.mu.RLock()
defer rewriters.mu.RUnlock()
rewriters.Mu.RLock()
defer rewriters.Mu.RUnlock()

var rewriter *URLRewriter
rewriter := &URLRewriter{}
switch mode {
case define.TYPE_LINUX_DISTROS_UBUNTU:
rewriter = rewriters.ubuntu
rewriter = rewriters.Ubuntu
case define.TYPE_LINUX_DISTROS_UBUNTU_PORTS:
rewriter = rewriters.ubuntuPorts
rewriter = rewriters.UbuntuPorts
case define.TYPE_LINUX_DISTROS_DEBIAN:
rewriter = rewriters.debian
rewriter = rewriters.Debian
case define.TYPE_LINUX_DISTROS_CENTOS:
rewriter = rewriters.centos
rewriter = rewriters.Centos
case define.TYPE_LINUX_DISTROS_ALPINE:
rewriter = rewriters.alpine
rewriter = rewriters.Alpine
}

if rewriter == nil || rewriter.mirror == nil || rewriter.pattern == nil {
Expand Down
7 changes: 4 additions & 3 deletions internal/server/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var hostPatternMap = map[*regexp.Regexp][]Define.Rule{
}

var (
rewriter *Rewriter.URLRewriters
rewriters *Rewriter.URLRewriters
defaultTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: 45 * time.Second,
Expand All @@ -45,7 +45,7 @@ type responseWriter struct {

func CreateAptProxyRouter() *AptProxy {
mode := State.GetProxyMode()
rewriter = Rewriter.CreateNewRewriters(mode)
rewriters = Rewriter.CreateNewRewriters(mode)

return &AptProxy{
Rules: Rewriter.GetRewriteRulesByMode(mode),
Expand Down Expand Up @@ -104,7 +104,8 @@ func (ap *AptProxy) processMatchingRule(r *http.Request, rules []Define.Rule) *D

func (ap *AptProxy) rewriteRequest(r *http.Request, rule *Define.Rule) {
before := r.URL.String()
Rewriter.RewriteRequestByMode(r, rewriter, rule.OS)
Rewriter.RewriteRequestByMode(r, rewriters, rule.OS)

r.Host = r.URL.Host
log.Printf("Rewrote %q to %q", before, r.URL.String())
}
Expand Down
2 changes: 1 addition & 1 deletion state/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ var (
// Mirror states for different distributions
UbuntuMirror = NewMirrorState(Define.TYPE_LINUX_DISTROS_UBUNTU)
UbuntuPortsMirror = NewMirrorState(Define.TYPE_LINUX_DISTROS_UBUNTU_PORTS)
CentOSMirror = NewMirrorState(Define.TYPE_LINUX_DISTROS_CENTOS)
DebianMirror = NewMirrorState(Define.TYPE_LINUX_DISTROS_DEBIAN)
CentOSMirror = NewMirrorState(Define.TYPE_LINUX_DISTROS_CENTOS)
AlpineMirror = NewMirrorState(Define.TYPE_LINUX_DISTROS_ALPINE)
)

Expand Down

0 comments on commit 29e1e53

Please sign in to comment.