Skip to content

Commit

Permalink
Updated to use latest httpcache
Browse files Browse the repository at this point in the history
  • Loading branch information
lox committed Nov 10, 2014
1 parent 5fe2a51 commit 11f68e3
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ rm last-cid
```
docker run -it --rm --publish=3142 --net host lox24/apt-proxy
```

## Debugging

```
http_proxy=http://192.168.33.1:3142 apt-get -o Debug::pkgProblemResolver=true -o Debug::Acquire::http=true update
http_proxy=http://192.168.33.1:3142 apt-get -o Debug::pkgProblemResolver=true -o Debug::Acquire::http=true install apache2
```
34 changes: 15 additions & 19 deletions apt-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"log"
"net/http"
"time"

"github.com/lox/apt-proxy/proxy"
"github.com/lox/httpcache"
Expand All @@ -16,44 +15,41 @@ const (
defaultDir = "./.aptcache"
)

var cachePatterns = proxy.CachePatternSlice{
proxy.NewCachePattern(`deb$`, time.Hour*24*7),
proxy.NewCachePattern(`udeb$`, time.Hour*24*7),
proxy.NewCachePattern(`DiffIndex$`, time.Hour),
proxy.NewCachePattern(`PackagesIndex$`, time.Hour),
proxy.NewCachePattern(`Packages\.(bz2|gz|lzma)$`, time.Hour),
proxy.NewCachePattern(`SourcesIndex$`, time.Hour),
proxy.NewCachePattern(`Sources\.(bz2|gz|lzma)$`, time.Hour),
proxy.NewCachePattern(`Release(\.gpg)?$`, time.Hour),
proxy.NewCachePattern(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`, time.Hour),
proxy.NewCachePattern(`Sources\.lzma$`, time.Hour),
}

var (
version string
listen string
dir string
debug bool
)

func init() {
flag.StringVar(&listen, "listen", defaultListen, "the host and port to bind to")
flag.StringVar(&dir, "cachedir", defaultDir, "the dir to store cache data in")
flag.BoolVar(&debug, "debug", false, "whether to output debugging logging")
flag.Parse()
}

func main() {
log.Printf("running apt-proxy %s", version)

if debug {
httpcache.DebugLogging = true
}

cache, err := httpcache.NewDiskCache(dir)
if err != nil {
log.Fatal(err)
}

ap := proxy.NewAptProxy()
ap.CachePatterns = cachePatterns
ap := proxy.NewAptProxyFromDefaults()
ap.Handler = httpcache.NewHandler(cache, ap.Handler)

logger := httplog.NewResponseLogger(ap.Handler)
logger.DumpRequests = debug
logger.DumpResponses = debug
logger.DumpErrors = debug
ap.Handler = logger

log.Printf("proxy listening on %s", listen)
log.Fatal(http.ListenAndServe(listen, httplog.NewResponseLogger(
httpcache.NewHandler(cache, ap.Handler()),
)))
log.Fatal(http.ListenAndServe(listen, ap))
}
45 changes: 0 additions & 45 deletions proxy/pattern.go

This file was deleted.

65 changes: 45 additions & 20 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,65 @@
package proxy

import (
"log"
"net/http"
"net/http/httputil"
"time"

"github.com/lox/apt-proxy/ubuntu"
)

var ubuntuRewriter = ubuntu.NewRewriter()

var defaultTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
ResponseHeaderTimeout: time.Second * 45,
DisableKeepAlives: true,
}

type AptProxy struct {
Transport http.RoundTripper
Rewriters []Rewriter
CachePatterns CachePatternSlice
Handler http.Handler
Rules []Rule
}

func NewAptProxy() *AptProxy {
func NewAptProxyFromDefaults() *AptProxy {
return &AptProxy{
Transport: http.DefaultTransport,
Rewriters: []Rewriter{ubuntu.NewRewriter()},
CachePatterns: CachePatternSlice{},
Rules: DefaultRules,
Handler: &httputil.ReverseProxy{
Director: func(r *http.Request) {},
Transport: defaultTransport,
},
}
}

func (ap *AptProxy) Handler() http.Handler {
return &handler{ap: ap, Handler: &httputil.ReverseProxy{
Director: func(r *http.Request) {
for _, rewrite := range ap.Rewriters {
rewrite.Rewrite(r)
}
func (ap *AptProxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rule, match := matchingRule(r.URL.Path, ap.Rules)
if match {
r.Header.Del("Cache-Control")
if rule.Rewrite {
ap.rewriteRequest(r)
}
}

r.Host = r.URL.Host
},
Transport: &cachePatternTransport{ap.CachePatterns, ap.Transport},
}}
ap.Handler.ServeHTTP(&responseWriter{rw, rule}, r)
}

type handler struct {
http.Handler
ap *AptProxy
func (ap *AptProxy) rewriteRequest(r *http.Request) {
before := r.URL.String()
ubuntuRewriter.Rewrite(r)
log.Printf("rewrote %q to %q", before, r.URL.String())
r.Host = r.URL.Host
}

type responseWriter struct {
http.ResponseWriter
rule *Rule
}

func (rw *responseWriter) WriteHeader(status int) {
if rw.rule != nil && rw.rule.CacheControl != "" &&
(status == http.StatusOK || status == http.StatusNotFound) {
rw.Header().Set("Cache-Control", rw.rule.CacheControl)
}
rw.ResponseWriter.WriteHeader(status)
}
14 changes: 0 additions & 14 deletions proxy/rewriter.go

This file was deleted.

39 changes: 39 additions & 0 deletions proxy/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package proxy

import (
"fmt"
"regexp"
)

var DefaultRules = []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`},
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`},
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`},
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`},
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`},
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`},
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`},
}

type Rule struct {
Pattern *regexp.Regexp
CacheControl string
Rewrite bool
}

func (r *Rule) String() string {
return fmt.Sprintf("%s Cache-Control=%s Rewrite=%#v",
r.Pattern.String(), r.CacheControl, r.Rewrite)
}

func matchingRule(subject string, rules []Rule) (*Rule, bool) {
for _, rule := range rules {
if rule.Pattern.MatchString(subject) {
return &rule, true
}
}

return nil, false
}
3 changes: 2 additions & 1 deletion ubuntu/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewRewriter() *ubuntuRewriter {

mirror, err := mirrors.Fastest()
if err != nil {
log.Println(err)
log.Println("Error finding fastest mirror", err)
}

if mirrorUrl, err := url.Parse(mirror); err == nil {
Expand All @@ -42,6 +42,7 @@ func NewRewriter() *ubuntuRewriter {
func (ur *ubuntuRewriter) Rewrite(r *http.Request) {
url := r.URL.String()
if ur.mirror != nil && hostPattern.MatchString(url) {
r.Header.Add("Content-Location", url)
m := hostPattern.FindAllStringSubmatch(url, -1)
r.URL.Host = ur.mirror.Host
r.URL.Path = ur.mirror.Path + m[0][2]
Expand Down

0 comments on commit 11f68e3

Please sign in to comment.