github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/_vendor/src/golang.org/x/tools/cmd/tip/cert.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build autocert
     6  
     7  // This file contains autocert and cloud.google.com/go/storage
     8  // dependencies we want to hide by default from the Go build system,
     9  // which currently doesn't know how to fetch non-golang.org/x/*
    10  // dependencies. The Dockerfile builds the production binary
    11  // with this code using --tags=autocert.
    12  
    13  package main
    14  
    15  import (
    16  	"context"
    17  	"crypto/tls"
    18  	"log"
    19  	"net/http"
    20  
    21  	"cloud.google.com/go/storage"
    22  	"golang.org/x/build/autocertcache"
    23  	"golang.org/x/crypto/acme/autocert"
    24  )
    25  
    26  func init() {
    27  	runHTTPS = runHTTPSAutocert
    28  	certInit = certInitAutocert
    29  	wrapHTTPMux = wrapHTTPMuxAutocert
    30  }
    31  
    32  var autocertManager *autocert.Manager
    33  
    34  func certInitAutocert() {
    35  	var cache autocert.Cache
    36  	if b := *autoCertCacheBucket; b != "" {
    37  		sc, err := storage.NewClient(context.Background())
    38  		if err != nil {
    39  			log.Fatalf("storage.NewClient: %v", err)
    40  		}
    41  		cache = autocertcache.NewGoogleCloudStorageCache(sc, b)
    42  	}
    43  	autocertManager = &autocert.Manager{
    44  		Prompt:     autocert.AcceptTOS,
    45  		HostPolicy: autocert.HostWhitelist(*autoCertDomain),
    46  		Cache:      cache,
    47  	}
    48  }
    49  
    50  func runHTTPSAutocert(h http.Handler) error {
    51  	s := &http.Server{
    52  		Addr:    ":https",
    53  		Handler: h,
    54  		TLSConfig: &tls.Config{
    55  			GetCertificate: autocertManager.GetCertificate,
    56  		},
    57  	}
    58  	return s.ListenAndServeTLS("", "")
    59  }
    60  
    61  func wrapHTTPMuxAutocert(h http.Handler) http.Handler {
    62  	return autocertManager.HTTPHandler(h)
    63  }