Commit b654fb2

Richard Luby <richluby@gmail.com>
2016-11-09 16:18:30
server can serve https
server uses configuration options to serve tls. note that the private key and cert must be generated before operation
1 parent 22dc8fd
Changed files (2)
README.md
@@ -15,6 +15,7 @@ LISTEN_PORT = 80
 PERMIT_BLANK_PASSWORD = true
 USE_HTTPS = false
 PRIVATE_KEY = "~/.ssh/question.priv"
+PRIVATE_CERT = "~/.ssh/server.crt"
 QUESTIONS = "/path/to/questions"
 USER_TESTS = "/path/to/tests"
 ```
server.go
@@ -23,6 +23,8 @@ type SERVER_CONFIG struct {
 	PERMIT_BLANK_PASSWORD bool
 	// PRIVATE_KEY defines the path to the server's private key for signing https connections
 	PRIVATE_KEY string
+	// PRIVATE_CERT defines the path to the server certificate
+	PRIVATE_CERT string
 	// USE_HTTPS determines if the server should use HTTPS
 	USE_HTTPS bool
 	// QUESTIONS contains the path to the questions directory
@@ -38,6 +40,7 @@ LISTEN_PORT = 80
 PERMIT_BLANK_PASSWORD = true
 USE_HTTPS = false
 PRIVATE_KEY = "~/.ssh/question.priv"
+PRIVATE_CERT = "~/.ssh/server.crt"
 QUESTIONS = "/path/to/questions"
 USER_TESTS = "/path/to/tests"
 `
@@ -74,10 +77,20 @@ func Listen(serverConfig SERVER_CONFIG) {
 	for _, handler := range handlers {
 		http.HandleFunc(handler.Request, handler.HandleFunction)
 	}
-	log.Fatal(http.ListenAndServe(
-		serverConfig.LISTEN_ADDRESS+":"+
-			strconv.Itoa(serverConfig.LISTEN_PORT),
-		nil))
+	if serverConfig.USE_HTTPS {
+		// check
+		// https://gist.github.com/denji/12b3a568f092ab951456
+		log.Fatal(http.ListenAndServeTLS(
+			serverConfig.LISTEN_ADDRESS+":"+
+				strconv.Itoa(serverConfig.LISTEN_PORT),
+			serverConfig.PRIVATE_CERT, serverConfig.PRIVATE_KEY,
+			nil))
+	} else {
+		log.Fatal(http.ListenAndServe(
+			serverConfig.LISTEN_ADDRESS+":"+
+				strconv.Itoa(serverConfig.LISTEN_PORT),
+			nil))
+	}
 }
 
 func ExecuteServer() {