2023-12-12 22:05:57 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-07-09 23:09:18 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2024-07-19 00:24:11 +00:00
|
|
|
|
|
|
|
"local-ip.sh/utils"
|
2023-12-12 22:05:57 +00:00
|
|
|
)
|
|
|
|
|
2024-07-09 23:09:18 +00:00
|
|
|
func ServeHttp() {
|
2024-07-19 00:24:11 +00:00
|
|
|
utils.Logger.Info().Msg("Waiting until \"local-ip.sh\" certificate is delivered to start up HTTP server...")
|
2024-07-09 23:09:18 +00:00
|
|
|
waitForCertificate()
|
|
|
|
|
|
|
|
go http.ListenAndServe(":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
url := r.URL
|
|
|
|
url.Host = r.Host
|
|
|
|
url.Scheme = "https"
|
|
|
|
http.Redirect(w, r, url.String(), http.StatusMovedPermanently)
|
|
|
|
}))
|
|
|
|
|
2023-12-12 22:05:57 +00:00
|
|
|
http.HandleFunc("/server.key", func(w http.ResponseWriter, r *http.Request) {
|
2023-12-12 22:33:14 +00:00
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
2024-07-09 23:09:18 +00:00
|
|
|
http.ServeFile(w, r, "/certs/wildcard/server.key")
|
2023-12-12 22:05:57 +00:00
|
|
|
})
|
|
|
|
http.HandleFunc("/server.pem", func(w http.ResponseWriter, r *http.Request) {
|
2023-12-12 22:33:14 +00:00
|
|
|
w.Header().Set("Content-Type", "application/x-x509-ca-cert")
|
2024-07-09 23:09:18 +00:00
|
|
|
http.ServeFile(w, r, "/certs/wildcard/server.pem")
|
|
|
|
})
|
|
|
|
http.HandleFunc("/og.png", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "image/png; charset=utf-8")
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
http.ServeFile(w, r, "./http/static/og.png")
|
|
|
|
})
|
|
|
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "image/x-icon; charset=utf-8")
|
|
|
|
w.Header().Set("Cache-Control", "public, max-age=3600")
|
|
|
|
http.ServeFile(w, r, "./http/static/favicon.ico")
|
2023-12-12 22:05:57 +00:00
|
|
|
})
|
2024-07-09 23:09:18 +00:00
|
|
|
http.HandleFunc("/styles.css", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/css; charset=utf-8")
|
|
|
|
http.ServeFile(w, r, "./http/static/styles.css")
|
|
|
|
})
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
http.ServeFile(w, r, "./http/static/index.html")
|
|
|
|
})
|
2024-07-19 00:24:11 +00:00
|
|
|
|
|
|
|
utils.Logger.Info().Msg("Starting up HTTPS server on :443\n")
|
|
|
|
err := http.ListenAndServeTLS(":443", "/certs/root/server.pem", "/certs/root/server.key", nil)
|
|
|
|
if err != nil {
|
|
|
|
utils.Logger.Fatal().Err(err).Msg("Failed to start HTTPS server")
|
|
|
|
}
|
2024-07-09 23:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func waitForCertificate() {
|
|
|
|
for {
|
|
|
|
_, err := os.Stat("/certs/root/output.json")
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "no such file or directory") {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
2024-07-19 00:24:11 +00:00
|
|
|
utils.Logger.Fatal().Err(err).Msg("Unexpected error while looking for /certs/root/output.json")
|
2024-07-09 23:09:18 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2023-12-12 22:05:57 +00:00
|
|
|
}
|