-
-
Save anonymous/95136ccbfe4b8a0f0924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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