Skip to content

Instantly share code, notes, and snippets.

/server.go Secret

Created October 10, 2014 00:25
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 anonymous/95136ccbfe4b8a0f0924 to your computer and use it in GitHub Desktop.
Save anonymous/95136ccbfe4b8a0f0924 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os/exec"
"strings"
"time"
)
//garbage for openssl prompts
var filller = []string{"CA", "XX", "XX", "XX", "", "", ""}
func DoCMD(rawcmd string, prompts ...string) {
s := strings.Split(rawcmd, " ")
cmdname := s[0]
argz := s[1:]
cmd := exec.Command(cmdname, argz...)
var out bytes.Buffer
var err bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &err
in, _ := cmd.StdinPipe()
cmd.Start()
time.Sleep(time.Second * 1)
fmt.Println(out.String())
fmt.Println(err.String())
out.Reset()
err.Reset()
for i := 0; i < len(prompts); i++ {
in.Write([]byte(prompts[i] + "\n\r"))
time.Sleep(time.Millisecond * 300)
fmt.Println(out.String())
fmt.Println(err.String())
out.Reset()
err.Reset()
}
}
func httpsResp(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("this is https"))
}
func httpResp(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("this is http"))
}
func main() {
fmt.Println("started")
DoCMD("openssl req -new -nodes -newkey rsa:2048 -days 365 -nodes -x509 -keyout ssl.key -out ssl.cert", filller...)
keypair, err := tls.LoadX509KeyPair("ssl.cert", "ssl.key")
if err != nil {
panic(err)
}
rootca := x509.NewCertPool()
cfg := tls.Config{Certificates: []tls.Certificate{keypair}, InsecureSkipVerify: false, RootCAs: rootca}
cfg.BuildNameToCertificate()
httpsM := http.NewServeMux()
httpsM.HandleFunc("/", httpsResp)
httpM := http.NewServeMux()
httpM.HandleFunc("/", httpResp)
s1 := http.Server{
Addr: ":https",
Handler: httpsM,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
MaxHeaderBytes: 16384,
TLSConfig: &cfg,
}
go s1.ListenAndServeTLS("ssl.cert", "ssl.key")
s2 := http.Server{
Addr: ":http",
Handler: httpM,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
MaxHeaderBytes: 16384,
}
fmt.Println("ready")
s2.ListenAndServe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment