From 38fe3338881373fad192fd03e49032d1eab200f9 Mon Sep 17 00:00:00 2001 From: PierreZ Date: Thu, 16 Jul 2015 21:54:40 +0200 Subject: [PATCH] adding first batch of code --- Makefile | 4 +++ main.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 Makefile create mode 100644 main.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..16f92af --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +default: static + +static: + docker run --rm -v $(pwd):/src centurylink/golang-builder \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..cfb347f --- /dev/null +++ b/main.go @@ -0,0 +1,98 @@ +package main // import "github.com/PierreZ/goStatic" + +import ( + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "flag" + "fmt" + "io/ioutil" + "log" + "math/big" + "os" + "strconv" + "time" + + "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" +) + +func main() { + // Def & parsing of flags + portPtr := flag.Int("p", 8080, "The proxy listening port") + pathPtr := flag.String("path for static file", "/var/www", "The path for the static files") + crtPtr := flag.String("crt", "/etc/ssl/server", "The path and the name whithout extension for your CRT/key for TLS. Example: /etc/ssl/server for /etc/ssl/server.{crt,key}") + HTTPPtr := flag.Bool("forceHTTP", false, "Forcing HTTP and not HTTPS") + + flag.Parse() + + // Echo instance + e := echo.New() + + // Middleware + e.Use(mw.Logger()) + e.Use(mw.Recover()) + + // Routes + e.Static("/", *pathPtr) + + log.Println("Starting goStatic") + + port := ":" + strconv.FormatInt(int64(*portPtr), 10) + + // Start server with unsecure HTTP + // or with awesome TLS + if *HTTPPtr { + e.Run(port) + + } else { + + if *crtPtr != "/etc/ssl/server" { + generateCert(*crtPtr) + } + + e.RunTLS(port, *pathPtr+".pem", *pathPtr+".crt") + } + +} + +// Mostly based on https://www.socketloop.com/tutorials/golang-create-x509-certificate-private-and-public-keys +func generateCert(path string) { + + template := &x509.Certificate{ + IsCA: true, + BasicConstraintsValid: true, + SubjectKeyId: []byte{1, 2, 3}, + SerialNumber: big.NewInt(1234), + Subject: pkix.Name{ + Country: []string{"Earth"}, + Organization: []string{"Hooli"}, + }, + NotBefore: time.Now(), + NotAfter: time.Now().AddDate(5, 5, 5), + // see http://golang.org/pkg/crypto/x509/#KeyUsage + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}, + KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, + } + + // generate private key + privatekey, err := rsa.GenerateKey(rand.Reader, 4096) + + if err != nil { + log.Println(err) + } + + // save cert + ioutil.WriteFile(path+".crt", cert, 0777) + fmt.Println("certificate saved to", path+".crt") + + // this will create plain text PEM file. + pemfile, _ := os.Create(path + ".pem") + var pemkey = &pem.Block{ + Type: "RSA PRIVATE KEY", + Bytes: x509.MarshalPKCS1PrivateKey(privatekey)} + pem.Encode(pemfile, pemkey) + pemfile.Close() +}