Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func NewContext(w http.ResponseWriter, r *http.Request) *Context {
s := sessions.NewCookieStore([]byte(SessionSecret))
s.Options.MaxAge = SessionExpiration
s.Options.SameSite = http.SameSiteLaxMode
s.Options.Secure = r.TLS != nil || strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https")
s.Options.HttpOnly = true

return &Context{
context: r.Context(),
Expand Down
7 changes: 6 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"net"
"net/http"
"time"

"github.com/convox/logger"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -65,7 +66,11 @@ func (s *Server) Listen(proto, addr string) error {
h = s
}

s.server = http.Server{Handler: h}
s.server = http.Server{
Handler: h,
ReadHeaderTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}

return s.server.Serve(l)
}
Expand Down
18 changes: 12 additions & 6 deletions ssl.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package stdapi

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
Expand All @@ -14,7 +15,7 @@ import (
)

func generateSelfSignedCertificate(host string) (tls.Certificate, error) {
rkey, err := rsa.GenerateKey(rand.Reader, 2048)
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return tls.Certificate{}, errors.WithStack(err)
}
Expand All @@ -32,21 +33,26 @@ func generateSelfSignedCertificate(host string) (tls.Certificate, error) {
},
NotBefore: time.Now(),
NotAfter: time.Now().Add(365 * 24 * time.Hour),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: []string{host},
}

data, err := x509.CreateCertificate(rand.Reader, &template, &template, &rkey.PublicKey, rkey)
data, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
return tls.Certificate{}, errors.WithStack(err)
}

der, err := x509.MarshalECPrivateKey(key)
if err != nil {
return tls.Certificate{}, errors.WithStack(err)
}

pub := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: data})
key := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(rkey)})
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: der})

cert, err := tls.X509KeyPair(pub, key)
cert, err := tls.X509KeyPair(pub, keyPEM)
if err != nil {
return tls.Certificate{}, errors.WithStack(err)
}
Expand Down