Skip to content

Commit

Permalink
Add -hsts flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Pervukhin committed May 21, 2016
1 parent 63f28f4 commit d14c25f
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions leproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
Addr string `flag:"addr,address to listen at"`
Conf string `flag:"map,file with host/backend mapping"`
Cache string `flag:"cache,path to letsencypt cache file"`
HSTS bool `flag:"hsts,add Strict-Transport-Security header"`
}{
Addr: ":https",
Conf: "mapping.yml",
Expand All @@ -41,14 +42,14 @@ func main() {
if params.Cache == "" {
log.Fatal("no cache specified")
}
srv, err := setupServer(params.Addr, params.Conf, params.Cache)
srv, err := setupServer(params.Addr, params.Conf, params.Cache, params.HSTS)
if err != nil {
log.Fatal(err)
}
log.Fatal(srv.ListenAndServeTLS("", ""))
}

func setupServer(addr, mapfile, cachefile string) (*http.Server, error) {
func setupServer(addr, mapfile, cachefile string, hsts bool) (*http.Server, error) {
mapping, err := readMapping(mapfile)
if err != nil {
return nil, err
Expand All @@ -57,6 +58,9 @@ func setupServer(addr, mapfile, cachefile string) (*http.Server, error) {
if err != nil {
return nil, err
}
if hsts {
proxy = &hstsProxy{proxy}
}
var m letsencrypt.Manager
if err := m.CacheFile(cachefile); err != nil {
return nil, err
Expand Down Expand Up @@ -133,6 +137,15 @@ func keys(m map[string]string) []string {
return out
}

type hstsProxy struct {
http.Handler
}

func (p *hstsProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
p.Handler.ServeHTTP(w, r)
}

type bufPool struct{}

func (bp bufPool) Get() []byte { return bufferPool.Get().([]byte) }
Expand Down

0 comments on commit d14c25f

Please sign in to comment.