dispatch/server/tls.go

58 lines
1.0 KiB
Go
Raw Normal View History

2016-01-04 18:26:32 +00:00
package server
import (
"crypto/tls"
"net"
"net/http"
"os"
"time"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/spf13/viper"
)
func listenAndServeTLS(srv *http.Server) error {
2016-01-08 17:53:02 +00:00
if srv.TLSConfig.NextProtos == nil {
srv.TLSConfig.NextProtos = []string{"http/1.1"}
}
2016-01-04 18:26:32 +00:00
ln, err := net.Listen("tcp", srv.Addr)
2016-01-04 18:26:32 +00:00
if err != nil {
return err
}
tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, srv.TLSConfig)
return srv.Serve(tlsListener)
2016-01-04 18:26:32 +00:00
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}
func certExists() bool {
cert := viper.GetString("https.cert")
key := viper.GetString("https.key")
if cert == "" || key == "" {
return false
}
if _, err := os.Stat(cert); err != nil {
return false
}
if _, err := os.Stat(key); err != nil {
return false
}
return true
}