2016-01-15 06:06:25 +02:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2016-02-14 17:56:14 +02:00
|
|
|
// TLSProviderServer implements ChallengeProvider for `TLS-SNI-01` challenge
|
|
|
|
// It may be instantiated without using the NewTLSProviderServer function if
|
|
|
|
// you want only to use the default values.
|
|
|
|
type TLSProviderServer struct {
|
2016-01-15 06:06:25 +02:00
|
|
|
iface string
|
|
|
|
port string
|
|
|
|
done chan bool
|
|
|
|
listener net.Listener
|
|
|
|
}
|
|
|
|
|
2016-02-14 17:56:14 +02:00
|
|
|
// NewTLSProviderServer creates a new TLSProviderServer on the selected interface and port.
|
|
|
|
// Setting iface and / or port to an empty string will make the server fall back to
|
|
|
|
// the "any" interface and port 443 respectively.
|
|
|
|
func NewTLSProviderServer(iface, port string) *TLSProviderServer {
|
|
|
|
return &TLSProviderServer{iface: iface, port: port}
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
// Present makes the keyAuth available as a cert
|
2016-02-14 17:56:14 +02:00
|
|
|
func (s *TLSProviderServer) Present(domain, token, keyAuth string) error {
|
2016-01-15 06:06:25 +02:00
|
|
|
if s.port == "" {
|
|
|
|
s.port = "443"
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := TLSSNI01ChallengeCert(keyAuth)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConf := new(tls.Config)
|
|
|
|
tlsConf.Certificates = []tls.Certificate{cert}
|
|
|
|
|
|
|
|
s.listener, err = tls.Listen("tcp", net.JoinHostPort(s.iface, s.port), tlsConf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not start HTTPS server for challenge -> %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.done = make(chan bool)
|
|
|
|
go func() {
|
|
|
|
http.Serve(s.listener, nil)
|
|
|
|
s.done <- true
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-14 17:56:14 +02:00
|
|
|
// CleanUp closes the HTTP server.
|
|
|
|
func (s *TLSProviderServer) CleanUp(domain, token, keyAuth string) error {
|
2016-01-15 06:06:25 +02:00
|
|
|
if s.listener == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.listener.Close()
|
|
|
|
<-s.done
|
|
|
|
return nil
|
|
|
|
}
|