Skip to content

Commit

Permalink
add implicit https support when a valid tls certificate and key are s…
Browse files Browse the repository at this point in the history
…upplied

add sample cert and key files that can simply be renamed for local testing
  • Loading branch information
stewartoallen committed Apr 20, 2016
1 parent 90eea4f commit 03fd375
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
48 changes: 42 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ var (
version = "1.91"
versionFloat = float32(1.91)
addr = flag.String("addr", ":8989", "http service address")
saddr = flag.String("saddr", ":8990", "https service address")
scert = flag.String("scert", "cert.pem", "https certificate file")
skey = flag.String("skey", "key.pem", "https key file")
//assets = flag.String("assets", defaultAssetPath(), "path to assets")
//verbose = flag.Bool("v", true, "show debug logging")
verbose = flag.Bool("v", false, "show debug logging")
Expand Down Expand Up @@ -91,7 +94,6 @@ func main() {
}

//getList()
f := flag.Lookup("addr")
log.Println("Version:" + version)

// hostname
Expand Down Expand Up @@ -122,12 +124,8 @@ func main() {
log.Println(err)
}

log.Print("Starting server and websocket on " + ip + "" + f.Value.String())
//homeTempl = template.Must(template.ParseFiles(filepath.Join(*assets, "home.html")))

log.Println("The Serial Port JSON Server is now running.")
log.Println("If you are using ChiliPeppr, you may go back to it and connect to this server.")

// see if they provided a regex filter
if len(*regExpFilter) > 0 {
log.Printf("You specified a serial port regular expression filter: %v\n", *regExpFilter)
Expand Down Expand Up @@ -188,11 +186,49 @@ func main() {

http.HandleFunc("/", homeHandler)
http.HandleFunc("/ws", wsHandler)

go startHttp(ip)
go startHttps(ip)

log.Println("The Serial Port JSON Server is now running.")
log.Println("If you are using ChiliPeppr, you may go back to it and connect to this server.")

// wait
ch := make(chan bool)
<-ch
}

func startHttp(ip string) {
f := flag.Lookup("addr")
log.Println("Starting http server and websocket on " + ip + "" + f.Value.String())
if err := http.ListenAndServe(*addr, nil); err != nil {
fmt.Printf("Error trying to bind to port: %v, so exiting...", err)
fmt.Printf("Error trying to bind to http port: %v, so exiting...\n", err)
log.Fatal("Error ListenAndServe:", err)
}
}

func startHttps(ip string) {
// generate self-signed cert for testing or local trusted networks
// openssl req -x509 -nodes -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365

f := flag.Lookup("saddr")
cert, certErr := os.Open(*scert)
key, keyErr := os.Open(*skey)

cert.Close()
key.Close()

if certErr != nil || keyErr != nil {
log.Println("Missing tls cert and/or key. Will not start HTTPS server.")
//fmt.Println("Missing tls cert and/or key. Will not start HTTPS server.")
return
}

log.Println("Starting https server and websocket on " + ip + "" + f.Value.String())
if err := http.ListenAndServeTLS(*saddr, *scert, *skey, nil); err != nil {
fmt.Printf("Error trying to bind to https port: %v, so exiting...\n", err)
log.Fatal("Error ListenAndServeTLS:", err)
}
}

func externalIP() (string, error) {
Expand Down
26 changes: 26 additions & 0 deletions sample-cert.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-----BEGIN CERTIFICATE-----
MIIETjCCAzagAwIBAgIJANLvDOX4UE/MMA0GCSqGSIb3DQEBBQUAMHcxCzAJBgNV
BAYTAlhYMQswCQYDVQQIEwJZWTELMAkGA1UEBxMCWloxDDAKBgNVBAoTA0FCQzEM
MAoGA1UECxMDREVGMRIwEAYDVQQDEwlsb2NhbGhvc3QxHjAcBgkqhkiG9w0BCQEW
D2FkbWluQGxvY2FsaG9zdDAeFw0xNjA0MjAxODEzNDdaFw0xNzA0MjAxODEzNDda
MHcxCzAJBgNVBAYTAlhYMQswCQYDVQQIEwJZWTELMAkGA1UEBxMCWloxDDAKBgNV
BAoTA0FCQzEMMAoGA1UECxMDREVGMRIwEAYDVQQDEwlsb2NhbGhvc3QxHjAcBgkq
hkiG9w0BCQEWD2FkbWluQGxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEP
ADCCAQoCggEBALgVUetvxfIYmcdw6XBvMWtXxnws6eI69NuPIXPPnuFtCVjh2CRF
+T2M5rVPtA+HyWPfbNrdX+9Mg1DgTSOMREKEJcEo6hh4O68Y2CtrcLR4HDZUbeqX
pgzUSi9G49ByGDlAMUKieD+LzxNq6biTbAXKpTrHbfwCkTvNtxzAac7ygeWL26Hi
hn67xp/KBi9RGM/w50nMe4AY9UDzM9UXpbYTGVr7GF5x8V8ETcO2BpaQw/fDV7TB
NJccaNYJbSYjCgu+7IxFrHwFCjC3xSmkVXexVdoPHqEuEy2QY71Mwz8YRFzBpuKh
/msxiQV4tpa8OhN9Ny6vJO2uDNTymACWvV0CAwEAAaOB3DCB2TAdBgNVHQ4EFgQU
qsW79WmhsWzbv8zC0KLTmJv4FEUwgakGA1UdIwSBoTCBnoAUqsW79WmhsWzbv8zC
0KLTmJv4FEWhe6R5MHcxCzAJBgNVBAYTAlhYMQswCQYDVQQIEwJZWTELMAkGA1UE
BxMCWloxDDAKBgNVBAoTA0FCQzEMMAoGA1UECxMDREVGMRIwEAYDVQQDEwlsb2Nh
bGhvc3QxHjAcBgkqhkiG9w0BCQEWD2FkbWluQGxvY2FsaG9zdIIJANLvDOX4UE/M
MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBACQd5Qt+9Z46MKHTteIU
l+roIHHtpO5eP2450/lbvmYHtf0IFX0CiTszS8KtgDdAPcRJNNsNOOh7VLDX09IT
aeAhNH1X2MAxNmM0FsDKWfT72G27TvXrP6I3Mgt0q1SJKGrxRxK/HesPzZ3TVypq
Yie8OQ7ZxNxzeAH0ZoHmHpFBe+cOyr87xjzkih2ls9bYkiqBGxqzGn0uATdVimjj
attRFU4zq/K9Rq1NVQIJDQWt0CgmJyR0V5GbEA2QcrS0ZLHPM1thxB3pSCTUXVQz
T2Z5eG4gEsXfrM0s17DFg6kx18SsYj1jQUVFoJJCVbudDQ1BUlUNfc7mRghvuuG8
pC4=
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions sample-key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAuBVR62/F8hiZx3DpcG8xa1fGf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