Pass req not url for fix

master
Bel LaPointe 2018-10-13 17:34:23 -06:00
parent c7f6262857
commit bd04d2de8d
1 changed files with 11 additions and 8 deletions

19
main.go
View File

@ -54,13 +54,15 @@ func NewServer(addr, clientcrt, clientkey, servercrt string, whitelist []string,
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// fix scheme if necessary // fix scheme if necessary
fixScheme(r.URL) fixScheme(r)
// if not from localhost // if not from localhost
if !fromLocalhost(r.RemoteAddr) { if !fromLocalhost(r.RemoteAddr) {
logger.Log("not from localhost", r.RemoteAddr)
denyAccess(w) denyAccess(w)
return return
} }
if !toWhitelist(s.whitelist, r.URL.Host) { if !toWhitelist(s.whitelist, r.URL.Host) {
logger.Log("not to whitelist", r.URL.Host)
denyAccess(w) denyAccess(w)
return return
} }
@ -121,14 +123,15 @@ func copyHeader(dst, src http.Header) {
} }
} }
func fixScheme(u *url.URL) { func fixScheme(r *http.Request) {
if u.Scheme == "" { if r.URL.Scheme == "" {
u.Scheme = "http" r.URL.Scheme = "http"
} }
if strings.HasSuffix(u.Host, ":443") { if strings.HasSuffix(r.URL.Host, ":443") {
u.Scheme = "https" r.URL.Scheme = "https"
u.Host = u.Host[:len(u.Host)-len(":443")] r.URL.Host = r.URL.Host[:len(r.URL.Host)-len(":443")]
} }
//r.URL.Scheme = "https"
} }
func toWhitelist(okay []string, host string) bool { func toWhitelist(okay []string, host string) bool {
@ -149,7 +152,7 @@ func toWhitelist(okay []string, host string) bool {
} }
func fromLocalhost(addr string) bool { func fromLocalhost(addr string) bool {
return strings.Contains(addr, "[::1]") || addr == "127.0.0.1" || addr == "::1" return strings.Contains(addr, "[::1]") || addr == "127.0.0.1" || addr == "::1" || strings.Contains(addr, "bel.pc") || strings.Contains(addr, "192.168.0.")
} }
func denyAccess(w http.ResponseWriter) { func denyAccess(w http.ResponseWriter) {