Skip to content

Commit

Permalink
Add isHostIp()
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeguy committed Feb 12, 2024
1 parent 275148b commit 1e705c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions service/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {

site := getSiteByDomainWithWww(r.Host)
if site == nil {
if isHostIp(r.Host) {
w.WriteHeader(http.StatusBadRequest)
return
}

responseError(w, "CasWAF error: site not found for host: %s", r.Host)
return
}
Expand Down
7 changes: 7 additions & 0 deletions service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package service
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"

Expand All @@ -35,6 +36,12 @@ func joinPath(a string, b string) string {
return res
}

func isHostIp(host string) bool {
hostWithoutPort := strings.Split(host, ":")[0]
ip := net.ParseIP(hostWithoutPort)
return ip != nil
}

func responseOk(w http.ResponseWriter, format string, a ...interface{}) {
w.WriteHeader(http.StatusOK)

Expand Down

0 comments on commit 1e705c0

Please sign in to comment.