tlscert

package module
v0.0.0-...-1322628 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2016 License: GPL-2.0 Imports: 16 Imported by: 0

README

tlscert

A small go library to quicky generate self-signed TLS cert

How to use this library?

go get github.com/PierreZ/tlscert

Fully working example with HTTP/2 server:

package main

import (
	"fmt"
	"html"
	"log"
	"net/http"
	"os"

	"github.com/PierreZ/tlscert"
	"golang.org/x/net/http2"
)

func main() {
	var srv http.Server
	http2.VerboseLogs = true
	srv.Addr = ":8080"

	// This enables http2 support
	http2.ConfigureServer(&srv, nil)
	
	path, _ := os.Getwd()
	
	// Checking if certs already exits
        if _, err := os.Stat(path + "cert.pem"); os.IsNotExist(err) {
                // Generating certificates
                err := tlscert.GenerateCert(path)
                if err != nil {
                        log.Fatalf("Failed generating certs:  %v", err)
                }
        }

	// Plain text test handler
	// Open
	// https://localhost:8080/randomtest
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hi tester %q\n", html.EscapeString(r.URL.Path))
		ShowRequestInfoHandler(w, r)
	})

	log.Print("HTTP/2 server started")
	log.Fatal(srv.ListenAndServeTLS(path+"/cert.pem", path+"/key.pem"))
}

func ShowRequestInfoHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	fmt.Fprintf(w, "Method: %s\n", r.Method)
	fmt.Fprintf(w, "Protocol: %s\n", r.Proto)
	fmt.Fprintf(w, "Host: %s\n", r.Host)
	fmt.Fprintf(w, "RemoteAddr: %s\n", r.RemoteAddr)
	fmt.Fprintf(w, "RequestURI: %q\n", r.RequestURI)
	fmt.Fprintf(w, "URL: %#v\n", r.URL)
	fmt.Fprintf(w, "Body.ContentLength: %d (-1 means unknown)\n", r.ContentLength)
	fmt.Fprintf(w, "Close: %v (relevant for HTTP/1 only)\n", r.Close)
	fmt.Fprintf(w, "TLS: %#v\n", r.TLS)
	fmt.Fprintf(w, "\nHeaders:\n")
	r.Header.Write(w)
}
``

Documentation

Overview

package tlscert provides a quick and dirty way to generate self-signed certificates, perfect to do quick test for HTTP/2. The whole code is based on https://golang.org/src/crypto/tls/generate_cert.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCert

func GenerateCert(path string) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL