Skip to content

Instantly share code, notes, and snippets.

@samthor
Last active February 16, 2025 09:39
Show Gist options
  • Save samthor/5ff8cfac1f80b03dfe5a9be62b29d7f2 to your computer and use it in GitHub Desktop.
Save samthor/5ff8cfac1f80b03dfe5a9be62b29d7f2 to your computer and use it in GitHub Desktop.

Revisions

  1. samthor revised this gist Aug 1, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion autocert-server.go
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@ import (

    func main() {
    // setup a simple handler which sends a HTHS header for six months (!)
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request)
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Strict-Transport-Security", "max-age=15768000 ; includeSubDomains")
    fmt.Fprintf(w, "Hello, HTTPS world!")
    })
  2. samthor revised this gist Apr 11, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions autocert-server.go
    Original file line number Diff line number Diff line change
    @@ -14,8 +14,9 @@ import (
    )

    func main() {
    // setup a simple handler which sends a HTHS header
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    // setup a simple handler which sends a HTHS header for six months (!)
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request)
    w.Header().Set("Strict-Transport-Security", "max-age=15768000 ; includeSubDomains")
    fmt.Fprintf(w, "Hello, HTTPS world!")
    })

  3. samthor revised this gist Apr 11, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions autocert-server.go
    Original file line number Diff line number Diff line change
    @@ -57,10 +57,10 @@ func main() {
    log.Fatal(server.ListenAndServeTLS("", ""))
    }

    // cacheDir makes a consiste nt cache directory inside /tmp. Returns "" on error.
    // cacheDir makes a consistent cache directory inside /tmp. Returns "" on error.
    func cacheDir() (dir string) {
    if u, _ := user.Current(); u != nil {
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
    if err := os.MkdirAll(dir, 0700); err == nil {
    return dir
    }
  4. samthor revised this gist Apr 11, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions autocert-server.go
    Original file line number Diff line number Diff line change
    @@ -57,10 +57,10 @@ func main() {
    log.Fatal(server.ListenAndServeTLS("", ""))
    }

    // cacheDir makes a consistent cache directory inside /tmp. Returns "" on error.
    // cacheDir makes a consiste nt cache directory inside /tmp. Returns "" on error.
    func cacheDir() (dir string) {
    if u, _ := user.Current(); u != nil {
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
    if err := os.MkdirAll(dir, 0700); err == nil {
    return dir
    }
  5. samthor revised this gist Apr 11, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions autocert-server.go
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,13 @@ package main

    import (
    "crypto/tls"
    "os"
    "os/user"
    "flag"
    "path/filepath"
    "fmt"
    "log"
    "net/http"
    "os"
    "os/user"
    "path/filepath"

    "golang.org/x/crypto/acme/autocert"
    )
    @@ -60,10 +60,10 @@ func main() {
    // cacheDir makes a consistent cache directory inside /tmp. Returns "" on error.
    func cacheDir() (dir string) {
    if u, _ := user.Current(); u != nil {
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-" + u.Username)
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
    if err := os.MkdirAll(dir, 0700); err == nil {
    return dir
    }
    }
    return ""
    }
    }
  6. samthor created this gist Apr 11, 2018.
    69 changes: 69 additions & 0 deletions autocert-server.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    package main

    import (
    "crypto/tls"
    "os"
    "os/user"
    "flag"
    "path/filepath"
    "fmt"
    "log"
    "net/http"

    "golang.org/x/crypto/acme/autocert"
    )

    func main() {
    // setup a simple handler which sends a HTHS header
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, HTTPS world!")
    })

    // look for the domains to be served from command line args
    flag.Parse()
    domains := flag.Args()
    if len(domains) == 0 {
    log.Fatalf("fatal; specify domains as arguments")
    }

    // create the autocert.Manager with domains and path to the cache
    certManager := autocert.Manager{
    Prompt: autocert.AcceptTOS,
    HostPolicy: autocert.HostWhitelist(domains...),
    }

    // optionally use a cache dir
    dir := cacheDir()
    if dir != "" {
    certManager.Cache = autocert.DirCache(dir)
    }

    // create the server itself
    server := &http.Server{
    Addr: ":https",
    TLSConfig: &tls.Config{
    GetCertificate: certManager.GetCertificate,
    },
    }

    log.Printf("Serving http/https for domains: %+v", domains)
    go func() {
    // serve HTTP, which will redirect automatically to HTTPS
    h := certManager.HTTPHandler(nil)
    log.Fatal(http.ListenAndServe(":http", h))
    }()

    // serve HTTPS!
    log.Fatal(server.ListenAndServeTLS("", ""))
    }

    // cacheDir makes a consistent cache directory inside /tmp. Returns "" on error.
    func cacheDir() (dir string) {
    if u, _ := user.Current(); u != nil {
    dir = filepath.Join(os.TempDir(), "cache-golang-autocert-" + u.Username)
    if err := os.MkdirAll(dir, 0700); err == nil {
    return dir
    }
    }
    return ""
    }