Skip to content

Instantly share code, notes, and snippets.

@ijt
Created October 8, 2012 05:49
Show Gist options
  • Save ijt/3850919 to your computer and use it in GitHub Desktop.
Save ijt/3850919 to your computer and use it in GitHub Desktop.
Web proxy that disallows distracting websites
package main
import (
"flag"
"fmt"
"github.com/elazarl/goproxy"
"log"
"net/http"
"os"
)
func main() {
flag.Parse()
if flag.NArg() != 1 {
fmt.Println("usage: focus PORT")
os.Exit(1)
}
portStr := flag.Arg(0)
proxy := goproxy.NewProxyHttpServer()
proxy.Verbose = true
blacklist := []string {
"www.reddit.com",
"news.ycombinator.com",
"www.youtube.com",
}
for _, host := range(blacklist) {
proxy.OnRequest(goproxy.DstHostIs(host)).DoFunc(scold)
}
log.Fatal(http.ListenAndServe(":" + portStr, proxy))
}
func scold(r *http.Request,ctx *goproxy.ProxyCtx)(*http.Request,*http.Response) {
return r, goproxy.NewResponse(r, goproxy.ContentTypeText,http.StatusForbidden,
"Don't waste your time!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment