Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coolaj86/16ed8fd810e19dec71be to your computer and use it in GitHub Desktop.
Save coolaj86/16ed8fd810e19dec71be to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net"
"os"
//"io"
"io/ioutil"
"crypto/tls"
"crypto/x509"
//"net/http/httputil"
//"net/http"
"github.com/coolaj86/authentication-as-a-service/http"
//"./http.go"
"github.com/inconshreveable/go-vhost"
)
type myHandler struct { }
func (m *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main () {
port := "8443"
fmt.Println("Binding.")
ln, err := net.Listen("tcp", ":" + port)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't listen on port %q: %s\n", port, err)
os.Exit(1)
}
fmt.Printf("Bound and Listening on %s.\n", port)
conn, err := ln.Accept()
if (err != nil) {
fmt.Fprintf(os.Stderr, "Couldn't accept TCP connection: %s\n", err)
os.Exit(1)
}
tlsConn, err := vhost.TLS(conn)
if (err != nil) {
fmt.Fprintf(os.Stderr, "Couldn't accept TLS connection: %s\n", err)
os.Exit(1)
}
fmt.Println(tlsConn.Host());
fmt.Println(tlsConn);
//fmt.Fprintf(os.Stderr, "NOT IMPLEMENTED\n")
//os.Exit(1)
certPath := "/Users/aj/etc/letsencrypt/live/shell.example.com/cert.pem"
privkeyPath := "/Users/aj/etc/letsencrypt/live/shell.example.com/privkey.pem"
cert, err := tls.LoadX509KeyPair(certPath, privkeyPath)
if (err != nil) {
fmt.Fprintf(os.Stderr, "Couldn't load certificates: %s\n", err)
os.Exit(1)
}
cas := x509.NewCertPool()
casPath := "/Users/aj/etc/letsencrypt/live/shell.example.com/chain.pem"
casBuf, err := ioutil.ReadFile(casPath)
if (err != nil) {
fmt.Fprintf(os.Stderr, "Couldn't load certificate authority chain: %s\n", err)
os.Exit(1)
}
cas.AppendCertsFromPEM(casBuf)
config := tls.Config{
//ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{cert},
ClientCAs: cas,
}
plainConn := tls.Server(tlsConn, &config)
mux := http.NewServeMux()
mux.Handle("/", &myHandler{})
srv := &http.Server{Handler: mux}
//httpConn := httputil.NewServerConn(plainConn, nil)
c, err := srv.NewConn(plainConn)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't HTTP NewConn: %s\n", err)
os.Exit(1)
}
//c.SetState(c.rwc, http.StateNew) // before Serve can return
//go c.serve()
c.Serve()
/*
fmt.Println("Connection accepted. Waiting for a data...");
buf := make([]byte, 1024, 4096)
for {
n, err := plainConn.Read(buf)
if err != nil {
plainConn.Close()
if io.EOF == err {
break
}
fmt.Fprintf(os.Stderr, "errtype %s\n", err.Error())
fmt.Fprintf(os.Stderr, "Couldn't from socket: %s\n", err)
os.Exit(1)
}
fmt.Printf("Got %d bytes of data:\n", n)
fmt.Println(buf[0:n])
}
*/
fmt.Println("Closing.")
ln.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't stop listening on port %q: %s\n", port, err)
os.Exit(1)
}
fmt.Println("Closed.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment