Skip to content

Commit

Permalink
chore: refactor proxy mode select
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jun 13, 2022
1 parent 0a6e68f commit 2f7ef13
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 80 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ Usage of apt-proxy:
the host to bind to (default "0.0.0.0")
-mirror string
the mirror for fetching packages
-mode all
select the mode of system to cache: all / `ubuntu` / `debian` (default "all")
-port string
the port to bind to (default "3142")
-type string
select the type of system to cache: ubuntu/debian (default "ubuntu")
```
## [WIP] Development
Expand All @@ -106,13 +106,14 @@ go run apt-proxy.go
# go test -cover ./...

? github.com/soulteary/apt-proxy [no test files]
ok github.com/soulteary/apt-proxy/cli 1.433s coverage: 69.2% of statements
ok github.com/soulteary/apt-proxy/linux 8.099s coverage: 80.2% of statements
ok github.com/soulteary/apt-proxy/pkgs/httpcache 2.357s coverage: 82.7% of statements
ok github.com/soulteary/apt-proxy/cli 0.852s coverage: 73.9% of statements
ok github.com/soulteary/apt-proxy/linux 7.465s coverage: 82.7% of statements
ok github.com/soulteary/apt-proxy/pkgs/httpcache (cached) coverage: 82.7% of statements
? github.com/soulteary/apt-proxy/pkgs/httplog [no test files]
ok github.com/soulteary/apt-proxy/pkgs/stream.v1 1.238s coverage: 100.0% of statements
ok github.com/soulteary/apt-proxy/pkgs/vfs 0.930s coverage: 59.3% of statements
ok github.com/soulteary/apt-proxy/pkgs/stream.v1 (cached) coverage: 100.0% of statements
ok github.com/soulteary/apt-proxy/pkgs/vfs (cached) coverage: 59.3% of statements
? github.com/soulteary/apt-proxy/proxy [no test files]
? github.com/soulteary/apt-proxy/state [no test files]
```
View coverage report:
Expand Down
48 changes: 30 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,57 @@ import (
"flag"

"github.com/soulteary/apt-proxy/linux"
"github.com/soulteary/apt-proxy/state"
)

const (
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = "3142"
DEFAULT_CACHE_DIR = "./.aptcache"
DEFAULT_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
DEFAULT_TYPE = linux.LINUX_ALL_DISTROS
DEFAULT_DEBUG = false
DEFAULT_HOST = "0.0.0.0"
DEFAULT_PORT = "3142"
DEFAULT_CACHE_DIR = "./.aptcache"
DEFAULT_UBUNTU_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"
DEFAULT_DEBIAN_MIRROR = "" // "https://mirrors.tuna.tsinghua.edu.cn/debian/"
DEFAULT_MODE_NAME = linux.LINUX_ALL_DISTROS
DEFAULT_DEBUG = false
)

var Version string

func getProxyMode(mode string) string {
if mode != linux.LINUX_ALL_DISTROS &&
mode != linux.LINUX_DISTROS_DEBIAN &&
mode != linux.LINUX_DISTROS_UBUNTU {
return linux.LINUX_ALL_DISTROS
func getProxyMode(mode string) int {
if mode == linux.LINUX_DISTROS_UBUNTU {
return linux.TYPE_LINUX_DISTROS_UBUNTU
}
return mode

if mode == linux.LINUX_DISTROS_DEBIAN {
return linux.TYPE_LINUX_DISTROS_DEBIAN
}

return linux.TYPE_LINUX_ALL_DISTROS
}

func ParseFlags() (appFlags AppFlags) {
var (
host string
port string
mode string
host string
port string
userMode string
)
flag.StringVar(&host, "host", DEFAULT_HOST, "the host to bind to")
flag.StringVar(&port, "port", DEFAULT_PORT, "the port to bind to")
flag.StringVar(&mode, "mode", DEFAULT_TYPE, "select the mode of system to cache: `all` / `ubuntu` / `debian`")
flag.StringVar(&userMode, "mode", DEFAULT_MODE_NAME, "select the mode of system to cache: `all` / `ubuntu` / `debian`")
flag.BoolVar(&appFlags.Debug, "debug", DEFAULT_DEBUG, "whether to output debugging logging")
flag.StringVar(&appFlags.CacheDir, "cachedir", DEFAULT_CACHE_DIR, "the dir to store cache data in")
flag.StringVar(&appFlags.Mirror, "mirror", DEFAULT_MIRROR, "the mirror for fetching packages")
flag.StringVar(&appFlags.Ubuntu, "ubuntu", DEFAULT_UBUNTU_MIRROR, "the ubuntu mirror for fetching packages")
flag.StringVar(&appFlags.Debian, "debian", DEFAULT_DEBIAN_MIRROR, "the debian mirror for fetching packages")
flag.Parse()

appFlags.Mode = getProxyMode(mode)
mode := getProxyMode(userMode)

appFlags.Mode = mode
appFlags.Listen = host + ":" + port
appFlags.Version = Version

state.SetProxyMode(mode)
state.SetDebianMirror(appFlags.Debian)
state.SetUbuntuMirror(appFlags.Ubuntu)

return appFlags
}
19 changes: 14 additions & 5 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
)

func TestGetProxyMode(t *testing.T) {
if getProxyMode("not-support-os") != linux.LINUX_ALL_DISTROS {
if getProxyMode("not-support-os") != linux.TYPE_LINUX_ALL_DISTROS {
t.Fatal("Incorrect return default value")
}
if getProxyMode(linux.LINUX_DISTROS_DEBIAN) != linux.LINUX_DISTROS_DEBIAN {

if getProxyMode(linux.LINUX_DISTROS_DEBIAN) != linux.TYPE_LINUX_DISTROS_DEBIAN {
t.Fatal("Incorrect return value")
}

if getProxyMode(linux.LINUX_DISTROS_UBUNTU) != linux.TYPE_LINUX_DISTROS_UBUNTU {
t.Fatal("Incorrect return value")
}
}
Expand All @@ -28,12 +33,16 @@ func TestParseFlagsAndDaemonInit(t *testing.T) {
t.Fatal("Default option `Listen` value mismatch")
}

if flags.Mode != DEFAULT_TYPE {
if flags.Mode != getProxyMode(DEFAULT_MODE_NAME) {
t.Fatal("Default option `Mode` value mismatch")
}

if flags.Mirror != DEFAULT_MIRROR {
t.Fatal("Default option `Mirror` value mismatch")
if flags.Ubuntu != DEFAULT_UBUNTU_MIRROR {
t.Fatal("Default option `Ubuntu` value mismatch")
}

if flags.Debian != DEFAULT_DEBIAN_MIRROR {
t.Fatal("Default option `Debian` value mismatch")
}

if flags.CacheDir != DEFAULT_CACHE_DIR {
Expand Down
10 changes: 7 additions & 3 deletions cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ type AppFlags struct {
Debug bool
Version string
CacheDir string
Mirror string
Mode string
Mode int
Listen string

// mirror
Ubuntu string
Debian string
}

func initStore(appFlags AppFlags) (cache httpcache.Cache, err error) {
Expand All @@ -27,7 +30,8 @@ func initStore(appFlags AppFlags) (cache httpcache.Cache, err error) {
}

func initProxy(appFlags AppFlags, cache httpcache.Cache) (ap *proxy.AptProxy) {
ap = proxy.CreateAptProxyRouter(appFlags.Mirror, appFlags.Mode)
// TODO support both ubuntu and debian
ap = proxy.CreateAptProxyRouter()
ap.Handler = httpcache.NewHandler(cache, ap.Handler)
return ap
}
Expand Down
2 changes: 1 addition & 1 deletion linux/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestResourceBenchmark(t *testing.T) {
}

func TestMirrorsBenchmark(t *testing.T) {
mirrors := getGeoMirrorUrlsByMode(LINUX_DISTROS_UBUNTU)
mirrors := getGeoMirrorUrlsByMode(TYPE_LINUX_DISTROS_UBUNTU)
mirror, err := getTheFastestMirror(mirrors, UBUNTU_BENCHMAKR_URL)
if err != nil {
t.Fatal(err)
Expand Down
47 changes: 27 additions & 20 deletions linux/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ const (
LINUX_DISTROS_DEBIAN = "debian"
)

const (
TYPE_LINUX_ALL_DISTROS int = 0
TYPE_LINUX_DISTROS_UBUNTU = 1
TYPE_LINUX_DISTROS_DEBIAN = 2
)

type Rule struct {
OS int
Pattern *regexp.Regexp
CacheControl string
Rewrite bool
Expand Down Expand Up @@ -44,17 +51,17 @@ var DEBIAN_HOST_PATTERN = regexp.MustCompile(
)

var DEBIAN_DEFAULT_CACHE_RULES = []Rule{
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true},
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true},
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
// Add file file hash
{Pattern: regexp.MustCompile(`/by-hash/`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`/by-hash/`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_DEBIAN},
}

// Ubuntu
Expand Down Expand Up @@ -91,15 +98,15 @@ var UBUNTU_HOST_PATTERN = regexp.MustCompile(
)

var UBUNTU_DEFAULT_CACHE_RULES = []Rule{
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true},
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true},
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`, Rewrite: true},
{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(`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},
// Add file file hash
{Pattern: regexp.MustCompile(`/by-hash/`), CacheControl: `max-age=3600`, Rewrite: true},
{Pattern: regexp.MustCompile(`/by-hash/`), CacheControl: `max-age=3600`, Rewrite: true, OS: TYPE_LINUX_DISTROS_UBUNTU},
}
10 changes: 5 additions & 5 deletions linux/mirrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"regexp"
)

func getGeoMirrorUrlsByMode(mode string) (mirrors []string) {
if mode == LINUX_DISTROS_UBUNTU {
func getGeoMirrorUrlsByMode(mode int) (mirrors []string) {
if mode == TYPE_LINUX_DISTROS_UBUNTU {
ubuntuMirrorsOnline, err := getUbuntuMirrorUrlsByGeo()
if err != nil {
return BUILDIN_UBUNTU_MIRRORS
}
return ubuntuMirrorsOnline
}

if mode == LINUX_DISTROS_DEBIAN {
if mode == TYPE_LINUX_DISTROS_DEBIAN {
return BUILDIN_DEBIAN_MIRRORS
}

Expand All @@ -40,8 +40,8 @@ func getUbuntuMirrorUrlsByGeo() (mirrors []string, err error) {
return mirrors, scanner.Err()
}

func getPredefinedConfiguration(osType string) (string, *regexp.Regexp) {
if osType == LINUX_DISTROS_UBUNTU {
func getPredefinedConfiguration(proxyMode int) (string, *regexp.Regexp) {
if proxyMode == TYPE_LINUX_DISTROS_UBUNTU {
return UBUNTU_BENCHMAKR_URL, UBUNTU_HOST_PATTERN
} else {
return DEBIAN_BENCHMAKR_URL, DEBIAN_HOST_PATTERN
Expand Down
10 changes: 5 additions & 5 deletions linux/mirrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@ func TestGetGeoMirrors(t *testing.T) {
}

func TestGetMirrorUrlsByGeo(t *testing.T) {
mirrors := getGeoMirrorUrlsByMode(LINUX_ALL_DISTROS)
mirrors := getGeoMirrorUrlsByMode(TYPE_LINUX_ALL_DISTROS)
if len(mirrors) == 0 {
t.Fatal("No mirrors found")
}

mirrors = getGeoMirrorUrlsByMode(LINUX_DISTROS_DEBIAN)
mirrors = getGeoMirrorUrlsByMode(TYPE_LINUX_DISTROS_DEBIAN)
if len(mirrors) != len(BUILDIN_DEBIAN_MIRRORS) {
t.Fatal("Get mirrors error")
}

mirrors = getGeoMirrorUrlsByMode(LINUX_DISTROS_UBUNTU)
mirrors = getGeoMirrorUrlsByMode(TYPE_LINUX_DISTROS_UBUNTU)
if len(mirrors) == 0 {
t.Fatal("No mirrors found")
}
}

func TestGetPredefinedConfiguration(t *testing.T) {
res, pattern := getPredefinedConfiguration(LINUX_DISTROS_UBUNTU)
res, pattern := getPredefinedConfiguration(TYPE_LINUX_DISTROS_UBUNTU)
if res != UBUNTU_BENCHMAKR_URL {
t.Fatal("Failed to get resource link")
}
if !pattern.MatchString("http://archive.ubuntu.com/ubuntu/InRelease") {
t.Fatal("Failed to verify domain name rules")
}

res, pattern = getPredefinedConfiguration(LINUX_DISTROS_DEBIAN)
res, pattern = getPredefinedConfiguration(TYPE_LINUX_DISTROS_DEBIAN)
if res != DEBIAN_BENCHMAKR_URL {
t.Fatal("Failed to get resource link")
}
Expand Down
23 changes: 18 additions & 5 deletions linux/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,38 @@ type URLRewriter struct {
pattern *regexp.Regexp
}

func NewRewriter(mirrorUrl string, osType string) *URLRewriter {
func GetRewriteRulesByMode(mode int) (rules []Rule) {
if mode == TYPE_LINUX_DISTROS_UBUNTU {
return UBUNTU_DEFAULT_CACHE_RULES
}
if mode == TYPE_LINUX_DISTROS_DEBIAN {
return DEBIAN_DEFAULT_CACHE_RULES
}

rules = append(rules, UBUNTU_DEFAULT_CACHE_RULES...)
rules = append(rules, DEBIAN_DEFAULT_CACHE_RULES...)
return rules
}

func NewRewriter(mirrorUrl string, proxyMode int) *URLRewriter {
u := &URLRewriter{}

// user specify mirror
// TODO Both systems are supported
// TODO support both ubuntu and debian
if len(mirrorUrl) > 0 {
mirror, err := url.Parse(mirrorUrl)
if err == nil {
log.Printf("using specify mirror %s", mirrorUrl)
u.mirror = mirror
_, pattern := getPredefinedConfiguration(osType)
_, pattern := getPredefinedConfiguration(proxyMode)
u.pattern = pattern
return u
}
}

benchmarkUrl, pattern := getPredefinedConfiguration(osType)
benchmarkUrl, pattern := getPredefinedConfiguration(proxyMode)
u.pattern = pattern
mirrors := getGeoMirrorUrlsByMode(osType)
mirrors := getGeoMirrorUrlsByMode(proxyMode)
mirrorUrl, err := getTheFastestMirror(mirrors, benchmarkUrl)
if err != nil {
log.Println("Error finding fastest mirror", err)
Expand Down
Loading

0 comments on commit 2f7ef13

Please sign in to comment.