Skip to content

Commit 11f68e3

Browse files
committed
Updated to use latest httpcache
1 parent 5fe2a51 commit 11f68e3

File tree

8 files changed

+109
-100
lines changed

8 files changed

+109
-100
lines changed

Godeps/Godeps.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,10 @@ rm last-cid
3333
```
3434
docker run -it --rm --publish=3142 --net host lox24/apt-proxy
3535
```
36+
37+
## Debugging
38+
39+
```
40+
http_proxy=http://192.168.33.1:3142 apt-get -o Debug::pkgProblemResolver=true -o Debug::Acquire::http=true update
41+
http_proxy=http://192.168.33.1:3142 apt-get -o Debug::pkgProblemResolver=true -o Debug::Acquire::http=true install apache2
42+
```

apt-proxy.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"flag"
55
"log"
66
"net/http"
7-
"time"
87

98
"github.com/lox/apt-proxy/proxy"
109
"github.com/lox/httpcache"
@@ -16,44 +15,41 @@ const (
1615
defaultDir = "./.aptcache"
1716
)
1817

19-
var cachePatterns = proxy.CachePatternSlice{
20-
proxy.NewCachePattern(`deb$`, time.Hour*24*7),
21-
proxy.NewCachePattern(`udeb$`, time.Hour*24*7),
22-
proxy.NewCachePattern(`DiffIndex$`, time.Hour),
23-
proxy.NewCachePattern(`PackagesIndex$`, time.Hour),
24-
proxy.NewCachePattern(`Packages\.(bz2|gz|lzma)$`, time.Hour),
25-
proxy.NewCachePattern(`SourcesIndex$`, time.Hour),
26-
proxy.NewCachePattern(`Sources\.(bz2|gz|lzma)$`, time.Hour),
27-
proxy.NewCachePattern(`Release(\.gpg)?$`, time.Hour),
28-
proxy.NewCachePattern(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`, time.Hour),
29-
proxy.NewCachePattern(`Sources\.lzma$`, time.Hour),
30-
}
31-
3218
var (
3319
version string
3420
listen string
3521
dir string
22+
debug bool
3623
)
3724

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

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

35+
if debug {
36+
httpcache.DebugLogging = true
37+
}
38+
4739
cache, err := httpcache.NewDiskCache(dir)
4840
if err != nil {
4941
log.Fatal(err)
5042
}
5143

52-
ap := proxy.NewAptProxy()
53-
ap.CachePatterns = cachePatterns
44+
ap := proxy.NewAptProxyFromDefaults()
45+
ap.Handler = httpcache.NewHandler(cache, ap.Handler)
46+
47+
logger := httplog.NewResponseLogger(ap.Handler)
48+
logger.DumpRequests = debug
49+
logger.DumpResponses = debug
50+
logger.DumpErrors = debug
51+
ap.Handler = logger
5452

5553
log.Printf("proxy listening on %s", listen)
56-
log.Fatal(http.ListenAndServe(listen, httplog.NewResponseLogger(
57-
httpcache.NewHandler(cache, ap.Handler()),
58-
)))
54+
log.Fatal(http.ListenAndServe(listen, ap))
5955
}

proxy/pattern.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

proxy/proxy.go

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,65 @@
11
package proxy
22

33
import (
4+
"log"
45
"net/http"
56
"net/http/httputil"
7+
"time"
68

79
"github.com/lox/apt-proxy/ubuntu"
810
)
911

12+
var ubuntuRewriter = ubuntu.NewRewriter()
13+
14+
var defaultTransport http.RoundTripper = &http.Transport{
15+
Proxy: http.ProxyFromEnvironment,
16+
ResponseHeaderTimeout: time.Second * 45,
17+
DisableKeepAlives: true,
18+
}
19+
1020
type AptProxy struct {
11-
Transport http.RoundTripper
12-
Rewriters []Rewriter
13-
CachePatterns CachePatternSlice
21+
Handler http.Handler
22+
Rules []Rule
1423
}
1524

16-
func NewAptProxy() *AptProxy {
25+
func NewAptProxyFromDefaults() *AptProxy {
1726
return &AptProxy{
18-
Transport: http.DefaultTransport,
19-
Rewriters: []Rewriter{ubuntu.NewRewriter()},
20-
CachePatterns: CachePatternSlice{},
27+
Rules: DefaultRules,
28+
Handler: &httputil.ReverseProxy{
29+
Director: func(r *http.Request) {},
30+
Transport: defaultTransport,
31+
},
2132
}
2233
}
2334

24-
func (ap *AptProxy) Handler() http.Handler {
25-
return &handler{ap: ap, Handler: &httputil.ReverseProxy{
26-
Director: func(r *http.Request) {
27-
for _, rewrite := range ap.Rewriters {
28-
rewrite.Rewrite(r)
29-
}
35+
func (ap *AptProxy) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
36+
rule, match := matchingRule(r.URL.Path, ap.Rules)
37+
if match {
38+
r.Header.Del("Cache-Control")
39+
if rule.Rewrite {
40+
ap.rewriteRequest(r)
41+
}
42+
}
3043

31-
r.Host = r.URL.Host
32-
},
33-
Transport: &cachePatternTransport{ap.CachePatterns, ap.Transport},
34-
}}
44+
ap.Handler.ServeHTTP(&responseWriter{rw, rule}, r)
3545
}
3646

37-
type handler struct {
38-
http.Handler
39-
ap *AptProxy
47+
func (ap *AptProxy) rewriteRequest(r *http.Request) {
48+
before := r.URL.String()
49+
ubuntuRewriter.Rewrite(r)
50+
log.Printf("rewrote %q to %q", before, r.URL.String())
51+
r.Host = r.URL.Host
52+
}
53+
54+
type responseWriter struct {
55+
http.ResponseWriter
56+
rule *Rule
57+
}
58+
59+
func (rw *responseWriter) WriteHeader(status int) {
60+
if rw.rule != nil && rw.rule.CacheControl != "" &&
61+
(status == http.StatusOK || status == http.StatusNotFound) {
62+
rw.Header().Set("Cache-Control", rw.rule.CacheControl)
63+
}
64+
rw.ResponseWriter.WriteHeader(status)
4065
}

proxy/rewriter.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

proxy/rules.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package proxy
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
var DefaultRules = []Rule{
9+
{Pattern: regexp.MustCompile(`deb$`), CacheControl: `max-age=100000`, Rewrite: true},
10+
{Pattern: regexp.MustCompile(`udeb$`), CacheControl: `max-age=100000`, Rewrite: true},
11+
{Pattern: regexp.MustCompile(`DiffIndex$`), CacheControl: `max-age=3600`},
12+
{Pattern: regexp.MustCompile(`PackagesIndex$`), CacheControl: `max-age=3600`},
13+
{Pattern: regexp.MustCompile(`Packages\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`},
14+
{Pattern: regexp.MustCompile(`SourcesIndex$`), CacheControl: `max-age=3600`},
15+
{Pattern: regexp.MustCompile(`Sources\.(bz2|gz|lzma)$`), CacheControl: `max-age=3600`},
16+
{Pattern: regexp.MustCompile(`Release(\.gpg)?$`), CacheControl: `max-age=3600`},
17+
{Pattern: regexp.MustCompile(`Translation-(en|fr)\.(gz|bz2|bzip2|lzma)$`), CacheControl: `max-age=3600`},
18+
}
19+
20+
type Rule struct {
21+
Pattern *regexp.Regexp
22+
CacheControl string
23+
Rewrite bool
24+
}
25+
26+
func (r *Rule) String() string {
27+
return fmt.Sprintf("%s Cache-Control=%s Rewrite=%#v",
28+
r.Pattern.String(), r.CacheControl, r.Rewrite)
29+
}
30+
31+
func matchingRule(subject string, rules []Rule) (*Rule, bool) {
32+
for _, rule := range rules {
33+
if rule.Pattern.MatchString(subject) {
34+
return &rule, true
35+
}
36+
}
37+
38+
return nil, false
39+
}

ubuntu/rewriter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func NewRewriter() *ubuntuRewriter {
2727

2828
mirror, err := mirrors.Fastest()
2929
if err != nil {
30-
log.Println(err)
30+
log.Println("Error finding fastest mirror", err)
3131
}
3232

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

0 commit comments

Comments
 (0)