Skip to content

Commit

Permalink
Add block filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dvob committed Jan 23, 2023
1 parent f23e8b8 commit 459eda0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
21 changes: 21 additions & 0 deletions filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"net/http"
)

func blockHostFilter(blockedHostnames []string, next http.HandlerFunc) http.HandlerFunc {
hosts := map[string]struct{}{}
for _, host := range blockedHostnames {
hosts[host] = struct{}{}
}
return func(w http.ResponseWriter, r *http.Request) {
if _, ok := hosts[r.URL.Host]; ok {
msg := fmt.Sprintf("access to %s blocked by proxy", r.URL.Host)
http.Error(w, msg, http.StatusForbidden)
return
}
next(w, r)
}
}
14 changes: 12 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httputil"
"os"
"strings"
)

func logRequest(next http.HandlerFunc) http.HandlerFunc {
Expand Down Expand Up @@ -70,9 +71,11 @@ func main() {
doCreateCA bool
caCertFile = "proxy-ca.crt"
caKeyFile = "proxy-ca.key"
blockList string
)

flag.BoolVar(&doCreateCA, "create-ca", false, "create a CA for the proxy")
flag.StringVar(&blockList, "block-list", blockList, "comma-separated list of hostnames which will be blocked")
flag.Parse()

if doCreateCA {
Expand All @@ -90,7 +93,14 @@ func main() {
os.Exit(1)
}

connectHandler := newInterceptHandler(certGen.Get, logRequest(forward))
forwardHandler := forward

if blockList != "" {
blockedHosts := strings.Split(blockList, ",")
forwardHandler = blockHostFilter(blockedHosts, forwardHandler)
}

connectHandler := newInterceptHandler(certGen.Get, logRequest(forwardHandler))
if err != nil {
log.Print(err)
os.Exit(1)
Expand All @@ -100,7 +110,7 @@ func main() {
if r.Method == "CONNECT" {
connectHandler.ServeHTTP(w, r)
} else {
forward(w, r)
forwardHandler(w, r)
}
})

Expand Down

0 comments on commit 459eda0

Please sign in to comment.