Skip to content

Commit

Permalink
Add redirectToNonWww()
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeguy committed Oct 6, 2023
1 parent 660a228 commit 820fc09
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions service/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,40 @@ func forwardHandler(targetUrl string, writer http.ResponseWriter, request *http.
proxy.ServeHTTP(writer, request)
}

func getHostNonWww(host string) string {
res := ""
tokens := strings.Split(host, ".")
if len(tokens) > 2 && tokens[0] == "www" {
res = strings.Join(tokens[1:], ".")
}
return res
}

func redirectToHttps(w http.ResponseWriter, r *http.Request) {
httpsUrl := fmt.Sprintf("https://%s", joinPath(r.Host, r.RequestURI))
http.Redirect(w, r, httpsUrl, http.StatusMovedPermanently)
targetUrl := fmt.Sprintf("https://%s", joinPath(r.Host, r.RequestURI))
http.Redirect(w, r, targetUrl, http.StatusMovedPermanently)
}

func redirectToNonWww(w http.ResponseWriter, r *http.Request, host string) {
protocol := "https"
if r.TLS == nil {
protocol = "http"
}

targetUrl := fmt.Sprintf("%s://%s", protocol, joinPath(host, r.RequestURI))
http.Redirect(w, r, targetUrl, http.StatusMovedPermanently)
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.UserAgent(), "Uptime-Kuma") {
fmt.Printf("handleRequest: %s\t%s\t%s\t%s\t%s\n", r.RemoteAddr, r.Method, r.Host, r.RequestURI, r.UserAgent())
}

hostNonWww := getHostNonWww(r.Host)
if hostNonWww != "" {
redirectToNonWww(w, r, hostNonWww)
}

site := object.GetSiteByDomain(r.Host)
if site == nil {
responseError(w, "CasWAF error: site not found for host: %s", r.Host)
Expand Down

0 comments on commit 820fc09

Please sign in to comment.